From 960d3995b5d7c46ccaceb94450f0333d3cf6ed1a Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 21 Aug 2019 14:18:16 +0200 Subject: [PATCH] EdcsaSignature to [u8; 65] (no tuple hacks) (#1331) --- packages/types/src/codec/U8aFixed.spec.ts | 70 +++++++++++++------ packages/types/src/codec/U8aFixed.ts | 41 +++++------ .../interfaces/claims/EdcsaSignature.spec.ts | 36 ++++++++++ .../src/interfaces/claims/definitions.ts | 2 +- packages/types/src/interfaces/claims/types.ts | 6 +- 5 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 packages/types/src/interfaces/claims/EdcsaSignature.spec.ts diff --git a/packages/types/src/codec/U8aFixed.spec.ts b/packages/types/src/codec/U8aFixed.spec.ts index fb3b99a4da..0444f95cce 100644 --- a/packages/types/src/codec/U8aFixed.spec.ts +++ b/packages/types/src/codec/U8aFixed.spec.ts @@ -5,35 +5,59 @@ import U8aFixed from './U8aFixed'; describe('U8aFixed', (): void => { - let u8a: U8aFixed; + describe('construction', (): void => { + it('allows empty values', (): void => { + expect( + new U8aFixed().toHex() + ).toEqual('0x0000000000000000000000000000000000000000000000000000000000000000'); + }); - beforeEach((): void => { - u8a = new U8aFixed([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 32); + it('allows construction via with', (): void => { + expect( + new (U8aFixed.with(64))().bitLength() + ).toEqual(64); + }); + + it('constructs from hex', (): void => { + expect( + new (U8aFixed.with(32))('0x01020304').toU8a() + ).toEqual( + new Uint8Array([0x01, 0x02, 0x03, 0x04]) + ); + }); + + it('constructs from number[]', (): void => { + expect( + new (U8aFixed.with(32))([0x02, 0x03]).toU8a() + ).toEqual( + new Uint8Array([0x02, 0x03, 0x00, 0x00]) + ); + }); }); - it('limits the length', (): void => { - expect(u8a.length).toEqual(4); - }); + describe('utils', (): void => { + let u8a: U8aFixed; - it('exposes the correct bitLength', (): void => { - expect(u8a.bitLength()).toEqual(32); - }); + beforeEach((): void => { + u8a = new U8aFixed([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 32); + }); - it('allows wrapping of a pre-existing instance', (): void => { - expect( - u8a.toU8a() - ).toEqual(new Uint8Array([1, 2, 3, 4])); - }); + it('limits the length', (): void => { + expect(u8a.length).toEqual(4); + }); - it('allows empty values', (): void => { - expect( - new U8aFixed().toHex() - ).toEqual('0x0000000000000000000000000000000000000000000000000000000000000000'); - }); + it('exposes the correct bitLength', (): void => { + expect(u8a.bitLength()).toEqual(32); + }); - it('allows construction via with', (): void => { - expect( - new (U8aFixed.with(64))().bitLength() - ).toEqual(64); + it('allows wrapping of a pre-existing instance', (): void => { + expect( + u8a.toU8a() + ).toEqual(new Uint8Array([1, 2, 3, 4])); + }); + + it('hash a sane toRawType', (): void => { + expect(u8a.toRawType()).toEqual('[u8;4]'); + }); }); }); diff --git a/packages/types/src/codec/U8aFixed.ts b/packages/types/src/codec/U8aFixed.ts index 6f33104503..f36b63959e 100644 --- a/packages/types/src/codec/U8aFixed.ts +++ b/packages/types/src/codec/U8aFixed.ts @@ -2,13 +2,16 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { isString, isU8a, u8aToU8a } from '@polkadot/util'; +import { isString, u8aToU8a } from '@polkadot/util'; import { AnyU8a, Constructor } from '../types'; import U8a from './U8a'; -export type BitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 512 | 1024 | 2048; +// The 520 here is a weird one - it is explicitly for a [u8; 65] as found as a EcdsaSignature +// Likewise 160 is for [u8; 20], which is also a H160, i.e. an Ethereum address. Both these are +// as a result of the Polkadot claims module. (Technically we don't need the 520 in here) +export type BitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 512 | 520 | 1024 | 2048; /** * @name U8aFixed @@ -23,27 +26,25 @@ export default class U8aFixed extends U8a { ); } - private static decodeU8aFixed (value: AnyU8a, bitLength: BitLength = 256): AnyU8a { - if (isU8a(value)) { - // ensure that we have an actual u8a with the full length as specified by - // the bitLength input (padded with zeros as required) - const byteLength = bitLength / 8; - const sub = value.subarray(0, byteLength); - - if (sub.length === byteLength) { - return sub; - } - - const u8a = new Uint8Array(byteLength); - - u8a.set(sub, 0); - - return u8a; - } else if (Array.isArray(value) || isString(value)) { + private static decodeU8aFixed (value: AnyU8a, bitLength: BitLength): AnyU8a { + if (Array.isArray(value) || isString(value)) { return U8aFixed.decodeU8aFixed(u8aToU8a(value), bitLength); } - return value; + // ensure that we have an actual u8a with the full length as specified by + // the bitLength input (padded with zeros as required) + const byteLength = bitLength / 8; + const sub = value.subarray(0, byteLength); + + if (sub.length === byteLength) { + return sub; + } + + const u8a = new Uint8Array(byteLength); + + u8a.set(sub, 0); + + return u8a; } public static with (bitLength: BitLength): Constructor { diff --git a/packages/types/src/interfaces/claims/EdcsaSignature.spec.ts b/packages/types/src/interfaces/claims/EdcsaSignature.spec.ts new file mode 100644 index 0000000000..7bfaeeff72 --- /dev/null +++ b/packages/types/src/interfaces/claims/EdcsaSignature.spec.ts @@ -0,0 +1,36 @@ +// Copyright 2017-2019 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import '../../injector'; + +import { hexToU8a } from '@polkadot/util'; + +import { createType } from '../../codec/create'; + +const TESTS = [ + // jaco's claims sig + '0x55bd020bdbbdc02de34e915effc9b18a99002f4c29f64e22e8dcbb69e722ea6c28e1bb53b9484063fbbfd205e49dcc1f620929f520c9c4c3695150f05a28f52a01', + // keith's claim sig + '0x98adffe14b1882ba5a861d6aaa10805d52aed56f480e1ece01505a77470f29f15cb4b0a1dc33177761de8270199282baf160f255e1ca0e4c8354b54b0059e40a1c' +]; + +describe('EdcsaSignature', (): void => { + it('constructs and has a valid representation (toHex)', (): void => { + expect( + createType('EcdsaSignature', TESTS[0]).toHex() + ).toEqual(TESTS[0]); + }); + + it('constructs and has a valid representation (toJSON)', (): void => { + expect( + createType('EcdsaSignature', TESTS[0]).toJSON() + ).toEqual(TESTS[0]); + }); + + it('constructs and has a valid representation (toU8a)', (): void => { + expect( + createType('EcdsaSignature', TESTS[1]).toU8a() + ).toEqual(hexToU8a(TESTS[1])); + }); +}); diff --git a/packages/types/src/interfaces/claims/definitions.ts b/packages/types/src/interfaces/claims/definitions.ts index 53313bc057..4b91924ad0 100644 --- a/packages/types/src/interfaces/claims/definitions.ts +++ b/packages/types/src/interfaces/claims/definitions.ts @@ -4,7 +4,7 @@ export default { types: { - EcdsaSignature: '(H256, H256, i8)', + EcdsaSignature: '[u8; 65]', EthereumAddress: 'H160' } }; diff --git a/packages/types/src/interfaces/claims/types.ts b/packages/types/src/interfaces/claims/types.ts index da021b119e..a556a3fcc7 100644 --- a/packages/types/src/interfaces/claims/types.ts +++ b/packages/types/src/interfaces/claims/types.ts @@ -2,10 +2,10 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { Codec } from '../../types'; -import { H160, H256, i8 } from '../../primitive'; +import { H160 } from '../../primitive'; -/** [H256, H256, i8] & Codec */ -export type EcdsaSignature = [H256, H256, i8] & Codec; +/** Uint8Array & Codec */ +export type EcdsaSignature = Uint8Array & Codec; /** H160 */ export type EthereumAddress = H160;