bitcoinjs-lib
bitcoinjs-lib copied to clipboard
How Can I sign transaction from two or more account to a recipient
Please how can I handle a situation where I have bitcoin on two or more different wallets with a single phrase, how can I sign the two transactions to a friend.
Example: I have 2BTC in a wallet address that is at path m/44'/0'/0'/0'/0 I also have 1BTC in another wallet address that is at path m/44'/0'/0'/0'/1
How can I sign the 3BTC to a friend.
Thanks!
Using the PSBT class you can sign each input individually. Have a look at the PSBT examples.
Before signing you must derive the two key-pairs from their respective master key.
@motorina0
Alright, Thanks a million.
Something like this 👇
`const bitcoin = require('bitcoinjs-lib'); const privateKey1 = 'private_key1'; // replace with your private key for address 1 const privateKey2 = 'private_key2'; // replace with your private key for address 2 const addressTo = 'recipient_address'; // replace with the recipient's BTC address
// Create a transaction builder const txb = new bitcoin.TransactionBuilder();
// Add inputs from the two addresses txb.addInput('txid1', 0); // replace with the transaction ID and output index for address 1 txb.addInput('txid2', 1); // replace with the transaction ID and output index for address 2
// Add the output to the recipient's address txb.addOutput(addressTo, 5000); // replace with the amount of BTC you want to send
// Sign the inputs with the private keys const keyPair1 = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKey1, 'hex')); txb.sign(0, keyPair1); // sign the first input const keyPair2 = bitcoin.ECPair.fromPrivateKey(Buffer.from(privateKey2, 'hex')); txb.sign(1, keyPair2); // sign the second input
// Build the transaction const tx = txb.build();
// Print the transaction in hex format console.log(tx.toHex());`
TransactionBuilderclass has been removed some time ago.- you should use the PSBT class. That is the standard way to build & share transactions
Alright 👍