11import assert from 'assert' ;
2+ import crypto from 'crypto' ;
23
34import * as utxolib from '@bitgo/utxo-lib' ;
4- import * as bitcoinMessage from 'bitcoinjs-message' ;
55import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal' ;
66import { KeyValue } from 'bip174/src/lib/interfaces' ;
77import { checkForOutput } from 'bip174/src/lib/utils' ;
88
99import {
10- addPaygoAddressProof ,
11- getPaygoAddressProofOutputIndex ,
10+ addPayGoAddressProof ,
11+ createPayGoAttestationBuffer ,
12+ getPayGoAddressProofOutputIndex ,
1213 psbtOutputIncludesPaygoAddressProof ,
13- verifyPaygoAddressProof ,
14+ verifyPayGoAddressProof ,
1415} from '../../../src/paygo/psbt/PayGoUtils' ;
1516import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils' ;
17+ import { parseVaspProof } from '../../../src/testutil/parseVaspProof' ;
18+ import { signMessage } from '../../../src/bip32utils' ;
1619
1720// To construct our PSBTs
1821const network = utxolib . networks . bitcoin ;
@@ -35,7 +38,7 @@ const attestationPubKey = dummyPub1.user.publicKey;
3538const attestationPrvKey = dummyPub1 . user . privateKey ! ;
3639
3740// UUID structure
38- const nilUUID = '00000000-0000-0000-0000-000000000000' ;
41+ const nillUUID = '00000000-0000-0000-0000-000000000000' ;
3942
4043// our xpub converted to base58 address
4144const addressToVerify = utxolib . address . toBase58Check (
@@ -45,9 +48,13 @@ const addressToVerify = utxolib.address.toBase58Check(
4548) ;
4649
4750// 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+
4956// signature with the given msg addressProofBuffer
50- const sig = bitcoinMessage . sign ( addressProofBuffer , attestationPrvKey ! , true , network . messagePrefix ) ;
57+ const sig = signMessage ( addressProofMsgBuffer . toString ( ) , attestationPrvKey ! , network ) ;
5158
5259function getTestPsbt ( ) {
5360 return utxolib . testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
@@ -67,38 +74,25 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
6774 } ) ;
6875 }
6976
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-
8377 it ( 'should add and verify a valid paygo address proof on the PSBT' , ( ) => {
8478 const psbt = getTestPsbt ( ) ;
8579 psbt . addOutput ( { script : utxolib . address . toOutputScript ( addressToVerify , network ) , value : BigInt ( 10000 ) } ) ;
8680 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 ) ;
8983 } ) ;
9084
9185 it ( 'should throw an error if there are multiple PayGo proprietary keys in the PSBT' , ( ) => {
9286 const outputIndex = 0 ;
9387 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 ) ) ;
9690 const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
9791 const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
9892 assert ( proofInPsbt . length !== 0 ) ;
9993 assert ( proofInPsbt . length > 1 ) ;
10094 assert . throws (
101- ( ) => verifyPaygoAddressProof ( psbt , outputIndex , addressProofBuffer , attestationPubKey ) ,
95+ ( ) => verifyPayGoAddressProof ( psbt , outputIndex , attestationPubKey ) ,
10296 ( e : any ) => e . message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.'
10397 ) ;
10498 } ) ;
@@ -108,7 +102,7 @@ describe('verifyPaygoAddressProof', () => {
108102 it ( 'should throw an error if there is no PayGo address in PSBT' , ( ) => {
109103 const psbt = getTestPsbt ( ) ;
110104 assert . throws (
111- ( ) => verifyPaygoAddressProof ( psbt , 0 , addressProofBuffer , attestationPubKey ) ,
105+ ( ) => verifyPayGoAddressProof ( psbt , 0 , attestationPubKey ) ,
112106 ( e : any ) => e . message === 'There is no paygo address proof encoded in the PSBT at output 0.'
113107 ) ;
114108 } ) ;
@@ -118,25 +112,43 @@ describe('getPaygoAddressProofIndex', () => {
118112 it ( 'should get PayGo address proof index from PSBT if there is one' , ( ) => {
119113 const psbt = getTestPsbt ( ) ;
120114 const outputIndex = 0 ;
121- addPaygoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
115+ addPayGoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
122116 assert ( psbtOutputIncludesPaygoAddressProof ( psbt ) ) ;
123- assert ( getPaygoAddressProofOutputIndex ( psbt ) === 0 ) ;
117+ assert ( getPayGoAddressProofOutputIndex ( psbt ) === 0 ) ;
124118 } ) ;
125119
126120 it ( 'should return undefined if there is no PayGo address proof in PSBT' , ( ) => {
127121 const psbt = getTestPsbt ( ) ;
128- assert ( getPaygoAddressProofOutputIndex ( psbt ) === undefined ) ;
122+ assert ( getPayGoAddressProofOutputIndex ( psbt ) === undefined ) ;
129123 assert ( ! psbtOutputIncludesPaygoAddressProof ( psbt ) ) ;
130124 } ) ;
131125
132126 it ( 'should return an error and fail if we have multiple PayGo address in the PSBT in the same output index' , ( ) => {
133127 const psbt = getTestPsbt ( ) ;
134128 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 ) ) ;
137131 assert . throws (
138- ( ) => getPaygoAddressProofOutputIndex ( psbt ) ,
132+ ( ) => getPayGoAddressProofOutputIndex ( psbt ) ,
139133 ( e : any ) => e . message === 'There are multiple PayGo addresses in the PSBT output 0.'
140134 ) ;
141135 } ) ;
142136} ) ;
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