77 Transaction as ITransaction ,
88 TransactionFromBuffer ,
99} from 'bip174/src/lib/interfaces' ;
10- import { checkForInput } from 'bip174/src/lib/utils' ;
10+ import { checkForInput , checkForOutput } from 'bip174/src/lib/utils' ;
1111import { BufferWriter , varuint } from 'bitcoinjs-lib/src/bufferutils' ;
1212import { SessionKey } from '@brandonblack/musig' ;
1313import { BIP32Factory , BIP32Interface } from 'bip32' ;
@@ -57,6 +57,7 @@ import { getTaprootOutputKey } from '../taproot';
5757import {
5858 getPsbtInputProprietaryKeyVals ,
5959 getPsbtInputSignatureCount ,
60+ getPsbtOutputProprietaryKeyVals ,
6061 ProprietaryKeySearch ,
6162 ProprietaryKeySubtype ,
6263 ProprietaryKeyValue ,
@@ -1109,7 +1110,7 @@ export class UtxoPsbt<Tx extends UtxoTransaction<bigint> = UtxoTransaction<bigin
11091110 }
11101111
11111112 /**
1112- * To search any data from proprietary key value against keydata.
1113+ * To search any data from proprietary key value against keydata in the inputs .
11131114 * Default identifierEncoding is utf-8 for identifier.
11141115 */
11151116 getProprietaryKeyVals ( inputIndex : number , keySearch ?: ProprietaryKeySearch ) : ProprietaryKeyValue [ ] {
@@ -1118,7 +1119,7 @@ export class UtxoPsbt<Tx extends UtxoTransaction<bigint> = UtxoTransaction<bigin
11181119 }
11191120
11201121 /**
1121- * To delete any data from proprietary key value.
1122+ * To delete any data from proprietary key value in PSBT input .
11221123 * Default identifierEncoding is utf-8 for identifier.
11231124 */
11241125 deleteProprietaryKeyVals ( inputIndex : number , keysToDelete ?: ProprietaryKeySearch ) : this {
@@ -1142,6 +1143,75 @@ export class UtxoPsbt<Tx extends UtxoTransaction<bigint> = UtxoTransaction<bigin
11421143 return this ;
11431144 }
11441145
1146+ /**
1147+ * Adds a proprietary key value pair to PSBT output
1148+ * Default identifier is utf-8 for identifier
1149+ */
1150+ addProprietaryKeyValToOutput ( outputIndex : number , keyValueData : ProprietaryKeyValue ) : this {
1151+ const output = checkForOutput ( this . data . outputs , outputIndex ) ;
1152+ assert ( output . unknownKeyVals ) ;
1153+ return this . addUnknownKeyValToOutput ( outputIndex , {
1154+ key : encodeProprietaryKey ( keyValueData . key ) ,
1155+ value : keyValueData . value ,
1156+ } ) ;
1157+ }
1158+
1159+ /**
1160+ * To search any data from proprietary key value against keydata in the PSBT outputs.
1161+ * Default identifierEncoding is utf-8 for identifier.
1162+ */
1163+ getOutputProprietaryKeyVals ( outputIndex : number , keySearch ?: ProprietaryKeySearch ) : ProprietaryKeyValue [ ] {
1164+ const output = checkForOutput ( this . data . outputs , outputIndex ) ;
1165+ return getPsbtOutputProprietaryKeyVals ( output , keySearch ) ;
1166+ }
1167+
1168+ /**
1169+ * Adds or updates (if exists) proprietary key value pair to PSBT output.
1170+ * Default identifierEncoding is utf-8 for identifier.
1171+ */
1172+ addOrUpdateProprietaryKeyValsToOutput ( outputIndex : number , keyValueData : ProprietaryKeyValue ) : this {
1173+ const output = checkForOutput ( this . data . outputs , outputIndex ) ;
1174+ const key = encodeProprietaryKey ( keyValueData . key ) ;
1175+ const { value } = keyValueData ;
1176+ if ( output . unknownKeyVals ?. length ) {
1177+ const ukvIndex = output . unknownKeyVals . findIndex ( ( ukv ) => ukv . key . equals ( key ) ) ;
1178+ if ( ukvIndex > - 1 ) {
1179+ output . unknownKeyVals [ ukvIndex ] = { key, value } ;
1180+ return this ;
1181+ }
1182+ }
1183+ this . addUnknownKeyValToOutput ( outputIndex , {
1184+ key,
1185+ value,
1186+ } ) ;
1187+ return this ;
1188+ }
1189+
1190+ /**
1191+ * To delete any data from proprietary key value in PSBT output.
1192+ * Default identifierEncoding is utf-8 for identifier.
1193+ */
1194+ deleteProprietaryKeyValsInOutput ( outputIndex : number , keysToDelete ?: ProprietaryKeySearch ) : this {
1195+ const output = checkForOutput ( this . data . outputs , outputIndex ) ;
1196+ if ( ! output . unknownKeyVals ?. length ) {
1197+ return this ;
1198+ }
1199+ if ( keysToDelete && keysToDelete . subtype === undefined && Buffer . isBuffer ( keysToDelete . keydata ) ) {
1200+ throw new Error ( 'invalid proprietary key search filter combination. subtype is required' ) ;
1201+ }
1202+ output . unknownKeyVals = output . unknownKeyVals . filter ( ( keyValue , i ) => {
1203+ const key = decodeProprietaryKey ( keyValue . key ) ;
1204+ return ! (
1205+ keysToDelete === undefined ||
1206+ ( keysToDelete . identifier === key . identifier &&
1207+ ( keysToDelete . subtype === undefined ||
1208+ ( keysToDelete . subtype === key . subtype &&
1209+ ( ! Buffer . isBuffer ( keysToDelete . keydata ) || keysToDelete . keydata . equals ( key . keydata ) ) ) ) )
1210+ ) ;
1211+ } ) ;
1212+ return this ;
1213+ }
1214+
11451215 private createMusig2NonceForInput (
11461216 inputIndex : number ,
11471217 keyPair : BIP32Interface ,
0 commit comments