1
1
import * as assert from 'assert' ;
2
+ import * as bitcoinMessage from 'bitcoinjs-message' ;
2
3
import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal' ;
3
4
import { KeyValue } from 'bip174/src/lib/interfaces' ;
4
5
import { checkForOutput } from 'bip174/src/lib/utils' ;
@@ -14,29 +15,31 @@ import {
14
15
psbtOutputIncludesPaygoAddressProof ,
15
16
} from '../../../src/bitgo/psbt/paygoAddressProof' ;
16
17
18
+ // To construct our PSBTs
17
19
const network = networks . bitcoin ;
18
20
const keys = [ 1 , 2 , 3 ] . map ( ( v ) => bip32 . fromSeed ( Buffer . alloc ( 16 , `test/2/${ v } ` ) , network ) ) ;
19
21
const rootWalletKeys = new RootWalletKeys ( [ keys [ 0 ] , keys [ 1 ] , keys [ 2 ] ] ) ;
20
- // const dummyKey1 = rootWalletKeys.deriveForChainAndIndex(50, 200);
21
- const dummyKey2 = rootWalletKeys . deriveForChainAndIndex ( 60 , 201 ) ;
22
-
23
22
const psbtInputs = inputScriptTypes . map ( ( scriptType ) => ( { scriptType, value : BigInt ( 1000 ) } ) ) ;
24
23
const psbtOutputs = outputScriptTypes . map ( ( scriptType ) => ( { scriptType, value : BigInt ( 900 ) } ) ) ;
25
- // const dummy1PubKey = dummyKey1.user.publicKey;
26
- // This generatePayGoAttestationProof function should be returning the bitcoin signed message
27
- const sig2 = dummyKey2 . user . privateKey ! ;
28
24
29
25
// wallet pub and priv key for tbtc
30
26
const attestationPubKey =
31
27
'xpub661MyMwAqRbcFU2Qx7pvGmmiQpVj8NcR7dSVpgqNChMkQyobpVWWERcrTb47WicmXwkhAY2VrC3hb29s18FDQWJf5pLm3saN6uLXAXpw1GV' ;
32
- const attestationPrvKey = 'red' ;
28
+ const attestationPrvKey = 'privkeyforpaygoattestationabdef12345' ;
29
+
30
+ // UUID structure
33
31
const nilUUID = '00000000-0000-0000-0000-000000000000' ;
32
+
33
+ // our xpub converted to base58 address
34
34
const address = toBase58Check (
35
35
crypto . hash160 ( Buffer . from ( attestationPubKey ) ) ,
36
36
networks . bitcoin . pubKeyHash ,
37
37
networks . bitcoin
38
38
) ;
39
+ // this should be retuning a Buffer
39
40
const addressProofBuffer = generatePayGoAttestationProof ( Buffer . from ( attestationPrvKey ) , nilUUID , address ) ;
41
+ // signature with the given msg addressProofBuffer
42
+ const sig = bitcoinMessage . sign ( addressProofBuffer , Buffer . from ( attestationPrvKey ) ) ;
40
43
41
44
function getTestPsbt ( ) {
42
45
return testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
@@ -59,36 +62,34 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
59
62
it ( "should fail a proof verification if the proof isn't valid" , ( ) => {
60
63
const outputIndex = 0 ;
61
64
const psbt = getTestPsbt ( ) ;
62
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , Buffer . from ( attestationPubKey ) ) ;
65
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
63
66
const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
64
67
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
65
68
assert ( proofInPsbt . length === 1 ) ;
66
69
assert . throws (
67
- ( ) => verifyPaygoAddressProof ( psbt , 0 , dummyKey2 . user . publicKey ) ,
70
+ ( ) => verifyPaygoAddressProof ( psbt , 0 , Buffer . from ( 'Random Signed Message' ) ) ,
68
71
( e : any ) => e . message === 'Cannot verify the paygo address signature with the provided pubkey.'
69
72
) ;
70
73
} ) ;
71
74
72
75
it ( 'should add and verify a valid paygo address proof on the PSBT' , ( ) => {
73
76
const outputIndex = 0 ;
74
77
const psbt = getTestPsbt ( ) ;
75
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , Buffer . from ( attestationPubKey ) ) ;
76
- // should verify function return a boolean? that way we can assert
77
- // if this is verified, throws an error otherwise or false + error msg as an object
78
- verifyPaygoAddressProof ( psbt , outputIndex , Buffer . from ( attestationPubKey ) ) ;
78
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
79
+ verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer ) ;
79
80
} ) ;
80
81
81
82
it ( 'should throw an error if there are multiple PayGo proprietary keys in the PSBT' , ( ) => {
82
83
const outputIndex = 0 ;
83
84
const psbt = getTestPsbt ( ) ;
84
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , Buffer . from ( attestationPubKey ) ) ;
85
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( sig2 ) , Buffer . from ( attestationPubKey ) ) ;
85
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
86
+ addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( 'signature2' ) , Buffer . from ( attestationPubKey ) ) ;
86
87
const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
87
88
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
88
89
assert ( proofInPsbt . length !== 0 ) ;
89
90
assert ( proofInPsbt . length <= 1 ) ;
90
91
assert . throws (
91
- ( ) => verifyPaygoAddressProof ( psbt , outputIndex , Buffer . from ( attestationPubKey ) ) ,
92
+ ( ) => verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer ) ,
92
93
( e : any ) => e . message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.'
93
94
) ;
94
95
} ) ;
@@ -98,7 +99,7 @@ describe('verifyPaygoAddressProof', () => {
98
99
it ( 'should throw an error if there is no PayGo address in PSBT' , ( ) => {
99
100
const psbt = getTestPsbt ( ) ;
100
101
assert . throws (
101
- ( ) => verifyPaygoAddressProof ( psbt , 0 , Buffer . from ( attestationPubKey ) ) ,
102
+ ( ) => verifyPaygoAddressProof ( psbt , 0 , addressProofBuffer ) ,
102
103
( e : any ) => e . message === 'here is no paygo address proof encoded in the PSBT.'
103
104
) ;
104
105
} ) ;
@@ -108,7 +109,7 @@ describe('getPaygoAddressProofIndex', () => {
108
109
it ( 'should get PayGo address proof index from PSBT if there is one' , ( ) => {
109
110
const psbt = getTestPsbt ( ) ;
110
111
const outputIndex = 0 ;
111
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , Buffer . from ( attestationPubKey ) ) ;
112
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
112
113
assert ( psbtOutputIncludesPaygoAddressProof ( psbt ) ) ;
113
114
assert ( getPaygoAddressProofOutputIndex ( psbt ) === 0 ) ;
114
115
} ) ;
@@ -122,13 +123,8 @@ describe('getPaygoAddressProofIndex', () => {
122
123
it ( 'should return an error and fail if we have multiple PayGo address in the PSBT in the same output index' , ( ) => {
123
124
const psbt = getTestPsbt ( ) ;
124
125
const outputIndex = 0 ;
125
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , Buffer . from ( attestationPubKey ) ) ;
126
- addPaygoAddressProof (
127
- psbt ,
128
- outputIndex ,
129
- Buffer . from ( addressProofBuffer ) ,
130
- Buffer . from ( 'xpub12345abcdef29a028510d3b2d4' )
131
- ) ;
126
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
127
+ addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( 'xpub12345abcdef29a028510d3b2d4' ) ) ;
132
128
assert . throws (
133
129
( ) => getPaygoAddressProofOutputIndex ( psbt ) ,
134
130
( e : any ) => e . message === 'There are multiple PayGo addresses in the PSBT output 0.'
0 commit comments