From b324ed7b34c2849bb2e7db2bc6a56c0a13ef1aeb Mon Sep 17 00:00:00 2001 From: Ilja Date: Tue, 15 Mar 2022 13:37:48 +0200 Subject: [PATCH] Cleanup --- wallets/react-wallet-v2/src/lib/EIP155.ts | 69 ------------------- .../src/lib/{Solana.ts => SolanaLib.ts} | 16 ++++- .../src/utils/SolanaWalletUtil.ts | 16 ++--- 3 files changed, 21 insertions(+), 80 deletions(-) delete mode 100644 wallets/react-wallet-v2/src/lib/EIP155.ts rename wallets/react-wallet-v2/src/lib/{Solana.ts => SolanaLib.ts} (85%) diff --git a/wallets/react-wallet-v2/src/lib/EIP155.ts b/wallets/react-wallet-v2/src/lib/EIP155.ts deleted file mode 100644 index c4c4fa1..0000000 --- a/wallets/react-wallet-v2/src/lib/EIP155.ts +++ /dev/null @@ -1,69 +0,0 @@ -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 MnemonicKeyring from 'mnemonic-keyring' - -/** - * Constants - */ -const DEFAULT_PATH = "m/44'/118'/0'/0/0" - -/** - * Types - */ -interface IInitArguments { - mnemonic?: string - path?: string - prefix?: string -} - -/** - * Utility - */ -export class EIP155 { - private keyring: MnemonicKeyring - private directSigner: DirectSecp256k1Wallet - private aminoSigner: Secp256k1Wallet - - constructor( - keyring: MnemonicKeyring, - directSigner: DirectSecp256k1Wallet, - aminoSigner: Secp256k1Wallet - ) { - this.directSigner = directSigner - this.keyring = keyring - this.aminoSigner = aminoSigner - } - - static async init({ mnemonic, path, prefix }: IInitArguments) { - const keyring = await MnemonicKeyring.init({ mnemonic }) - const privateKey = fromHex(keyring.getPrivateKey(path ?? DEFAULT_PATH)) - const chainPrefix = prefix ?? 'cosmos' - const directSigner = await DirectSecp256k1Wallet.fromKey(privateKey, chainPrefix) - const aminoSigner = await Secp256k1Wallet.fromKey(privateKey, chainPrefix) - - return new EIP155(keyring, directSigner, aminoSigner) - } - - public async getAccount(number = 0) { - const account = await this.directSigner.getAccounts() - - return account[number] - } - - public getMnemonic() { - return this.keyring.mnemonic - } - - public async signDirect(address: string, signDoc: 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/Solana.ts b/wallets/react-wallet-v2/src/lib/SolanaLib.ts similarity index 85% rename from wallets/react-wallet-v2/src/lib/Solana.ts rename to wallets/react-wallet-v2/src/lib/SolanaLib.ts index 140136f..aca412f 100644 --- a/wallets/react-wallet-v2/src/lib/Solana.ts +++ b/wallets/react-wallet-v2/src/lib/SolanaLib.ts @@ -2,17 +2,27 @@ import { Keypair, PublicKey, Transaction, TransactionInstructionCtorFields } fro import bs58 from 'bs58' import nacl from 'tweetnacl' -export class Solana { +/** + * Types + */ +interface IInitArguments { + secretKey?: Uint8Array +} + +/** + * Library + */ +export default class SolanaLib { keypair: Keypair constructor(keypair: Keypair) { this.keypair = keypair } - static init(secretKey?: Uint8Array) { + static init({ secretKey }: IInitArguments) { const keypair = secretKey ? Keypair.fromSecretKey(secretKey) : Keypair.generate() - return new Solana(keypair) + return new SolanaLib(keypair) } public async getAccount() { diff --git a/wallets/react-wallet-v2/src/utils/SolanaWalletUtil.ts b/wallets/react-wallet-v2/src/utils/SolanaWalletUtil.ts index 1ff70f1..d1d336c 100644 --- a/wallets/react-wallet-v2/src/utils/SolanaWalletUtil.ts +++ b/wallets/react-wallet-v2/src/utils/SolanaWalletUtil.ts @@ -1,8 +1,8 @@ -import { Solana } from '@/lib/Solana' +import SolanaLib from '@/lib/SolanaLib' -export let wallet1: Solana -export let wallet2: Solana -export let solanaWallets: Record +export let wallet1: SolanaLib +export let wallet2: SolanaLib +export let solanaWallets: Record export let solanaAddresses: string[] let address1: string @@ -19,13 +19,13 @@ export async function createOrRestoreSolanaWallet() { const secretArray1: number[] = Object.values(JSON.parse(secretKey1)) const secretArray2: number[] = Object.values(JSON.parse(secretKey2)) - wallet1 = Solana.init(Uint8Array.from(secretArray1)) - wallet2 = Solana.init(Uint8Array.from(secretArray2)) + wallet1 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray1) }) + wallet2 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray2) }) address1 = await wallet1.getAccount() address2 = await wallet2.getAccount() } else { - wallet1 = Solana.init() - wallet2 = Solana.init() + wallet1 = SolanaLib.init({}) + wallet2 = SolanaLib.init({}) address1 = await wallet1.getAccount() address2 = await wallet2.getAccount() // Don't store secretKey in local storage in a production project!