Properly support multiple pubkey types in decodePubkey

Closes #27
This commit is contained in:
Simon Warta 2020-02-01 15:43:13 +01:00
parent 7b25a2e11c
commit 9514982d32
2 changed files with 38 additions and 6 deletions

View File

@ -17,7 +17,7 @@ import { chainId, nonce, signedTxJson, txId } from "./testdata.spec";
import data from "./testdata/cosmoshub.json";
import { TokenInfos } from "./types";
const { fromBase64 } = Encoding;
const { fromBase64, fromHex } = Encoding;
describe("decode", () => {
const defaultPubkey = {
@ -62,13 +62,33 @@ describe("decode", () => {
];
describe("decodePubkey", () => {
it("works", () => {
it("works for secp256k1", () => {
const pubkey = {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
};
expect(decodePubkey(pubkey)).toEqual(defaultPubkey);
});
it("works for ed25519", () => {
const pubkey = {
type: "tendermint/PubKeyEd25519",
value: "s69CnMgLTpuRyEfecjws3mWssBrOICUx8C2O1DkKSto=",
};
expect(decodePubkey(pubkey)).toEqual({
algo: Algorithm.Ed25519,
data: fromHex("b3af429cc80b4e9b91c847de723c2cde65acb01ace202531f02d8ed4390a4ada"),
});
});
it("throws for unsupported types", () => {
// https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/sr25519/codec.go#L12
const pubkey = {
type: "tendermint/PubKeySr25519",
value: "N4FJNPE5r/Twz55kO1QEIxyaGF5/HTXH6WgLQJWsy1o=",
};
expect(() => decodePubkey(pubkey)).toThrowError(/unsupported pubkey type/i);
});
});
describe("decodeSignature", () => {

View File

@ -24,10 +24,22 @@ import { coinToAmount, isAminoStdTx, TokenInfos } from "./types";
const { fromBase64 } = Encoding;
export function decodePubkey(pubkey: amino.PubKey): PubkeyBundle {
return {
algo: Algorithm.Secp256k1,
data: fromBase64(pubkey.value) as PubkeyBytes,
};
switch (pubkey.type) {
// https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/secp256k1/secp256k1.go#L23
case "tendermint/PubKeySecp256k1":
return {
algo: Algorithm.Secp256k1,
data: fromBase64(pubkey.value) as PubkeyBytes,
};
// https://github.com/tendermint/tendermint/blob/v0.33.0/crypto/ed25519/ed25519.go#L22
case "tendermint/PubKeyEd25519":
return {
algo: Algorithm.Ed25519,
data: fromBase64(pubkey.value) as PubkeyBytes,
};
default:
throw new Error("Unsupported pubkey type");
}
}
export function decodeSignature(signature: string): SignatureBytes {