Skip to content

Commit 1cc2a87

Browse files
chore(utxo-core): moved files to utxo-core
Issue: BTC-2150 TICKET: BTC-2150
1 parent 502e561 commit 1cc2a87

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from 'assert';
2+
3+
import { varuint } from 'bitcoinjs-lib/src/bufferutils';
4+
5+
// The signed address will always have the following structure:
6+
// 0x18Bitcoin Signed Message:\n<varint_length><ENTROPY><ADDRESS><UUID>
7+
8+
const PrefixLength = Buffer.from([0x18]).length + Buffer.from('Bitcoin Signed Message:\n').length;
9+
// UUID has the structure 00000000-0000-0000-0000-000000000000, and after
10+
// we Buffer.from and get it's length its 36.
11+
const UuidBufferLength = 36;
12+
// The entropy will always be 64 bytes
13+
const EntropyLen = 64;
14+
15+
/**
16+
* 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
18+
* the address given its length. It is assumed that the ENTROPY is 64 bytes in the Buffer
19+
* so if not given an address proof length we can still extract the address from the proof.
20+
*
21+
* @param message
22+
* @param adressProofLength
23+
*/
24+
export function extractAddressBufferFromPayGoAttestationProof(message: Buffer): Buffer {
25+
if (message.length <= PrefixLength + EntropyLen + UuidBufferLength) {
26+
throw new Error('PayGo attestation proof is too short to contain a valid address');
27+
}
28+
29+
// This generates the first part before the varint length so that we can
30+
// determine how many bytes this is and iterate through the Buffer.
31+
let offset = PrefixLength;
32+
33+
// we decode the varint of the message which is uint32
34+
// https://en.bitcoin.it/wiki/Protocol_documentation
35+
const varInt = varuint.decode(message, offset);
36+
assert(varInt);
37+
offset += 1;
38+
39+
const addressLength = varInt - EntropyLen - UuidBufferLength;
40+
offset += EntropyLen;
41+
42+
// we return what the Buffer subarray from the offset (beginning of address)
43+
// to the end of the address index in the buffer.
44+
return message.subarray(offset, offset + addressLength);
45+
}

modules/utxo-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * as testutil from './testutil';
44
export * from './dustThreshold';
55
export * from './Output';
66
export * from './xOnlyPubkey';
7+
export * from './ExtractAddressPayGoAttestation';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as assert from 'assert';
2+
3+
import * as utxolib from '@bitgo/utxo-lib';
4+
5+
import { extractAddressBufferFromPayGoAttestationProof } from '../src/ExtractAddressPayGoAttestation';
6+
7+
const addressFromPubKeyBase58 = 'bitgoAddressToExtract';
8+
const bufferAddressPubKeyB58 = Buffer.from(addressFromPubKeyBase58);
9+
10+
describe('extractAddressBufferFromPayGoAttestationProof', () => {
11+
it('should extractAddressBufferFromPayGoAttestationProof properly', () => {
12+
const paygoAttestationProof = utxolib.testutil.generatePayGoAttestationProof(
13+
'00000000-0000-0000-0000-000000000000',
14+
bufferAddressPubKeyB58
15+
);
16+
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
17+
assert.deepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
18+
});
19+
20+
it('should extract the paygo address paygo attestation proof given a non nilUUID', () => {
21+
const paygoAttestationProof = utxolib.testutil.generatePayGoAttestationProof(
22+
'12345678-1234-4567-6890-231928472123',
23+
bufferAddressPubKeyB58
24+
);
25+
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
26+
assert.deepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
27+
});
28+
29+
it('should not extract the correct address given a uuid of wrong format', () => {
30+
const paygoAttestationProof = utxolib.testutil.generatePayGoAttestationProof(
31+
'000000000000000-000000-0000000-000000-0000000000000000',
32+
bufferAddressPubKeyB58
33+
);
34+
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
35+
assert.notDeepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
36+
});
37+
38+
it('should throw an error if the paygo attestation proof is too short', () => {
39+
assert.throws(
40+
() => extractAddressBufferFromPayGoAttestationProof(Buffer.from('shortproof-shrug')),
41+
'PayGo attestation proof is too short to contain a valid address.'
42+
);
43+
});
44+
});

0 commit comments

Comments
 (0)