Trying to test with P2SH with Node and bitcore-lib


(Robert H) #1

I am trying to reproduce some code and execute spend tx from un unspent txo P2SH non-multisig. I am a bit stuck. Figures are dummy values I am interested in the concept and trying to learn about these types of transactions.

    > var bitcore = require('bitcore-lib');
    undefined
    > var redeemScript = bitcore.Script.fromASM('OP_SHA256 001xxxxxxxxxxxxxxxxxxxx6 OP_EQUAL');
    undefined
    > var scriptPubKey = redeemScript.toScriptHashOut();
    undefined
    > scriptPubKey
    <Script: OP_HASH160 20 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6 OP_EQUAL>
    > scriptPubKey.toAddress()
    3xxxxxxxxxxxx
    > scriptPubKey.toHex()
    a914xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx287

Questions so far:

Q1: The redeem script I have has the form:

ScriptSig: PUSHDATA(23)[001426xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]

With > var redeemScript = bitcore.Script.fromASM('OP_SHA256 001xxxxxxxxxxxxxxxxxxxx6 OP_EQUAL'); should I include the word PUSHDATA(23) or just the string between []? Is this where I should paste the redeem script from the previous tx? It’s not giving me the correct hash based on the redeemScript entered.

Q2. I can see the resulted hash as having 0x04 as prefix. I need this to be 0x05. How do I set it up correctly? Right now it results in a different address then the one I need under scriptPubKey.toAddress().


I continued nevertheless with following the instructions in the video:

> var UnspentOutput = bitcore.Transaction.UnspentOutput;
undefined
> var utxo = new UnspentOutput({
...   "txId" : "2abxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9",
...   "outputIndex" : 0,
...   "address" : "3xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
...   "script" : "a914xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7",
...   "satoshis" : 10015996
... });
undefined
> var address = '1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
undefined
> var tx = new bitcore.Transaction().from(utxo).to(address, 10015000);
undefined
> tx.inputs[0]
Input {
  output: <Output (10015996 sats) <Script: OP_HASH160 20 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6 OP_EQUAL>>,
  prevTxId: <Buffer 2a ce 57 9f 68 06 9d fd 9e 5b 46 7d 98 cd 86 44 85 79 8c 34 a1 a3 27 3b 0e 66 12 04 5c 86 c8 d8>,
  outputIndex: 0,
  sequenceNumber: 42735965294,
  _script: <Script: >,
  _scriptBuffer: <Buffer > }
> tx.outputs
[ <Output (10015996 sats) <Script: OP_DUP OP_HASH160 20 0xdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx OP_EQUALVERIFY OP_CHECKSIG>> ]
> var scriptSig = bitcore.Script.fromASM('04678afd04678a ' + redeemScript.toHex())
undefined
> tx.inputs[0].setScript(scriptSig);
Input {
  output: <Output (10015996 sats) <Script: OP_HASH160 20 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxOP_EQUAL>>,
  prevTxId: <Buffer 2a ce 57 9f 68 06 9d fd 9e 5b 46 7d 98 cd 86 44 85 79 8c 34 a1 a3 27 3b 0e 66 12 04 5c 86 c8 d8>,
  outputIndex: 0,
  sequenceNumber: 42735965294,
  _script: <Script: 7 0x04xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx87>,
  _scriptBuffer: <Buffer 08 14 65 8a fd 04 67 8a 29 a8 17 00 14 b1 16 d9 ed 6d 0d ce c6 06 57 64 cc 5b c9 a3 b2 26 5d 92 37 87> }
> var rawTx = tx.toString('hex');
undefined
> rawTx
'010000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx00000000'

Q3: What is this line?

> var scriptSig = bitcore.Script.fromASM('04678afd04678a ' + redeemScript.toHex())

It has something to do with the > var redeemScript = bitcore.Script.fromASM('OP_SHA256 001xxxxxxxxxxxxxxxxxxxx6 OP_EQUAL'); I guess but I can’t put the pieces together.

More about these transaction types in Adreas’s book , page 135.

I am trying to execute a tx, unspent and this video above is the closest I could get to node.js instructions.

Any help appreciated.