Add rawSecp256k1PubkeyToRawAddress to @cosmjs/amino

This commit is contained in:
Simon Warta 2021-03-22 16:29:53 +01:00
parent 4f5d919c2c
commit 3fb79b3dd7
7 changed files with 25 additions and 21 deletions

View File

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

View File

@ -5,6 +5,7 @@ export {
encodeBech32Pubkey,
encodeSecp256k1Pubkey,
pubkeyToRawAddress,
rawSecp256k1PubkeyToRawAddress,
} from "./encoding";
export {
MultisigThresholdPubkey,

View File

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

View File

@ -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<readonly AccountData[]> {

View File

@ -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<readonly AccountData[]> {

View File

@ -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<readonly AccountData[]> {

View File

@ -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<readonly AccountData[]> {