Skip to content

Commit 42974af

Browse files
Merge pull request #6392 from BitGo/BTC-2246.paygo-tostring
refactor(utxo-core): Pay Go Util functions fix
2 parents 9223f46 + c0cf207 commit 42974af

File tree

4 files changed

+10
-18
lines changed

4 files changed

+10
-18
lines changed

modules/utxo-core/src/paygo/parsePayGoAttestation.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import assert from 'assert';
33
import { bufferutils } from '@bitgo/utxo-lib';
44

55
// The signed address will always have the following structure:
6-
// 0x18Bitcoin Signed Message:\n<varint_length><ENTROPY><ADDRESS><UUID>
6+
// <varint_length><ENTROPY><ADDRESS><UUID>
77

8-
const PrefixLength = Buffer.from([0x18]).length + Buffer.from('Bitcoin Signed Message:\n').length;
98
// UUID has the structure 00000000-0000-0000-0000-000000000000, and after
109
// we Buffer.from and get it's length its 36.
1110
const UuidBufferLength = 36;
@@ -14,7 +13,7 @@ const EntropyLen = 64;
1413

1514
/**
1615
* This function takes in the attestation proof of a PayGo address of the from
17-
* 0x18Bitcoin Signed Message:\n<varint_length><ENTROPY><ADDRESS><UUID> and returns
16+
* <varint_length><ENTROPY><ADDRESS><UUID> and returns
1817
* the address given its length. It is assumed that the ENTROPY is 64 bytes in the Buffer
1918
* so if not given an address proof length we can still extract the address from the proof.
2019
*
@@ -26,13 +25,13 @@ export function parsePayGoAttestation(message: Buffer): {
2625
address: Buffer;
2726
uuid: Buffer;
2827
} {
29-
if (message.length <= PrefixLength + EntropyLen + UuidBufferLength) {
28+
if (message.length <= EntropyLen + UuidBufferLength) {
3029
throw new Error('PayGo attestation proof is too short to contain a valid address');
3130
}
3231

3332
// This generates the first part before the varint length so that we can
3433
// determine how many bytes this is and iterate through the Buffer.
35-
let offset = PrefixLength;
34+
let offset = 0;
3635

3736
// we decode the varint of the message which is uint32
3837
// https://en.bitcoin.it/wiki/Protocol_documentation

modules/utxo-core/src/paygo/psbt/payGoAddressProof.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
* @param psbt - PSBT that we need to encode our paygo address into
2020
* @param outputIndex - the index of the address in our output
2121
* @param sig - the signature that we want to encode
22+
* @param entropy - the arbitrary entropy bytes from our vasp proof
2223
*/
2324
export function addPayGoAddressProof(
2425
psbt: utxolib.bitgo.UtxoPsbt,
@@ -40,7 +41,7 @@ export function addPayGoAddressProof(
4041
*
4142
* @param psbt - PSBT we want to verify that the paygo address is in
4243
* @param outputIndex - we have the output index that address is in
43-
* @param uuid
44+
* @param verificationPubkey - the pubkey signed by the HSM to verify our message
4445
* @returns
4546
*/
4647
export function verifyPayGoAddressProof(
@@ -76,7 +77,8 @@ export function verifyPayGoAddressProof(
7677
// We construct our message <ENTROPY><ADDRESS><UUID>
7778
const message = createPayGoAttestationBuffer(addressFromOutput, entropy, psbt.network);
7879

79-
if (!verifyMessage(message.toString(), verificationPubkey, signature, utxolib.networks.bitcoin)) {
80+
// bip32utils.verifyMessage now takes in message as a Buffer
81+
if (!verifyMessage(message, verificationPubkey, signature, utxolib.networks.bitcoin)) {
8082
throw new ErrorPayGoAddressProofFailedVerification();
8183
}
8284
}

modules/utxo-core/src/testutil/generatePayGoAttestationProof.utils.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import { bufferutils } from '@bitgo/utxo-lib';
1212
* @returns
1313
*/
1414
export function generatePayGoAttestationProof(uuid: string, address: Buffer): Buffer {
15-
// <0x18Bitcoin Signed Message:\n
16-
const prefixByte = Buffer.from([0x18]);
17-
const prefixMessage = Buffer.from('Bitcoin Signed Message:\n');
18-
const prefixBuffer = Buffer.concat([prefixByte, prefixMessage]);
19-
2015
// <ENTROPY>
2116
const entropyLength = 64;
2217
const entropy = crypto.randomBytes(entropyLength);
@@ -33,11 +28,7 @@ export function generatePayGoAttestationProof(uuid: string, address: Buffer): Bu
3328
const msgLengthBuffer = bufferutils.varuint.encode(msgLength);
3429

3530
// <0x18Bitcoin Signed Message:\n<LENGTH><ENTROPY><ADDRESS><UUID>
36-
const proofMessage = Buffer.concat([prefixBuffer, msgLengthBuffer, entropy, address, uuidBuffer]);
31+
const proofMessage = Buffer.concat([msgLengthBuffer, entropy, address, uuidBuffer]);
3732

38-
// we sign this with the priv key
39-
// don't know what sign function to call. Since this is just a mirrored function don't know if we need
40-
// to include this part.
41-
// const signedMsg = sign(attestationPrvKey, proofMessage);
4233
return proofMessage;
4334
}

modules/utxo-core/test/paygo/psbt/payGoAddressProof.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const addressProofMsgBuffer = trimMessagePrefix(addressProofBuffer);
5151
export const addressProofEntropy = addressProofMsgBuffer.subarray(0, 65);
5252

5353
// signature with the given msg addressProofBuffer
54-
export const sig = signMessage(addressProofMsgBuffer.toString(), attestationPrvKey!, network);
54+
export const sig = signMessage(addressProofMsgBuffer, attestationPrvKey!, network);
5555

5656
function getTestPsbt() {
5757
return utxolib.testutil.constructPsbt(psbtInputs, psbtOutputs, network, rootWalletKeys, 'unsigned');

0 commit comments

Comments
 (0)