mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Pull out rawSecp256k1PubkeyToAddress
This commit is contained in:
parent
cc6a98fd33
commit
8388399c24
@ -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) {
|
||||
|
1
packages/sdk/types/address.d.ts
vendored
1
packages/sdk/types/address.d.ts
vendored
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user