Closed bajian closed 5 years ago
I got the way:
var txhex = '0200000001b110155b1b9f13f887127a7c8b2f9a784ca0181efd70f73e5de2b2e45d49807f0100000000fdffffff03102700000000000017a91412b59e2a7a5c5a17c1cade66d299668f3c43e08387e02e0000000000001976a914ef596990496fef9a45ce0af7514ea599169dcd6788aca67b8c060000000017a914da6f35de0ae98385fb999b7268550e02963fa9468700000000';
const BTC_TESTNET = bitcoin.networks.testnet;
var txb = bitcoin.TransactionBuilder.fromTransaction (
bitcoin.Transaction.fromHex (txhex), BTC_TESTNET);
const alice = bitcoin.ECPair.fromWIF('cTeXHvyWUow9yqbzN69aEM3qPBvUGL11eA5aDCqLr22vAipgCeEB',BTC_TESTNET)
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: alice.publicKey, network: BTC_TESTNET })
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: BTC_TESTNET })
txb.sign({
prevOutScriptType: 'p2sh-p2wpkh',
vin: 0,
keyPair: alice,
redeemScript: p2sh.redeem.output,
witnessValue: 109896990,
})
txb.build().toHex()
I used the
bitcoin-cli createrawtransaction
to create the txhex. And it can be signed by using:bitcoin-cli signrawtransactionwithwallet '0200000001b110155b1b9f13f887127a7c8b2f9a784ca0181efd70f73e5de2b2e45d49807f0100000000fdffffff03102700000000000017a91412b59e2a7a5c5a17c1cade66d299668f3c43e08387e02e0000000000001976a914ef596990496fef9a45ce0af7514ea599169dcd6788aca67b8c060000000017a914da6f35de0ae98385fb999b7268550e02963fa9468700000000' '[{"txid":"7f80495de4b2e25d3ef770fd1e18a04c789a2f8b7c7a1287f8139f1b5b1510b1","vout":1,"address":"2N766arHWByFRf2LWPEkoh4uRzpeaFkKqn7","label":"watch_only","scriptPubKey":"a91497d7d63528e0980dfc3f5db3036b5fcd8dce5cb587","amount":1.09896990,"confirmations":62360,"spendable":false,"solvable":false,"safe":true}]'
//output: { "hex": "02000000000101b110155b1b9f13f887127a7c8b2f9a784ca0181efd70f73e5de2b2e45d49807f0100000017160014e6fa27edac0471f43f408377b3a5bd0bd5c0fb8efdffffff03102700000000000017a91412b59e2a7a5c5a17c1cade66d299668f3c43e08387e02e0000000000001976a914ef596990496fef9a45ce0af7514ea599169dcd6788aca67b8c060000000017a914da6f35de0ae98385fb999b7268550e02963fa946870247304402205af9c4954c5dfb5a44aab9842d99948010b37bb004acbda31fbd8c9bf8bd61fd0220446b1842a984f73ebec7042cdaa115b7bb54b4b12e24ed7252707305c99f3525012103a51e150c20885d476d4d95b9adee7d08a0ec65520314d8e95474582a3ec6fc5a00000000", "complete": true }Now,I need to sign the txhex by using bitcoinjs-lib. I have tried the code below,and use the
bitcoin-cli sendrawtransaction
to broadcast tx,but got err:error code: -26 error message:mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) (code 16)I decoded the signed code and found it doesnot have the key
txinwitness
comparing with the one signed bybitcoin-cli signrawtransactionwithwallet
from beginning code.【The right is signed by bitcoinjs-lib using the code above】