Add createUri and createExternal (#96)

* Add createUri and createExternal

* Update signatures

* Expose uiSettings.availableCryptos
This commit is contained in:
Jaco Greeff
2019-03-19 10:15:25 +01:00
committed by GitHub
parent 9e1771bd78
commit 10d22e2b84
5 changed files with 64 additions and 21 deletions
+6
View File
@@ -1,3 +1,9 @@
# 0.32.1
- Add createUri to create an account from a path uri
- Add createExternal as an alias for createAccountExternal
- Deprecate createAccount, createAccountExternal and createAccountMnemonic
# 0.31.1
- Allow for latest keyring with sr25519 derived support
+37 -18
View File
@@ -3,12 +3,13 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types';
import { KeypairType } from '@polkadot/util-crypto/types';
import { SingleAddress } from './observable/types';
import { KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
import store from 'store';
import createPair from '@polkadot/keyring/pair';
import { hexToU8a, isHex, isString } from '@polkadot/util';
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
import env from './observable/development';
import Base from './Base';
@@ -36,28 +37,42 @@ export class Keyring extends Base implements KeyringStruct {
return pair.toJson(password);
}
createAccount (seed: Uint8Array, password?: string, meta: KeyringPair$Meta = {}): KeyringPair {
const pair = this.keyring.addFromSeed(seed, meta);
createAccount (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccount deprecated, use createUri instead');
this.saveAccount(pair, password);
return pair;
return this.createUri(u8aToHex(seed), password, meta).pair;
}
createAccountExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): KeyringPair {
createAccountExternal (publicKey: Uint8Array, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccountExternal deprecated, use createUri instead');
return this.createExternal(publicKey, meta).pair;
}
createAccountMnemonic (seed: string, password?: string, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccountMnemonic deprecated, use createUri instead');
return this.createUri(seed, password, meta).pair;
}
createExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
const json = this.saveAccount(pair);
this.saveAccount(pair);
return pair;
return {
json,
pair
};
}
createAccountMnemonic (seed: string, password?: string, meta: KeyringPair$Meta = {}): KeyringPair {
const pair = this.keyring.addFromMnemonic(seed, meta);
createUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
const pair = this.keyring.addFromUri(suri, meta, type);
const json = this.saveAccount(pair, password);
this.saveAccount(pair, password);
return pair;
return {
json,
pair
};
}
encryptAccount (pair: KeyringPair, password: string): void {
@@ -185,13 +200,15 @@ export class Keyring extends Base implements KeyringStruct {
return pair;
}
saveAccount (pair: KeyringPair, password?: string): void {
saveAccount (pair: KeyringPair, password?: string): KeyringPair$Json {
this.addTimestamp(pair);
const json = pair.toJson(password);
this.keyring.addFromJson(json);
this.accounts.add(json.address, json);
return json;
}
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
@@ -204,7 +221,7 @@ export class Keyring extends Base implements KeyringStruct {
this.accounts.add(json.address, json);
}
saveAddress (address: string, meta: KeyringPair$Meta): void {
saveAddress (address: string, meta: KeyringPair$Meta): KeyringPair$Json {
const available = this.addresses.subject.getValue();
const json = (available[address] && available[address].json) || {
@@ -222,6 +239,8 @@ export class Keyring extends Base implements KeyringStruct {
delete json.meta.isRecent;
this.addresses.add(address, json);
return json as KeyringPair$Json;
}
saveRecent (address: string): SingleAddress {
+10 -2
View File
@@ -3,6 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta, KeyringPair$Json, KeyringOptions as KeyringOptionsBase } from '@polkadot/keyring/types';
import { KeypairType } from '@polkadot/util-crypto/types';
import { AddressSubject, SingleAddress } from './observable/types';
export type KeyringOptions = KeyringOptionsBase & {
@@ -31,6 +32,11 @@ export type KeyringAddress = {
getMeta: () => KeyringJson$Meta
};
export type CreateResult = {
json: KeyringPair$Json,
pair: KeyringPair
};
export interface KeyringStruct {
readonly accounts: AddressSubject;
readonly addresses: AddressSubject;
@@ -41,6 +47,8 @@ export interface KeyringStruct {
createAccount: (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta) => KeyringPair;
createAccountExternal: (publicKey: Uint8Array, meta?: KeyringPair$Meta) => KeyringPair;
createAccountMnemonic: (seed: string, password?: string, meta?: KeyringPair$Meta) => KeyringPair;
createExternal: (publicKey: Uint8Array, meta?: KeyringPair$Meta) => CreateResult;
createUri: (suri: string, password?: string, meta?: KeyringPair$Meta, type?: KeypairType) => CreateResult;
decodeAddress: (key: string | Uint8Array) => Uint8Array;
encodeAddress: (key: string | Uint8Array) => string;
encryptAccount: (pair: KeyringPair, password: string) => void;
@@ -56,9 +64,9 @@ export interface KeyringStruct {
isPassValid: (password: string) => boolean;
loadAll: (options: KeyringOptions) => void;
restoreAccount: (json: KeyringPair$Json, password: string) => KeyringPair;
saveAccount: (pair: KeyringPair, password?: string) => void;
saveAccount: (pair: KeyringPair, password?: string) => KeyringPair$Json;
saveAccountMeta: (pair: KeyringPair, meta: KeyringPair$Meta) => void;
saveAddress: (address: string, meta: KeyringPair$Meta) => void;
saveAddress: (address: string, meta: KeyringPair$Meta) => KeyringPair$Json;
saveRecent: (address: string) => SingleAddress;
setDevMode: (isDevelopment: boolean) => void;
}
+5 -1
View File
@@ -4,7 +4,7 @@
import store from 'store';
import { ENDPOINT_DEFAULT, ENDPOINTS, LANGUAGE_DEFAULT, LANGUAGES, UIMODE_DEFAULT, UIMODES, UITHEME_DEFAULT, UITHEMES } from './defaults';
import { CRYPTOS, ENDPOINT_DEFAULT, ENDPOINTS, LANGUAGE_DEFAULT, LANGUAGES, UIMODE_DEFAULT, UIMODES, UITHEME_DEFAULT, UITHEMES } from './defaults';
import { Options, SettingsStruct } from './types';
export class Settings implements SettingsStruct {
@@ -42,6 +42,10 @@ export class Settings implements SettingsStruct {
return ENDPOINTS;
}
get availableCryptos (): Options {
return CRYPTOS;
}
get availableLanguages (): Options {
return LANGUAGES;
}
+6
View File
@@ -11,6 +11,11 @@ const WSS_POLKADOT = 'wss://poc3-rpc.polkadot.io/';
const WSS_SUBSTRATE = 'wss://substrate-rpc.parity.io/';
const LANGUAGE_DEFAULT = 'default';
const CRYPTOS: Options = [
{ text: 'Edwards (ed25519)', value: 'ed25519' },
{ text: 'Schnorrkel (sr25519)', value: 'sr25519' }
];
const ENDPOINTS: Options = [
{ text: 'Alexander (Polkadot, hosted by Parity)', value: WSS_POLKADOT },
{ text: 'Dried Danta (Substrate, hosted by Parity)', value: WSS_SUBSTRATE },
@@ -44,6 +49,7 @@ const UIMODE_DEFAULT = !isPolkadot && window.location.host.indexOf('ui-light') !
: 'full';
export {
CRYPTOS,
ENDPOINT_DEFAULT,
ENDPOINTS,
LANGUAGE_DEFAULT,