wip cosmos wallet util

This commit is contained in:
Ilja 2022-02-24 15:26:02 +02:00
parent 948f66aef2
commit b73855a561
5 changed files with 57 additions and 0 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
wallets/.DS_Store vendored

Binary file not shown.

View File

@ -1,4 +1,5 @@
import SettingsStore from '@/store/SettingsStore'
import { createOrRestoreCosmosWallet } from '@/utils/CosmosWalletUtil'
import { createOrRestoreEIP155Wallet } from '@/utils/EIP155WalletUtil'
import { createWalletConnectClient } from '@/utils/WalletConnectUtil'
import { useCallback, useEffect, useState } from 'react'
@ -9,6 +10,8 @@ export default function useInitialization() {
const onInitialize = useCallback(async () => {
try {
const { eip155Addresses } = createOrRestoreEIP155Wallet()
const { cosmosAddresses } = createOrRestoreCosmosWallet()
console.log(cosmosAddresses)
SettingsStore.setAddress(eip155Addresses[0])
await createWalletConnectClient()
setInitialized(true)

View File

@ -0,0 +1,51 @@
import { COSMOS_MAINNET_CHAINS } from '@/data/COSMOSData'
// @ts-expect-error
import { Cosmos } from '@cosmostation/cosmosjs/src/index'
export const wallet1 = new Cosmos(
'https://api.cosmos.network',
COSMOS_MAINNET_CHAINS['cosmos:cosmoshub-4'].chainId
)
wallet1.path("m/44'/118'/0'/0/0")
export const wallet2 = new Cosmos(
'https://api.cosmos.network',
COSMOS_MAINNET_CHAINS['cosmos:cosmoshub-4'].chainId
)
wallet1.path("m/44'/118'/0'/0/1")
export let cosmosWallets: Record<string, Cosmos>
export let cosmosAddresses: string[]
let address1: string
let address2: string
/**
* Utilities
*/
export function createOrRestoreCosmosWallet() {
const mnemonic = localStorage.getItem('WALLET_MNEMONIC')
if (mnemonic) {
address1 = wallet1.getAddress(mnemonic)
address2 = wallet2.getAddress(mnemonic)
} else {
// We can reuse same mnemonic for both wallets
const mnemonic = wallet1.getRandomMnemonic()
address1 = wallet1.getAddress(mnemonic)
address2 = wallet2.getAddress(mnemonic)
// Don't store mnemonic in local storage in a production project!
localStorage.setItem('WALLET_MNEMONIC', mnemonic)
}
cosmosWallets = {
[address1]: wallet1,
[address2]: wallet2
}
cosmosAddresses = Object.keys(cosmosWallets)
return {
cosmosWallets,
cosmosAddresses
}
}

View File

@ -6,6 +6,9 @@ export let eip155Addresses: string[]
let wallet1: Wallet
let wallet2: Wallet
/**
* Utilities
*/
export function createOrRestoreEIP155Wallet() {
const mnemonic = localStorage.getItem('WALLET_MNEMONIC')