diff --git a/wallets/react-wallet-v2/src/lib/CosmosLib.ts b/wallets/react-wallet-v2/src/lib/CosmosLib.ts new file mode 100644 index 0000000..ca0afc4 --- /dev/null +++ b/wallets/react-wallet-v2/src/lib/CosmosLib.ts @@ -0,0 +1,66 @@ +import { Secp256k1Wallet, StdSignDoc } from '@cosmjs/amino' +import { fromHex } from '@cosmjs/encoding' +import { DirectSecp256k1Wallet, makeSignBytes } from '@cosmjs/proto-signing' +// @ts-expect-error +import { SignDoc } from '@cosmjs/proto-signing/build/codec/cosmos/tx/v1beta1/tx' +import Keyring from 'mnemonic-keyring' + +/** + * Constants + */ +const DEFAULT_PATH = "m/44'/118'/0'/0/0" +const DEFAULT_PREFIX = 'cosmos' + +/** + * Types + */ +interface IInitArguments { + mnemonic?: string + path?: string + prefix?: string +} + +/** + * Library + */ +export default class CosmosLib { + private keyring: Keyring + private directSigner: DirectSecp256k1Wallet + private aminoSigner: Secp256k1Wallet + + constructor(keyring: Keyring, directSigner: DirectSecp256k1Wallet, aminoSigner: Secp256k1Wallet) { + this.directSigner = directSigner + this.keyring = keyring + this.aminoSigner = aminoSigner + } + + static async init({ mnemonic, path, prefix }: IInitArguments) { + const keyring = await Keyring.init({ mnemonic: mnemonic ?? Keyring.generateMnemonic() }) + const privateKey = fromHex(keyring.getPrivateKey(path ?? DEFAULT_PATH)) + const directSigner = await DirectSecp256k1Wallet.fromKey(privateKey, prefix ?? DEFAULT_PREFIX) + const aminoSigner = await Secp256k1Wallet.fromKey(privateKey, prefix ?? DEFAULT_PREFIX) + + return new CosmosLib(keyring, directSigner, aminoSigner) + } + + public getMnemonic() { + return this.keyring.mnemonic + } + + public async getAccount() { + const account = await this.directSigner.getAccounts() + + return account[0] + } + + public async signDirect(address: string, signDoc: SignDoc) { + console.log(signDoc) + const signDocBytes = makeSignBytes(signDoc) + // @ts-expect-error + return await this.directSigner.signDirect(address, signDocBytes) + } + + public async signAmino(address: string, signDoc: StdSignDoc) { + return await this.aminoSigner.signAmino(address, signDoc) + } +} diff --git a/wallets/react-wallet-v2/src/lib/Cosmos.ts b/wallets/react-wallet-v2/src/lib/EIP155.ts similarity index 95% rename from wallets/react-wallet-v2/src/lib/Cosmos.ts rename to wallets/react-wallet-v2/src/lib/EIP155.ts index 5e423f4..4258d97 100644 --- a/wallets/react-wallet-v2/src/lib/Cosmos.ts +++ b/wallets/react-wallet-v2/src/lib/EIP155.ts @@ -22,7 +22,7 @@ interface IInitArguments { /** * Utility */ -export class Cosmos { +export class EIP155 { private keyring: MnemonicKeyring private directSigner: DirectSecp256k1Wallet private aminoSigner: Secp256k1Wallet @@ -44,7 +44,7 @@ export class Cosmos { const directSigner = await DirectSecp256k1Wallet.fromKey(privateKey, chainPrefix) const aminoSigner = await Secp256k1Wallet.fromKey(privateKey, chainPrefix) - return new Cosmos(keyring, directSigner, aminoSigner) + return new EIP155(keyring, directSigner, aminoSigner) } public async getAccount(number = 0) { diff --git a/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts b/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts index 5d988e7..fd63298 100644 --- a/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts +++ b/wallets/react-wallet-v2/src/utils/CosmosWalletUtil.ts @@ -1,8 +1,8 @@ -import { Cosmos } from '@/lib/Cosmos' +import CosmosLib from '@/lib/CosmosLib' -export let wallet1: Cosmos -export let wallet2: Cosmos -export let cosmosWallets: Record +export let wallet1: CosmosLib +export let wallet2: CosmosLib +export let cosmosWallets: Record export let cosmosAddresses: string[] let address1: string @@ -12,26 +12,26 @@ let address2: string * Utilities */ export async function createOrRestoreCosmosWallet() { - const mnemonic = localStorage.getItem('WALLET_MNEMONIC') + const mnemonic1 = localStorage.getItem('COSMOS_MNEMONIC_1') + const mnemonic2 = localStorage.getItem('COSMOS_MNEMONIC_2') - 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" }) + if (mnemonic1 && mnemonic2) { + wallet1 = await CosmosLib.init({ mnemonic: mnemonic1 }) + wallet2 = await CosmosLib.init({ mnemonic: mnemonic2 }) 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.getMnemonic() - // We can reuse same mnemonic for both wallets - wallet2 = await Cosmos.init({ mnemonic, path: "m/44'/118'/0'/0/1" }) + wallet1 = await CosmosLib.init({}) + wallet2 = await CosmosLib.init({}) 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) + localStorage.setItem('COSMOS_MNEMONIC_1', wallet1.getMnemonic()) + localStorage.setItem('COSMOS_MNEMONIC_2', wallet2.getMnemonic()) } cosmosWallets = {