Avoid u8a -> string conversion costs (#106)
* Avoid u8a -> string conversion for pair keys * Update hexToBn tests
This commit is contained in:
@@ -6,11 +6,10 @@
|
||||
import type { KeyringInstance, KeyringPair } from './types';
|
||||
|
||||
type KeyringPairMap = {
|
||||
[string]: KeyringPair
|
||||
[Uint8Array]: KeyringPair
|
||||
};
|
||||
|
||||
const naclKeypairFromSeed = require('@polkadot/util-crypto/nacl/keypair/fromSeed');
|
||||
const u8aToString = require('@polkadot/util/u8a/toString');
|
||||
|
||||
const createPair = require('./pair');
|
||||
|
||||
@@ -24,12 +23,12 @@ module.exports = function keyring (): KeyringInstance {
|
||||
addFromSeed: (seed: Uint8Array | string): KeyringPair => {
|
||||
const pair = createPair(naclKeypairFromSeed(seed));
|
||||
|
||||
pairs[pair.id] = pair;
|
||||
pairs[pair.publicKey] = pair;
|
||||
|
||||
return pair;
|
||||
},
|
||||
getPair: (publicKey: Uint8Array): ?KeyringPair =>
|
||||
pairs[u8aToString(publicKey)],
|
||||
pairs[publicKey],
|
||||
getPairs,
|
||||
getPublicKeys: (): Array<Uint8Array> =>
|
||||
getPairs().map(({ publicKey }) => publicKey)
|
||||
|
||||
@@ -8,11 +8,9 @@ import type { KeyringPair } from './types';
|
||||
|
||||
const naclSign = require('@polkadot/util-crypto/nacl/sign');
|
||||
const naclVerify = require('@polkadot/util-crypto/nacl/verify');
|
||||
const u8aToString = require('@polkadot/util/u8a/toString');
|
||||
|
||||
module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringPair {
|
||||
return {
|
||||
id: u8aToString(publicKey),
|
||||
publicKey,
|
||||
sign: (message: Uint8Array): Uint8Array =>
|
||||
naclSign(message, secretKey),
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
// @flow
|
||||
|
||||
export type KeyringPair = {
|
||||
id: string,
|
||||
publicKey: Uint8Array,
|
||||
|
||||
sign (message: Uint8Array): Uint8Array,
|
||||
|
||||
@@ -30,7 +30,7 @@ module.exports = function hexToBn (_value?: string, isLe: boolean = false): BN {
|
||||
return new BN(0);
|
||||
}
|
||||
|
||||
const value = hexStripPrefix(_value) || '00';
|
||||
const value = hexStripPrefix(_value);
|
||||
|
||||
return new BN(isLe ? reverse(value) : value, 16);
|
||||
return new BN((isLe ? reverse(value) : value) || '00', 16);
|
||||
};
|
||||
|
||||
@@ -23,19 +23,25 @@ describe('hexToBn', () => {
|
||||
).toEqual('0');
|
||||
});
|
||||
|
||||
it('converts 0x values to BN(0) (LE)', () => {
|
||||
expect(
|
||||
hexToBn('0x', true).toString()
|
||||
).toEqual('0');
|
||||
});
|
||||
|
||||
it('converts little-endian', () => {
|
||||
expect(
|
||||
hexToBn('0x4500000000000000', true).toString()
|
||||
).toEqual('69');
|
||||
});
|
||||
|
||||
it('handles starting zeros correctly (be)', () => {
|
||||
it('handles starting zeros correctly (BE)', () => {
|
||||
expect(
|
||||
hexToBn('0x0000000000000100', false).toString()
|
||||
).toEqual('256');
|
||||
});
|
||||
|
||||
it('handles starting zeros correctly (le)', () => {
|
||||
it('handles starting zeros correctly (LE)', () => {
|
||||
expect(
|
||||
hexToBn('0x0001000000000000', true).toString()
|
||||
).toEqual('256');
|
||||
|
||||
Reference in New Issue
Block a user