Skip to content

Commit d9531d7

Browse files
Merge pull request #6713 from BitGo/BTC-2375
feat(utxo-core): implement function to extract BIP322 message by index
2 parents e62ac10 + 2666645 commit d9531d7

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ export function addBip322ProofMessage(psbt: utxolib.Psbt, inputIndex: number, me
1111
});
1212
}
1313

14-
export function getBip322ProofInputIndex(psbt: utxolib.Psbt): number | undefined {
15-
const res = psbt.data.inputs.flatMap((input, inputIndex) => {
16-
const proprietaryKeyVals = utxolib.bitgo.getPsbtInputProprietaryKeyVals(input, {
17-
identifier: utxolib.bitgo.PSBT_PROPRIETARY_IDENTIFIER,
18-
subtype: utxolib.bitgo.ProprietaryKeySubtype.BIP322_MESSAGE,
19-
});
20-
if (proprietaryKeyVals.length > 1) {
21-
throw new Error(`Multiple BIP322 messages found at input index ${inputIndex}`);
22-
}
23-
return proprietaryKeyVals.length === 0 ? [] : [inputIndex];
14+
/**
15+
* Get the BIP322 proof message at a specific input index of the PSBT
16+
* @param psbt
17+
* @param inputIndex
18+
* @returns The BIP322 proof message as a Buffer, or undefined if not found
19+
*/
20+
export function getBip322ProofMessageAtIndex(psbt: utxolib.Psbt, inputIndex: number): Buffer | undefined {
21+
if (psbt.data.inputs.length <= inputIndex) {
22+
throw new Error(`Input index ${inputIndex} is out of bounds for the PSBT`);
23+
}
24+
const input = psbt.data.inputs[inputIndex];
25+
const proprietaryKeyVals = utxolib.bitgo.getPsbtInputProprietaryKeyVals(input, {
26+
identifier: utxolib.bitgo.PSBT_PROPRIETARY_IDENTIFIER,
27+
subtype: utxolib.bitgo.ProprietaryKeySubtype.BIP322_MESSAGE,
2428
});
25-
return res.length === 0 ? undefined : res[0];
26-
}
27-
28-
export function psbtIsBip322Proof(psbt: utxolib.Psbt): boolean {
29-
return getBip322ProofInputIndex(psbt) !== undefined;
29+
if (proprietaryKeyVals.length === 0) {
30+
return undefined;
31+
} else if (proprietaryKeyVals.length > 1) {
32+
throw new Error(`Multiple BIP322 messages found at input index ${inputIndex}`);
33+
}
34+
return Buffer.from(proprietaryKeyVals[0].value);
3035
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'assert';
22

33
import * as utxolib from '@bitgo/utxo-lib';
44

5-
import { getBip322ProofInputIndex, psbtIsBip322Proof } from '../../src/bip322';
5+
import { getBip322ProofMessageAtIndex } from '../../src/bip322';
66

77
import { BIP322_FIXTURE_HELLO_WORLD_TOSIGN_PSBT } from './bip322.utils';
88

@@ -31,8 +31,8 @@ describe('BIP322 Proof utils', function () {
3131
utxolib.networks.bitcoin
3232
);
3333

34-
const resultIndex = getBip322ProofInputIndex(psbt);
35-
assert.strictEqual(resultIndex, 0);
36-
assert.ok(psbtIsBip322Proof(psbt));
34+
const messageBuffer = getBip322ProofMessageAtIndex(psbt, 0);
35+
assert.ok(messageBuffer, 'Message buffer should not be undefined');
36+
assert.deepStrictEqual(messageBuffer.toString('utf-8'), 'Hello World', 'Message does not match expected value');
3737
});
3838
});

0 commit comments

Comments
 (0)