From 0d03eac307444b259c27e041af8557ad7b95b6c6 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 9 Aug 2020 14:26:37 +0200 Subject: [PATCH] Add deriveAddress (#653) --- CHANGELOG.md | 5 ++-- .../util-crypto/src/address/decode.spec.ts | 2 +- .../util-crypto/src/address/derive.spec.ts | 25 ++++++++++++++++ packages/util-crypto/src/address/derive.ts | 29 +++++++++++++++++++ .../util-crypto/src/address/encodeDerived.ts | 2 +- packages/util-crypto/src/address/index.ts | 2 ++ .../src/schnorrkel/derivePublic.ts | 11 +++++++ packages/util-crypto/src/schnorrkel/index.ts | 1 + 8 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 packages/util-crypto/src/address/derive.spec.ts create mode 100644 packages/util-crypto/src/address/derive.ts create mode 100644 packages/util-crypto/src/schnorrkel/derivePublic.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f81e2d37..43fa6a356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ## 3.1.0-beta.x -- Use `scrypt` from WASM as available -- Add `base32{Encode, Decode}` utilities +- Add `deriveAddress(
, '//')` via sr25519 soft +- Use `scrypt` from WASM as available (JS fallback in place) +- Add `base32{Encode, Decode}` utilities (IPFS-compatible alphabet) - Support IPFS compatibility flag in base58 encoding/decoding - Update to `@polkadot/wasm-crypto` 1.3.1 diff --git a/packages/util-crypto/src/address/decode.spec.ts b/packages/util-crypto/src/address/decode.spec.ts index 8ff11a485..e411a03c2 100644 --- a/packages/util-crypto/src/address/decode.spec.ts +++ b/packages/util-crypto/src/address/decode.spec.ts @@ -7,7 +7,7 @@ import { stringToU8a, u8aToHex } from '@polkadot/util'; import testingPairs, { TestKeyringMap } from '../../../keyring/src/testingPairs'; import decode from './decode'; -describe('decode', (): void => { +describe('decodeAddress', (): void => { let keyring: TestKeyringMap; beforeAll((): void => { diff --git a/packages/util-crypto/src/address/derive.spec.ts b/packages/util-crypto/src/address/derive.spec.ts new file mode 100644 index 000000000..6b9c24f81 --- /dev/null +++ b/packages/util-crypto/src/address/derive.spec.ts @@ -0,0 +1,25 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { waitReady } from '@polkadot/wasm-crypto'; + +import derive from './derive'; + +describe('deriveAddress', (): void => { + beforeEach(async (): Promise => { + await waitReady(); + }); + + it('derives a known path', (): void => { + expect( + derive('5CZtJLXtVzrBJq1fMWfywDa6XuRwXekGdShPR4b8i9GWSbzB', '/joe/polkadot/0') + ).toEqual('5GZ4srnepXvdsuNVoxCGyVZd8ScDm4gkGLTKuaGARy9akjTa'); + }); + + it('fails on hard paths', (): void => { + expect( + () => derive('5CZtJLXtVzrBJq1fMWfywDa6XuRwXekGdShPR4b8i9GWSbzB', '//bob') + ).toThrow(/Expected suri to contain a combination of non-hard paths/); + }); +}); diff --git a/packages/util-crypto/src/address/derive.ts b/packages/util-crypto/src/address/derive.ts new file mode 100644 index 000000000..71edc47e6 --- /dev/null +++ b/packages/util-crypto/src/address/derive.ts @@ -0,0 +1,29 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { Prefix } from './types'; + +import { assert } from '@polkadot/util'; + +import DeriveJunction from '../key/DeriveJunction'; +import { keyExtractPath } from '../key'; +import { schnorrkelDerivePublic } from '../schnorrkel'; +import decode from './decode'; +import encode from './encode'; + +/** + * @name deriveAddress + * @summary Creates a sr25519 derived address from the supplied and path. + * @description + * Creates a sr25519 derived address based on the input address/publicKey and the uri supplied. + */ +export default function deriveAddress (who: 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'); + + return encode(path.reduce((publicKey: Uint8Array, path: DeriveJunction): Uint8Array => { + return schnorrkelDerivePublic(publicKey, path.chainCode); + }, decode(who)), ss58Format); +} diff --git a/packages/util-crypto/src/address/encodeDerived.ts b/packages/util-crypto/src/address/encodeDerived.ts index 870f71161..953036416 100644 --- a/packages/util-crypto/src/address/encodeDerived.ts +++ b/packages/util-crypto/src/address/encodeDerived.ts @@ -12,7 +12,7 @@ import keyDerived from './keyDerived'; /** * @name encodeDerivedAddress - * @summary Creates a derived address. + * @summary Creates a derived address as used in Substrate utility. * @description * Creates a Substrate derived address based on the input address/publicKey and the index supplied. */ diff --git a/packages/util-crypto/src/address/index.ts b/packages/util-crypto/src/address/index.ts index 853e9c01b..9d63dfc90 100644 --- a/packages/util-crypto/src/address/index.ts +++ b/packages/util-crypto/src/address/index.ts @@ -7,6 +7,7 @@ import checkAddressChecksum from './checkChecksum'; import createKeyMulti from './keyMulti'; import createKeyDerived from './keyDerived'; import decodeAddress from './decode'; +import deriveAddress from './derive'; import encodeAddress from './encode'; import encodeDerivedAddress from './encodeDerived'; import encodeMultiAddress from './encodeMulti'; @@ -21,6 +22,7 @@ export { createKeyDerived, createKeyMulti, decodeAddress, + deriveAddress, encodeAddress, encodeDerivedAddress, encodeMultiAddress, diff --git a/packages/util-crypto/src/schnorrkel/derivePublic.ts b/packages/util-crypto/src/schnorrkel/derivePublic.ts new file mode 100644 index 000000000..9edf50899 --- /dev/null +++ b/packages/util-crypto/src/schnorrkel/derivePublic.ts @@ -0,0 +1,11 @@ +// Copyright 2017-2020 @polkadot/util-crypto authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import '../polyfill'; + +import { sr25519DerivePublicSoft } from '@polkadot/wasm-crypto'; + +export default function deriveSoft (publicKey: Uint8Array, chainCode: Uint8Array): Uint8Array { + return sr25519DerivePublicSoft(publicKey, chainCode); +} diff --git a/packages/util-crypto/src/schnorrkel/index.ts b/packages/util-crypto/src/schnorrkel/index.ts index 7cb8672a0..047b09b7d 100644 --- a/packages/util-crypto/src/schnorrkel/index.ts +++ b/packages/util-crypto/src/schnorrkel/index.ts @@ -5,6 +5,7 @@ import '../polyfill'; export { default as schnorrkelDeriveHard } from './deriveHard'; +export { default as schnorrkelDerivePublic } from './derivePublic'; export { default as schnorrkelDeriveSoft } from './deriveSoft'; export { default as schnorrkelKeypairFromSeed } from './keypair/fromSeed'; export { default as schnorrkelSign } from './sign';