UNPKG

1.03 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { assert } from '@polkadot/util';
4import { keyExtractPath } from "../key/index.js";
5import { sr25519DerivePublic } from "../sr25519/index.js";
6import { decodeAddress } from "./decode.js";
7import { encodeAddress } from "./encode.js";
8
9function filterHard({
10 isHard
11}) {
12 return isHard;
13}
14/**
15 * @name deriveAddress
16 * @summary Creates a sr25519 derived address from the supplied and path.
17 * @description
18 * Creates a sr25519 derived address based on the input address/publicKey and the uri supplied.
19 */
20
21
22export function deriveAddress(who, suri, ss58Format) {
23 const {
24 path
25 } = keyExtractPath(suri);
26 assert(path.length && !path.every(filterHard), 'Expected suri to contain a combination of non-hard paths');
27 let publicKey = decodeAddress(who);
28
29 for (const {
30 chainCode
31 } of path) {
32 publicKey = sr25519DerivePublic(publicKey, chainCode);
33 }
34
35 return encodeAddress(publicKey, ss58Format);
36}
\No newline at end of file