From cc0dd2a2b539fa1beb88baa03d66d1733437f972 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 11 Nov 2021 14:41:54 +0100 Subject: [PATCH] Address validation tests (#1173) --- CHANGELOG.md | 1 + packages/util-crypto/src/address/is.spec.ts | 111 ++++++++++++++++++ .../util-crypto/src/address/validate.spec.ts | 111 ++++++++++++++++++ 3 files changed, 223 insertions(+) create mode 100644 packages/util-crypto/src/address/is.spec.ts create mode 100644 packages/util-crypto/src/address/validate.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a3e7a548e..bae544196 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Changes: - Add `stringPascalCase` utility - Add strongly typed `object{Entries, Keys, Values}` utilities +- Expand tests for uncovered functions ## 7.8.2 Nov 7, 2021 diff --git a/packages/util-crypto/src/address/is.spec.ts b/packages/util-crypto/src/address/is.spec.ts new file mode 100644 index 000000000..0af311ab7 --- /dev/null +++ b/packages/util-crypto/src/address/is.spec.ts @@ -0,0 +1,111 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { isAddress } from '.'; + +describe('isAddress', (): void => { + it('decodes an address', (): void => { + expect( + isAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') + ).toEqual(true); + }); + + it('decodes the council address', (): void => { + expect( + isAddress('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29') + ).toEqual(true); + }); + + it('converts a publicKey (hex) as-is', (): void => { + expect( + isAddress('0x01020304') + ).toEqual(true); + }); + + it('decodes a short address', (): void => { + expect( + isAddress('F7NZ') + ).toEqual(true); + }); + + it('decodes a 1-byte accountId (with prefix)', (): void => { + expect( + isAddress('g4b', false, 2) + ).toEqual(true); + }); + + it('decodes a 2-byte accountId', (): void => { + expect( + isAddress('3xygo', false, 2) + ).toEqual(true); + }); + + it('encodes a 4-byte address', (): void => { + expect( + isAddress('zswfoZa', false, 2) + ).toEqual(true); + }); + + it('decodes a 8-byte address', (): void => { + expect( + isAddress('848Gh2GcGaZia', false, 2) + ).toEqual(true); + }); + + it('decodes a 33-byte address', (): void => { + expect( + isAddress('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (65)', (): void => { + expect( + isAddress('cLtA6nCDyvwKcEHH4QkZDSHMhS9s78BvUJUsKUbUAn1Jc2SCF') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (69)', (): void => { + expect( + isAddress('cnUaoo5wodnTVA4bnr4woSweto8hWZADUvLFXkR9Q6U7BRsbF') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (252)', (): void => { + expect( + isAddress('xw9Hca4RJTmBRgzJT4ieJBh7XCK9gE3NXBDSEmgGHd4TCrbnG') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (255)', (): void => { + expect( + isAddress('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (ecdsa, from Substrate)', (): void => { + expect( + isAddress('4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV') + ).toEqual(true); + }); + + it('fails when length is invalid', (): void => { + expect( + isAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b') + ).toEqual(false); + }); + + it('fails when the checksum does not match', (): void => { + expect( + isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj') + ).toEqual(false); + expect( + isAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU') + ).toEqual(false); + }); + + it('fails when invalid base58 encoded address is found', (): void => { + expect( + isAddress('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29') + ).toEqual(false); + }); +}); diff --git a/packages/util-crypto/src/address/validate.spec.ts b/packages/util-crypto/src/address/validate.spec.ts new file mode 100644 index 000000000..5985fbe59 --- /dev/null +++ b/packages/util-crypto/src/address/validate.spec.ts @@ -0,0 +1,111 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { validateAddress } from '.'; + +describe('validateAddress', (): void => { + it('decodes an address', (): void => { + expect( + validateAddress('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY') + ).toEqual(true); + }); + + it('decodes the council address', (): void => { + expect( + validateAddress('F3opxRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29') + ).toEqual(true); + }); + + it('converts a publicKey (hex) as-is', (): void => { + expect( + validateAddress('0x01020304') + ).toEqual(true); + }); + + it('decodes a short address', (): void => { + expect( + validateAddress('F7NZ') + ).toEqual(true); + }); + + it('decodes a 1-byte accountId (with prefix)', (): void => { + expect( + validateAddress('g4b', false, 2) + ).toEqual(true); + }); + + it('decodes a 2-byte accountId', (): void => { + expect( + validateAddress('3xygo', false, 2) + ).toEqual(true); + }); + + it('encodes a 4-byte address', (): void => { + expect( + validateAddress('zswfoZa', false, 2) + ).toEqual(true); + }); + + it('decodes a 8-byte address', (): void => { + expect( + validateAddress('848Gh2GcGaZia', false, 2) + ).toEqual(true); + }); + + it('decodes a 33-byte address', (): void => { + expect( + validateAddress('KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (65)', (): void => { + expect( + validateAddress('cLtA6nCDyvwKcEHH4QkZDSHMhS9s78BvUJUsKUbUAn1Jc2SCF') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (69)', (): void => { + expect( + validateAddress('cnUaoo5wodnTVA4bnr4woSweto8hWZADUvLFXkR9Q6U7BRsbF') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (252)', (): void => { + expect( + validateAddress('xw9Hca4RJTmBRgzJT4ieJBh7XCK9gE3NXBDSEmgGHd4TCrbnG') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (255)', (): void => { + expect( + validateAddress('yGHU8YKprxHbHdEv7oUK4rzMZXtsdhcXVG2CAMyC9WhzhjH2k') + ).toEqual(true); + }); + + it('decodes a 2-byte prefix (ecdsa, from Substrate)', (): void => { + expect( + validateAddress('4pbsSkWcBaYoFHrKJZp5fDVUKbqSYD9dhZZGvpp3vQ5ysVs5ybV') + ).toEqual(true); + }); + + it('fails when length is invalid', (): void => { + expect( + () => validateAddress('y9EMHt34JJo4rWLSaxoLGdYXvjgSXEd4zHUnQgfNzwES8b') + ).toThrow(/address length/); + }); + + it('fails when the checksum does not match', (): void => { + expect( + () => validateAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMa9cj') + ).toThrow(/address checksum/); + expect( + () => validateAddress('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDwU') + ).toThrow(/address checksum/); + }); + + it('fails when invalid base58 encoded address is found', (): void => { + expect( + () => validateAddress('F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29') + ).toThrow(/Decoding F3opIRbN5ZbjJNU511Kj2TLuzFcDq9BGduA9TgiECafpg29: Invalid base58 character "I" \(0x49\) at index 4/); + }); +});