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 * Utility
*/ */
export class Cosmos { export class Cosmos {
keyring: MnemonicKeyring private keyring: MnemonicKeyring
wallet: CosmosWallet private wallet: CosmosWallet
derivationPath: string
constructor(keyring: MnemonicKeyring, wallet: CosmosWallet, derivationPath: string) { constructor(keyring: MnemonicKeyring, wallet: CosmosWallet) {
this.wallet = wallet this.wallet = wallet
this.keyring = keyring this.keyring = keyring
this.derivationPath = derivationPath
} }
static async init({ mnemonic, path }: IInitArguments) { static async init({ mnemonic, path }: IInitArguments) {
const keyring = await MnemonicKeyring.init({ mnemonic }) const keyring = await MnemonicKeyring.init({ mnemonic })
const derivationPath = path ?? DEFAULT_PATH const wallet = await CosmosWallet.init(keyring.getPrivateKey(path ?? DEFAULT_PATH))
const wallet = await CosmosWallet.init(keyring.getPrivateKey(derivationPath))
return new Cosmos(keyring, wallet, derivationPath) return new Cosmos(keyring, wallet)
} }
public getPublicKey() { public async getAccount(number = 0) {
return this.keyring.getPublicKey(this.derivationPath) 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) { if (mnemonic) {
wallet1 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/0" }) wallet1 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/0" })
wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" }) wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" })
const accounts1 = await wallet1.wallet.getAccounts() const account1 = await wallet1.getAccount()
const accounts2 = await wallet2.wallet.getAccounts() const account2 = await wallet2.getAccount()
address1 = accounts1[0].address address1 = account1.address
address2 = accounts2[0].address address2 = account2.address
} else { } else {
wallet1 = await Cosmos.init({ path: "m/44'/118'/0'/0/0" }) 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 // We can reuse same mnemonic for both wallets
wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" }) wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" })
const accounts1 = await wallet1.wallet.getAccounts() const account1 = await wallet1.getAccount()
const accounts2 = await wallet2.wallet.getAccounts() const account2 = await wallet2.getAccount()
address1 = accounts1[0].address address1 = account1.address
address2 = accounts2[0].address address2 = account2.address
// Don't store mnemonic in local storage in a production project! // Don't store mnemonic in local storage in a production project!
localStorage.setItem('WALLET_MNEMONIC', mnemonic) localStorage.setItem('WALLET_MNEMONIC', mnemonic)
} }