Skip to content

Commit 6c164e6

Browse files
Merge pull request #6341 from BitGo/BTC-2226.refactor-extractAddressBufferFromPayGoAttestationProof
feat(utxo-core): extractMsgBufferFromPayGoAttestationProof refactor
2 parents 0f91ee3 + f551baf commit 6c164e6

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './ExtractAddressPayGoAttestation';
1+
export * from './parsePayGoAttestation';
22
export * from './psbt';

modules/utxo-core/src/paygo/ExtractAddressPayGoAttestation.ts renamed to modules/utxo-core/src/paygo/parsePayGoAttestation.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ const EntropyLen = 64;
2121
* @param message
2222
* @param adressProofLength
2323
*/
24-
export function extractAddressBufferFromPayGoAttestationProof(message: Buffer): Buffer {
24+
export function parsePayGoAttestation(message: Buffer): {
25+
entropy: Buffer;
26+
address: Buffer;
27+
uuid: Buffer;
28+
} {
2529
if (message.length <= PrefixLength + EntropyLen + UuidBufferLength) {
2630
throw new Error('PayGo attestation proof is too short to contain a valid address');
2731
}
@@ -34,12 +38,14 @@ export function extractAddressBufferFromPayGoAttestationProof(message: Buffer):
3438
// https://en.bitcoin.it/wiki/Protocol_documentation
3539
const varInt = bufferutils.varuint.decode(message, offset);
3640
assert(varInt);
37-
offset += 1;
41+
offset += bufferutils.varuint.decode.bytes;
3842

39-
const addressLength = varInt - EntropyLen - UuidBufferLength;
43+
const entropy = message.subarray(offset, offset + EntropyLen);
4044
offset += EntropyLen;
45+
const address = message.subarray(offset, message.length - UuidBufferLength);
46+
offset += address.length;
47+
const uuid = message.subarray(message.length - UuidBufferLength);
4148

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);
49+
// we break up the original message and retuen the entropy, address and uuid in their buffers
50+
return { entropy, address, uuid };
4551
}

modules/utxo-core/test/paygo/ExtractAddressPayGoAttestation.ts renamed to modules/utxo-core/test/paygo/parsePayGoAttestation.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22

3-
import { extractAddressBufferFromPayGoAttestationProof } from '../../src/paygo';
3+
import { parsePayGoAttestation } from '../../src/paygo';
44
import { generatePayGoAttestationProof } from '../../src/testutil';
55

66
const addressFromPubKeyBase58 = 'bitgoAddressToExtract';
@@ -12,31 +12,35 @@ describe('extractAddressBufferFromPayGoAttestationProof', () => {
1212
'00000000-0000-0000-0000-000000000000',
1313
bufferAddressPubKeyB58
1414
);
15-
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
16-
assert.deepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
15+
const { entropy, address, uuid } = parsePayGoAttestation(paygoAttestationProof);
16+
assert.deepStrictEqual(Buffer.compare(address, bufferAddressPubKeyB58), 0);
17+
assert.deepStrictEqual(uuid.toString(), '00000000-0000-0000-0000-000000000000');
18+
assert.deepStrictEqual(entropy.length, 64);
1719
});
1820

1921
it('should extract the paygo address paygo attestation proof given a non nilUUID', () => {
2022
const paygoAttestationProof = generatePayGoAttestationProof(
2123
'12345678-1234-4567-6890-231928472123',
2224
bufferAddressPubKeyB58
2325
);
24-
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
25-
assert.deepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
26+
const { entropy, address, uuid } = parsePayGoAttestation(paygoAttestationProof);
27+
assert.deepStrictEqual(Buffer.compare(address, bufferAddressPubKeyB58), 0);
28+
assert.deepStrictEqual(uuid.toString(), '12345678-1234-4567-6890-231928472123');
29+
assert.deepStrictEqual(entropy.length, 64);
2630
});
2731

2832
it('should not extract the correct address given a uuid of wrong format', () => {
2933
const paygoAttestationProof = generatePayGoAttestationProof(
3034
'000000000000000-000000-0000000-000000-0000000000000000',
3135
bufferAddressPubKeyB58
3236
);
33-
const addressFromProof = extractAddressBufferFromPayGoAttestationProof(paygoAttestationProof);
34-
assert.notDeepStrictEqual(Buffer.compare(addressFromProof, bufferAddressPubKeyB58), 0);
37+
const { address } = parsePayGoAttestation(paygoAttestationProof);
38+
assert.notDeepStrictEqual(Buffer.compare(address, bufferAddressPubKeyB58), 0);
3539
});
3640

3741
it('should throw an error if the paygo attestation proof is too short', () => {
3842
assert.throws(
39-
() => extractAddressBufferFromPayGoAttestationProof(Buffer.from('shortproof-shrug')),
43+
() => parsePayGoAttestation(Buffer.from('shortproof-shrug')),
4044
'PayGo attestation proof is too short to contain a valid address.'
4145
);
4246
});

0 commit comments

Comments
 (0)