bitcoinjs / bitcoinjs-lib

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

Can not generate valid hex (transaction) #1411

Closed tiehm closed 5 years ago

tiehm commented 5 years ago

Hi!

No matter how often I try, the generated HEX seems to be invalid when I try to decode it on https://testnet.smartbit.com.au/txs/decodetx

import { ECPair, networks, TransactionBuilder } from 'bitcoinjs-lib';

const sender = ECPair.fromWIF('cQXwgpbjJTnQshUzcyFWkK2Vy17TgfoxH4iBD8gEvfGjLeYW5AAx', networks.testnet);

const txb = new TransactionBuilder(networks.testnet);
txb.setVersion(1);
txb.addInput('b6a80cad7def0d648fbce9b0a38ffa117eba26064975702cd2cbdbc04ea152b0', 0);
txb.addOutput('mv4rnyY3Su5gjcDNzbMLKBQkBicCtHUtFB', 5590000);

txb.sign(0, sender);
const txHex = txb.build().toHex();

The generated HEX is

0100000001b052a14ec0dbcbd22c7075490626ba7e11fa8fa3b0e9bc8f640def7dad0ca8b6000000006a473044022072596fd769f627a77ef49c4d4e549752d1e633ef549de05b8ddf8eaa994ee23c02207f9aa1ed7183973b2ac420e8b356b2c0d6265b4f77dc28e50349059a40eeb7f401210338ee3d6546e88f6193ea207dc0281c8ae
0ae5d17a48f5d4647be6afb2905077bffffffff01f04b5500000000001976a9149f9a7abd600c0caa03983a77c8c3df8e062cb2fa88ac00000000

The Testnet BTC address is 2My21TchEytPUHj2ESoXp2ntRfY7t1vbdMA. The input transaction should be correct as I see here https://testnet-api.smartbit.com.au/v1/blockchain/address/2My21TchEytPUHj2ESoXp2ntRfY7t1vbdMA/unspent

Could someone please help me find where that error is. Whenever I try to push it via the blockcypher API I am getting the error Error validating transaction: Transaction ... orphaned, missing reference ...

junderw commented 5 years ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/b3def6b4006683190657ef40efa7a8bcbb78b5cd/test/integration/transactions.js#L126-L149

You are signing for P2PKH when you should be signing for P2SH-P2WPKH.