mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Create pubkeyToAddress in @cosmjs/amino
This commit is contained in:
parent
cb37451f8f
commit
843b82badd
@ -6,6 +6,7 @@ import {
|
||||
encodeAminoPubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
pubkeyToAddress,
|
||||
pubkeyToRawAddress,
|
||||
} from "./encoding";
|
||||
import { MultisigThresholdPubkey, Pubkey } from "./pubkeys";
|
||||
@ -262,4 +263,45 @@ describe("encoding", () => {
|
||||
expect(pubkeyToRawAddress(testgroup1)).toEqual(fromHex("0892a77fab2fa7e192c3b7b2741e6682f3abb72f"));
|
||||
});
|
||||
});
|
||||
|
||||
describe("pubkeyToAddress", () => {
|
||||
it("works for Secp256k1", () => {
|
||||
const prefix = "cosmos";
|
||||
const pubkey = {
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
|
||||
};
|
||||
expect(pubkeyToAddress(pubkey, prefix)).toEqual("cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r");
|
||||
});
|
||||
|
||||
it("works for Ed25519", () => {
|
||||
const prefix = "cosmos";
|
||||
const pubkey = {
|
||||
type: "tendermint/PubKeyEd25519",
|
||||
value: toBase64(fromHex("12ee6f581fe55673a1e9e1382a0829e32075a0aa4763c968bc526e1852e78c95")),
|
||||
};
|
||||
expect(pubkeyToAddress(pubkey, prefix)).toEqual("cosmos1pfq05em6sfkls66ut4m2257p7qwlk448h8mysz");
|
||||
});
|
||||
|
||||
it("works for multisig", () => {
|
||||
const test1 = decodeBech32Pubkey(
|
||||
"wasmpub1addwnpepqwxttx8w2sfs6d8cuzqcuau84grp8xsw95qzdjkmvc44tnckskdxw3zw2km",
|
||||
);
|
||||
const test2 = decodeBech32Pubkey(
|
||||
"wasmpub1addwnpepq2gx7x7e29kge5a4ycunytyqr0u8ynql5h583s8r9wdads9m3v8ks6y0nhc",
|
||||
);
|
||||
const test3 = decodeBech32Pubkey(
|
||||
"wasmpub1addwnpepq0xfx5vavxmgdkn0p6x0l9p3udttghu3qcldd7ql08wa3xy93qq0xuzvtxc",
|
||||
);
|
||||
|
||||
const testgroup1: MultisigThresholdPubkey = {
|
||||
type: "tendermint/PubKeyMultisigThreshold",
|
||||
value: {
|
||||
threshold: "2",
|
||||
pubkeys: [test1, test2, test3],
|
||||
},
|
||||
};
|
||||
expect(pubkeyToAddress(testgroup1, "wasm")).toEqual("wasm1pzf2wlat97n7rykrk7e8g8nxste6hde0r8jqsy");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -145,6 +145,10 @@ export function pubkeyToRawAddress(pubkey: Pubkey): Uint8Array {
|
||||
}
|
||||
}
|
||||
|
||||
export function pubkeyToAddress(pubkey: Pubkey, prefix: string): string {
|
||||
return Bech32.encode(prefix, pubkeyToRawAddress(pubkey));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a public key to binary Amino and then to bech32.
|
||||
*
|
||||
|
@ -4,6 +4,7 @@ export {
|
||||
encodeAminoPubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
pubkeyToAddress,
|
||||
pubkeyToRawAddress,
|
||||
rawSecp256k1PubkeyToRawAddress,
|
||||
} from "./encoding";
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { MultisigThresholdPubkey, pubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import { Bech32 } from "@cosmjs/encoding";
|
||||
import { MultisigThresholdPubkey, pubkeyToAddress } from "@cosmjs/amino";
|
||||
|
||||
// https://github.com/cosmos/cosmjs/issues/673#issuecomment-779847238
|
||||
const multisigPubkey: MultisigThresholdPubkey = {
|
||||
@ -27,5 +26,5 @@ const multisigPubkey: MultisigThresholdPubkey = {
|
||||
}
|
||||
};
|
||||
|
||||
const address = Bech32.encode("cosmos", pubkeyToRawAddress(multisigPubkey));
|
||||
const address = pubkeyToAddress(multisigPubkey, "cosmos");
|
||||
console.log(address);
|
||||
|
@ -1,25 +0,0 @@
|
||||
import { fromHex, toBase64 } from "@cosmjs/encoding";
|
||||
|
||||
import { pubkeyToAddress } from "./address";
|
||||
|
||||
describe("address", () => {
|
||||
describe("pubkeyToAddress", () => {
|
||||
it("works for Secp256k1 compressed", () => {
|
||||
const prefix = "cosmos";
|
||||
const pubkey = {
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
|
||||
};
|
||||
expect(pubkeyToAddress(pubkey, prefix)).toEqual("cosmos1h806c7khnvmjlywdrkdgk2vrayy2mmvf9rxk2r");
|
||||
});
|
||||
|
||||
it("works for Ed25519", () => {
|
||||
const prefix = "cosmos";
|
||||
const pubkey = {
|
||||
type: "tendermint/PubKeyEd25519",
|
||||
value: toBase64(fromHex("12ee6f581fe55673a1e9e1382a0829e32075a0aa4763c968bc526e1852e78c95")),
|
||||
};
|
||||
expect(pubkeyToAddress(pubkey, prefix)).toEqual("cosmos1pfq05em6sfkls66ut4m2257p7qwlk448h8mysz");
|
||||
});
|
||||
});
|
||||
});
|
@ -1,13 +1,6 @@
|
||||
import { SinglePubkey } from "@cosmjs/amino";
|
||||
import { pubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino/build/encoding";
|
||||
import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import { Bech32 } from "@cosmjs/encoding";
|
||||
|
||||
export function rawSecp256k1PubkeyToAddress(pubkeyRaw: Uint8Array, prefix: string): string {
|
||||
return Bech32.encode(prefix, rawSecp256k1PubkeyToRawAddress(pubkeyRaw));
|
||||
}
|
||||
|
||||
// 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: SinglePubkey, prefix: string): string {
|
||||
return Bech32.encode(prefix, pubkeyToRawAddress(pubkey));
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ export {
|
||||
encodeAminoPubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
pubkeyToAddress,
|
||||
pubkeyType,
|
||||
SinglePubkey as PubKey,
|
||||
} from "@cosmjs/amino";
|
||||
@ -12,7 +13,7 @@ export {
|
||||
import * as logs from "./logs";
|
||||
export { logs };
|
||||
|
||||
export { pubkeyToAddress, rawSecp256k1PubkeyToAddress } from "./address";
|
||||
export { rawSecp256k1PubkeyToAddress } from "./address";
|
||||
export { Coin, coin, coins, parseCoins } from "./coins";
|
||||
|
||||
export {
|
||||
|
Loading…
x
Reference in New Issue
Block a user