Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/chains/stellar/meta-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export function decodeStealthMetaAddress(metaAddress: string): StealthMetaAddres
);
}

if (!/^[0-9a-fA-F]{128}$/.test(hex)) {
throw new InvalidMetaAddressError(
metaAddress,
'Invalid stealth meta-address hex. Expected 128 hexadecimal chars',
);
}

const spendingPubKey = hexToBytes(hex.slice(0, 64));
const viewingPubKey = hexToBytes(hex.slice(64));

Expand Down
32 changes: 32 additions & 0 deletions test/chains/stellar/meta-address.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, test, expect } from 'vitest';
import * as fc from 'fast-check';
import { deriveStealthKeys } from '../../../src/chains/stellar/keys';
import {
encodeStealthMetaAddress,
Expand Down Expand Up @@ -37,6 +38,37 @@ describe('meta-address', () => {
);
});

test.each(['g', 'G', '-', ' ', '\u00e9'])('rejects non-hex payload character %j', (character) => {
const keys = deriveStealthKeys(testSig);
const meta = encodeStealthMetaAddress(keys.spendingPubKey, keys.viewingPubKey);
const malformed = `${meta.slice(0, 7)}${character}${meta.slice(8)}`;

expect(() => decodeStealthMetaAddress(malformed)).toThrow(
'Invalid stealth meta-address hex. Expected 128 hexadecimal chars',
);
});

test('rejects invalid hex characters at every payload position', () => {
const keys = deriveStealthKeys(testSig);
const meta = encodeStealthMetaAddress(keys.spendingPubKey, keys.viewingPubKey);

fc.assert(
fc.property(
fc.integer({ min: 0, max: 127 }),
fc.constantFrom('g', 'G', 'z', '-', ' ', '\u00e9'),
(index, character) => {
const payloadIndex = 7 + index;
const malformed = `${meta.slice(0, payloadIndex)}${character}${meta.slice(payloadIndex + 1)}`;

expect(() => decodeStealthMetaAddress(malformed)).toThrow(
'Invalid stealth meta-address hex. Expected 128 hexadecimal chars',
);
},
),
{ numRuns: 256 },
);
});

test('encode rejects wrong key length', () => {
const shortKey = new Uint8Array(31);
const keys = deriveStealthKeys(testSig);
Expand Down
Loading