mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-06-12 20:01:02 +00:00
Ki contracts (#143)
* add contracts to keyring * genesisHash * requested changes + allPlus option type * remove duplicate fn * add contracts to keyring * genesisHash * requested changes + allPlus option type * remove duplicate fn * changes * contract key * prefixes * remove fallback hell * errors * remove breaking api
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import { KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types';
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { SingleAddress } from './observable/types';
|
||||
import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
|
||||
import { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
|
||||
|
||||
import createPair from '@polkadot/keyring/pair';
|
||||
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
|
||||
@@ -21,6 +21,12 @@ const RECENT_EXPIRY = 24 * 60 * 60;
|
||||
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
|
||||
// from the API after the chain is received
|
||||
export class Keyring extends Base implements KeyringStruct {
|
||||
stores = {
|
||||
address: () => this.addresses,
|
||||
contract: () => this.contracts,
|
||||
account: () => this.accounts
|
||||
};
|
||||
|
||||
addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
|
||||
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
|
||||
const json = this.saveAccount(pair);
|
||||
@@ -118,22 +124,29 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
.filter((account) => env.isDevelopment() || account.getMeta().isTesting !== true);
|
||||
}
|
||||
|
||||
getAddress (_address: string | Uint8Array, type: 'account' | 'address' | 'contract' = 'address'): KeyringAddress {
|
||||
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress {
|
||||
const address = isString(_address)
|
||||
? _address
|
||||
: this.encodeAddress(_address);
|
||||
const publicKey = this.decodeAddress(address);
|
||||
const subject = (() => {
|
||||
switch (type) {
|
||||
case 'account':
|
||||
return this.accounts.subject;
|
||||
case 'address':
|
||||
return this.addresses.subject;
|
||||
case 'contract':
|
||||
return this.contracts.subject;
|
||||
if (type && this.stores[type]) {
|
||||
return this.stores[type]().subject;
|
||||
}
|
||||
|
||||
let subject;
|
||||
Object.values(this.stores).forEach((store) => {
|
||||
if (store && store().subject.getValue()[address]) {
|
||||
subject = store().subject;
|
||||
}
|
||||
});
|
||||
return subject;
|
||||
})();
|
||||
|
||||
if (!subject) {
|
||||
throw new Error('Address not found');
|
||||
}
|
||||
|
||||
return {
|
||||
address: (): string =>
|
||||
address,
|
||||
@@ -305,7 +318,7 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
});
|
||||
}
|
||||
|
||||
saveAddress (address: string, meta: KeyringPair$Meta, type: 'address' | 'contract' = 'address'): KeyringPair$Json {
|
||||
saveAddress (address: string, meta: KeyringPair$Meta, type: KeyringAddressType = 'address'): KeyringPair$Json {
|
||||
const available = this.addresses.subject.getValue();
|
||||
|
||||
const json = (available[address] && available[address].json) || {
|
||||
@@ -322,16 +335,7 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
|
||||
delete json.meta.isRecent;
|
||||
|
||||
const key = (() => {
|
||||
switch (type) {
|
||||
case 'contract':
|
||||
return 'contracts';
|
||||
default:
|
||||
return 'addresses';
|
||||
}
|
||||
})();
|
||||
|
||||
this[key].add(this._store, address, json);
|
||||
this.stores[type]().add(this._store, address, json);
|
||||
|
||||
return json as KeyringPair$Json;
|
||||
}
|
||||
|
||||
@@ -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 { KeyringStruct } from '../types';
|
||||
import { KeyringItemType, KeyringStruct } from '../types';
|
||||
|
||||
export type KeyringSectionOption = {
|
||||
className?: string,
|
||||
@@ -17,13 +17,7 @@ export type KeyringSectionOption = {
|
||||
export type KeyringSectionOptions = Array<KeyringSectionOption>;
|
||||
|
||||
export type KeyringOptions = {
|
||||
account: KeyringSectionOptions,
|
||||
address: KeyringSectionOptions,
|
||||
all: KeyringSectionOptions,
|
||||
allPlus: KeyringSectionOptions,
|
||||
contract: KeyringSectionOptions,
|
||||
recent: KeyringSectionOptions,
|
||||
testing: KeyringSectionOptions
|
||||
[type in KeyringItemType | 'all' | 'allPlus' | 'recent' | 'testing']: KeyringSectionOptions
|
||||
};
|
||||
|
||||
export type KeyringOption$Type = keyof KeyringOptions;
|
||||
|
||||
@@ -50,6 +50,10 @@ export type KeyringAddress = {
|
||||
getMeta: () => KeyringJson$Meta
|
||||
};
|
||||
|
||||
export type KeyringAddressType = 'address' | 'contract';
|
||||
|
||||
export type KeyringItemType = 'account' | KeyringAddressType;
|
||||
|
||||
export type CreateResult = {
|
||||
json: KeyringPair$Json,
|
||||
pair: KeyringPair
|
||||
@@ -78,7 +82,7 @@ export interface KeyringStruct {
|
||||
forgetContract: (address: string) => void;
|
||||
getAccount: (address: string | Uint8Array) => KeyringAddress;
|
||||
getAccounts: () => Array<KeyringAddress>;
|
||||
getAddress: (address: string | Uint8Array) => KeyringAddress;
|
||||
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress;
|
||||
getAddresses: () => Array<KeyringAddress>;
|
||||
getContract: (address: string | Uint8Array) => KeyringAddress;
|
||||
getContracts: (genesisHash?: string) => Array<KeyringAddress>;
|
||||
|
||||
Reference in New Issue
Block a user