-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e684a14
commit 7d603fc
Showing
2 changed files
with
83 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |