bitcoinjs / bitcoinjs-lib

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

Problem broadcasting with script on input 0 #1514

Closed aitorjs closed 3 years ago

aitorjs commented 4 years ago

Hello!

Having this code:

const alice = ECPair.fromWIF(
  'cQVWFq9CMaFtUPrYat4by6JQuWDoiSB2uzJR84nzDmdUBkkY6YTM',
  TESTNET
)

const p2pkh = payments.p2pkh({ pubkey: alice.publicKey, network: TESTNET })

const txb = new TransactionBuilder(TESTNET)

const txid = '58993dce9fec2ba9096080eb85ae996080c6eaa9f7715e6cc3d95ba7958d7fc3'
const vout = 0

txb.addInput(txid, vout, null, p2pkh.output)
txb.addOutput('2N2kuwmUqaYm3iifDrrxXQW6oW2uGX46Pf6', 500000)

txb.sign(0, alice)
const tx2 = txb.build()
console.log('tx', tx2.toHex())

I get this hexadecimal, 0200000001c37f8d95a75bd9c36c5e71f7a9eac6806099ae85eb806009a92bec9fce3d9958000000006b48304502210085b7074689d0ce164a65a8d2d23ed3a194dad8d549ba4f8625869a4f9778281a02201237b0ebe2e0ee11d6abdf8ae1dc71a23b2f9c92cbdbc1fe7cb4b7f1ab39d8ff0121022761f7b06edd78c2c0aaf6f222da02b5b7668269f42467fcd4cff7cfb26eb640ffffffff0120a107000000000017a91468568c8e852f81ccc8503abe3d140e0aaba506328700000000

When I send using blockcypher to the network, I get this validation error: Error validating transaction: Error running script for input 0 referencing 58993dce9fec2ba9096080eb85ae996080c6eaa9f7715e6cc3d95ba7958d7fc3 at 0: Script was NOT verified successfully..

If I try to get the address of the WIF private key I'm using, I get a different address:

const alice = ECPair.fromWIF(
  'cQVWFq9CMaFtUPrYat4by6JQuWDoiSB2uzJR84nzDmdUBkkY6YTM',
  TESTNET
)
const { address } = payments.p2pkh({ pubkey: alice.publicKey, network: TESTNET })
const publicKey = alice.publicKey.toString('hex')
const privateKey = alice.toWIF()
console.log('alice address', { address, privateKey, publicKey })
{ address: 'mzWY7FWn3PYDMPXKW62yywRuYKawpqXmZ8',
  privateKey: 'cQVWFq9CMaFtUPrYat4by6JQuWDoiSB2uzJR84nzDmdUBkkY6YTM',
  publicKey:
   '022761f7b06edd78c2c0aaf6f222da02b5b7668269f42467fcd4cff7cfb26eb640' }

I dont know if the WIF is the problem to get the validation error I get, the type of UTXO or something else.

¿Any ideas please?

Thank you very much :)

UTXOs for "2N8K8uf9ND1fQGxZdBUb9whd73Ukwi8ZEJu" :

 bitcoin-cli --testnet listunspent
[
  {
    "txid": "58993dce9fec2ba9096080eb85ae996080c6eaa9f7715e6cc3d95ba7958d7fc3",
    "vout": 0,
    "address": "2N8K8uf9ND1fQGxZdBUb9whd73Ukwi8ZEJu",
    "redeemScript": "0014c4a10a23bceb0e7b659fac1ef2ff2dc752cdd457",
    "scriptPubKey": "a914a5474f6c0bd2b26b84738bb717cdc44f5d5df4a187",
    "amount": 0.02197712,
    "confirmations": 3,
    "spendable": true,
    "solvable": true,
    "safe": true
  },
]
junderw commented 4 years ago

Your address is a p2sh-p2wpkh, not a p2pkh.

Please see the integration tests for an example.

aitorjs commented 4 years ago

Thanks for the fast reply :-)

¿Are you speaking about this [0] integration test?

I try to use the code but I have ended up breaking my head, especially with regtestUtils, without any result.

Next. I try with TransactionBuilder instead of PSBT because the tests are much more clear there. I use this tests [1]. Same validation error trying to broadcast the transaction with this hex : 02000000000101c37f8d95a75bd9c36c5e71f7a9eac6806099ae85eb806009a92bec9fce3d99580000000017160014d0563341a19c68839cc560a647f84583218842feffffffff01204e00000000000017a91468568c8e852f81ccc8503abe3d140e0aaba506328702473044022007dd261b865d2e0c92e9e420e997061f878e4b40458564e7ce8d1313742027d102201bc92583f826f2b0094e18356dfeb15e26907bac8bc98bc1917845f1456439080121022761f7b06edd78c2c0aaf6f222da02b5b7668269f42467fcd4cff7cfb26eb64000000000 and this code:


const unspent = {
  "txId": "58993dce9fec2ba9096080eb85ae996080c6eaa9f7715e6cc3d95ba7958d7fc3",
  "vout": 0
}
const keyPair = ECPair.fromWIF(
  'cQVWFq9CMaFtUPrYat4by6JQuWDoiSB2uzJR84nzDmdUBkkY6YTM',
  TESTNET
)
const p2wpkh = payments.p2wpkh({ pubkey: keyPair.publicKey, network: TESTNET })
const p2sh = payments.p2sh({ redeem: p2wpkh, network: TESTNET })

const txb = new TransactionBuilder(TESTNET)
txb.addInput(unspent.txId, unspent.vout)
txb.addOutput('2N2kuwmUqaYm3iifDrrxXQW6oW2uGX46Pf6', 2e4)
txb.sign(0, keyPair, p2sh.redeem.output, null, 10000)

const tx = txb.build().toHex()

Meanwhile, I try this with bitcoin-cli and I get the valid hex mainly using this command: bitcoin-cli -named createrawtransaction inputs='''[ { "txid": "'$utxo_txid'", "vout": '$utxo_vout' } ]''' outputs='''{ "data": "'$op_return_data'", "'$changeaddress'": 0.09123014 }''' [2]

Thank you very much! :-D

[0] https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts#L241 [1] https://github.com/bitcoinjs/bitcoinjs-lib/blob/582727f6de251441c75027a6292699b6f1e1b8f2/test/integration/transactions.js#L136 [2] https://live.blockcypher.com/btc-testnet/tx/e8cd0001a0e91e49fcb9490170401700aa73558b9e8bebdb84561fa8be7d2042/