diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index d39287a9..42b4ff56 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -41,6 +41,7 @@ export function main(originalArgs: readonly string[]): void { [ "@cosmwasm/sdk", [ + "encodeSecp256k1Pubkey", "encodeSecp256k1Signature", "makeSignBytes", "marshalTx", diff --git a/packages/sdk/src/encoding.ts b/packages/sdk/src/encoding.ts index ad3a4fa1..49a9fde4 100644 --- a/packages/sdk/src/encoding.ts +++ b/packages/sdk/src/encoding.ts @@ -1,7 +1,8 @@ import { Secp256k1 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; -import { Msg, NonceInfo, pubkeyType, StdFee, StdSignature, StdTx } from "./types"; +import { encodeSecp256k1Pubkey } from "./pubkey"; +import { Msg, NonceInfo, StdFee, StdSignature, StdTx } from "./types"; const { toBase64, toUtf8 } = Encoding; @@ -61,10 +62,7 @@ export function makeSignBytes( export function encodeSecp256k1Signature(pubkey: Uint8Array, signature: Uint8Array): StdSignature { return { // eslint-disable-next-line @typescript-eslint/camelcase - pub_key: { - type: pubkeyType.secp256k1, - value: toBase64(Secp256k1.compressPubkey(pubkey)), - }, + pub_key: encodeSecp256k1Pubkey(pubkey), // Recovery seems to be unused signature: toBase64(Secp256k1.trimRecoveryByte(signature)), }; diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 271e6a18..1908af35 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -7,3 +7,4 @@ export { unmarshalTx } from "./decoding"; export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen"; +export { encodeSecp256k1Pubkey } from "./pubkey"; diff --git a/packages/sdk/src/pubkey.spec.ts b/packages/sdk/src/pubkey.spec.ts new file mode 100644 index 00000000..0b700496 --- /dev/null +++ b/packages/sdk/src/pubkey.spec.ts @@ -0,0 +1,27 @@ +import { Encoding } from "@iov/encoding"; + +import { encodeSecp256k1Pubkey } from "./pubkey"; + +const { fromBase64 } = Encoding; + +describe("pubkey", () => { + describe("encodeSecp256k1Pubkey", () => { + it("encodes a full signature", () => { + const pubkey = fromBase64("AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP"); + expect(encodeSecp256k1Pubkey(pubkey)).toEqual({ + type: "tendermint/PubKeySecp256k1", + value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP", + }); + }); + + it("compresses uncompressed public keys", () => { + const pubkey = fromBase64( + "BE8EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQE7WHpoHoNswYeoFkuYpYSKK4mzFzMV/dB0DVAy4lnNU=", + ); + expect(encodeSecp256k1Pubkey(pubkey)).toEqual({ + type: "tendermint/PubKeySecp256k1", + value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ", + }); + }); + }); +}); diff --git a/packages/sdk/src/pubkey.ts b/packages/sdk/src/pubkey.ts new file mode 100644 index 00000000..73b10695 --- /dev/null +++ b/packages/sdk/src/pubkey.ts @@ -0,0 +1,11 @@ +import { Secp256k1 } from "@iov/crypto"; +import { Encoding } from "@iov/encoding"; + +import { PubKey, pubkeyType } from "./types"; + +export function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey { + return { + type: pubkeyType.secp256k1, + value: Encoding.toBase64(Secp256k1.compressPubkey(pubkey)), + }; +} diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 084a2cfb..abe78f64 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -6,3 +6,4 @@ export { unmarshalTx } from "./decoding"; export { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; export { RestClient, TxsResponse } from "./restclient"; export { makeCosmoshubPath, Pen, PrehashType, Secp256k1Pen } from "./pen"; +export { encodeSecp256k1Pubkey } from "./pubkey"; diff --git a/packages/sdk/types/pubkey.d.ts b/packages/sdk/types/pubkey.d.ts new file mode 100644 index 00000000..72188ff7 --- /dev/null +++ b/packages/sdk/types/pubkey.d.ts @@ -0,0 +1,2 @@ +import { PubKey } from "./types"; +export declare function encodeSecp256k1Pubkey(pubkey: Uint8Array): PubKey;