Skip to content

Commit fd96317

Browse files
feat(utxo-core): add bip32derivation fields to BIP322 message signing
Add BIP32 derivation info to Psbt message signing for chain and index. Issue: BTC-2384
1 parent 851458d commit fd96317

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

modules/utxo-core/src/bip322/toSign.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,20 @@ export function buildToSignPsbtForChainAndIndex(
6565
if (isTaprootChain(chain)) {
6666
throw new Error('BIP322 is not supported for Taproot script types.');
6767
}
68-
const output = bitgo.outputScripts.createOutputScript2of3(
69-
rootWalletKeys.deriveForChainAndIndex(chain, index).publicKeys,
70-
bitgo.scriptTypeForChain(chain)
71-
);
68+
const walletKeys = rootWalletKeys.deriveForChainAndIndex(chain, index);
69+
const output = bitgo.outputScripts.createOutputScript2of3(walletKeys.publicKeys, bitgo.scriptTypeForChain(chain));
7270

73-
return buildToSignPsbt(message, {
71+
const psbt = buildToSignPsbt(message, {
7472
scriptPubKey: output.scriptPubKey,
7573
redeemScript: output.redeemScript,
7674
witnessScript: output.witnessScript,
7775
});
76+
77+
psbt.updateInput(
78+
0,
79+
bitgo.getPsbtBip32DerivationOutputUpdate(rootWalletKeys, walletKeys, bitgo.scriptTypeForChain(chain))
80+
);
81+
bitgo.addXpubsToPsbt(psbt, rootWalletKeys);
82+
83+
return psbt;
7884
}

modules/utxo-core/test/bip322/toSign.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,14 @@ describe('BIP322 toSign', function () {
5353
const toSpendTx = bip322.buildToSpendTransactionFromChainAndIndex(rootWalletKeys, chain, index, message);
5454
const toSignPsbt = bip322.buildToSignPsbtForChainAndIndex(message, rootWalletKeys, chain, index);
5555

56-
const derivedKeys = rootWalletKeys.deriveForChainAndIndex(chain, index);
57-
const prv1 = derivedKeys.triple[0];
58-
const prv2 = derivedKeys.triple[1];
59-
assert.ok(prv1);
60-
assert.ok(prv2);
61-
6256
// Can sign the PSBT with the keys
63-
toSignPsbt.signAllInputs(prv1, [utxolib.Transaction.SIGHASH_ALL]);
64-
toSignPsbt.signAllInputs(prv2, [utxolib.Transaction.SIGHASH_ALL]);
57+
// Should be able to use HD because we have the bip32Derivation information
58+
toSignPsbt.signAllInputsHD(rootWalletKeys.triple[0]);
59+
toSignPsbt.signAllInputsHD(rootWalletKeys.triple[1]);
6560

6661
// Wrap the PSBT as a UtxoPsbt so that we can use the validateSignaturesOfInputCommon method
6762
const utxopsbt = utxolib.bitgo.createPsbtFromBuffer(toSignPsbt.toBuffer(), utxolib.networks.bitcoin);
68-
derivedKeys.publicKeys.forEach((pubkey, i) => {
69-
assert.deepStrictEqual(
70-
utxopsbt.validateSignaturesOfInputCommon(0, pubkey),
71-
i !== 2,
72-
`Signature validation failed for public key at index ${i}`
73-
);
74-
});
63+
utxopsbt.validateSignaturesOfAllInputs();
7564

7665
// finalize and extract
7766
const tx = toSignPsbt.finalizeAllInputs().extractTransaction();
@@ -100,6 +89,7 @@ describe('BIP322 toSign', function () {
10089
assert.deepStrictEqual(toSpendTx.ins.length, 1, 'to_spend transaction must have one input');
10190
assert.deepStrictEqual(toSpendTx.outs.length, 1, 'to_spend transaction must have one output');
10291
assert.deepStrictEqual(toSpendTx.outs[0].value, BigInt(0), 'output value must be 0');
92+
const derivedKeys = rootWalletKeys.deriveForChainAndIndex(chain, index);
10393
assert.deepStrictEqual(
10494
toSpendTx.outs[0].script.toString('hex'),
10595
utxolib.bitgo.outputScripts

0 commit comments

Comments
 (0)