Let OfflineSigner.sign return a SignResponse
This commit is contained in:
@@ -56,7 +56,7 @@ describe("CosmosClient.searchTx", () => {
|
||||
const { accountNumber, sequence } = await client.getSequence();
|
||||
const chainId = await client.getChainId();
|
||||
const signDoc = makeStdSignDoc([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(walletAddress, signDoc);
|
||||
const { signature } = await wallet.sign(walletAddress, signDoc);
|
||||
const tx: CosmosSdkTx = {
|
||||
type: "cosmos-sdk/StdTx",
|
||||
value: {
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
unused,
|
||||
wasmd,
|
||||
} from "./testutils.spec";
|
||||
import { StdFee } from "./types";
|
||||
import { StdFee, StdTx } from "./types";
|
||||
|
||||
const blockTime = 1_000; // ms
|
||||
|
||||
@@ -230,8 +230,8 @@ describe("CosmosClient", () => {
|
||||
const chainId = await client.getChainId();
|
||||
const { accountNumber, sequence } = await client.getSequence(faucet.address);
|
||||
const signDoc = makeStdSignDoc([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx = {
|
||||
const { signature } = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx: StdTx = {
|
||||
msg: [sendMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
|
||||
@@ -98,7 +98,7 @@ export {
|
||||
} from "./pubkey";
|
||||
export { findSequenceForSignedTx } from "./sequence";
|
||||
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
|
||||
export { AccountData, Algo, OfflineSigner } from "./signer";
|
||||
export { AccountData, Algo, OfflineSigner, SignResponse } from "./signer";
|
||||
export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient";
|
||||
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
|
||||
export { makeCosmoshubPath, executeKdf, KdfConfiguration } from "./wallet";
|
||||
|
||||
@@ -46,7 +46,7 @@ describe("DistributionExtension", () => {
|
||||
const memo = "Test delegation for wasmd";
|
||||
const { accountNumber, sequence } = await client.getSequence();
|
||||
const signDoc = makeStdSignDoc([msg], defaultFee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(faucet.address, signDoc);
|
||||
const { signature } = await wallet.sign(faucet.address, signDoc);
|
||||
const tx = {
|
||||
msg: [msg],
|
||||
fee: defaultFee,
|
||||
|
||||
@@ -58,7 +58,7 @@ describe("GovExtension", () => {
|
||||
proposalAccountNumber,
|
||||
proposalSequence,
|
||||
);
|
||||
const proposalSignature = await wallet.sign(faucet.address, proposalSignDoc);
|
||||
const { signature: proposalSignature } = await wallet.sign(faucet.address, proposalSignDoc);
|
||||
const proposalTx = {
|
||||
msg: [proposalMsg],
|
||||
fee: defaultFee,
|
||||
@@ -90,7 +90,7 @@ describe("GovExtension", () => {
|
||||
voteAccountNumber,
|
||||
voteSequence,
|
||||
);
|
||||
const voteSignature = await wallet.sign(faucet.address, voteSignDoc);
|
||||
const { signature: voteSignature } = await wallet.sign(faucet.address, voteSignDoc);
|
||||
const voteTx = {
|
||||
msg: [voteMsg],
|
||||
fee: defaultFee,
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
wasmd,
|
||||
wasmdEnabled,
|
||||
} from "../testutils.spec";
|
||||
import { StdFee } from "../types";
|
||||
import { StdFee, StdTx } from "../types";
|
||||
import { makeCosmoshubPath } from "../wallet";
|
||||
import { setupAuthExtension } from "./auth";
|
||||
import { TxsResponse } from "./base";
|
||||
@@ -240,8 +240,8 @@ describe("LcdClient", () => {
|
||||
const { accountNumber, sequence } = await client.getSequence();
|
||||
const chainId = await client.getChainId();
|
||||
const signDoc = makeStdSignDoc([sendMsg], fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx = {
|
||||
const { signature } = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx: StdTx = {
|
||||
msg: [sendMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@@ -538,7 +538,7 @@ describe("LcdClient", () => {
|
||||
const { account_number, sequence } = (await client.auth.account(faucet.address)).result.value;
|
||||
|
||||
const signDoc = makeStdSignDoc([theMsg], fee, wasmd.chainId, memo, account_number, sequence);
|
||||
const signature = await wallet.sign(walletAddress, signDoc);
|
||||
const { signature } = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx = makeSignedTx(theMsg, fee, memo, signature);
|
||||
const result = await client.broadcastTx(signedTx);
|
||||
expect(result.code).toBeUndefined();
|
||||
@@ -597,10 +597,10 @@ describe("LcdClient", () => {
|
||||
const signDoc1 = makeStdSignDoc([theMsg], fee, wasmd.chainId, memo, an1, sequence1);
|
||||
const signDoc2 = makeStdSignDoc([theMsg], fee, wasmd.chainId, memo, an2, sequence2);
|
||||
const signDoc3 = makeStdSignDoc([theMsg], fee, wasmd.chainId, memo, an3, sequence3);
|
||||
const signature1 = await account1.sign(address1, signDoc1);
|
||||
const signature2 = await account2.sign(address2, signDoc2);
|
||||
const signature3 = await account3.sign(address3, signDoc3);
|
||||
const signedTx = {
|
||||
const { signature: signature1 } = await account1.sign(address1, signDoc1);
|
||||
const { signature: signature2 } = await account2.sign(address2, signDoc2);
|
||||
const { signature: signature3 } = await account3.sign(address3, signDoc3);
|
||||
const signedTx: StdTx = {
|
||||
msg: [theMsg],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@@ -659,8 +659,8 @@ describe("LcdClient", () => {
|
||||
const { account_number, sequence } = (await client.auth.account(walletAddress)).result.value;
|
||||
|
||||
const signDoc = makeStdSignDoc([msg1, msg2], fee, wasmd.chainId, memo, account_number, sequence);
|
||||
const signature = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx = {
|
||||
const { signature } = await wallet.sign(walletAddress, signDoc);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg1, msg2],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@@ -724,9 +724,9 @@ describe("LcdClient", () => {
|
||||
|
||||
const signDoc1 = makeStdSignDoc([msg2, msg1], fee, wasmd.chainId, memo, an1, sequence1);
|
||||
const signDoc2 = makeStdSignDoc([msg2, msg1], fee, wasmd.chainId, memo, an2, sequence2);
|
||||
const signature1 = await account1.sign(address1, signDoc1);
|
||||
const signature2 = await account2.sign(address2, signDoc2);
|
||||
const signedTx = {
|
||||
const { signature: signature1 } = await account1.sign(address1, signDoc1);
|
||||
const { signature: signature2 } = await account2.sign(address2, signDoc2);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg2, msg1],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@@ -795,9 +795,9 @@ describe("LcdClient", () => {
|
||||
|
||||
const signDoc1 = makeStdSignDoc([msg1, msg2], fee, wasmd.chainId, memo, an1, sequence1);
|
||||
const signDoc2 = makeStdSignDoc([msg1, msg2], fee, wasmd.chainId, memo, an2, sequence2);
|
||||
const signature1 = await account1.sign(address1, signDoc1);
|
||||
const signature2 = await account2.sign(address2, signDoc2);
|
||||
const signedTx = {
|
||||
const { signature: signature1 } = await account1.sign(address1, signDoc1);
|
||||
const { signature: signature2 } = await account2.sign(address2, signDoc2);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg1, msg2],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
@@ -861,9 +861,9 @@ describe("LcdClient", () => {
|
||||
|
||||
const signDoc1 = makeStdSignDoc([msg2, msg1], fee, wasmd.chainId, memo, an1, sequence1);
|
||||
const signDoc2 = makeStdSignDoc([msg2, msg1], fee, wasmd.chainId, memo, an2, sequence2);
|
||||
const signature1 = await account1.sign(address1, signDoc1);
|
||||
const signature2 = await account2.sign(address2, signDoc2);
|
||||
const signedTx = {
|
||||
const { signature: signature1 } = await account1.sign(address1, signDoc1);
|
||||
const { signature: signature2 } = await account2.sign(address2, signDoc2);
|
||||
const signedTx: StdTx = {
|
||||
msg: [msg2, msg1],
|
||||
fee: fee,
|
||||
memo: memo,
|
||||
|
||||
@@ -47,7 +47,7 @@ describe("StakingExtension", () => {
|
||||
const memo = "Test delegation for wasmd";
|
||||
const { accountNumber, sequence } = await client.getSequence();
|
||||
const signDoc = makeStdSignDoc([msg], defaultFee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(faucet.address, signDoc);
|
||||
const { signature } = await wallet.sign(faucet.address, signDoc);
|
||||
const tx = {
|
||||
msg: [msg],
|
||||
fee: defaultFee,
|
||||
@@ -70,7 +70,7 @@ describe("StakingExtension", () => {
|
||||
const memo = "Test undelegation for wasmd";
|
||||
const { accountNumber, sequence } = await client.getSequence();
|
||||
const signDoc = makeStdSignDoc([msg], defaultFee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await wallet.sign(faucet.address, signDoc);
|
||||
const { signature } = await wallet.sign(faucet.address, signDoc);
|
||||
const tx = {
|
||||
msg: [msg],
|
||||
fee: defaultFee,
|
||||
|
||||
@@ -120,7 +120,7 @@ describe("Secp256k1Wallet", () => {
|
||||
account_number: "7",
|
||||
sequence: "54",
|
||||
};
|
||||
const signature = await wallet.sign(defaultAddress, signDoc);
|
||||
const { signature } = await wallet.sign(defaultAddress, signDoc);
|
||||
const valid = await Secp256k1.verifySignature(
|
||||
Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)),
|
||||
new Sha256(serializeSignDoc(signDoc)).digest(),
|
||||
|
||||
@@ -16,8 +16,7 @@ import { assert, isNonNullObject } from "@cosmjs/utils";
|
||||
import { rawSecp256k1PubkeyToAddress } from "./address";
|
||||
import { serializeSignDoc, StdSignDoc } from "./encoding";
|
||||
import { encodeSecp256k1Signature } from "./signature";
|
||||
import { AccountData, OfflineSigner } from "./signer";
|
||||
import { StdSignature } from "./types";
|
||||
import { AccountData, OfflineSigner, SignResponse } from "./signer";
|
||||
import {
|
||||
decrypt,
|
||||
encrypt,
|
||||
@@ -256,14 +255,17 @@ export class Secp256k1Wallet implements OfflineSigner {
|
||||
];
|
||||
}
|
||||
|
||||
public async sign(signerAddress: string, signDoc: StdSignDoc): Promise<StdSignature> {
|
||||
public async sign(signerAddress: string, signDoc: StdSignDoc): Promise<SignResponse> {
|
||||
if (signerAddress !== this.address) {
|
||||
throw new Error(`Address ${signerAddress} not found in wallet`);
|
||||
}
|
||||
const message = new Sha256(serializeSignDoc(signDoc)).digest();
|
||||
const signature = await Secp256k1.createSignature(message, this.privkey);
|
||||
const signatureBytes = new Uint8Array([...signature.r(32), ...signature.s(32)]);
|
||||
return encodeSecp256k1Signature(this.pubkey, signatureBytes);
|
||||
return {
|
||||
signedDoc: signDoc,
|
||||
signature: encodeSecp256k1Signature(this.pubkey, signatureBytes),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,15 @@ export interface AccountData {
|
||||
readonly pubkey: Uint8Array;
|
||||
}
|
||||
|
||||
export interface SignResponse {
|
||||
/**
|
||||
* The sign doc that was signed.
|
||||
* This may be different from the input signDoc when the signer modifies it as part of the signing process.
|
||||
*/
|
||||
readonly signedDoc: StdSignDoc;
|
||||
readonly signature: StdSignature;
|
||||
}
|
||||
|
||||
export interface OfflineSigner {
|
||||
/**
|
||||
* Get AccountData array from wallet. Rejects if not enabled.
|
||||
@@ -19,8 +28,11 @@ export interface OfflineSigner {
|
||||
/**
|
||||
* Request signature from whichever key corresponds to provided bech32-encoded address. Rejects if not enabled.
|
||||
*
|
||||
* The signer implementation may offer the user the ability to override parts of the signDoc. It must
|
||||
* return the doc that was signed in the response.
|
||||
*
|
||||
* @param signerAddress The address of the account that should sign the transaction
|
||||
* @param signDoc The content that should be signed
|
||||
*/
|
||||
readonly sign: (signerAddress: string, signDoc: StdSignDoc) => Promise<StdSignature>;
|
||||
readonly sign: (signerAddress: string, signDoc: StdSignDoc) => Promise<SignResponse>;
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ export class SigningCosmosClient extends CosmosClient {
|
||||
const { accountNumber, sequence } = await this.getSequence();
|
||||
const chainId = await this.getChainId();
|
||||
const signDoc = makeStdSignDoc(msgs, fee, chainId, memo, accountNumber, sequence);
|
||||
const signature = await this.signer.sign(this.senderAddress, signDoc);
|
||||
const { signature } = await this.signer.sign(this.senderAddress, signDoc);
|
||||
const signedTx: StdTx = {
|
||||
msg: msgs,
|
||||
fee: fee,
|
||||
|
||||
Reference in New Issue
Block a user