Skip to content

Commit

Permalink
feat: add getSigner/s for Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
phamnam1805 committed Oct 14, 2022
1 parent e684a14 commit 7d603fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 34 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
116 changes: 82 additions & 34 deletions src/lib/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,105 @@
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,
SigningStargateClientOptions,
} 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<Wallet> {
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<Wallet> {
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<Wallet[]> {
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;
}
}

0 comments on commit 7d603fc

Please sign in to comment.