sdk38: Pass signer (wallet) into SigningCosmosClient

This commit is contained in:
willclarktech 2020-07-09 12:28:20 +02:00
parent 717ff6caf7
commit 6a02704805
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
9 changed files with 27 additions and 55 deletions

View File

@ -36,9 +36,7 @@ describe("CosmosClient.searchTx", () => {
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(walletAddress, signBytes),
);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
{
const memo = "Sending more than I can afford";

View File

@ -53,6 +53,6 @@ export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs";
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
export { findSequenceForSignedTx } from "./sequence";
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
export { FeeTable, SigningCallback, SigningCosmosClient } from "./signingcosmosclient";
export { FeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
export { OfflineWallet, Secp256k1OfflineWallet, makeCosmoshubPath } from "./wallet";
export { OfflineSigner, Secp256k1OfflineWallet, makeCosmoshubPath } from "./wallet";

View File

@ -220,9 +220,7 @@ describe("LcdClient", () => {
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(walletAddress, signBytes),
);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
{
const recipient = makeRandomAddress();
@ -354,12 +352,7 @@ describe("LcdClient", () => {
beforeAll(async () => {
if (wasmdEnabled()) {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address: walletAddress } = accounts[0];
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, async (signBytes) =>
wallet.sign(walletAddress, signBytes),
);
const client = new SigningCosmosClient(wasmd.endpoint, faucet.address, wallet);
const recipient = makeRandomAddress();
const transferAmount = [

View File

@ -22,12 +22,7 @@ describe("SigningCosmosClient", () => {
describe("makeReadOnly", () => {
it("can be constructed", async () => {
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(address, signBytes),
);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
expect(client).toBeTruthy();
});
});
@ -36,12 +31,7 @@ describe("SigningCosmosClient", () => {
it("always uses authAccount implementation", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(address, signBytes),
);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
const openedClient = (client as unknown) as PrivateCosmWasmClient;
const blockLatestSpy = spyOn(openedClient.lcdClient, "blocksLatest").and.callThrough();
@ -59,12 +49,7 @@ describe("SigningCosmosClient", () => {
it("works", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1OfflineWallet.fromMnemonic(faucet.mnemonic);
await wallet.enable();
const accounts = await wallet.getAccounts();
const { address } = accounts[0];
const client = new SigningCosmosClient(httpUrl, faucet.address, async (signBytes) =>
wallet.sign(address, signBytes),
);
const client = new SigningCosmosClient(httpUrl, faucet.address, wallet);
// instantiate
const transferAmount: readonly Coin[] = [

View File

@ -3,11 +3,8 @@ import { Account, CosmosClient, GetNonceResult, PostTxResult } from "./cosmoscli
import { makeSignBytes } from "./encoding";
import { BroadcastMode } from "./lcdapi";
import { MsgSend } from "./msgs";
import { StdFee, StdSignature, StdTx } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
}
import { StdFee, StdTx } from "./types";
import { OfflineSigner } from "./wallet";
export interface FeeTable {
readonly upload: StdFee;
@ -38,7 +35,7 @@ const defaultFees: FeeTable = {
export class SigningCosmosClient extends CosmosClient {
public readonly senderAddress: string;
private readonly signCallback: SigningCallback;
private readonly signer: OfflineSigner;
private readonly fees: FeeTable;
/**
@ -49,14 +46,14 @@ export class SigningCosmosClient extends CosmosClient {
*
* @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API)
* @param senderAddress The address that will sign and send transactions using this instance
* @param signCallback An asynchonous callback to create a signature for a given transaction. This can be implemented using secure key stores that require user interaction.
* @param signer A wallet provider which can provide signatures for transactions, potentially requiring user input.
* @param customFees The fees that are paid for transactions
* @param broadcastMode Defines at which point of the transaction processing the postTx method (i.e. transaction broadcasting) returns
*/
public constructor(
apiUrl: string,
senderAddress: string,
signCallback: SigningCallback,
signer: OfflineSigner,
customFees?: Partial<FeeTable>,
broadcastMode = BroadcastMode.Block,
) {
@ -64,7 +61,7 @@ export class SigningCosmosClient extends CosmosClient {
this.anyValidAddress = senderAddress;
this.senderAddress = senderAddress;
this.signCallback = signCallback;
this.signer = signer;
this.fees = { ...defaultFees, ...(customFees || {}) };
}
@ -81,6 +78,7 @@ export class SigningCosmosClient extends CosmosClient {
transferAmount: readonly Coin[],
memo = "",
): Promise<PostTxResult> {
await this.signer.enable();
const sendMsg: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
@ -95,7 +93,7 @@ export class SigningCosmosClient extends CosmosClient {
const { accountNumber, sequence } = await this.getNonce();
const chainId = await this.getChainId();
const signBytes = makeSignBytes([sendMsg], fee, chainId, memo, accountNumber, sequence);
const signature = await this.signCallback(signBytes);
const signature = await this.signer.sign(this.senderAddress, signBytes);
const signedTx: StdTx = {
msg: [sendMsg],
fee: fee,

View File

@ -24,7 +24,7 @@ export interface AccountData {
readonly pubkey: Uint8Array;
}
export interface OfflineWallet {
export interface OfflineSigner {
/**
* Request access to the user's accounts. Wallet should ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
@ -68,7 +68,7 @@ export function makeCosmoshubPath(a: number): readonly Slip10RawIndex[] {
];
}
export class Secp256k1OfflineWallet implements OfflineWallet {
export class Secp256k1OfflineWallet implements OfflineSigner {
public static async fromMnemonic(
mnemonic: string,
hdPath: readonly Slip10RawIndex[] = makeCosmoshubPath(0),

View File

@ -51,6 +51,6 @@ export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs";
export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey";
export { findSequenceForSignedTx } from "./sequence";
export { encodeSecp256k1Signature, decodeSignature } from "./signature";
export { FeeTable, SigningCallback, SigningCosmosClient } from "./signingcosmosclient";
export { FeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types";
export { OfflineWallet, Secp256k1OfflineWallet, makeCosmoshubPath } from "./wallet";
export { OfflineSigner, Secp256k1OfflineWallet, makeCosmoshubPath } from "./wallet";

View File

@ -1,10 +1,8 @@
import { Coin } from "./coins";
import { Account, CosmosClient, GetNonceResult, PostTxResult } from "./cosmosclient";
import { BroadcastMode } from "./lcdapi";
import { StdFee, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
}
import { StdFee } from "./types";
import { OfflineSigner } from "./wallet";
export interface FeeTable {
readonly upload: StdFee;
readonly init: StdFee;
@ -13,7 +11,7 @@ export interface FeeTable {
}
export declare class SigningCosmosClient extends CosmosClient {
readonly senderAddress: string;
private readonly signCallback;
private readonly signer;
private readonly fees;
/**
* Creates a new client with signing capability to interact with a CosmWasm blockchain. This is the bigger brother of CosmWasmClient.
@ -23,14 +21,14 @@ export declare class SigningCosmosClient extends CosmosClient {
*
* @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API)
* @param senderAddress The address that will sign and send transactions using this instance
* @param signCallback An asynchonous callback to create a signature for a given transaction. This can be implemented using secure key stores that require user interaction.
* @param signer A wallet provider which can provide signatures for transactions, potentially requiring user input.
* @param customFees The fees that are paid for transactions
* @param broadcastMode Defines at which point of the transaction processing the postTx method (i.e. transaction broadcasting) returns
*/
constructor(
apiUrl: string,
senderAddress: string,
signCallback: SigningCallback,
signer: OfflineSigner,
customFees?: Partial<FeeTable>,
broadcastMode?: BroadcastMode,
);

View File

@ -7,7 +7,7 @@ export interface AccountData {
readonly algo: Algo;
readonly pubkey: Uint8Array;
}
export interface OfflineWallet {
export interface OfflineSigner {
/**
* Request access to the user's accounts. Wallet should ask the user to approve or deny access. Returns true if granted access or false if denied.
*/
@ -26,7 +26,7 @@ export interface OfflineWallet {
* with 0-based account index `a`.
*/
export declare function makeCosmoshubPath(a: number): readonly Slip10RawIndex[];
export declare class Secp256k1OfflineWallet implements OfflineWallet {
export declare class Secp256k1OfflineWallet implements OfflineSigner {
static fromMnemonic(
mnemonic: string,
hdPath?: readonly Slip10RawIndex[],