mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-06-20 23:21:04 +00:00
Simplify KeyringAddress interface (#144)
* Simplify ui-keyring KeyringAddress * Adapt to new @polkadot/keyring KeyPair interface * Pull in common 0.93.0-beta.4 * KeyringAddress | undefined
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
- expose `address` getter instead of `address()`
|
- expose `address` getter instead of `address()`
|
||||||
- expose `publicKey` getter instead of `publicKey()`
|
- expose `publicKey` getter instead of `publicKey()`
|
||||||
- expose `meta` getter instead of `getMeta()`
|
- expose `meta` getter instead of `getMeta()`
|
||||||
|
- The functions `getAccount` `getAddress` `getContract` in `@polkadot/ui-keyring` now return either undefined or an object with the above properties.
|
||||||
- Add support for the saving of contracts to the keyring
|
- Add support for the saving of contracts to the keyring
|
||||||
- Use the injection of stores, providing an additional `ExtensionStore` for saving to Chrome/FF extensions (in addition to the standard localStorage saving)
|
- Use the injection of stores, providing an additional `ExtensionStore` for saving to Chrome/FF extensions (in addition to the standard localStorage saving)
|
||||||
- Remove previously deprecated kering functions, `createAccount`, `createAccountExternal` & `createAccountMnemonic`
|
- Remove previously deprecated kering functions, `createAccount`, `createAccountExternal` & `createAccountMnemonic`
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ render () {
|
|||||||
const pair = keyring.getPair(address);
|
const pair = keyring.getPair(address);
|
||||||
|
|
||||||
// ask questions about that particular keyring pair
|
// ask questions about that particular keyring pair
|
||||||
const isLocked = pair.isLocked();
|
const isLocked = pair.isLocked;
|
||||||
const meta = pair.getMeta();
|
const meta = pair.meta;
|
||||||
|
|
||||||
// save account from pair
|
// save account from pair
|
||||||
keyring.saveAccount(pair, password);
|
keyring.saveAccount(pair, password);
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
this.contracts.remove(this._store, address);
|
this.contracts.remove(this._store, address);
|
||||||
}
|
}
|
||||||
|
|
||||||
getAccount (address: string | Uint8Array): KeyringAddress {
|
getAccount (address: string | Uint8Array): KeyringAddress | undefined {
|
||||||
return this.getAddress(address, 'account');
|
return this.getAddress(address, 'account');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,48 +102,28 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
|
|
||||||
return Object
|
return Object
|
||||||
.keys(available)
|
.keys(available)
|
||||||
.map((address) => this.getAddress(address, 'account'))
|
.map((address) => this.getAddress(address, 'account') as KeyringAddress)
|
||||||
.filter((account) => env.isDevelopment() || account.meta.isTesting !== true);
|
.filter((account) => env.isDevelopment() || account.meta.isTesting !== true);
|
||||||
}
|
}
|
||||||
|
|
||||||
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress {
|
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
|
||||||
const encodeAddress = this.encodeAddress;
|
|
||||||
const address = isString(_address)
|
const address = isString(_address)
|
||||||
? _address
|
? _address
|
||||||
: this.encodeAddress(_address);
|
: this.encodeAddress(_address);
|
||||||
const publicKey = this.decodeAddress(address);
|
const publicKey = this.decodeAddress(address);
|
||||||
const subject = (() => {
|
|
||||||
if (type && this.stores[type]) {
|
|
||||||
return this.stores[type]().subject;
|
|
||||||
}
|
|
||||||
|
|
||||||
let subject;
|
const stores = type
|
||||||
Object.values(this.stores).forEach((store) => {
|
? [this.stores[type]]
|
||||||
if (store && store().subject.getValue()[address]) {
|
: Object.values(this.stores);
|
||||||
subject = store().subject;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return subject;
|
|
||||||
})();
|
|
||||||
|
|
||||||
if (!subject) {
|
const info = stores.reduce<SingleAddress | undefined>((lastInfo, store) =>
|
||||||
throw new Error('Address not found');
|
(store().subject.getValue()[address] || lastInfo),
|
||||||
}
|
undefined);
|
||||||
|
|
||||||
return {
|
return info && {
|
||||||
get address (): string {
|
address,
|
||||||
return encodeAddress(publicKey);
|
publicKey,
|
||||||
},
|
meta: info.json.meta
|
||||||
get isValid (): boolean {
|
|
||||||
return !!(subject && subject.getValue()[address]);
|
|
||||||
},
|
|
||||||
get publicKey (): Uint8Array {
|
|
||||||
return publicKey;
|
|
||||||
},
|
|
||||||
get meta (): KeyringJson$Meta {
|
|
||||||
// This is actually non-applicable, i.e. here we will have a subject
|
|
||||||
return subject ? subject.getValue()[address].json.meta : {};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,10 +132,10 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
|
|
||||||
return Object
|
return Object
|
||||||
.keys(available)
|
.keys(available)
|
||||||
.map((address) => this.getAddress(address));
|
.map((address) => this.getAddress(address) as KeyringAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
getContract (address: string | Uint8Array): KeyringAddress {
|
getContract (address: string | Uint8Array): KeyringAddress | undefined {
|
||||||
return this.getAddress(address, 'contract');
|
return this.getAddress(address, 'contract');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +148,7 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
const { json: { meta: { contract } } } = data;
|
const { json: { meta: { contract } } } = data;
|
||||||
return contract && contract.genesisHash === this.genesisHash;
|
return contract && contract.genesisHash === this.genesisHash;
|
||||||
})
|
})
|
||||||
.map(([address]) => this.getContract(address));
|
.map(([address]) => this.getContract(address) as KeyringAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string) {
|
private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string) {
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ export type KeyringJson = {
|
|||||||
|
|
||||||
export type KeyringAddress = {
|
export type KeyringAddress = {
|
||||||
readonly address: string,
|
readonly address: string,
|
||||||
readonly isValid: boolean,
|
|
||||||
readonly meta: KeyringJson$Meta,
|
readonly meta: KeyringJson$Meta,
|
||||||
readonly publicKey: Uint8Array
|
readonly publicKey: Uint8Array
|
||||||
};
|
};
|
||||||
@@ -77,11 +76,11 @@ export interface KeyringStruct {
|
|||||||
forgetAccount: (address: string) => void;
|
forgetAccount: (address: string) => void;
|
||||||
forgetAddress: (address: string) => void;
|
forgetAddress: (address: string) => void;
|
||||||
forgetContract: (address: string) => void;
|
forgetContract: (address: string) => void;
|
||||||
getAccount: (address: string | Uint8Array) => KeyringAddress;
|
getAccount: (address: string | Uint8Array) => KeyringAddress | undefined;
|
||||||
getAccounts: () => Array<KeyringAddress>;
|
getAccounts: () => Array<KeyringAddress>;
|
||||||
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress;
|
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress | undefined;
|
||||||
getAddresses: () => Array<KeyringAddress>;
|
getAddresses: () => Array<KeyringAddress>;
|
||||||
getContract: (address: string | Uint8Array) => KeyringAddress;
|
getContract: (address: string | Uint8Array) => KeyringAddress | undefined;
|
||||||
getContracts: (genesisHash?: string) => Array<KeyringAddress>;
|
getContracts: (genesisHash?: string) => Array<KeyringAddress>;
|
||||||
getPair: (address: string | Uint8Array) => KeyringPair;
|
getPair: (address: string | Uint8Array) => KeyringPair;
|
||||||
getPairs: () => Array<KeyringPair>;
|
getPairs: () => Array<KeyringPair>;
|
||||||
|
|||||||
Reference in New Issue
Block a user