@@ -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}
0 commit comments