1
1
import assert from 'assert' ;
2
2
3
+ import * as utxolib from '@bitgo/utxo-lib' ;
3
4
import * as bitcoinMessage from 'bitcoinjs-message' ;
4
5
import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal' ;
5
6
import { KeyValue } from 'bip174/src/lib/interfaces' ;
6
7
import { checkForOutput } from 'bip174/src/lib/utils' ;
7
- import { bitgo , networks , testutil , bip32 , crypto , address } from '@bitgo/utxo-lib' ;
8
8
9
9
import {
10
10
addPaygoAddressProof ,
@@ -15,37 +15,42 @@ import {
15
15
import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils' ;
16
16
17
17
// To construct our PSBTs
18
- const network = networks . bitcoin ;
19
- const keys = [ 1 , 2 , 3 ] . map ( ( v ) => bip32 . fromSeed ( Buffer . alloc ( 16 , `test/2/${ v } ` ) , network ) ) ;
20
- const rootWalletKeys = new bitgo . RootWalletKeys ( [ keys [ 0 ] , keys [ 1 ] , keys [ 2 ] ] ) ;
21
- const psbtInputs = testutil . inputScriptTypes . map ( ( scriptType ) => ( { scriptType, value : BigInt ( 1000 ) } ) ) ;
22
- const psbtOutputs = testutil . outputScriptTypes . map ( ( scriptType ) => ( { scriptType, value : BigInt ( 900 ) } ) ) ;
23
- const dummyPub1 = rootWalletKeys . deriveForChainAndIndex ( 50 , 200 ) ;
18
+ const network = utxolib . networks . bitcoin ;
19
+ const keys = [ 1 , 2 , 3 ] . map ( ( v ) => utxolib . bip32 . fromSeed ( Buffer . alloc ( 16 , `test/2/${ v } ` ) , network ) ) ;
20
+ const rootWalletKeys = new utxolib . bitgo . RootWalletKeys ( [ keys [ 0 ] , keys [ 1 ] , keys [ 2 ] ] ) ;
21
+
22
+ // PSBT INPUTS AND OUTPUTS
23
+ const psbtInputs = utxolib . testutil . inputScriptTypes . map ( ( scriptType ) => ( {
24
+ scriptType,
25
+ value : BigInt ( 1000 ) ,
26
+ } ) ) ;
27
+ const psbtOutputs = utxolib . testutil . outputScriptTypes . map ( ( scriptType ) => ( {
28
+ scriptType,
29
+ value : BigInt ( 900 ) ,
30
+ } ) ) ;
31
+
24
32
// wallet pub and priv key for tbtc
33
+ const dummyPub1 = rootWalletKeys . deriveForChainAndIndex ( 50 , 200 ) ;
25
34
const attestationPubKey = dummyPub1 . user . publicKey ;
26
35
const attestationPrvKey = dummyPub1 . user . privateKey ! ;
27
- // const attestationPubKey =
28
- // 'xpub661MyMwAqRbcFU2Qx7pvGmmiQpVj8NcR7dSVpgqNChMkQyobpVWWERcrTb47WicmXwkhAY2VrC3hb29s18FDQWJf5pLm3saN6uLXAXpw1GV';
29
- // const attestationPrvKey = '3FlzrW1WuPbab2GWGyx+k/pHUNefDlw3SV0NQHLrWA+YEzqbvEQmosFGXMslYqtgpeIy6HiEoEvKzbNKM7yGY8GQHv7E++/sQRFDprOyklaW7GVyC2yEZe/LdaEfBvxf2VHBmu2hYubjsdHYF5+RQ3FhnyaNT+0=';
30
- // const keypair = ECPair.fromPrivateKey(Buffer.from(attestationPrvKey));
31
36
32
37
// UUID structure
33
38
const nilUUID = '00000000-0000-0000-0000-000000000000' ;
34
39
35
40
// our xpub converted to base58 address
36
- const addressFromPubkey = address . toBase58Check (
37
- crypto . hash160 ( Buffer . from ( attestationPubKey ) ) ,
38
- networks . bitcoin . pubKeyHash ,
39
- networks . bitcoin
41
+ const addressToVerify = utxolib . address . toBase58Check (
42
+ utxolib . crypto . hash160 ( Buffer . from ( dummyPub1 . backup . publicKey ) ) ,
43
+ utxolib . networks . bitcoin . pubKeyHash ,
44
+ utxolib . networks . bitcoin
40
45
) ;
46
+
41
47
// this should be retuning a Buffer
42
- const addressProofBuffer = generatePayGoAttestationProof ( nilUUID , Buffer . from ( addressFromPubkey ) ) ;
48
+ const addressProofBuffer = generatePayGoAttestationProof ( nilUUID , Buffer . from ( addressToVerify ) ) ;
43
49
// signature with the given msg addressProofBuffer
44
- // console.log(attestationPrvKey)
45
- const sig = bitcoinMessage . sign ( addressProofBuffer , attestationPrvKey ! ) ;
50
+ const sig = bitcoinMessage . sign ( addressProofBuffer , attestationPrvKey ! , true , network . messagePrefix ) ;
46
51
47
52
function getTestPsbt ( ) {
48
- return testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
53
+ return utxolib . testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
49
54
}
50
55
51
56
describe ( 'addPaygoAddressProof and verifyPaygoAddressProof' , ( ) => {
@@ -56,8 +61,8 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
56
61
} )
57
62
. filter ( ( keyValue ) => {
58
63
return (
59
- keyValue . key . identifier === bitgo . PSBT_PROPRIETARY_IDENTIFIER &&
60
- keyValue . key . subtype === bitgo . ProprietaryKeySubtype . PAYGO_ADDRESS_ATTESTATION_PROOF
64
+ keyValue . key . identifier === utxolib . bitgo . PSBT_PROPRIETARY_IDENTIFIER &&
65
+ keyValue . key . subtype === utxolib . bitgo . ProprietaryKeySubtype . PAYGO_ADDRESS_ATTESTATION_PROOF
61
66
) ;
62
67
} ) ;
63
68
}
@@ -70,16 +75,17 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
70
75
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
71
76
assert ( proofInPsbt . length === 1 ) ;
72
77
assert . throws (
73
- ( ) => verifyPaygoAddressProof ( psbt , 0 , Buffer . from ( 'Random Signed Message' ) ) ,
78
+ ( ) => verifyPaygoAddressProof ( psbt , 0 , Buffer . from ( 'Random Signed Message' ) , attestationPubKey ) ,
74
79
( e : any ) => e . message === 'Cannot verify the paygo address signature with the provided pubkey.'
75
80
) ;
76
81
} ) ;
77
82
78
83
it ( 'should add and verify a valid paygo address proof on the PSBT' , ( ) => {
79
- const outputIndex = 0 ;
80
84
const psbt = getTestPsbt ( ) ;
85
+ psbt . addOutput ( { script : utxolib . address . toOutputScript ( addressToVerify , network ) , value : BigInt ( 10000 ) } ) ;
86
+ const outputIndex = psbt . data . outputs . length - 1 ;
81
87
addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
82
- verifyPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) ) ;
88
+ verifyPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , attestationPubKey ) ;
83
89
} ) ;
84
90
85
91
it ( 'should throw an error if there are multiple PayGo proprietary keys in the PSBT' , ( ) => {
@@ -92,7 +98,7 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
92
98
assert ( proofInPsbt . length !== 0 ) ;
93
99
assert ( proofInPsbt . length > 1 ) ;
94
100
assert . throws (
95
- ( ) => verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer ) ,
101
+ ( ) => verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer , attestationPubKey ) ,
96
102
( e : any ) => e . message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.'
97
103
) ;
98
104
} ) ;
@@ -102,7 +108,7 @@ describe('verifyPaygoAddressProof', () => {
102
108
it ( 'should throw an error if there is no PayGo address in PSBT' , ( ) => {
103
109
const psbt = getTestPsbt ( ) ;
104
110
assert . throws (
105
- ( ) => verifyPaygoAddressProof ( psbt , 0 , addressProofBuffer ) ,
111
+ ( ) => verifyPaygoAddressProof ( psbt , 0 , addressProofBuffer , attestationPubKey ) ,
106
112
( e : any ) => e . message === 'There is no paygo address proof encoded in the PSBT at output 0.'
107
113
) ;
108
114
} ) ;
0 commit comments