launchpad: Use signature functions/type from amino
This commit is contained in:
parent
3a5877452e
commit
7b880c9a66
@ -2,11 +2,14 @@
|
||||
export {
|
||||
decodeAminoPubkey,
|
||||
decodeBech32Pubkey,
|
||||
decodeSignature,
|
||||
encodeAminoPubkey,
|
||||
encodeBech32Pubkey,
|
||||
encodeSecp256k1Pubkey,
|
||||
encodeSecp256k1Signature,
|
||||
pubkeyToAddress,
|
||||
pubkeyType,
|
||||
StdSignature,
|
||||
} from "@cosmjs/amino";
|
||||
import { SinglePubkey } from "@cosmjs/amino";
|
||||
/** @deprecated PubKey is deprecated. Use `SinglePubkey` or the more general `Pubkey` from `@cosmjs/amino`. */
|
||||
@ -139,11 +142,10 @@ export {
|
||||
} from "./msgs";
|
||||
export { makeCosmoshubPath } from "./paths";
|
||||
export { findSequenceForSignedTx } from "./sequence";
|
||||
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
|
||||
export { AccountData, Algo, AminoSignResponse, OfflineSigner } from "./signer";
|
||||
export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient";
|
||||
export { isStdTx, isWrappedStdTx, makeStdTx, CosmosSdkTx, StdTx, WrappedStdTx, WrappedTx } from "./tx";
|
||||
export { StdFee, StdSignature } from "./types";
|
||||
export { StdFee } from "./types";
|
||||
export { executeKdf, KdfConfiguration } from "./wallet";
|
||||
export { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
|
||||
export { Secp256k1Wallet } from "./secp256k1wallet";
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import { encodeSecp256k1Signature, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import {
|
||||
Bip39,
|
||||
EnglishMnemonic,
|
||||
@ -16,7 +16,6 @@ import { assert, isNonNullObject } from "@cosmjs/utils";
|
||||
|
||||
import { serializeSignDoc, StdSignDoc } from "./encoding";
|
||||
import { makeCosmoshubPath } from "./paths";
|
||||
import { encodeSecp256k1Signature } from "./signature";
|
||||
import { AccountData, AminoSignResponse, OfflineSigner } from "./signer";
|
||||
import {
|
||||
decrypt,
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import { encodeSecp256k1Signature, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
|
||||
import { Secp256k1, Sha256 } from "@cosmjs/crypto";
|
||||
import { Bech32 } from "@cosmjs/encoding";
|
||||
|
||||
import { serializeSignDoc, StdSignDoc } from "./encoding";
|
||||
import { encodeSecp256k1Signature } from "./signature";
|
||||
import { AccountData, AminoSignResponse, OfflineSigner } from "./signer";
|
||||
|
||||
/**
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { decodeSignature } from "@cosmjs/amino";
|
||||
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
|
||||
|
||||
import { makeSignDoc, serializeSignDoc } from "./encoding";
|
||||
import { decodeSignature } from "./signature";
|
||||
import { WrappedStdTx } from "./tx";
|
||||
|
||||
/**
|
||||
|
||||
@ -1,66 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { fromBase64 } from "@cosmjs/encoding";
|
||||
|
||||
import { decodeSignature, encodeSecp256k1Signature } from "./signature";
|
||||
import { StdSignature } from "./types";
|
||||
|
||||
describe("signature", () => {
|
||||
describe("encodeSecp256k1Signature", () => {
|
||||
it("encodes a full signature", () => {
|
||||
const pubkey = fromBase64("AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP");
|
||||
const signature = fromBase64(
|
||||
"1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
);
|
||||
expect(encodeSecp256k1Signature(pubkey, signature)).toEqual({
|
||||
pub_key: {
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
|
||||
},
|
||||
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
});
|
||||
});
|
||||
|
||||
it("throws when getting uncompressed public keys", () => {
|
||||
const pubkey = fromBase64(
|
||||
"BE8EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQE7WHpoHoNswYeoFkuYpYSKK4mzFzMV/dB0DVAy4lnNU=",
|
||||
);
|
||||
const signature = fromBase64(
|
||||
"1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
);
|
||||
expect(() => encodeSecp256k1Signature(pubkey, signature)).toThrowError(
|
||||
/public key must be compressed secp256k1/i,
|
||||
);
|
||||
});
|
||||
|
||||
it("throws if signature contains recovery byte", () => {
|
||||
const pubkey = fromBase64("AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP");
|
||||
const signature = Uint8Array.from([
|
||||
...fromBase64(
|
||||
"1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
),
|
||||
99,
|
||||
]);
|
||||
expect(() => encodeSecp256k1Signature(pubkey, signature)).toThrowError(
|
||||
/signature must be 64 bytes long/i,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeSignature", () => {
|
||||
it("works for secp256k1", () => {
|
||||
const signature: StdSignature = {
|
||||
pub_key: {
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
|
||||
},
|
||||
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
};
|
||||
expect(decodeSignature(signature)).toEqual({
|
||||
pubkey: fromBase64("AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP"),
|
||||
signature: fromBase64(
|
||||
"1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
|
||||
),
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -1,39 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { encodeSecp256k1Pubkey, pubkeyType } from "@cosmjs/amino";
|
||||
import { fromBase64, toBase64 } from "@cosmjs/encoding";
|
||||
|
||||
import { StdSignature } from "./types";
|
||||
|
||||
/**
|
||||
* Takes a binary pubkey and signature to create a signature object
|
||||
*
|
||||
* @param pubkey a compressed secp256k1 public key
|
||||
* @param signature a 64 byte fixed length representation of secp256k1 signature components r and s
|
||||
*/
|
||||
export function encodeSecp256k1Signature(pubkey: Uint8Array, signature: Uint8Array): StdSignature {
|
||||
if (signature.length !== 64) {
|
||||
throw new Error(
|
||||
"Signature must be 64 bytes long. Cosmos SDK uses a 2x32 byte fixed length encoding for the secp256k1 signature integers r and s.",
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
pub_key: encodeSecp256k1Pubkey(pubkey),
|
||||
signature: toBase64(signature),
|
||||
};
|
||||
}
|
||||
|
||||
export function decodeSignature(
|
||||
signature: StdSignature,
|
||||
): { readonly pubkey: Uint8Array; readonly signature: Uint8Array } {
|
||||
switch (signature.pub_key.type) {
|
||||
// Note: please don't add cases here without writing additional unit tests
|
||||
case pubkeyType.secp256k1:
|
||||
return {
|
||||
pubkey: fromBase64(signature.pub_key.value),
|
||||
signature: fromBase64(signature.signature),
|
||||
};
|
||||
default:
|
||||
throw new Error("Unsupported pubkey type");
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import { StdSignature } from "@cosmjs/amino";
|
||||
|
||||
import { StdSignDoc } from "./encoding";
|
||||
import { StdSignature } from "./types";
|
||||
|
||||
export type Algo = "secp256k1" | "ed25519" | "sr25519";
|
||||
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { StdSignature } from "@cosmjs/amino";
|
||||
|
||||
import { coins } from "./coins";
|
||||
import { makeSignDoc } from "./encoding";
|
||||
import { makeStdTx } from "./tx";
|
||||
import { StdFee, StdSignature } from "./types";
|
||||
import { StdFee } from "./types";
|
||||
|
||||
describe("tx", () => {
|
||||
describe("makeStdTx", () => {
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { StdSignature } from "@cosmjs/amino";
|
||||
|
||||
import { StdSignDoc } from "./encoding";
|
||||
import { Msg } from "./msgs";
|
||||
import { StdFee, StdSignature } from "./types";
|
||||
import { StdFee } from "./types";
|
||||
|
||||
/**
|
||||
* A Cosmos SDK StdTx
|
||||
|
||||
@ -1,14 +1,7 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { Pubkey } from "@cosmjs/amino";
|
||||
|
||||
import { Coin } from "./coins";
|
||||
|
||||
export interface StdFee {
|
||||
readonly amount: readonly Coin[];
|
||||
readonly gas: string;
|
||||
}
|
||||
|
||||
export interface StdSignature {
|
||||
readonly pub_key: Pubkey;
|
||||
readonly signature: string;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user