From 349508e8d740e7ab3b112d2c4f469534be020961 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Feb 2021 13:58:09 +0100 Subject: [PATCH 1/3] Duplicate AccountData/Algo in proto-signing --- .../src/signingcosmwasmclient.ts | 3 +-- .../src/directsecp256k1hdwallet.ts | 9 ++------- .../proto-signing/src/directsecp256k1wallet.ts | 4 ++-- packages/proto-signing/src/index.ts | 9 ++++++++- packages/proto-signing/src/signer.ts | 17 ++++++++++++++++- packages/stargate/src/signingstargateclient.ts | 3 +-- 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index af6ca673..4fca5778 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -13,7 +13,6 @@ import { import { sha256 } from "@cosmjs/crypto"; import { fromBase64, toAscii, toHex } from "@cosmjs/encoding"; import { - AccountData, buildFeeTable, Coin, CosmosFeeTable, @@ -339,7 +338,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { memo = "", ): Promise { const accountFromSigner = (await this.signer.getAccounts()).find( - (account: AccountData) => account.address === signerAddress, + (account) => account.address === signerAddress, ); if (!accountFromSigner) { throw new Error("Failed to retrieve account from signer"); diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.ts b/packages/proto-signing/src/directsecp256k1hdwallet.ts index aff62c23..53f7cc29 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.ts @@ -8,15 +8,10 @@ import { Slip10, Slip10Curve, } from "@cosmjs/crypto"; -import { - AccountData, - encodeSecp256k1Signature, - makeCosmoshubPath, - rawSecp256k1PubkeyToAddress, -} from "@cosmjs/launchpad"; +import { encodeSecp256k1Signature, makeCosmoshubPath, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; -import { DirectSignResponse, OfflineDirectSigner } from "./signer"; +import { AccountData, DirectSignResponse, OfflineDirectSigner } from "./signer"; import { makeSignBytes } from "./signing"; /** diff --git a/packages/proto-signing/src/directsecp256k1wallet.ts b/packages/proto-signing/src/directsecp256k1wallet.ts index 783fbdae..28133f10 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.ts @@ -1,8 +1,8 @@ import { Secp256k1, sha256 } from "@cosmjs/crypto"; -import { AccountData, encodeSecp256k1Signature, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; +import { encodeSecp256k1Signature, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; -import { DirectSignResponse, OfflineDirectSigner } from "./signer"; +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 634ff401..74b25d2c 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -2,5 +2,12 @@ export { EncodeObject, GeneratedType, Registry } from "./registry"; export { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; export { decodePubkey, encodePubkey } from "./pubkey"; -export { DirectSignResponse, isOfflineDirectSigner, OfflineDirectSigner, OfflineSigner } from "./signer"; +export { + AccountData, + Algo, + DirectSignResponse, + isOfflineDirectSigner, + OfflineDirectSigner, + OfflineSigner, +} from "./signer"; export { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; diff --git a/packages/proto-signing/src/signer.ts b/packages/proto-signing/src/signer.ts index c3ed353e..072f3255 100644 --- a/packages/proto-signing/src/signer.ts +++ b/packages/proto-signing/src/signer.ts @@ -1,7 +1,22 @@ -import { AccountData, OfflineSigner as OfflineAminoSigner, StdSignature } from "@cosmjs/launchpad"; +import { OfflineSigner as OfflineAminoSigner, StdSignature } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; +/** + * This is the same as Algo from @cosmjs/launchpad but those might diverge in the future. + */ +export type Algo = "secp256k1" | "ed25519" | "sr25519"; + +/** + * This is the same as AccountData from @cosmjs/launchpad but those might diverge in the future. + */ +export interface AccountData { + /** A printable address (typically bech32 encoded) */ + readonly address: string; + readonly algo: Algo; + readonly pubkey: Uint8Array; +} + export interface DirectSignResponse { /** * The sign doc that was signed. diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 0340b54e..93f20bc0 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -1,6 +1,5 @@ import { fromBase64 } from "@cosmjs/encoding"; import { - AccountData, buildFeeTable, Coin, CosmosFeeTable, @@ -133,7 +132,7 @@ export class SigningStargateClient extends StargateClient { memo = "", ): Promise { const accountFromSigner = (await this.signer.getAccounts()).find( - (account: AccountData) => account.address === signerAddress, + (account) => account.address === signerAddress, ); if (!accountFromSigner) { throw new Error("Failed to retrieve account from signer"); From 83a4b0ef809c58cbb7cd3cf9d2602c118f5c974c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Feb 2021 14:08:31 +0100 Subject: [PATCH 2/3] Extract and test makeCosmoshubPath --- packages/launchpad/src/index.ts | 3 ++- .../launchpad/src/lcdapi/lcdclient.spec.ts | 2 +- packages/launchpad/src/paths.spec.ts | 26 +++++++++++++++++++ packages/launchpad/src/paths.ts | 15 +++++++++++ packages/launchpad/src/secp256k1hdwallet.ts | 2 +- .../launchpad/src/signingcosmosclient.spec.ts | 2 +- packages/launchpad/src/wallet.ts | 16 ------------ 7 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 packages/launchpad/src/paths.spec.ts create mode 100644 packages/launchpad/src/paths.ts diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 5d2647c7..dc7a11d0 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -124,6 +124,7 @@ export { MsgWithdrawDelegatorReward, MsgWithdrawValidatorCommission, } from "./msgs"; +export { makeCosmoshubPath } from "./paths"; export { decodeAminoPubkey, decodeBech32Pubkey, @@ -137,6 +138,6 @@ export { AccountData, Algo, AminoSignResponse, OfflineSigner } from "./signer"; export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient"; export { isStdTx, isWrappedStdTx, makeStdTx, CosmosSdkTx, StdTx, WrappedStdTx, WrappedTx } from "./tx"; export { pubkeyType, PubKey, StdFee, StdSignature } from "./types"; -export { makeCosmoshubPath, executeKdf, KdfConfiguration } from "./wallet"; +export { executeKdf, KdfConfiguration } from "./wallet"; export { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet"; export { Secp256k1Wallet } from "./secp256k1wallet"; diff --git a/packages/launchpad/src/lcdapi/lcdclient.spec.ts b/packages/launchpad/src/lcdapi/lcdclient.spec.ts index d42c3d5d..1813b349 100644 --- a/packages/launchpad/src/lcdapi/lcdclient.spec.ts +++ b/packages/launchpad/src/lcdapi/lcdclient.spec.ts @@ -6,6 +6,7 @@ import { isBroadcastTxFailure } from "../cosmosclient"; import { makeSignDoc } from "../encoding"; import { parseLogs } from "../logs"; import { MsgSend } from "../msgs"; +import { makeCosmoshubPath } from "../paths"; import { Secp256k1HdWallet } from "../secp256k1hdwallet"; import { SigningCosmosClient } from "../signingcosmosclient"; import cosmoshub from "../testdata/cosmoshub.json"; @@ -21,7 +22,6 @@ import { } from "../testutils.spec"; import { isWrappedStdTx, makeStdTx, StdTx } from "../tx"; import { StdFee } from "../types"; -import { makeCosmoshubPath } from "../wallet"; import { setupAuthExtension } from "./auth"; import { TxsResponse } from "./base"; import { LcdApiArray, LcdClient } from "./lcdclient"; diff --git a/packages/launchpad/src/paths.spec.ts b/packages/launchpad/src/paths.spec.ts new file mode 100644 index 00000000..17fd85b6 --- /dev/null +++ b/packages/launchpad/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/launchpad/src/paths.ts b/packages/launchpad/src/paths.ts new file mode 100644 index 00000000..bf4dd185 --- /dev/null +++ b/packages/launchpad/src/paths.ts @@ -0,0 +1,15 @@ +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`. + */ +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/launchpad/src/secp256k1hdwallet.ts b/packages/launchpad/src/secp256k1hdwallet.ts index acdbd10f..cea4e63b 100644 --- a/packages/launchpad/src/secp256k1hdwallet.ts +++ b/packages/launchpad/src/secp256k1hdwallet.ts @@ -15,6 +15,7 @@ import { assert, isNonNullObject } from "@cosmjs/utils"; import { rawSecp256k1PubkeyToAddress } from "./address"; import { serializeSignDoc, StdSignDoc } from "./encoding"; +import { makeCosmoshubPath } from "./paths"; import { encodeSecp256k1Signature } from "./signature"; import { AccountData, AminoSignResponse, OfflineSigner } from "./signer"; import { @@ -23,7 +24,6 @@ import { EncryptionConfiguration, executeKdf, KdfConfiguration, - makeCosmoshubPath, supportedAlgorithms, } from "./wallet"; diff --git a/packages/launchpad/src/signingcosmosclient.spec.ts b/packages/launchpad/src/signingcosmosclient.spec.ts index 02f73981..d898a447 100644 --- a/packages/launchpad/src/signingcosmosclient.spec.ts +++ b/packages/launchpad/src/signingcosmosclient.spec.ts @@ -5,6 +5,7 @@ import { Coin, coin, coins } from "./coins"; import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient"; import { GasPrice } from "./gas"; import { MsgDelegate, MsgSend } from "./msgs"; +import { makeCosmoshubPath } from "./paths"; import { Secp256k1HdWallet } from "./secp256k1hdwallet"; import { PrivateSigningCosmosClient, SigningCosmosClient } from "./signingcosmosclient"; import { @@ -14,7 +15,6 @@ import { makeRandomAddress, pendingWithoutLaunchpad, } from "./testutils.spec"; -import { makeCosmoshubPath } from "./wallet"; describe("SigningCosmosClient", () => { describe("makeReadOnly", () => { diff --git a/packages/launchpad/src/wallet.ts b/packages/launchpad/src/wallet.ts index 1c64e773..dc85068a 100644 --- a/packages/launchpad/src/wallet.ts +++ b/packages/launchpad/src/wallet.ts @@ -1,28 +1,12 @@ import { Argon2id, - HdPath, isArgon2idOptions, Random, - Slip10RawIndex, xchacha20NonceLength, Xchacha20poly1305Ietf, } from "@cosmjs/crypto"; import { toAscii } from "@cosmjs/encoding"; -/** - * The Cosmoshub derivation path in the form `m/44'/118'/0'/0/a` - * with 0-based account index `a`. - */ -export function makeCosmoshubPath(a: number): HdPath { - return [ - Slip10RawIndex.hardened(44), - Slip10RawIndex.hardened(118), - Slip10RawIndex.hardened(0), - Slip10RawIndex.normal(0), - Slip10RawIndex.normal(a), - ]; -} - /** * A fixed salt is chosen to archive a deterministic password to key derivation. * This reduces the scope of a potential rainbow attack to all CosmJS users. From ab1b51485c6e3612de17700ce11efd5f37b79c38 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 10 Feb 2021 14:24:15 +0100 Subject: [PATCH 3/3] 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";