diff --git a/packages/sdk38/src/cosmosclient.searchtx.spec.ts b/packages/sdk38/src/cosmosclient.searchtx.spec.ts index efaeb440..1f44bc88 100644 --- a/packages/sdk38/src/cosmosclient.searchtx.spec.ts +++ b/packages/sdk38/src/cosmosclient.searchtx.spec.ts @@ -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"; diff --git a/packages/sdk38/src/index.ts b/packages/sdk38/src/index.ts index 5af7b788..5caf70db 100644 --- a/packages/sdk38/src/index.ts +++ b/packages/sdk38/src/index.ts @@ -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"; diff --git a/packages/sdk38/src/lcdapi/lcdclient.spec.ts b/packages/sdk38/src/lcdapi/lcdclient.spec.ts index 2b712fba..ca314d42 100644 --- a/packages/sdk38/src/lcdapi/lcdclient.spec.ts +++ b/packages/sdk38/src/lcdapi/lcdclient.spec.ts @@ -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 = [ diff --git a/packages/sdk38/src/signingcosmosclient.spec.ts b/packages/sdk38/src/signingcosmosclient.spec.ts index 81f69689..93a833e0 100644 --- a/packages/sdk38/src/signingcosmosclient.spec.ts +++ b/packages/sdk38/src/signingcosmosclient.spec.ts @@ -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[] = [ diff --git a/packages/sdk38/src/signingcosmosclient.ts b/packages/sdk38/src/signingcosmosclient.ts index 74f23fef..9456a67a 100644 --- a/packages/sdk38/src/signingcosmosclient.ts +++ b/packages/sdk38/src/signingcosmosclient.ts @@ -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; -} +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, 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 { + 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, diff --git a/packages/sdk38/src/wallet.ts b/packages/sdk38/src/wallet.ts index 3441f9f9..40f89d76 100644 --- a/packages/sdk38/src/wallet.ts +++ b/packages/sdk38/src/wallet.ts @@ -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), diff --git a/packages/sdk38/types/index.d.ts b/packages/sdk38/types/index.d.ts index 76c332f0..0deb1c37 100644 --- a/packages/sdk38/types/index.d.ts +++ b/packages/sdk38/types/index.d.ts @@ -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"; diff --git a/packages/sdk38/types/signingcosmosclient.d.ts b/packages/sdk38/types/signingcosmosclient.d.ts index a733b8f6..dfcb9a70 100644 --- a/packages/sdk38/types/signingcosmosclient.d.ts +++ b/packages/sdk38/types/signingcosmosclient.d.ts @@ -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; -} +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, broadcastMode?: BroadcastMode, ); diff --git a/packages/sdk38/types/wallet.d.ts b/packages/sdk38/types/wallet.d.ts index f27bea6f..57569fb4 100644 --- a/packages/sdk38/types/wallet.d.ts +++ b/packages/sdk38/types/wallet.d.ts @@ -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[],