diff --git a/package.json b/package.json index 5189203..97d944f 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ }, "dependencies": { "@bitauth/libauth": "^1.17.1", + "@cosmjs/cosmwasm-stargate": "^0.29.2", "@cosmjs/proto-signing": "^0.29.0", "@cosmjs/stargate": "^0.29.0", "cosmjs-types": "^0.5.1" diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index 785d6b4..0f300fa 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -1,4 +1,12 @@ -import { OfflineSigner } from '@cosmjs/proto-signing'; +import { + SigningCosmWasmClient, + SigningCosmWasmClientOptions, +} from '@cosmjs/cosmwasm-stargate'; +import { + DirectSecp256k1HdWallet, + makeCosmoshubPath, + OfflineSigner, +} from '@cosmjs/proto-signing'; import { AccountData } from '@cosmjs/proto-signing/build/signer'; import { SigningStargateClient, @@ -6,52 +14,92 @@ import { } from '@cosmjs/stargate'; import { Tendermint34Client } from '@cosmjs/tendermint-rpc'; import { HttpEndpoint } from '@cosmjs/tendermint-rpc/build/rpcclients'; +import { provider } from '../providers'; import { TendermintBatchClient } from '../tendermint-rpc/tendermintbatchclient'; export default OfflineSigner; -export class Wallet extends SigningStargateClient { - _signer: OfflineSigner; - _tendermintBatchClient: TendermintBatchClient; - _tendermintClient: Tendermint34Client; - private _accounts: readonly AccountData[]; +export interface WalletOptions { + readonly cosmWasmOptions: SigningCosmWasmClientOptions; + readonly stargateOptions: SigningStargateClientOptions; +} - public static async connectWithSigner( - endpoint: string | HttpEndpoint, - signer: OfflineSigner, - options: SigningStargateClientOptions = {} - ): Promise { - const tmClient = await Tendermint34Client.connect(endpoint); - const tmBatchClient = await TendermintBatchClient.connect(endpoint); - return new Wallet(tmClient, signer, options, tmBatchClient); - } +export class Wallet { + private _signer: DirectSecp256k1HdWallet; + private _cosmWasmSigner: SigningCosmWasmClient; + private _stargateSigner: SigningStargateClient; + private _account: AccountData; - private constructor(tmClient, signer, options, tmBatchClient) { - super(tmClient, signer, options); - this._tendermintClient = tmClient; - this._tendermintBatchClient = tmBatchClient; - this._signer = signer; + public static async getSigner( + provider: provider, + mnemonic: string, + prefix: string, + options?: WalletOptions + ): Promise { + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { + hdPaths: [makeCosmoshubPath(0)], + prefix: prefix, + }); + const cosmWasmClient = await SigningCosmWasmClient.connectWithSigner( + provider.rpcUrl, + wallet, + options.cosmWasmOptions + ); + const stargateClient = await SigningStargateClient.connectWithSigner( + provider.rpcUrl, + wallet, + options.stargateOptions + ); + const [account] = await wallet.getAccounts(); + return new Wallet(wallet, account, cosmWasmClient, stargateClient); } - async getAccounts() { - if (!this._accounts) { - this._accounts = await this._signer.getAccounts(); + public static async getSigners( + provider: provider, + mnemonic: string, + prefix: string, + amount: number, + options?: WalletOptions + ): Promise { + const wallets = []; + if (amount <= 0) { + throw 'Amount must be greater than zero'; } - return this._accounts; + for (let i = 0; i < amount; i++) { + const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { + hdPaths: [makeCosmoshubPath(i)], + prefix: prefix, + }); + const cosmWasmClient = await SigningCosmWasmClient.connectWithSigner( + provider.rpcUrl, + wallet, + options.cosmWasmOptions + ); + const stargateClient = await SigningStargateClient.connectWithSigner( + provider.rpcUrl, + wallet, + options.stargateOptions + ); + const [account] = await wallet.getAccounts(); + wallets.push(new Wallet(wallet, account, cosmWasmClient, stargateClient)); + } + return wallets; } - async getAddresses() { - const accounts = await this.getAccounts(); - const listAddresses = []; - for (const account of accounts) { - listAddresses.push(account); - } - return listAddresses; + private constructor( + wallet: DirectSecp256k1HdWallet, + account: AccountData, + cosmWasmSigner: SigningCosmWasmClient, + stargateSigner: SigningStargateClient + ) { + this._signer = wallet; + this._account = account; + this._cosmWasmSigner = cosmWasmSigner; + this._stargateSigner = stargateSigner; } - async getDefaultAddress() { - const listAddress = await this.getAddresses(); - return listAddress[0]; + get address(): string { + return this._account.address; } }