Pull out rawSecp256k1PubkeyToAddress

This commit is contained in:
Simon Warta 2020-05-07 09:46:25 +02:00
parent cc6a98fd33
commit 8388399c24
2 changed files with 11 additions and 6 deletions

View File

@ -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) {

View File

@ -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;