1
1
import assert from 'assert' ;
2
+ import crypto from 'crypto' ;
2
3
3
4
import * as utxolib from '@bitgo/utxo-lib' ;
5
+ import * as bitcoinMessage from 'bitcoinjs-message' ;
4
6
import { decodeProprietaryKey } from 'bip174/src/lib/proprietaryKeyVal' ;
5
7
import { KeyValue } from 'bip174/src/lib/interfaces' ;
6
8
import { checkForOutput } from 'bip174/src/lib/utils' ;
@@ -12,7 +14,8 @@ import {
12
14
verifyPayGoAddressProof ,
13
15
} from '../../../src/paygo/psbt/PayGoUtils' ;
14
16
import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils' ;
15
- import { signMessage } from '../../../src/bip32utils' ;
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 ;
@@ -46,8 +49,12 @@ const addressToVerify = utxolib.address.toBase58Check(
46
49
47
50
// this should be retuning a Buffer
48
51
const addressProofBuffer = generatePayGoAttestationProof ( nilUUID , 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 = signMessage ( addressProofBuffer . toString ( ) , attestationPrvKey ! , network ) ;
57
+ const sig = bitcoinMessage . sign ( addressProofMsgBuffer , attestationPrvKey ! , true ) ;
51
58
52
59
function getTestPsbt ( ) {
53
60
return utxolib . testutil . constructPsbt ( psbtInputs , psbtOutputs , network , rootWalletKeys , 'unsigned' ) ;
@@ -67,15 +74,15 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
67
74
} ) ;
68
75
}
69
76
70
- it ( " should fail a proof verification if the proof isn't valid" , ( ) => {
77
+ it ( ' should fail a proof verification with wrong UUID' , ( ) => {
71
78
const outputIndex = 0 ;
72
79
const psbt = getTestPsbt ( ) ;
73
- addPayGoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
80
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
74
81
const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
75
82
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
76
83
assert ( proofInPsbt . length === 1 ) ;
77
84
assert . throws (
78
- ( ) => verifyPayGoAddressProof ( psbt , 0 , Buffer . from ( 'Random Signed Message' ) , attestationPubKey ) ,
85
+ ( ) => verifyPayGoAddressProof ( psbt , 0 , '00000000-0000-0000-0000-000000000001' ) ,
79
86
( e : any ) => e . message === 'Cannot verify the paygo address signature with the provided pubkey.'
80
87
) ;
81
88
} ) ;
@@ -84,21 +91,21 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
84
91
const psbt = getTestPsbt ( ) ;
85
92
psbt . addOutput ( { script : utxolib . address . toOutputScript ( addressToVerify , network ) , value : BigInt ( 10000 ) } ) ;
86
93
const outputIndex = psbt . data . outputs . length - 1 ;
87
- addPayGoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
88
- verifyPayGoAddressProof ( psbt , outputIndex , Buffer . from ( addressProofBuffer ) , attestationPubKey ) ;
94
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
95
+ verifyPayGoAddressProof ( psbt , outputIndex , nilUUID , Buffer . from ( addressToVerify ) ) ;
89
96
} ) ;
90
97
91
98
it ( 'should throw an error if there are multiple PayGo proprietary keys in the PSBT' , ( ) => {
92
99
const outputIndex = 0 ;
93
100
const psbt = getTestPsbt ( ) ;
94
- addPayGoAddressProof ( psbt , outputIndex , sig , Buffer . from ( attestationPubKey ) ) ;
95
- addPayGoAddressProof ( psbt , outputIndex , Buffer . from ( 'signature2' ) , Buffer . from ( 'fakepubkey2s' ) ) ;
101
+ addPayGoAddressProof ( psbt , outputIndex , sig , addressProofEntropy ) ;
102
+ addPayGoAddressProof ( psbt , outputIndex , Buffer . from ( 'signature2' ) , crypto . randomBytes ( 64 ) ) ;
96
103
const output = checkForOutput ( psbt . data . outputs , outputIndex ) ;
97
104
const proofInPsbt = getPaygoProprietaryKey ( output . unknownKeyVals ! ) ;
98
105
assert ( proofInPsbt . length !== 0 ) ;
99
106
assert ( proofInPsbt . length > 1 ) ;
100
107
assert . throws (
101
- ( ) => verifyPayGoAddressProof ( psbt , outputIndex , addressProofBuffer , attestationPubKey ) ,
108
+ ( ) => verifyPayGoAddressProof ( psbt , outputIndex , nilUUID ) ,
102
109
( e : any ) => e . message === 'There are multiple paygo address proofs encoded in the PSBT. Something went wrong.'
103
110
) ;
104
111
} ) ;
@@ -108,7 +115,7 @@ describe('verifyPaygoAddressProof', () => {
108
115
it ( 'should throw an error if there is no PayGo address in PSBT' , ( ) => {
109
116
const psbt = getTestPsbt ( ) ;
110
117
assert . throws (
111
- ( ) => verifyPayGoAddressProof ( psbt , 0 , addressProofBuffer , attestationPubKey ) ,
118
+ ( ) => verifyPayGoAddressProof ( psbt , 0 , nilUUID ) ,
112
119
( e : any ) => e . message === 'There is no paygo address proof encoded in the PSBT at output 0.'
113
120
) ;
114
121
} ) ;
0 commit comments