dashhive / DashTx.js

Create TX hex for payments and such
MIT License
3 stars 0 forks source link

Needs test for L2 AssetLockProof TX #86

Open coolaj86 opened 2 months ago

coolaj86 commented 2 months ago

We updated the other tests to work with the L2 messages, but forgot to put a test fixture in for the AssetLock specifically.

Here's the code that can produce the signed payload, verified by virtue of being broadcast to the network and accepted.

  let utxos = await DashTx.utils.rpc(rpcAuthUrl, "getaddressutxos", {
    addresses: [addr],
  });
  let total = DashTx.sum(utxos);
  console.log();
  console.log(`utxos (${total})`);
  console.log(utxos);

  // TODO which hd paths to use for which addresses?
  let creditOutputs = [{ satoshis: total - 10000, pubKeyHash: pkh }];
  let totalCredits = DashTx.sum(creditOutputs);
  let burnOutput = { satoshis: totalCredits, pubKeyHash: pkh };
  //@ts-ignore - TODO add types
  let assetLockScript = DashPlatform.Tx.packAssetLock({
    creditOutputs,
  });

  let txDraft = {
    version: L1_VERSION_PLATFORM,
    type: TYPE_ASSET_LOCK,
    inputs: utxos,
    outputs: [burnOutput],
    extraPayload: assetLockScript,
  };
  console.log();
  console.log(`txDraft:`);
  console.log(txDraft);

  txDraft.inputs.sort(DashTx.sortInputs);
  txDraft.outputs.sort(DashTx.sortOutputs);
  let vout = txDraft.outputs.indexOf(burnOutput);

  let vin = 0;
  let txSigHash = DashTx.createForSig(txDraft, vin, DashTx.SIGHASH_DEFAULT);
  let txHex = DashTx.serialize(txSigHash, _sigHashType);
  console.log(txHex);

  let txSigned = await dashTx.hashAndSignAll(txDraft);
  console.log();
  console.log(`txSigned:`);
  console.log(txSigned);

  let txid = await DashTx.utils.rpc(
    rpcAuthUrl,
    "sendrawtransaction",
    txSigned.transaction,
  );