Closed iamgamelover closed 4 years ago
Bitcoinjs-lib support BIP174 PSBT(Partially Signed Bitcoin Transaction Format). You can build a tx with PSBT format, after Alice signed the transaction she can export tx as hex format (psbt.toHex()
) or base64 format(psbt.toBase64()
). Bob can import it (.fromBase64()
) then sign last one. You can check the below examples:
https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts
@longhoangwkm Thank you for your reply.
In examples that using addInput, addOutput to create a transaction. For my actual situation, I'm creating a raw transaction for Omni Layer platform on one machine. That has 8 steps to create a tx. Explain in https://github.com/OmniLayer/omnicore/wiki/Use-the-raw-transaction-API-to-create-a-Simple-Send-transaction.
So, situation is:
a) On NO.1 machine creating a Omni raw transaction from step 1 to 6 and will get a hex.
b) Send the hex to Alice (NO.2 machine). Alice create a txb and sign it with code below.
let txb = btctool.bitcoin.TransactionBuilder.fromTransaction (
btctool.bitcoin.Transaction.fromHex (txhex), BTC_TESTNET);
... ...
// Alice sign and export hex (How to get the hex?)
c) Send the hex from Alice's machine to Bob (NO.3 machine). Bob create a txb with Alice's hex and sign it.
let txb = btctool.bitcoin.TransactionBuilder.fromTransaction (
btctool.bitcoin.Transaction.fromHex (Alice's hex), BTC_TESTNET);
... ...
// Bob sign and get final hex to be able to broadcast.
So, in my case, How to solve this problem with PSBT or TransactionBuilder?
The problem has been fixed! I got the way with txb.buildIncomplete()
function. Now, the used 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 p2sh = btctool.bitcoin.payments.p2sh({ redeem: p2ms, network: BTC_TESTNET });
const wifs = [
'cUAdadTkjeVFsNz5ifhkETfAzk5PvhnLWtmdSKgbyTTjSCE4MYWy',
'cV6dif91LHD8Czk8BTgvYZR3ipUrqyMDMtUXSWsThqpHaQJUuHKA',
].map((wif) => btctool.bitcoin.ECPair.fromWIF(wif, BTC_TESTNET));
// Alice sign
txb.sign(0, wifs[0], p2sh.redeem.output, undefined, 20000, undefined);
// Got the way
let aliceHex = txb.buildIncomplete();
console.info('aliceHex => ' + aliceHex.toHex());
//----------------------------------
// Bob sign (on an other machine)
txb = btctool.bitcoin.TransactionBuilder.fromTransaction (
btctool.bitcoin.Transaction.fromHex (aliceHex.toHex()), BTC_TESTNET);
txb.sign(0, wifs[1], p2sh.redeem.output, undefined, 20000, undefined);
// Final hex to broadcast.
let sig = txb.build().toHex();
console.info('sig multisig => ' + sig);
That's done. Thanks, everyone!
just FYI, TransactionBuilder is going to be removed in the next major version.
@junderw Got it, thanks! :)
Hello everyone, I have a case is this :
First, Alice use her private key to sign a 2-2 multisig p2sh transaction to output a hex string. And send the hex to Bob (an other machine) to sign the transaction with Bob's private key. Then should be get the final hex that be able to broadcast.
Question is: How to get the hex that after Alice sign the transaction?
Used code is here:
Anyone could be give me some suggestion. Thanks!