From 8388399c24e849a036e5a18bbf63e16d38cbe2c2 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 7 May 2020 09:46:25 +0200 Subject: [PATCH] Pull out rawSecp256k1PubkeyToAddress --- packages/sdk/src/address.ts | 16 ++++++++++------ packages/sdk/types/address.d.ts | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/address.ts b/packages/sdk/src/address.ts index 58ec7da3..126d8fb8 100644 --- a/packages/sdk/src/address.ts +++ b/packages/sdk/src/address.ts @@ -5,18 +5,22 @@ import { PubKey, pubkeyType } from "./types"; const { fromBase64 } = 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 = new Sha256(pubkeyRaw).digest(); + const hash2 = new Ripemd160(hash1).digest(); + return Bech32.encode(prefix, hash2); +} + // See https://github.com/tendermint/tendermint/blob/f2ada0a604b4c0763bda2f64fac53d506d3beca7/docs/spec/blockchain/encoding.md#public-key-cryptography // This assumes we already have a cosmos-compressed pubkey export function pubkeyToAddress(pubkey: PubKey, prefix: string): string { const pubkeyBytes = fromBase64(pubkey.value); switch (pubkey.type) { case pubkeyType.secp256k1: { - if (pubkeyBytes.length !== 33) { - throw new Error(`Invalid Secp256k1 pubkey length (compressed): ${pubkeyBytes.length}`); - } - const hash1 = new Sha256(pubkeyBytes).digest(); - const hash2 = new Ripemd160(hash1).digest(); - return Bech32.encode(prefix, hash2); + return rawSecp256k1PubkeyToAddress(pubkeyBytes, prefix); } case pubkeyType.ed25519: { if (pubkeyBytes.length !== 32) { diff --git a/packages/sdk/types/address.d.ts b/packages/sdk/types/address.d.ts index a8f251fb..9eb5af82 100644 --- a/packages/sdk/types/address.d.ts +++ b/packages/sdk/types/address.d.ts @@ -1,2 +1,3 @@ import { PubKey } from "./types"; +export declare function rawSecp256k1PubkeyToAddress(pubkeyRaw: Uint8Array, prefix: string): string; export declare function pubkeyToAddress(pubkey: PubKey, prefix: string): string;