Let OfflineSigner.sign return a SignResponse

This commit is contained in:
Simon Warta 2020-09-23 14:17:22 +02:00
parent 5f2cf289eb
commit fbd8bbce9c
22 changed files with 84 additions and 54 deletions

View File

@ -48,7 +48,8 @@
- @cosmjs/launchpad: Export `StdSignDoc` and create helpers to make and
serialize a `StdSignDoc`: `makeStdSignDoc` and `serializeSignDoc`.
- @cosmjs/launchpad: Let `OfflineSigner.sign` take an `StdSignDoc` instead of an
encoded message.
encoded message and return a `SignResponse` that includes the document which
was signed.
- @cosmjs/launchpad: Remove `PrehashType` and the prehash type argument in
`OfflineSigner.sign` because the signer now needs to know how to serialize an
`StdSignDoc`.

View File

@ -104,7 +104,7 @@ describe("CosmWasmClient.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(alice.address0, signDoc);
const { signature } = await wallet.sign(alice.address0, signDoc);
const tx: CosmosSdkTx = {
type: "cosmos-sdk/StdTx",
value: {

View File

@ -7,6 +7,7 @@ import {
MsgSend,
Secp256k1Wallet,
StdFee,
StdTx,
} from "@cosmjs/launchpad";
import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
@ -237,8 +238,8 @@ describe("CosmWasmClient", () => {
const chainId = await client.getChainId();
const { accountNumber, sequence } = await client.getSequence(alice.address0);
const signDoc = makeStdSignDoc([sendMsg], fee, chainId, memo, accountNumber, sequence);
const signature = await wallet.sign(alice.address0, signDoc);
const signedTx = {
const { signature } = await wallet.sign(alice.address0, signDoc);
const signedTx: StdTx = {
msg: [sendMsg],
fee: fee,
memo: memo,

View File

@ -126,7 +126,7 @@ async function executeContract(
const { account_number, sequence } = (await client.auth.account(alice.address0)).result.value;
const signDoc = makeStdSignDoc([theMsg], fee, wasmd.chainId, memo, account_number, sequence);
const signature = await signer.sign(alice.address0, signDoc);
const { signature } = await signer.sign(alice.address0, signDoc);
const signedTx = makeSignedTx(theMsg, fee, memo, signature);
return client.broadcastTx(signedTx);
}

View File

@ -361,7 +361,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
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,

View File

@ -56,5 +56,6 @@ export async function sign(
accountNumber,
defaultSequence,
);
return signer.sign(fromAddress, signDoc);
const { signature } = await signer.sign(fromAddress, signDoc);
return signature;
}

View File

@ -4,10 +4,9 @@ import {
encodeSecp256k1Signature,
makeCosmoshubPath,
OfflineSigner,
StdSignature,
StdSignDoc,
} from "@cosmjs/launchpad";
import { serializeSignDoc } from "@cosmjs/launchpad";
import { serializeSignDoc, SignResponse } from "@cosmjs/launchpad";
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
@ -36,7 +35,7 @@ export class LedgerSigner implements OfflineSigner {
return this.accounts;
}
public async sign(signerAddress: string, signDoc: StdSignDoc): Promise<StdSignature> {
public async sign(signerAddress: string, signDoc: StdSignDoc): Promise<SignResponse> {
const accounts = this.accounts || (await this.getAccounts());
const accountIndex = accounts.findIndex((account) => account.address === signerAddress);
@ -48,6 +47,9 @@ export class LedgerSigner implements OfflineSigner {
const accountForAddress = accounts[accountIndex];
const hdPath = this.hdPaths[accountIndex];
const signature = await this.ledger.sign(message, hdPath);
return encodeSecp256k1Signature(accountForAddress.pubkey, signature);
return {
signedDoc: signDoc,
signature: encodeSecp256k1Signature(accountForAddress.pubkey, signature),
};
}
}

View File

@ -1,4 +1,5 @@
import { AccountData, OfflineSigner, StdSignature, StdSignDoc } from "@cosmjs/launchpad";
import { AccountData, OfflineSigner, StdSignDoc } from "@cosmjs/launchpad";
import { SignResponse } from "@cosmjs/launchpad";
import { LaunchpadLedgerOptions } from "./launchpadledger";
export declare class LedgerSigner implements OfflineSigner {
private readonly ledger;
@ -6,5 +7,5 @@ export declare class LedgerSigner implements OfflineSigner {
private accounts?;
constructor(options?: LaunchpadLedgerOptions);
getAccounts(): Promise<readonly AccountData[]>;
sign(signerAddress: string, signDoc: StdSignDoc): Promise<StdSignature>;
sign(signerAddress: string, signDoc: StdSignDoc): Promise<SignResponse>;
}

View File

@ -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: {

View File

@ -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,

View File

@ -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";

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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(),

View File

@ -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),
};
}
/**

View File

@ -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>;
}

View File

@ -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,

View File

@ -96,7 +96,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";

View File

@ -1,7 +1,6 @@
import { HdPath } from "@cosmjs/crypto";
import { StdSignDoc } from "./encoding";
import { AccountData, OfflineSigner } from "./signer";
import { StdSignature } from "./types";
import { AccountData, OfflineSigner, SignResponse } from "./signer";
import { EncryptionConfiguration, KdfConfiguration } from "./wallet";
/**
* This interface describes a JSON object holding the encrypted wallet and the meta data.
@ -87,7 +86,7 @@ export declare class Secp256k1Wallet implements OfflineSigner {
get mnemonic(): string;
private get address();
getAccounts(): Promise<readonly AccountData[]>;
sign(signerAddress: string, signDoc: StdSignDoc): Promise<StdSignature>;
sign(signerAddress: string, signDoc: StdSignDoc): Promise<SignResponse>;
/**
* Generates an encrypted serialization of this wallet.
*

View File

@ -7,6 +7,14 @@ export interface AccountData {
readonly algo: Algo;
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.
@ -15,8 +23,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>;
}