EdcsaSignature to [u8; 65] (no tuple hacks) (#1331)
This commit is contained in:
@@ -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]');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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<U8aFixed> {
|
||||
|
||||
@@ -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]));
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
export default {
|
||||
types: {
|
||||
EcdsaSignature: '(H256, H256, i8)',
|
||||
EcdsaSignature: '[u8; 65]',
|
||||
EthereumAddress: 'H160'
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user