Skip to content

Commit ae1ebea

Browse files
chore(utxo-core): renamed files and moved helper to own file
TICKET: BTC-2047 chore(utxo-core): updated index.ts TICKET: BTC-2047 chore(utxo-core): added asserts for address in attestation TICKET: BTC-2047 chore(utxo-core): naming convention TICKET: BTC-2047
1 parent e933da1 commit ae1ebea

File tree

5 files changed

+58
-49
lines changed

5 files changed

+58
-49
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'assert';
2+
3+
import * as utxolib from '@bitgo/utxo-lib';
4+
export const NIL_UUID = '00000000-0000-0000-0000-000000000000';
5+
6+
/** This function reconstructs the proof <ENTROPY><ADDRESS><UUID>
7+
* given the address and entropy.
8+
*
9+
* @param address
10+
* @param entropy
11+
* @returns
12+
*/
13+
export function createPayGoAttestationBuffer(address: string, entropy: Buffer, network: utxolib.Network): Buffer {
14+
assert(address.length > 0);
15+
const isValidAddress = utxolib.address.toOutputScript(address, network);
16+
assert(isValidAddress, `Address ${address} is not a valid address.`);
17+
const addressBuffer = Buffer.from(address);
18+
return Buffer.concat([entropy, addressBuffer, Buffer.from(NIL_UUID)]);
19+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './PayGoUtils';
1+
export * from './payGoAddressProof';

modules/utxo-core/src/paygo/psbt/PayGoUtils.ts renamed to modules/utxo-core/src/paygo/psbt/payGoAddressProof.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as utxolib from '@bitgo/utxo-lib';
22
import { checkForOutput } from 'bip174/src/lib/utils';
33

44
import { verifyMessage } from '../../bip32utils';
5+
import { createPayGoAttestationBuffer } from '../attestation';
56

67
import {
78
ErrorMultiplePayGoProof,
@@ -11,8 +12,6 @@ import {
1112
ErrorPayGoAddressProofFailedVerification,
1213
} from './Errors';
1314

14-
const NILLUUID = '00000000-0000-0000-0000-000000000000';
15-
1615
/** This function adds the entropy and signature into the PSBT output unknown key vals.
1716
* We store the entropy so that we reconstruct the message <ENTROPY><ADDRESS><UUID>
1817
* to later verify.
@@ -75,7 +74,7 @@ export function verifyPayGoAddressProof(
7574
const addressFromOutput = utxolib.address.fromOutputScript(output.script, psbt.network);
7675

7776
// We construct our message <ENTROPY><ADDRESS><UUID>
78-
const message = createPayGoAttestationBuffer(addressFromOutput, entropy);
77+
const message = createPayGoAttestationBuffer(addressFromOutput, entropy, psbt.network);
7978

8079
if (!verifyMessage(message.toString(), verificationPubkey, signature, utxolib.networks.bitcoin)) {
8180
throw new ErrorPayGoAddressProofFailedVerification();
@@ -109,15 +108,3 @@ export function getPayGoAddressProofOutputIndex(psbt: utxolib.bitgo.UtxoPsbt): n
109108
export function psbtOutputIncludesPaygoAddressProof(psbt: utxolib.bitgo.UtxoPsbt): boolean {
110109
return getPayGoAddressProofOutputIndex(psbt) !== undefined;
111110
}
112-
113-
/** This function reconstructs the proof <ENTROPY><ADDRESS><UUID>
114-
* given the address and entropy.
115-
*
116-
* @param address
117-
* @param entropy
118-
* @returns
119-
*/
120-
export function createPayGoAttestationBuffer(address: string, entropy: Buffer): Buffer {
121-
const addressBuffer = Buffer.from(address);
122-
return Buffer.concat([entropy, addressBuffer, Buffer.from(NILLUUID)]);
123-
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import assert from 'assert';
2+
3+
import { createPayGoAttestationBuffer } from '../../src/paygo/attestation';
4+
import { generatePayGoAttestationProof } from '../../src/testutil';
5+
6+
import { addressProofEntropy, addressProofMsgBuffer, addressToVerify, network } from './psbt/payGoAddressProof';
7+
8+
describe('createPayGoAttestationBuffer', () => {
9+
it('should create a PayGo Attestation proof matching with original proof', () => {
10+
const payGoAttestationProof = createPayGoAttestationBuffer(addressToVerify, addressProofEntropy, network);
11+
assert.strictEqual(payGoAttestationProof.toString(), addressProofMsgBuffer.toString());
12+
assert(Buffer.compare(payGoAttestationProof, addressProofMsgBuffer) === 0);
13+
});
14+
15+
it('should create a PayGo Attestation proof that does not match with different uuid', () => {
16+
const addressProofBufferDiffUuid = generatePayGoAttestationProof(
17+
'00000000-0000-0000-0000-000000000001',
18+
Buffer.from(addressToVerify)
19+
);
20+
const payGoAttestationProof = createPayGoAttestationBuffer(addressToVerify, addressProofEntropy, network);
21+
assert.notStrictEqual(payGoAttestationProof.toString(), addressProofBufferDiffUuid.toString());
22+
assert(Buffer.compare(payGoAttestationProof, addressProofBufferDiffUuid) !== 0);
23+
});
24+
});

modules/utxo-core/test/paygo/psbt/PayGoUtils.ts renamed to modules/utxo-core/test/paygo/psbt/payGoAddressProof.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import { checkForOutput } from 'bip174/src/lib/utils';
88

99
import {
1010
addPayGoAddressProof,
11-
createPayGoAttestationBuffer,
1211
getPayGoAddressProofOutputIndex,
1312
psbtOutputIncludesPaygoAddressProof,
1413
verifyPayGoAddressProof,
15-
} from '../../../src/paygo/psbt/PayGoUtils';
14+
} from '../../../src/paygo/psbt/payGoAddressProof';
1615
import { generatePayGoAttestationProof } from '../../../src/testutil/generatePayGoAttestationProof.utils';
1716
import { parseVaspProof } from '../../../src/testutil/parseVaspProof';
1817
import { signMessage } from '../../../src/bip32utils';
18+
import { NIL_UUID } from '../../../src/paygo/attestation';
1919

2020
// To construct our PSBTs
21-
const network = utxolib.networks.bitcoin;
21+
export const network = utxolib.networks.bitcoin;
2222
const keys = [1, 2, 3].map((v) => utxolib.bip32.fromSeed(Buffer.alloc(16, `test/2/${v}`), network));
2323
const rootWalletKeys = new utxolib.bitgo.RootWalletKeys([keys[0], keys[1], keys[2]]);
2424

@@ -34,34 +34,31 @@ const psbtOutputs = utxolib.testutil.outputScriptTypes.map((scriptType) => ({
3434

3535
// wallet pub and priv key for tbtc
3636
const dummyPub1 = rootWalletKeys.deriveForChainAndIndex(50, 200);
37-
const attestationPubKey = dummyPub1.user.publicKey;
38-
const attestationPrvKey = dummyPub1.user.privateKey!;
39-
40-
// UUID structure
41-
const nillUUID = '00000000-0000-0000-0000-000000000000';
37+
export const attestationPubKey = dummyPub1.user.publicKey;
38+
export const attestationPrvKey = dummyPub1.user.privateKey!;
4239

4340
// our xpub converted to base58 address
44-
const addressToVerify = utxolib.address.toBase58Check(
41+
export const addressToVerify = utxolib.address.toBase58Check(
4542
utxolib.crypto.hash160(Buffer.from(dummyPub1.backup.publicKey)),
4643
utxolib.networks.bitcoin.pubKeyHash,
4744
utxolib.networks.bitcoin
4845
);
4946

5047
// this should be retuning a Buffer
51-
const addressProofBuffer = generatePayGoAttestationProof(nillUUID, Buffer.from(addressToVerify));
52-
const addressProofMsgBuffer = parseVaspProof(addressProofBuffer);
48+
export const addressProofBuffer = generatePayGoAttestationProof(NIL_UUID, Buffer.from(addressToVerify));
49+
export const addressProofMsgBuffer = parseVaspProof(addressProofBuffer);
5350
// We know that that the entropy is a set 64 bytes.
54-
const addressProofEntropy = addressProofMsgBuffer.subarray(0, 65);
51+
export const addressProofEntropy = addressProofMsgBuffer.subarray(0, 65);
5552

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

5956
function getTestPsbt() {
6057
return utxolib.testutil.constructPsbt(psbtInputs, psbtOutputs, network, rootWalletKeys, 'unsigned');
6158
}
6259

6360
describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
64-
function getPaygoProprietaryKey(proprietaryKeyVals: KeyValue[]) {
61+
function getPayGoProprietaryKey(proprietaryKeyVals: KeyValue[]) {
6562
return proprietaryKeyVals
6663
.map(({ key, value }) => {
6764
return { key: decodeProprietaryKey(key), value };
@@ -88,7 +85,7 @@ describe('addPaygoAddressProof and verifyPaygoAddressProof', () => {
8885
addPayGoAddressProof(psbt, outputIndex, sig, addressProofEntropy);
8986
addPayGoAddressProof(psbt, outputIndex, Buffer.from('signature2'), crypto.randomBytes(64));
9087
const output = checkForOutput(psbt.data.outputs, outputIndex);
91-
const proofInPsbt = getPaygoProprietaryKey(output.unknownKeyVals!);
88+
const proofInPsbt = getPayGoProprietaryKey(output.unknownKeyVals!);
9289
assert(proofInPsbt.length !== 0);
9390
assert(proofInPsbt.length > 1);
9491
assert.throws(
@@ -134,21 +131,3 @@ describe('getPaygoAddressProofIndex', () => {
134131
);
135132
});
136133
});
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

Comments
 (0)