Cleanup CosmosUtil api

This commit is contained in:
Ilja 2022-02-28 15:45:44 +02:00
parent bf2764b6c6
commit 30f5f7cb1e
2 changed files with 23 additions and 19 deletions

View File

@ -18,24 +18,28 @@ interface IInitArguments {
* Utility
*/
export class Cosmos {
keyring: MnemonicKeyring
wallet: CosmosWallet
derivationPath: string
private keyring: MnemonicKeyring
private wallet: CosmosWallet
constructor(keyring: MnemonicKeyring, wallet: CosmosWallet, derivationPath: string) {
constructor(keyring: MnemonicKeyring, wallet: CosmosWallet) {
this.wallet = wallet
this.keyring = keyring
this.derivationPath = derivationPath
}
static async init({ mnemonic, path }: IInitArguments) {
const keyring = await MnemonicKeyring.init({ mnemonic })
const derivationPath = path ?? DEFAULT_PATH
const wallet = await CosmosWallet.init(keyring.getPrivateKey(derivationPath))
return new Cosmos(keyring, wallet, derivationPath)
const wallet = await CosmosWallet.init(keyring.getPrivateKey(path ?? DEFAULT_PATH))
return new Cosmos(keyring, wallet)
}
public getPublicKey() {
return this.keyring.getPublicKey(this.derivationPath)
public async getAccount(number = 0) {
const account = await this.wallet.getAccounts()
return account[number]
}
public getMnemonic() {
return this.keyring.mnemonic
}
}

View File

@ -17,19 +17,19 @@ export async function createOrRestoreCosmosWallet() {
if (mnemonic) {
wallet1 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/0" })
wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" })
const accounts1 = await wallet1.wallet.getAccounts()
const accounts2 = await wallet2.wallet.getAccounts()
address1 = accounts1[0].address
address2 = accounts2[0].address
const account1 = await wallet1.getAccount()
const account2 = await wallet2.getAccount()
address1 = account1.address
address2 = account2.address
} else {
wallet1 = await Cosmos.init({ path: "m/44'/118'/0'/0/0" })
const mnemonic = wallet1.keyring.mnemonic
const mnemonic = wallet1.getMnemonic()
// We can reuse same mnemonic for both wallets
wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" })
const accounts1 = await wallet1.wallet.getAccounts()
const accounts2 = await wallet2.wallet.getAccounts()
address1 = accounts1[0].address
address2 = accounts2[0].address
const account1 = await wallet1.getAccount()
const account2 = await wallet2.getAccount()
address1 = account1.address
address2 = account2.address
// Don't store mnemonic in local storage in a production project!
localStorage.setItem('WALLET_MNEMONIC', mnemonic)
}