bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.65k stars 2.09k forks source link

My scriptSig is empty (newbie's here) #1612

Closed nbass closed 4 years ago

nbass commented 4 years ago

My code:

const tx = new bitcoinjsLib.Psbt({ network });
tx.addInput({
    hash: 'be55eb607dc8da41ea2ca3e728fc437a5f5f04d63a11dd1846f6c79db4a3a3c3',
    index: 1,
    nonWitnessUtxo: Buffer.from('020000000204fc7e16abe1da2f491c9e7ab4384e3f718d6635369eb1b7fadb03fb56c1310d010000006b483045022100eeca6fcfdb38e44c539b58ef2279a911430b5565dba901bd5db9dad0787b4fc402201a7fafa32a1ebe236c662149c9b7a574a4c407bf32d0be235a90fab668515f1b012102d7dbfde9b4c72c321c94ee8dbdd3fb901f5db5de93848fb6e8234be2dfe64308ffffffff048879e0fac302e6e5ab83b1b77e6283431620371161820e5b8dae38227d80e2010000006b48304502210085119581791f4d59265c3594ae27bb51d1ee9d09b8d94428a8bb1327521cc5ad0220679ab684c8fd590e584c14465e52742457b13ee1f002d54e3395e41571b9530a012102d7dbfde9b4c72c321c94ee8dbdd3fb901f5db5de93848fb6e8234be2dfe64308ffffffff02c0ad0000000000001976a914f5693e59d064b7d4a37153eb7af25ecd9dcf68ee88ac484301000000000017a91408bb452773c7e46c945ab2f01115c57f61fbe3308700000000', 'hex'),
    redeemScript: bitcoinjsLib.script.fromASM('OP_2 027230825bcff470b314859288eeeff84cb2ea86271a302f15f40d511ef6f2b734 02d7dbfde9b4c72c321c94ee8dbdd3fb901f5db5de93848fb6e8234be2dfe64308 OP_2 OP_CHECKMULTISIG'),
});

tx.addOutput({ address: 'n3jqjLLrmJ2jzLx9ei3mwvN5cWNxKqxxXy', value: 10000 });
tx.signInput(0, keyPair);

And, my txn.ins[0]:

hash: Uint8Array(32) [195, 163, 163, 180, 157, 199, 246, 70, 24, 221, 17, 58, 214, 4, 95, 95, 122, 67, 252, 40, 231, 163, 44, 234, 65, 218, 200, 125, 96, 235, 85, 190]
index: 1
script: Uint8Array []
sequence: 4294967295
witness: []

I wonder why the scriptSig is empty. Even without this, is this txn still valid? I'm little confused...

junderw commented 4 years ago

Your code doesn't have txn anywhere. How did you get it?

nbass commented 4 years ago

I converted this in the following way:

function toHexString(byteArray) {
    return Array.prototype.map.call(byteArray, function (byte) {
        return ('0' + (byte & 0xFF).toString(16)).slice(-2);
    }).join('');
}

...
tx.signInput(0, keyPair);
const txn = this.toHexString(tx.data.getTransaction(), 'hex');

The reason I didn't do tx.toBase64() is because the second signer asked me to change this into hex and send it

nbass commented 4 years ago

Second signer is looking forward to the following:

0100000001e77457975db0320623c097096216374a5abc79f75e20eb6b193234c86f3275de0100000092000047304402201c8eec772929d6c516bddc8fc2457b032440aafc874aacf2202926e75f232c2f02205bb5c79e1cb50e02c6daf01b7b46252ff653b30c40d6dfd24c296f186b3923fe0147522102719b2a6857356abd3fa4db602b11713e0a3727b579622341d18def72320779b8210399c60f9e91f5a67995de7ded11e8207712022fee91949c2cf9957a4aff8adf0752aeffffffff01e8030000000000001976a9144d1606a1397e8873cf6b936219fc426757ae36a888ac00000000

But my hex is following:

0200000001e77457975db0320623c097096216374a5abc79f75e20eb6b193234c86f3275de0100000000ffffffff01e8030000000000001976a9144d1606a1397e8873cf6b936219fc426757ae36a888ac00000000
junderw commented 4 years ago

psbt.data.getTransaction() is an internal method that should only be called from inside of the Psbt class.

psbt.extractTransaction() is the only way to get a Transaction out of Psbt.

Use psbt.toHex() or psbt.toBase64() and have the second signer use Psbt.fromHex() or Psbt.fromBase64() to recreate the Psbt for signing.

nbass commented 4 years ago

My second signer is using c#, so they can't use bitcoinjs-lib. Is there any other way? Thanks for the answer, though! You have been a great help

junderw commented 4 years ago

NBitcoin (C#) supports Psbt.

It is a widely used standard.

nbass commented 4 years ago

Thank you! We follow your help, this will probably be solved.