From b73855a561297a829a681f4a998e544866de5909 Mon Sep 17 00:00:00 2001 From: Ilja Date: Thu, 24 Feb 2022 15:26:02 +0200 Subject: [PATCH] wip cosmos wallet util --- .DS_Store | Bin 6148 -> 6148 bytes wallets/.DS_Store | Bin 6148 -> 6148 bytes .../src/hooks/useInitialization.ts | 3 ++ .../src/utils/CosmosWalletUtil.ts | 51 ++++++++++++++++++ .../src/utils/EIP155WalletUtil.ts | 3 ++ 5 files changed, 57 insertions(+) create mode 100644 wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts diff --git a/.DS_Store b/.DS_Store index 2a41f050f747665fde977308ee6b4d932ee283b2..e4b1ad821c844583560d4656f78af63ea3c2dad3 100644 GIT binary patch delta 20 bcmZoMXffCj$jokGsiRyCIL|ZM@a@Q delta 20 bcmZoMXffFEmWkcKKu5vE!g%u+CIL|ZM$!f- diff --git a/wallets/react-wallet-v2/src/hooks/useInitialization.ts b/wallets/react-wallet-v2/src/hooks/useInitialization.ts index 65c8a3f..1f677a0 100644 --- a/wallets/react-wallet-v2/src/hooks/useInitialization.ts +++ b/wallets/react-wallet-v2/src/hooks/useInitialization.ts @@ -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) diff --git a/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts b/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts new file mode 100644 index 0000000..9c4e46f --- /dev/null +++ b/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts @@ -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 +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 + } +} diff --git a/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts b/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts index 65355ec..fa212f8 100644 --- a/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts +++ b/wallets/react-wallet-v2/src/utils/EIP155WalletUtil.ts @@ -6,6 +6,9 @@ export let eip155Addresses: string[] let wallet1: Wallet let wallet2: Wallet +/** + * Utilities + */ export function createOrRestoreEIP155Wallet() { const mnemonic = localStorage.getItem('WALLET_MNEMONIC')