From 25e0ee4e185ca7565bc992d2beb63844e7ce2ea2 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 6 Apr 2021 18:21:19 +0200 Subject: [PATCH] amino: Add rawEd25519PubkeyToRawAddress helper --- packages/amino/src/addresses.ts | 12 ++++++++---- packages/amino/src/index.ts | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/amino/src/addresses.ts b/packages/amino/src/addresses.ts index 133f8f8b..4ecedf4b 100644 --- a/packages/amino/src/addresses.ts +++ b/packages/amino/src/addresses.ts @@ -6,6 +6,13 @@ import { Bech32, fromBase64 } from "@cosmjs/encoding"; import { encodeAminoPubkey } from "./encoding"; import { isEd25519Pubkey, isMultisigThresholdPubkey, isSecp256k1Pubkey, Pubkey } from "./pubkeys"; +export function rawEd25519PubkeyToRawAddress(pubkeyData: Uint8Array): Uint8Array { + if (pubkeyData.length !== 32) { + throw new Error(`Invalid Ed25519 pubkey length: ${pubkeyData.length}`); + } + return sha256(pubkeyData).slice(0, 20); +} + export function rawSecp256k1PubkeyToRawAddress(pubkeyData: Uint8Array): Uint8Array { if (pubkeyData.length !== 33) { throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyData.length}`); @@ -20,10 +27,7 @@ export function pubkeyToRawAddress(pubkey: Pubkey): Uint8Array { return rawSecp256k1PubkeyToRawAddress(pubkeyData); } else if (isEd25519Pubkey(pubkey)) { const pubkeyData = fromBase64(pubkey.value); - if (pubkeyData.length !== 32) { - throw new Error(`Invalid Ed25519 pubkey length: ${pubkeyData.length}`); - } - return sha256(pubkeyData).slice(0, 20); + return rawEd25519PubkeyToRawAddress(pubkeyData); } else if (isMultisigThresholdPubkey(pubkey)) { // https://github.com/tendermint/tendermint/blob/38b401657e4ad7a7eeb3c30a3cbf512037df3740/crypto/multisig/threshold_pubkey.go#L71-L74 const pubkeyData = encodeAminoPubkey(pubkey); diff --git a/packages/amino/src/index.ts b/packages/amino/src/index.ts index cbe92581..42d9720c 100644 --- a/packages/amino/src/index.ts +++ b/packages/amino/src/index.ts @@ -1,4 +1,9 @@ -export { pubkeyToAddress, pubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress } from "./addresses"; +export { + pubkeyToAddress, + pubkeyToRawAddress, + rawEd25519PubkeyToRawAddress, + rawSecp256k1PubkeyToRawAddress, +} from "./addresses"; export { Coin, coin, coins, parseCoins } from "./coins"; export { decodeAminoPubkey,