1
- import { Psbt , Transaction , bitgo } from '@bitgo/utxo-lib' ;
1
+ import { Psbt , bitgo } from '@bitgo/utxo-lib' ;
2
2
3
- import { isTaprootChain } from './utils' ;
3
+ import { addBip322ProofMessage , isTaprootChain } from './utils' ;
4
+ import { BIP322_TAG , buildToSpendTransaction } from './toSpend' ;
4
5
5
6
export type AddressDetails = {
6
7
redeemScript ?: Buffer ;
@@ -13,11 +14,14 @@ export type AddressDetails = {
13
14
* Source implementation:
14
15
* https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki#full
15
16
*
16
- * @param {string } toSpendTxHex - The hex representation of the `toSpend ` transaction.
17
+ * @param {string } message - The message that is hashed into the `to_spend ` transaction.
17
18
* @param {AddressDetails } addressDetails - The details of the address, including redeemScript and/or witnessScript.
18
- * @returns {string } - The hex representation of the constructed PSBT.
19
+ * @param {string } [tag=BIP322_TAG] - The tag to use for hashing, defaults to BIP322_TAG.
20
+ * @returns {Psbt } - The hex representation of the constructed PSBT.
19
21
*/
20
- export function buildToSignPsbt ( toSpendTx : Transaction < bigint > , addressDetails : AddressDetails ) : Psbt {
22
+ export function buildToSignPsbt ( message : string , addressDetails : AddressDetails , tag = BIP322_TAG ) : Psbt {
23
+ const toSpendTx = buildToSpendTransaction ( addressDetails . scriptPubKey , message , tag ) ;
24
+
21
25
// Create PSBT object for constructing the transaction
22
26
const psbt = new Psbt ( ) ;
23
27
// Set default value for nVersion and nLockTime
@@ -41,6 +45,9 @@ export function buildToSignPsbt(toSpendTx: Transaction<bigint>, addressDetails:
41
45
psbt . updateInput ( 0 , { witnessScript : addressDetails . witnessScript } ) ;
42
46
}
43
47
48
+ // Add the message as a proprietary key value to the PSBT so we can verify it later
49
+ addBip322ProofMessage ( psbt as bitgo . UtxoPsbt , 0 , Buffer . from ( message ) ) ;
50
+
44
51
// Set the output
45
52
psbt . addOutput ( {
46
53
value : BigInt ( 0 ) , // vout[0].nValue = 0
@@ -50,7 +57,7 @@ export function buildToSignPsbt(toSpendTx: Transaction<bigint>, addressDetails:
50
57
}
51
58
52
59
export function buildToSignPsbtForChainAndIndex (
53
- toSpendTx : Transaction < bigint > ,
60
+ message : string ,
54
61
rootWalletKeys : bitgo . RootWalletKeys ,
55
62
chain : bitgo . ChainCode ,
56
63
index : number
@@ -63,12 +70,7 @@ export function buildToSignPsbtForChainAndIndex(
63
70
bitgo . scriptTypeForChain ( chain )
64
71
) ;
65
72
66
- const toSpendScriptPubKey = toSpendTx . outs [ 0 ] . script ;
67
- if ( ! toSpendScriptPubKey . equals ( output . scriptPubKey ) ) {
68
- throw new Error ( 'Output scriptPubKey does not match the expected output script for the chain and index.' ) ;
69
- }
70
-
71
- return buildToSignPsbt ( toSpendTx , {
73
+ return buildToSignPsbt ( message , {
72
74
scriptPubKey : output . scriptPubKey ,
73
75
redeemScript : output . redeemScript ,
74
76
witnessScript : output . witnessScript ,
0 commit comments