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
);
}
}