Adjust base library usage of for vs reduce/map (#1151)

* Adjust base library usage of `for` vs `reduce/map`

* Additional

* Adjust
This commit is contained in:
Jaco
2021-10-31 08:27:14 +02:00
committed by GitHub
parent 7ef169585f
commit a4cefa4bfc
20 changed files with 130 additions and 87 deletions
+8 -5
View File
@@ -7,7 +7,6 @@ import type { Prefix } from './types';
import { assert } from '@polkadot/util';
import { keyExtractPath } from '../key';
import { DeriveJunction } from '../key/DeriveJunction';
import { schnorrkelDerivePublic } from '../schnorrkel';
import { decodeAddress } from './decode';
import { encodeAddress } from './encode';
@@ -21,9 +20,13 @@ import { encodeAddress } from './encode';
export function deriveAddress (who: HexString | Uint8Array | string, suri: string, ss58Format?: Prefix): string {
const { path } = keyExtractPath(suri);
assert(path.length && !path.some((path) => path.isHard), 'Expected suri to contain a combination of non-hard paths');
assert(path.length && !path.some((p) => p.isHard), 'Expected suri to contain a combination of non-hard paths');
return encodeAddress(path.reduce((publicKey: Uint8Array, path: DeriveJunction): Uint8Array => {
return schnorrkelDerivePublic(publicKey, path.chainCode);
}, decodeAddress(who)), ss58Format);
let publicKey = decodeAddress(who);
for (const { chainCode } of path) {
publicKey = schnorrkelDerivePublic(publicKey, chainCode);
}
return encodeAddress(publicKey, ss58Format);
}