Schnorrkel support (#72)

* Schnorrkel support

* Uuse KeyringOptions in loadAll

* Bump

* Bump version

* Integrate new updates
This commit is contained in:
Jaco Greeff
2019-02-14 23:38:49 +01:00
committed by GitHub
parent 6c851035b8
commit c48b6ff6f8
10 changed files with 97 additions and 84 deletions
+32 -31
View File
@@ -3,11 +3,11 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Prefix } from '@polkadot/keyring/address/types';
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types';
import { KeyringInstance, KeyringPair, KeyringOptions } from '@polkadot/keyring/types';
import { AddressSubject } from './observable/types';
import testKeyring from '@polkadot/keyring/testing';
import { isString } from '@polkadot/util';
import { isBoolean, isString } from '@polkadot/util';
import accounts from './observable/accounts';
import addresses from './observable/addresses';
@@ -18,13 +18,12 @@ export default class Base {
private _accounts: AddressSubject;
private _addresses: AddressSubject;
private _keyring?: KeyringInstance;
private _prefix: Prefix;
private _prefix?: Prefix;
constructor () {
this._accounts = accounts;
this._addresses = addresses;
this._keyring = undefined;
this._prefix = 42;
}
get accounts () {
@@ -51,6 +50,30 @@ export default class Base {
return this.keyring.encodeAddress(key);
}
getPair (address: string | Uint8Array): KeyringPair {
return this.keyring.getPair(address);
}
getPairs (): Array<KeyringPair> {
return this.keyring.getPairs().filter((pair: KeyringPair) =>
env.isDevelopment() || pair.getMeta().isTesting !== true
);
}
isAvailable (_address: Uint8Array | string): boolean {
const accountsValue = this.accounts.subject.getValue();
const addressesValue = this.addresses.subject.getValue();
const address = isString(_address)
? _address
: this.encodeAddress(_address);
return !accountsValue[address] && !addressesValue[address];
}
isPassValid (password: string): boolean {
return password.length > 0 && password.length <= MAX_PASS_LEN;
}
setAddressPrefix (prefix: number): void {
this._prefix = prefix as Prefix;
}
@@ -59,10 +82,12 @@ export default class Base {
env.set(isDevelopment);
}
protected initKeyring (): void {
const keyring = testKeyring();
protected initKeyring (options: KeyringOptions & { isDevelopment?: boolean }): void {
const keyring = testKeyring({ addressPrefix: this._prefix, ...options });
keyring.setAddressPrefix(this._prefix);
if (isBoolean(options.isDevelopment)) {
this.setDevMode(options.isDevelopment);
}
this._keyring = keyring;
@@ -87,28 +112,4 @@ export default class Base {
pair.setMeta({ whenCreated: Date.now() });
}
}
isAvailable (_address: Uint8Array | string): boolean {
const accountsValue = this.accounts.subject.getValue();
const addressesValue = this.addresses.subject.getValue();
const address = isString(_address)
? _address
: this.encodeAddress(_address);
return !accountsValue[address] && !addressesValue[address];
}
isPassValid (password: string): boolean {
return password.length > 0 && password.length <= MAX_PASS_LEN;
}
getPair (address: string | Uint8Array): KeyringPair {
return this.keyring.getPair(address);
}
getPairs (): Array<KeyringPair> {
return this.keyring.getPairs().filter((pair: KeyringPair) =>
env.isDevelopment() || pair.getMeta().isTesting !== true
);
}
}
+7 -7
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, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types';
import { KeyringPair, KeyringPair$Meta, KeyringPair$Json, KeyringOptions } from '@polkadot/keyring/types';
import { SingleAddress } from './observable/types';
import { KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringStruct } from './types';
@@ -16,7 +16,7 @@ import { accountKey, addressKey, accountRegex, addressRegex } from './defaults';
import keyringOption from './options';
// No accounts (or test accounts) should be loaded until after the chain determination.
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll()` is triggered
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
// from the API after the chain is received
class Keyring extends Base implements KeyringStruct {
addAccountPair (pair: KeyringPair, password: string): KeyringPair {
@@ -45,7 +45,7 @@ class Keyring extends Base implements KeyringStruct {
}
createAccountExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): KeyringPair {
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true });
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
this.saveAccount(pair);
@@ -153,8 +153,8 @@ class Keyring extends Base implements KeyringStruct {
this.rewriteKey(json, key, hexAddr, addressKey);
}
loadAll (): void {
super.initKeyring();
loadAll (options: KeyringOptions): void {
super.initKeyring(options);
store.each((json: KeyringJson, key: string) => {
if (accountRegex.test(key)) {
@@ -169,9 +169,9 @@ class Keyring extends Base implements KeyringStruct {
restoreAccount (json: KeyringPair$Json, password: string): KeyringPair {
const pair = createPair(
this.keyring.type,
{
publicKey: this.decodeAddress(json.address),
secretKey: new Uint8Array()
publicKey: this.decodeAddress(json.address)
},
json.meta,
hexToU8a(json.encoded)
+2 -2
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 { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types';
import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta, KeyringPair$Json, KeyringOptions } from '@polkadot/keyring/types';
import { AddressSubject, SingleAddress } from './observable/types';
export type KeyringJson$Meta = {
@@ -50,7 +50,7 @@ export interface KeyringStruct {
getPairs: () => Array<KeyringPair>;
isAvailable: (address: string | Uint8Array) => boolean;
isPassValid: (password: string) => boolean;
loadAll: () => void;
loadAll: (options: KeyringOptions) => void;
restoreAccount: (json: KeyringPair$Json, password: string) => KeyringPair;
saveAccount: (pair: KeyringPair, password?: string) => void;
saveAccountMeta: (pair: KeyringPair, meta: KeyringPair$Meta) => void;