testingPairs for inetrnal testing (#118)
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { PairState, PairState$Meta } from './types';
|
||||
import type { KeyringPair$Meta } from '../types';
|
||||
import type { PairState } from './types';
|
||||
|
||||
module.exports = function getMeta (state: PairState): PairState$Meta {
|
||||
module.exports = function getMeta (state: PairState): KeyringPair$Meta {
|
||||
return state.meta;
|
||||
};
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
// @flow
|
||||
|
||||
import type { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import type { KeyringPair } from '../types';
|
||||
import type { PairState, PairState$Meta } from './types';
|
||||
import type { KeyringPair, KeyringPair$Meta } from '../types';
|
||||
import type { PairState } from './types';
|
||||
|
||||
const naclSign = require('@polkadot/util-crypto/nacl/sign');
|
||||
const naclVerify = require('@polkadot/util-crypto/nacl/verify');
|
||||
@@ -29,11 +29,11 @@ module.exports = function pair ({ publicKey, secretKey }: KeypairType): KeyringP
|
||||
},
|
||||
encodePkcs8: (passphrase?: Uint8Array | string): Uint8Array =>
|
||||
encode(secretKey, passphrase),
|
||||
getMeta: (): PairState$Meta =>
|
||||
getMeta: (): KeyringPair$Meta =>
|
||||
getMeta(state),
|
||||
publicKey: (): Uint8Array =>
|
||||
publicKey,
|
||||
setMeta: (meta: PairState$Meta): void =>
|
||||
setMeta: (meta: KeyringPair$Meta): void =>
|
||||
setMeta(state, meta),
|
||||
sign: (message: Uint8Array): Uint8Array =>
|
||||
naclSign(message, secretKey),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const keyring = require('../testing')();
|
||||
const keyring = require('../testingPairs')();
|
||||
|
||||
// aligned with @polkadot/util-crypto tests
|
||||
describe('pair', () => {
|
||||
|
||||
@@ -3,8 +3,9 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { PairState, PairState$Meta } from './types';
|
||||
import type { KeyringPair$Meta } from '../types';
|
||||
import type { PairState } from './types';
|
||||
|
||||
module.exports = function setMeta (state: PairState, meta: PairState$Meta): void {
|
||||
module.exports = function setMeta (state: PairState, meta: KeyringPair$Meta): void {
|
||||
state.meta = meta;
|
||||
};
|
||||
|
||||
@@ -3,10 +3,8 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
export type PairState$Meta = {
|
||||
[string]: string
|
||||
};
|
||||
import type { KeyringPair$Meta } from '../types';
|
||||
|
||||
export type PairState = {
|
||||
meta: PairState$Meta
|
||||
meta: KeyringPair$Meta
|
||||
};
|
||||
|
||||
@@ -3,11 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { KeyringPair } from './types';
|
||||
|
||||
type TestKeyring = {
|
||||
[string]: KeyringPair
|
||||
};
|
||||
import type { KeyringInstance } from './types';
|
||||
|
||||
const hexToU8a = require('@polkadot/util/hex/toU8a');
|
||||
const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
@@ -15,11 +11,7 @@ const u8aFromString = require('@polkadot/util/u8a/fromString');
|
||||
const createKeyring = require('./index');
|
||||
|
||||
function padSeed (seed: string): Uint8Array {
|
||||
while (seed.length < 32) {
|
||||
seed += ' ';
|
||||
}
|
||||
|
||||
return u8aFromString(seed);
|
||||
return u8aFromString(seed.padEnd(32, ' '));
|
||||
}
|
||||
|
||||
const SEEDS = {
|
||||
@@ -41,14 +33,14 @@ const SEEDS = {
|
||||
hexToU8a('0x9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60')
|
||||
};
|
||||
|
||||
module.exports = function testKeyring (): TestKeyring {
|
||||
module.exports = function testKeyring (): KeyringInstance {
|
||||
const keyring = createKeyring();
|
||||
|
||||
return Object
|
||||
.keys(SEEDS)
|
||||
.reduce((result, key) => {
|
||||
result[key] = keyring.addFromSeed(SEEDS[key]);
|
||||
Object.keys(SEEDS).forEach((name) => {
|
||||
keyring
|
||||
.addFromSeed(SEEDS[name])
|
||||
.setMeta({ name });
|
||||
});
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
return keyring;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { KeyringPair } from './types';
|
||||
|
||||
type TestKeyringMap = {
|
||||
[string]: KeyringPair
|
||||
};
|
||||
|
||||
const createKeyring = require('./testing');
|
||||
|
||||
module.exports = function testKeyringPairs (): TestKeyringMap {
|
||||
const keyring = createKeyring();
|
||||
const pairs = keyring.getPairs();
|
||||
|
||||
return pairs.reduce((result, pair) => {
|
||||
const { name } = pair.getMeta();
|
||||
|
||||
result[(name: string)] = pair;
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
};
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
describe('testing', () => {
|
||||
it('creates without failing', () => {
|
||||
expect(
|
||||
require('./testing')
|
||||
require('./testingPairs')
|
||||
).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -3,10 +3,17 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
export type KeyringPair$Meta = {
|
||||
// flowlint-next-line unclear-type:off
|
||||
[string]: any
|
||||
}
|
||||
|
||||
export type KeyringPair = {
|
||||
decodePkcs8: (encoded: Uint8Array, passphrase: Uint8Array | string) => void,
|
||||
encodePkcs8: (passphrase: Uint8Array | string) => Uint8Array,
|
||||
getMeta: () => KeyringPair$Meta,
|
||||
publicKey: () => Uint8Array,
|
||||
setMeta: (meta: KeyringPair$Meta) => void,
|
||||
sign (message: Uint8Array): Uint8Array,
|
||||
verify (message: Uint8Array, signature: Uint8Array): boolean
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user