From 3fb79b3dd7c027e87a8ab39ad91b05b1ce07e9ff Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 22 Mar 2021 16:29:53 +0100 Subject: [PATCH] Add rawSecp256k1PubkeyToRawAddress to @cosmjs/amino --- packages/amino/src/encoding.ts | 12 ++++++++---- packages/amino/src/index.ts | 1 + packages/launchpad/src/address.ts | 10 ++-------- packages/launchpad/src/secp256k1hdwallet.ts | 6 +++--- packages/launchpad/src/secp256k1wallet.ts | 5 +++-- .../proto-signing/src/directsecp256k1hdwallet.ts | 6 ++++-- packages/proto-signing/src/directsecp256k1wallet.ts | 6 ++++-- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/amino/src/encoding.ts b/packages/amino/src/encoding.ts index 571d516b..78ccb34d 100644 --- a/packages/amino/src/encoding.ts +++ b/packages/amino/src/encoding.ts @@ -117,15 +117,19 @@ export function encodeAminoPubkey(pubkey: Pubkey): Uint8Array { } } +export function rawSecp256k1PubkeyToRawAddress(pubkeyData: Uint8Array): Uint8Array { + if (pubkeyData.length !== 33) { + throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyData.length}`); + } + return ripemd160(sha256(pubkeyData)); +} + // See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography // For secp256k1 this assumes we already have a compressed pubkey. export function pubkeyToRawAddress(pubkey: Pubkey): Uint8Array { if (isSecp256k1Pubkey(pubkey)) { const pubkeyData = fromBase64(pubkey.value); - if (pubkeyData.length !== 33) { - throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyData.length}`); - } - return ripemd160(sha256(pubkeyData)).slice(0, 20); + return rawSecp256k1PubkeyToRawAddress(pubkeyData); } else if (isEd25519Pubkey(pubkey)) { const pubkeyData = fromBase64(pubkey.value); if (pubkeyData.length !== 32) { diff --git a/packages/amino/src/index.ts b/packages/amino/src/index.ts index 803dbe19..3cfc656d 100644 --- a/packages/amino/src/index.ts +++ b/packages/amino/src/index.ts @@ -5,6 +5,7 @@ export { encodeBech32Pubkey, encodeSecp256k1Pubkey, pubkeyToRawAddress, + rawSecp256k1PubkeyToRawAddress, } from "./encoding"; export { MultisigThresholdPubkey, diff --git a/packages/launchpad/src/address.ts b/packages/launchpad/src/address.ts index d31e6d52..8c8e3d17 100644 --- a/packages/launchpad/src/address.ts +++ b/packages/launchpad/src/address.ts @@ -1,15 +1,9 @@ import { SinglePubkey } from "@cosmjs/amino"; -import { pubkeyToRawAddress } from "@cosmjs/amino/build/encoding"; -import { ripemd160, sha256 } from "@cosmjs/crypto"; +import { pubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino/build/encoding"; import { Bech32 } from "@cosmjs/encoding"; export function rawSecp256k1PubkeyToAddress(pubkeyRaw: Uint8Array, prefix: string): string { - if (pubkeyRaw.length !== 33) { - throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyRaw.length}`); - } - const hash1 = sha256(pubkeyRaw); - const hash2 = ripemd160(hash1); - return Bech32.encode(prefix, hash2); + return Bech32.encode(prefix, rawSecp256k1PubkeyToRawAddress(pubkeyRaw)); } // See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography diff --git a/packages/launchpad/src/secp256k1hdwallet.ts b/packages/launchpad/src/secp256k1hdwallet.ts index cea4e63b..fa7d8b9c 100644 --- a/packages/launchpad/src/secp256k1hdwallet.ts +++ b/packages/launchpad/src/secp256k1hdwallet.ts @@ -1,3 +1,4 @@ +import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino"; import { Bip39, EnglishMnemonic, @@ -10,10 +11,9 @@ import { Slip10Curve, stringToPath, } from "@cosmjs/crypto"; -import { fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding"; +import { Bech32, fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding"; import { assert, isNonNullObject } from "@cosmjs/utils"; -import { rawSecp256k1PubkeyToAddress } from "./address"; import { serializeSignDoc, StdSignDoc } from "./encoding"; import { makeCosmoshubPath } from "./paths"; import { encodeSecp256k1Signature } from "./signature"; @@ -246,7 +246,7 @@ export class Secp256k1HdWallet implements OfflineSigner { } private get address(): string { - return rawSecp256k1PubkeyToAddress(this.pubkey, this.accounts[0].prefix); + return Bech32.encode(this.accounts[0].prefix, rawSecp256k1PubkeyToRawAddress(this.pubkey)); } public async getAccounts(): Promise { diff --git a/packages/launchpad/src/secp256k1wallet.ts b/packages/launchpad/src/secp256k1wallet.ts index b8e84960..94552d84 100644 --- a/packages/launchpad/src/secp256k1wallet.ts +++ b/packages/launchpad/src/secp256k1wallet.ts @@ -1,6 +1,7 @@ +import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino"; import { Secp256k1, Sha256 } from "@cosmjs/crypto"; +import { Bech32 } from "@cosmjs/encoding"; -import { rawSecp256k1PubkeyToAddress } from "./address"; import { serializeSignDoc, StdSignDoc } from "./encoding"; import { encodeSecp256k1Signature } from "./signature"; import { AccountData, AminoSignResponse, OfflineSigner } from "./signer"; @@ -33,7 +34,7 @@ export class Secp256k1Wallet implements OfflineSigner { } private get address(): string { - return rawSecp256k1PubkeyToAddress(this.pubkey, this.prefix); + return Bech32.encode(this.prefix, rawSecp256k1PubkeyToRawAddress(this.pubkey)); } public async getAccounts(): Promise { diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.ts b/packages/proto-signing/src/directsecp256k1hdwallet.ts index bb6058d7..cfdbc8c4 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.ts @@ -1,3 +1,4 @@ +import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino"; import { Bip39, EnglishMnemonic, @@ -8,7 +9,8 @@ import { Slip10, Slip10Curve, } from "@cosmjs/crypto"; -import { encodeSecp256k1Signature, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; +import { Bech32 } from "@cosmjs/encoding"; +import { encodeSecp256k1Signature } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; import { makeCosmoshubPath } from "./paths"; @@ -99,7 +101,7 @@ export class DirectSecp256k1HdWallet implements OfflineDirectSigner { } private get address(): string { - return rawSecp256k1PubkeyToAddress(this.pubkey, this.accounts[0].prefix); + return Bech32.encode(this.accounts[0].prefix, rawSecp256k1PubkeyToRawAddress(this.pubkey)); } public async getAccounts(): Promise { diff --git a/packages/proto-signing/src/directsecp256k1wallet.ts b/packages/proto-signing/src/directsecp256k1wallet.ts index 28133f10..9207d9c6 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.ts @@ -1,5 +1,7 @@ +import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino"; import { Secp256k1, sha256 } from "@cosmjs/crypto"; -import { encodeSecp256k1Signature, rawSecp256k1PubkeyToAddress } from "@cosmjs/launchpad"; +import { Bech32 } from "@cosmjs/encoding"; +import { encodeSecp256k1Signature } from "@cosmjs/launchpad"; import { SignDoc } from "./codec/cosmos/tx/v1beta1/tx"; import { AccountData, DirectSignResponse, OfflineDirectSigner } from "./signer"; @@ -33,7 +35,7 @@ export class DirectSecp256k1Wallet implements OfflineDirectSigner { } private get address(): string { - return rawSecp256k1PubkeyToAddress(this.pubkey, this.prefix); + return Bech32.encode(this.prefix, rawSecp256k1PubkeyToRawAddress(this.pubkey)); } public async getAccounts(): Promise {