Use objectSpread as applicable (#538)

* Use objectSpread as applicable

* Updates

* Add objectSpread import
This commit is contained in:
Jaco
2021-11-05 05:27:07 +01:00
committed by GitHub
parent 2910128399
commit f6e6c83950
14 changed files with 139 additions and 139 deletions
+12 -12
View File
@@ -10,7 +10,7 @@ import type { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType,
import { createPair } from '@polkadot/keyring';
import { chains } from '@polkadot/ui-settings';
import { bnToBn, hexToU8a, isHex, isString, stringToU8a, u8aSorted, u8aToString } from '@polkadot/util';
import { bnToBn, hexToU8a, isHex, isString, objectSpread, stringToU8a, u8aSorted, u8aToString } from '@polkadot/util';
import { base64Decode, createKeyMulti, jsonDecrypt, jsonEncrypt } from '@polkadot/util-crypto';
import { env } from './observable/env';
@@ -33,7 +33,7 @@ export class Keyring extends Base implements KeyringStruct {
};
public addExternal (address: string | Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
const pair = this.keyring.addFromAddress(address, { ...meta, isExternal: true }, null);
const pair = this.keyring.addFromAddress(address, objectSpread<KeyringJson$Meta>({}, meta, { isExternal: true }), null);
return {
json: this.saveAccount(pair),
@@ -42,7 +42,7 @@ export class Keyring extends Base implements KeyringStruct {
}
public addHardware (address: string | Uint8Array, hardwareType: string, meta: KeyringPair$Meta = {}): CreateResult {
return this.addExternal(address, { ...meta, hardwareType, isHardware: true });
return this.addExternal(address, objectSpread<KeyringPair$Meta>({}, meta, { hardwareType, isHardware: true }));
}
public addMultisig (addresses: (string | Uint8Array)[], threshold: bigint | BN | number, meta: KeyringPair$Meta = {}): CreateResult {
@@ -51,7 +51,7 @@ export class Keyring extends Base implements KeyringStruct {
// we could use `sortAddresses`, but rather use internal encode/decode so we are 100%
const who = u8aSorted(addresses.map((who) => this.decodeAddress(who))).map((who) => this.encodeAddress(who));
return this.addExternal(address, { ...meta, isMultisig: true, threshold: bnToBn(threshold).toNumber(), who });
return this.addExternal(address, objectSpread<KeyringPair$Meta>({}, meta, { isMultisig: true, threshold: bnToBn(threshold).toNumber(), who }));
}
public addPair (pair: KeyringPair, password: string): CreateResult {
@@ -91,17 +91,20 @@ export class Keyring extends Base implements KeyringStruct {
const accounts = await Promise.all(accountPromises);
return {
...jsonEncrypt(stringToU8a(JSON.stringify(accounts)), ['batch-pkcs8'], password),
return objectSpread({}, jsonEncrypt(stringToU8a(JSON.stringify(accounts)), ['batch-pkcs8'], password), {
accounts: accounts.map((account) => ({
address: account.address,
meta: account.meta
}))
};
});
}
public createFromJson (json: KeyringPair$Json, meta: KeyringPair$Meta = {}): KeyringPair {
return this.keyring.createFromJson({ ...json, meta: { ...(json.meta || {}), meta } });
return this.keyring.createFromJson(
objectSpread({}, json, {
meta: objectSpread({}, json.meta, meta)
})
);
}
public createFromUri (suri: string, meta: KeyringPair$Meta = {}, type?: KeypairType): KeyringPair {
@@ -249,10 +252,7 @@ export class Keyring extends Base implements KeyringStruct {
private loadInjected (address: string, meta: KeyringJson$Meta, type?: KeypairType): void {
const json = {
address,
meta: {
...meta,
isInjected: true
}
meta: objectSpread<KeyringJson$Meta>({}, meta, { isInjected: true })
};
const pair = this.keyring.addFromAddress(address, json.meta, null, type);