bitcoinjs / bitcoinjs-lib

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

Error: Key pair cannot sign for this input when building p2sh multisig #1639

Closed iamgamelover closed 3 years ago

iamgamelover commented 3 years ago

@junderw Excuse me, because previous issue is closed and I opened a new issue, thank you!

previous issue: https://github.com/bitcoinjs/bitcoinjs-lib/issues/1637

I changed correct wifs and got a new error:

Uncaught (in promise) Error: Transaction has absurd fees at TransactionBuilder.__build at TransactionBuilder.build

I set fee is 0.00002 test btc.

New code is here:

let txhex = '02000000018e8851d0f15175cf97819d66c830907672c95e6481aff8b77d349430e341eb9a0000000000ffffffff03504600000000000017a9145761a1d45b8a6e7caa10a4bcecca97630c67af46870000000000000000166a146f6d6e6900000000000000790000000005f5e10022020000000000001976a9144ff2611bf373454410ba5fe61d258544aab681f088ac00000000';

const BTC_TESTNET = btctool.bitcoin.networks.testnet;

let txb = btctool.bitcoin.TransactionBuilder.fromTransaction (
        btctool.bitcoin.Transaction.fromHex (txhex), BTC_TESTNET);

const pubkeys = [
        '021d475729c52f86df24b36aa231945bd090f9c23ccbfb91e4ade6813b2419d32d',
        '03efd8923f1829ece87202892d31cd75c20b7a7b5adf888f7ba04fa2c1bc931ce9',
    ].map(hex => btctool.buffer.Buffer.from(hex, 'hex'));

const p2ms  = btctool.bitcoin.payments.p2ms({ m: 2, pubkeys, network: BTC_TESTNET });
// const p2wsh = btctool.bitcoin.payments.p2wsh({ redeem: p2ms, network: BTC_TESTNET });
const p2sh   = btctool.bitcoin.payments.p2sh({ redeem: p2ms, network: BTC_TESTNET });

const wifs = [
        'cUAdadTkjeVFsNz5ifhkETfAzk5PvhnLWtmdSKgbyTTjSCE4MYWy',
        'cV6dif91LHD8Czk8BTgvYZR3ipUrqyMDMtUXSWsThqpHaQJUuHKA',
    ].map((wif) => btctool.bitcoin.ECPair.fromWIF(wif, BTC_TESTNET));

// sign
txb.sign(0, wifs[0], p2sh.redeem.output, undefined, 1000000, undefined);
txb.sign(0, wifs[1], p2sh.redeem.output, undefined, 1000000, undefined);

txb.build().toHex();

I'm looking forward to your reply. Thanks!

junderw commented 3 years ago

I don't see any code "setting a fee."

So how can I possibly help you?

junderw commented 3 years ago

Do you understand what the 1000000 that you pass to txb.sign means?

junderw commented 3 years ago

You are telling TransactionBuilder "the input I am signing is worth 0.01 tBTC"...

But looking at the blockchain, your input is only worth 0.0002 tBTC...

So it thinks you are paying 0.00998 tBTC fee and not a 0.00002 tBTC fee.

iamgamelover commented 3 years ago

I used Transaction.fromHex (txhex) to create txb object and the "setting a fee." thing is in the txhex.

Code is here:

---------------
Step 1
---------------

./omnicore-cli listunspent 0 99999 '["2N1DFjaE4yCcECdFwgLQcLmNrLV5zetgQtE"]'

{
    "txid": "9aeb41e33094347db7f8af81645ec972769030c8669d8197cf7551f1d051888e",
    "vout": 0,
    "address": "2N1DFjaE4yCcECdFwgLQcLmNrLV5zetgQtE",
    "label": "",
    "scriptPubKey": "a9145761a1d45b8a6e7caa10a4bcecca97630c67af4687",
    "amount": 0.00020000,
    "confirmations": 1,
    "spendable": false,
    "solvable": false,
    "safe": true
  }

---------------
Step 2
---------------

// miner fee: 0.00002

./omnicore-cli createrawtransaction '[{"txid":"9aeb41e33094347db7f8af81645ec972769030c8669d8197cf7551f1d051888e","vout":0}]' '{"2N1DFjaE4yCcECdFwgLQcLmNrLV5zetgQtE":0.00018}'  

---------------
Step 3
---------------

// miner fee: 0.00002

./omnicore-cli "omni_createrawtx_change" "02000000018e8851d0f15175cf97819d66c830907672c95e6481aff8b77d349430e341eb9a0000000000ffffffff03504600000000000017a9145761a1d45b8a6e7caa10a4bcecca97630c67af46870000000000000000166a146f6d6e6900000000000000790000000005f5e10022020000000000001976a9144ff2611bf373454410ba5fe61d258544aab681f088ac00000000" '[{"txid":"9aeb41e33094347db7f8af81645ec972769030c8669d8197cf7551f1d051888e","vout":0,"scriptPubKey":"a9145761a1d45b8a6e7caa10a4bcecca97630c67af4687","value":0.00020000}]' "2N1DFjaE4yCcECdFwgLQcLmNrLV5zetgQtE" 0.00002

Thank you for fast reply :)