Warn on unexpected decoded prefix (#223)

This commit is contained in:
Jaco Greeff
2018-10-13 17:12:58 +02:00
committed by GitHub
parent 776b986e2f
commit 016b8b1809
3 changed files with 8 additions and 3 deletions
+2 -2
View File
@@ -63,9 +63,9 @@ describe('decode', () => {
).toEqual(new Uint8Array([42, 44, 10, 0, 0, 0, 0, 0]));
});
it('fails when prefix is invalid', () => {
it('allows invalid prefix (in list)', () => {
expect(
() => decode('BXHKtJjKdXjvbvTg6qK275sgcKKq8dnU38MQQAiR3LN2stP')
() => decode('6GfvWUvHvU8otbZ7sFhXH4eYeMcKdUkL61P3nFy52efEPVUx')
).toThrow(/address prefix/);
});
+5 -1
View File
@@ -26,9 +26,13 @@ export default function decode (encoded: string | Uint8Array, prefix: Prefix = d
const error = (message: string) =>
`Decoding ${encoded}: ${message}`;
assert(decoded[0] === prefix, error('Invalid decoded address prefix'));
assert(defaults.allowedPrefix.includes(decoded[0] as Prefix), error('Invalid decoded address prefix'));
assert(defaults.allowedEncodedLengths.includes(decoded.length), error('Invalid decoded address length'));
if (decoded[0] !== prefix) {
console.log(`WARN: Expected ${prefix}, found ${decoded[0]}`);
}
const isPublicKey = decoded.length === 35;
// non-publicKeys has 1 byte checksums, else default to 2
+1
View File
@@ -8,5 +8,6 @@ export default {
allowedDecodedLengths: [1, 2, 4, 8, 32],
// publicKey has prefix + 2 checksum bytes, short only prefix + 1 checksum byte
allowedEncodedLengths: [3, 4, 6, 10, 35],
allowedPrefix: [0, 1, 42, 43, 68, 69] as Array<Prefix>,
prefix: 42 as Prefix
};