1
1
import assert from 'assert' ;
2
+ import crypto from 'crypto' ;
2
3
3
4
import * as utxolib from '@bitgo/utxo-lib' ;
4
- import * as bitcoinMessage from 'bitcoinjs-message' ;
5
5
import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal' ;
6
6
import { KeyValue } from 'bip174/src/lib/interfaces' ;
7
7
import { checkForOutput } from 'bip174/src/lib/utils' ;
8
8
9
9
import {
10
- addPaygoAddressProof ,
11
- getPaygoAddressProofOutputIndex ,
10
+ addPayGoAddressProof ,
11
+ createPayGoAttestationBuffer ,
12
+ getPayGoAddressProofOutputIndex ,
12
13
psbtOutputIncludesPaygoAddressProof ,
13
- verifyPaygoAddressProof ,
14
+ verifyPayGoAddressProof ,
14
15
} from '../../../src/paygo/psbt/PayGoUtils' ;
15
16
import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils' ;
17
+ import { parseVaspProof } from '../../../src/testutil/parseVaspProof' ;
18
+ import { signMessage } from '../../../src/bip32utils' ;
16
19
17
20
// To construct our PSBTs
18
21
const network = utxolib . networks . bitcoin ;
@@ -35,7 +38,7 @@ const attestationPubKey = dummyPub1.user.publicKey;
35
38
const attestationPrvKey = dummyPub1 . user . privateKey ! ;
36
39
37
40
// UUID structure
38
- const nilUUID = '00000000-0000-0000-0000-000000000000' ;
41
+ const nillUUID = '00000000-0000-0000-0000-000000000000' ;
39
42
40
43
// our xpub converted to base58 address
41
44
const addressToVerify = utxolib . address . toBase58Check (
@@ -45,9 +48,13 @@ const addressToVerify = utxolib.address.toBase58Check(
45
48
) ;
46
49
47
50
// this should be retuning a Buffer
48
- const addressProofBuffer = generatePayGoAttestationProof ( nilUUID , Buffer . from ( addressToVerify ) ) ;
51
+ const addressProofBuffer = generatePayGoAttestationProof ( nillUUID , Buffer . from ( addressToVerify ) ) ;
52
+ const addressProofMsgBuffer = parseVaspProof ( addressProofBuffer ) ;
53
+ // We know that that the entropy is a set 64 bytes.
54
+ const addressProofEntropy = addressProofMsgBuffer . subarray ( 0 , 65 ) ;
55
+
49
56
// signature with the given msg addressProofBuffer
50
- const sig = bitcoinMessage . sign ( addressProofBuffer , attestationPrvKey ! , true , network . messagePrefix ) ;
57
+ const sig = signMessage ( addressProofMsgBuffer . toString ( ) , attestationPrvKey ! , network ) ;
51
58
52
59
function getTestPsbt ( ) {
53
60
return utxolib . testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
@@ -67,38 +74,25 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
67
74
} ) ;
68
75
}
69
76
70
- it ( "should fail a proof verification if the proof isn't valid" , ( ) => {
71
- const outputIndex = 0 ;
72
- const psbt = getTestPsbt ( ) ;
73
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
74
- const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
75
- const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
76
- assert ( proofInPsbt . length === 1 ) ;
77
- assert . throws (
78
- ( ) => verifyPaygoAddressProof ( psbt , 0 , Buffer . from ( 'Random Signed Message' ) , attestationPubKey ) ,
79
- ( e : any ) => e . message === 'Cannot verify the paygo address signature with the provided pubkey.'
80
- ) ;
81
- } ) ;
82
-
83
77
it ( 'should add and verify a valid paygo address proof on the PSBT' , ( ) => {
84
78
const psbt = getTestPsbt ( ) ;
85
79
psbt . addOutput ( { script : utxolib . address . toOutputScript ( addressToVerify , network ) , value : BigInt ( 10000 ) } ) ;
86
80
const outputIndex = psbt . data . outputs . length - 1 ;
87
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
88
- verifyPaygoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , attestationPubKey ) ;
81
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
82
+ verifyPayGoAddressProof ( psbt , outputIndex , attestationPubKey ) ;
89
83
} ) ;
90
84
91
85
it ( 'should throw an error if there are multiple PayGo proprietary keys in the PSBT' , ( ) => {
92
86
const outputIndex = 0 ;
93
87
const psbt = getTestPsbt ( ) ;
94
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
95
- addPaygoAddressProof ( psbt , outputIndex , Buffer . from ( 'signature2' ) , Buffer . from ( 'fakepubkey2s' ) ) ;
88
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
89
+ addPayGoAddressProof ( psbt , outputIndex , Buffer . from ( 'signature2' ) , crypto . randomBytes ( 64 ) ) ;
96
90
const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
97
91
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
98
92
assert ( proofInPsbt . length !== 0 ) ;
99
93
assert ( proofInPsbt . length > 1 ) ;
100
94
assert . throws (
101
- ( ) => verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer , attestationPubKey ) ,
95
+ ( ) => verifyPayGoAddressProof ( psbt , outputIndex , attestationPubKey ) ,
102
96
( e : any ) => e . message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.'
103
97
) ;
104
98
} ) ;
@@ -108,7 +102,7 @@ describe('verifyPaygoAddressProof', () => {
108
102
it ( 'should throw an error if there is no PayGo address in PSBT' , ( ) => {
109
103
const psbt = getTestPsbt ( ) ;
110
104
assert . throws (
111
- ( ) => verifyPaygoAddressProof ( psbt , 0 , addressProofBuffer , attestationPubKey ) ,
105
+ ( ) => verifyPayGoAddressProof ( psbt , 0 , attestationPubKey ) ,
112
106
( e : any ) => e . message === 'There is no paygo address proof encoded in the PSBT at output 0.'
113
107
) ;
114
108
} ) ;
@@ -118,25 +112,43 @@ describe('getPaygoAddressProofIndex', () => {
118
112
it ( 'should get PayGo address proof index from PSBT if there is one' , ( ) => {
119
113
const psbt = getTestPsbt ( ) ;
120
114
const outputIndex = 0 ;
121
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
115
+ addPayGoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
122
116
assert ( psbtOutputIncludesPaygoAddressProof ( psbt ) ) ;
123
- assert ( getPaygoAddressProofOutputIndex ( psbt ) === 0 ) ;
117
+ assert ( getPayGoAddressProofOutputIndex ( psbt ) === 0 ) ;
124
118
} ) ;
125
119
126
120
it ( 'should return undefined if there is no PayGo address proof in PSBT' , ( ) => {
127
121
const psbt = getTestPsbt ( ) ;
128
- assert ( getPaygoAddressProofOutputIndex ( psbt ) === undefined ) ;
122
+ assert ( getPayGoAddressProofOutputIndex ( psbt ) === undefined ) ;
129
123
assert ( ! psbtOutputIncludesPaygoAddressProof ( psbt ) ) ;
130
124
} ) ;
131
125
132
126
it ( 'should return an error and fail if we have multiple PayGo address in the PSBT in the same output index' , ( ) => {
133
127
const psbt = getTestPsbt ( ) ;
134
128
const outputIndex = 0 ;
135
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
136
- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( 'xpub12345abcdef29a028510d3b2d4' ) ) ;
129
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
130
+ addPayGoAddressProof ( psbt , outputIndex , sig , crypto . randomBytes ( 64 ) ) ;
137
131
assert . throws (
138
- ( ) => getPaygoAddressProofOutputIndex ( psbt ) ,
132
+ ( ) => getPayGoAddressProofOutputIndex ( psbt ) ,
139
133
( e : any ) => e . message === 'There are multiple PayGo addresses in the PSBT output 0.'
140
134
) ;
141
135
} ) ;
142
136
} ) ;
137
+
138
+ describe ( 'createPayGoAttestationBuffer' , ( ) => {
139
+ it ( 'should create a PayGo Attestation proof matching with original proof' , ( ) => {
140
+ const payGoAttestationProof = createPayGoAttestationBuffer ( addressToVerify , addressProofEntropy ) ;
141
+ assert . strictEqual ( payGoAttestationProof . toString ( ) , addressProofMsgBuffer . toString ( ) ) ;
142
+ assert ( Buffer . compare ( payGoAttestationProof , addressProofMsgBuffer ) === 0 ) ;
143
+ } ) ;
144
+
145
+ it ( 'should create a PayGo Attestation proof that does not match with different uuid' , ( ) => {
146
+ const addressProofBufferDiffUuid = generatePayGoAttestationProof (
147
+ '00000000-0000-0000-0000-000000000001' ,
148
+ Buffer . from ( addressToVerify )
149
+ ) ;
150
+ const payGoAttestationProof = createPayGoAttestationBuffer ( addressToVerify , addressProofEntropy ) ;
151
+ assert . notStrictEqual ( payGoAttestationProof . toString ( ) , addressProofBufferDiffUuid . toString ( ) ) ;
152
+ assert ( Buffer . compare ( payGoAttestationProof , addressProofBufferDiffUuid ) !== 0 ) ;
153
+ } ) ;
154
+ } ) ;
0 commit comments