Expand keyring tests for sr25519 (#302)

* Expand keyring tests for sr25519

* Update index.spec.ts

* Update index.spec.ts

* Support type conversion, type per pair

* toType tests
This commit is contained in:
Jaco Greeff
2019-02-14 22:04:12 +01:00
committed by GitHub
parent 97cee907e4
commit c3fafbeab7
14 changed files with 183 additions and 105 deletions
+2 -2
View File
@@ -4,6 +4,7 @@
import { assert, stringToU8a, u8aFixLength } from '@polkadot/util/index';
import { naclDecrypt } from '@polkadot/util-crypto/index';
import { PairInfo } from './types';
import { KEY_LENGTH, NONCE_LENGTH, PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
@@ -11,8 +12,7 @@ const SEED_OFFSET = PKCS8_HEADER.length;
const DIV_OFFSET = SEED_OFFSET + KEY_LENGTH;
const PUBLIC_OFFSET = DIV_OFFSET + PKCS8_DIVIDER.length;
type DecodeResult = {
publicKey: Uint8Array,
type DecodeResult = PairInfo & {
seed: Uint8Array
};
+3 -1
View File
@@ -2,12 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { PairInfo } from './types';
import { stringToU8a, u8aConcat, u8aFixLength, assert } from '@polkadot/util/index';
import { naclEncrypt } from '@polkadot/util-crypto/index';
import { PKCS8_DIVIDER, PKCS8_HEADER } from './defaults';
export default function encode (publicKey: Uint8Array, seed?: Uint8Array | null, passphrase?: string): Uint8Array {
export default function encode ({ publicKey, seed }: PairInfo, passphrase?: string): Uint8Array {
assert(seed, 'Expected a valid seed to be passed to encode');
const encoded = u8aConcat(
+33 -22
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeypairType } from '@polkadot/util-crypto/types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, PairType } from '../types';
import { KeyringPair, KeyringPair$Json, KeyringPair$Meta, KeyringPairType } from '../types';
import { PairInfo } from './types';
import { naclKeypairFromSeed as naclFromSeed, naclSign, naclVerify, schnorrkelKeypairFromSeed as schnorrkelFromSeed, schnorrkelSign, schnorrkelVerify } from '@polkadot/util-crypto/index';
@@ -13,6 +13,21 @@ import decode from './decode';
import encode from './encode';
import toJson from './toJson';
const fromSeed = (type: KeyringPairType, seed: Uint8Array) =>
type === 'sr25519'
? schnorrkelFromSeed(seed)
: naclFromSeed(seed);
const sign = (type: KeyringPairType, message: Uint8Array, pair: Partial<KeypairType>) =>
type === 'sr25519'
? schnorrkelSign(message, pair)
: naclSign(message, pair);
const verify = (type: KeyringPairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array) =>
type === 'sr25519'
? schnorrkelVerify(message, signature, publicKey)
: naclVerify(message, signature, publicKey);
/**
* @name pair
* @summary Creates a keyring pair object
@@ -44,24 +59,18 @@ import toJson from './toJson';
* an `encoded` property that is assigned with the encoded public key in hex format, and an `encoding`
* property that indicates whether the public key value of the `encoded` property is encoded or not.
*/
export default function pair (type: PairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null): KeyringPair {
const fromSeed = (seed: Uint8Array) =>
type === 'sr25519'
? schnorrkelFromSeed(seed)
: naclFromSeed(seed);
const sign = (message: Uint8Array, pair: Partial<KeypairType>) =>
type === 'sr25519'
? schnorrkelSign(message, pair)
: naclSign(message, pair);
const verify = (message: Uint8Array, signature: Uint8Array) =>
type === 'sr25519'
? schnorrkelVerify(message, signature, publicKey)
: naclVerify(message, signature, publicKey);
let secretKey: Uint8Array | undefined = seed
? fromSeed(seed).secretKey
: undefined;
export default function createPair (type: KeyringPairType, { publicKey, seed }: PairInfo, meta: KeyringPair$Meta = {}, encoded: Uint8Array | null): KeyringPair {
let secretKey: Uint8Array | undefined;
if (seed) {
const pair = fromSeed(type, seed);
publicKey = pair.publicKey;
secretKey = pair.secretKey;
}
return {
type,
address: (): string =>
encodeAddress(publicKey),
decodePkcs8: (passphrase?: string, _encoded?: Uint8Array | null): void => {
@@ -69,10 +78,10 @@ export default function pair (type: PairType, { publicKey, seed }: PairInfo, met
publicKey = decoded.publicKey;
seed = decoded.seed;
secretKey = fromSeed(seed).secretKey;
secretKey = fromSeed(type, seed).secretKey;
},
encodePkcs8: (passphrase?: string): Uint8Array =>
encode(publicKey, seed, passphrase),
encode({ publicKey, seed }, passphrase),
getMeta: (): KeyringPair$Meta =>
meta,
isLocked: (): boolean =>
@@ -86,10 +95,12 @@ export default function pair (type: PairType, { publicKey, seed }: PairInfo, met
meta = meta;
},
sign: (message: Uint8Array): Uint8Array =>
sign(message, { publicKey, secretKey }),
sign(type, message, { publicKey, secretKey }),
toJson: (passphrase?: string): KeyringPair$Json =>
toJson({ meta, publicKey }, encode(publicKey, seed, passphrase), !!passphrase),
toJson(type, { meta, publicKey }, encode({ publicKey, seed }, passphrase), !!passphrase),
toType: (type: KeyringPairType): KeyringPair =>
createPair(type, { publicKey, seed }, meta, null),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
verify(message, signature)
verify(type, message, signature, publicKey)
};
}
+4 -1
View File
@@ -15,7 +15,7 @@ const json: KeyringPair$Json = {
address,
encoded: '',
encoding: {
content: 'none',
content: ['pkcs8', 'ed25519'],
type: 'none',
version: '0'
},
@@ -24,6 +24,7 @@ const json: KeyringPair$Json = {
export default function everybody (): KeyringPair {
return {
type: 'ed25519',
address: (): string =>
address,
decodePkcs8: (passphrase?: string, encoded?: Uint8Array): void =>
@@ -45,6 +46,8 @@ export default function everybody (): KeyringPair {
new Uint8Array(64),
toJson: (passphrase?: string): KeyringPair$Json =>
json,
toType: (): KeyringPair =>
everybody(),
verify: (message: Uint8Array, signature: Uint8Array): boolean =>
false
};
+2 -2
View File
@@ -14,7 +14,7 @@ describe('toJson', () => {
address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ',
encoded: '0x3053020101300506032b657004220420416c696365202020202020202020202020202020202020202020202020202020a123032100d172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f',
encoding: {
content: 'pkcs8',
content: ['pkcs8', 'ed25519'],
type: 'none',
version: '1'
},
@@ -32,7 +32,7 @@ describe('toJson', () => {
expect(json).toMatchObject({
address: '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ',
encoding: {
content: 'pkcs8',
content: ['pkcs8', 'ed25519'],
type: 'xsalsa20-poly1305',
version: '1'
}
+3 -3
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair$Json, KeyringPair$Meta } from '../types';
import { KeyringPair$Json, KeyringPair$Meta, KeyringPairType } from '../types';
import { u8aToHex } from '@polkadot/util/index';
@@ -12,12 +12,12 @@ type PairStateJson = KeyringPair$Meta & {
publicKey: Uint8Array
};
export default function toJson ({ publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
export default function toJson (type: KeyringPairType, { publicKey, meta }: PairStateJson, encoded: Uint8Array, isEncrypted: boolean): KeyringPair$Json {
return {
address: encodeAddress(publicKey),
encoded: u8aToHex(encoded),
encoding: {
content: 'pkcs8',
content: ['pkcs8', type],
type: isEncrypted
? 'xsalsa20-poly1305'
: 'none',
+1 -1
View File
@@ -4,5 +4,5 @@
export type PairInfo = {
publicKey: Uint8Array,
seed?: Uint8Array
seed?: Uint8Array | null
};