From ab1b51485c6e3612de17700ce11efd5f37b79c38 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Feb 2021 14:24:15 +0100 Subject: [PATCH] Copy makeCosmoshubPath to @cosmjs/proto-signing --- .../cosmwasm-stargate/src/testutils.spec.ts | 11 ++++---- .../src/directsecp256k1hdwallet.ts | 3 ++- packages/proto-signing/src/index.ts | 1 + packages/proto-signing/src/paths.spec.ts | 26 +++++++++++++++++++ packages/proto-signing/src/paths.ts | 17 ++++++++++++ packages/stargate/src/testutils.spec.ts | 11 ++++---- 6 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 packages/proto-signing/src/paths.spec.ts create mode 100644 packages/proto-signing/src/paths.ts diff --git a/packages/cosmwasm-stargate/src/testutils.spec.ts b/packages/cosmwasm-stargate/src/testutils.spec.ts index 4481be86..9288bf1b 100644 --- a/packages/cosmwasm-stargate/src/testutils.spec.ts +++ b/packages/cosmwasm-stargate/src/testutils.spec.ts @@ -1,14 +1,13 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { Bip39, EnglishMnemonic, Random, Secp256k1, Slip10, Slip10Curve } from "@cosmjs/crypto"; import { Bech32, fromBase64 } from "@cosmjs/encoding"; +import { AminoSignResponse, coins, Secp256k1HdWallet, StdSignDoc } from "@cosmjs/launchpad"; import { - AminoSignResponse, - coins, + DirectSecp256k1HdWallet, + DirectSignResponse, + makeAuthInfoBytes, makeCosmoshubPath, - Secp256k1HdWallet, - StdSignDoc, -} from "@cosmjs/launchpad"; -import { DirectSecp256k1HdWallet, DirectSignResponse, makeAuthInfoBytes } from "@cosmjs/proto-signing"; +} from "@cosmjs/proto-signing"; import { AuthExtension, BankExtension, diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.ts b/packages/proto-signing/src/directsecp256k1hdwallet.ts index 53f7cc29..bb6058d7 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.ts @@ -8,9 +8,10 @@ import { Slip10, Slip10Curve, } from "@cosmjs/crypto"; -import { encodeSecp256k1Signature, makeCosmoshubPath, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; +import { encodeSecp256k1Signature, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; +import { makeCosmoshubPath } from "./paths"; import { AccountData, DirectSignResponse, OfflineDirectSigner } from "./signer"; import { makeSignBytes } from "./signing"; diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index 74b25d2c..b975b396 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -1,6 +1,7 @@ export { EncodeObject, GeneratedType, Registry } from "./registry"; export { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; +export { makeCosmoshubPath } from "./paths"; export { decodePubkey, encodePubkey } from "./pubkey"; export { AccountData, diff --git a/packages/proto-signing/src/paths.spec.ts b/packages/proto-signing/src/paths.spec.ts new file mode 100644 index 00000000..17fd85b6 --- /dev/null +++ b/packages/proto-signing/src/paths.spec.ts @@ -0,0 +1,26 @@ +import { Slip10RawIndex } from "@cosmjs/crypto"; + +import { makeCosmoshubPath } from "./paths"; + +describe("paths", () => { + describe("makeCosmoshubPath", () => { + it("works", () => { + // m/44'/118'/0'/0/0 + expect(makeCosmoshubPath(0)).toEqual([ + Slip10RawIndex.hardened(44), + Slip10RawIndex.hardened(118), + Slip10RawIndex.hardened(0), + Slip10RawIndex.normal(0), + Slip10RawIndex.normal(0), + ]); + // m/44'/118'/0'/0/123 + expect(makeCosmoshubPath(123)).toEqual([ + Slip10RawIndex.hardened(44), + Slip10RawIndex.hardened(118), + Slip10RawIndex.hardened(0), + Slip10RawIndex.normal(0), + Slip10RawIndex.normal(123), + ]); + }); + }); +}); diff --git a/packages/proto-signing/src/paths.ts b/packages/proto-signing/src/paths.ts new file mode 100644 index 00000000..1b4f0d8a --- /dev/null +++ b/packages/proto-signing/src/paths.ts @@ -0,0 +1,17 @@ +import { HdPath, Slip10RawIndex } from "@cosmjs/crypto"; + +/** + * The Cosmos Hub derivation path in the form `m/44'/118'/0'/0/a` + * with 0-based account index `a`. + * + * This is the same as makeCosmoshubPath from @cosmjs/launchpad but those might diverge in the future. + */ +export function makeCosmoshubPath(a: number): HdPath { + return [ + Slip10RawIndex.hardened(44), + Slip10RawIndex.hardened(118), + Slip10RawIndex.hardened(0), + Slip10RawIndex.normal(0), + Slip10RawIndex.normal(a), + ]; +} diff --git a/packages/stargate/src/testutils.spec.ts b/packages/stargate/src/testutils.spec.ts index 1f15a89b..93328416 100644 --- a/packages/stargate/src/testutils.spec.ts +++ b/packages/stargate/src/testutils.spec.ts @@ -1,14 +1,13 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { Bip39, EnglishMnemonic, Random, Secp256k1, Slip10, Slip10Curve } from "@cosmjs/crypto"; import { Bech32 } from "@cosmjs/encoding"; +import { AminoSignResponse, coins, Secp256k1HdWallet, StdSignDoc } from "@cosmjs/launchpad"; import { - AminoSignResponse, - coins, + DirectSecp256k1HdWallet, + DirectSignResponse, + makeAuthInfoBytes, makeCosmoshubPath, - Secp256k1HdWallet, - StdSignDoc, -} from "@cosmjs/launchpad"; -import { DirectSecp256k1HdWallet, DirectSignResponse, makeAuthInfoBytes } from "@cosmjs/proto-signing"; +} from "@cosmjs/proto-signing"; import { SignMode } from "./codec/cosmos/tx/signing/v1beta1/signing"; import { AuthInfo, SignDoc, TxBody } from "./codec/cosmos/tx/v1beta1/tx";