Create EIP155Lib for consistency with Cosmos and Solana
This commit is contained in:
parent
b324ed7b34
commit
0c2f1350e1
@ -47,10 +47,10 @@ export default class CosmosLib {
|
||||
return this.keyring.mnemonic
|
||||
}
|
||||
|
||||
public async getAccount() {
|
||||
public async getAddress() {
|
||||
const account = await this.directSigner.getAccounts()
|
||||
|
||||
return account[0]
|
||||
return account[0].address
|
||||
}
|
||||
|
||||
public async signDirect(address: string, signDoc: SignDoc) {
|
||||
|
49
wallets/react-wallet-v2/src/lib/EIP155Lib.ts
Normal file
49
wallets/react-wallet-v2/src/lib/EIP155Lib.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { providers, Wallet } from 'ethers'
|
||||
|
||||
/**
|
||||
* Types
|
||||
*/
|
||||
interface IInitArgs {
|
||||
mnemonic?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Library
|
||||
*/
|
||||
export default class EIP155Lib {
|
||||
wallet: Wallet
|
||||
|
||||
constructor(wallet: Wallet) {
|
||||
this.wallet = wallet
|
||||
}
|
||||
|
||||
static init({ mnemonic }: IInitArgs) {
|
||||
const wallet = mnemonic ? Wallet.fromMnemonic(mnemonic) : Wallet.createRandom()
|
||||
|
||||
return new EIP155Lib(wallet)
|
||||
}
|
||||
|
||||
getMnemonic() {
|
||||
return this.wallet.mnemonic.phrase
|
||||
}
|
||||
|
||||
getAddress() {
|
||||
return this.wallet.address
|
||||
}
|
||||
|
||||
signMessage(message: string) {
|
||||
return this.wallet.signMessage(message)
|
||||
}
|
||||
|
||||
_signTypedData(domain: any, types: any, data: any) {
|
||||
return this.wallet._signTypedData(domain, types, data)
|
||||
}
|
||||
|
||||
connect(provider: providers.JsonRpcProvider) {
|
||||
return this.wallet.connect(provider)
|
||||
}
|
||||
|
||||
signTransaction(transaction: providers.TransactionRequest) {
|
||||
return this.wallet.signTransaction(transaction)
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ export default class SolanaLib {
|
||||
return new SolanaLib(keypair)
|
||||
}
|
||||
|
||||
public async getAccount() {
|
||||
public async getAddress() {
|
||||
return await this.keypair.publicKey.toBase58()
|
||||
}
|
||||
|
||||
|
@ -18,22 +18,18 @@ export async function createOrRestoreCosmosWallet() {
|
||||
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 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('COSMOS_MNEMONIC_1', wallet1.getMnemonic())
|
||||
localStorage.setItem('COSMOS_MNEMONIC_2', wallet2.getMnemonic())
|
||||
}
|
||||
|
||||
address1 = await wallet1.getAddress()
|
||||
address2 = await wallet2.getAddress()
|
||||
|
||||
cosmosWallets = {
|
||||
[address1]: wallet1,
|
||||
[address2]: wallet2
|
||||
|
@ -1,30 +1,38 @@
|
||||
import { Wallet } from 'ethers'
|
||||
import EIP155Lib from '@/lib/EIP155Lib'
|
||||
|
||||
export let eip155Wallets: Record<string, Wallet>
|
||||
export let wallet1: EIP155Lib
|
||||
export let wallet2: EIP155Lib
|
||||
export let eip155Wallets: Record<string, EIP155Lib>
|
||||
export let eip155Addresses: string[]
|
||||
|
||||
let wallet1: Wallet
|
||||
let wallet2: Wallet
|
||||
let address1: string
|
||||
let address2: string
|
||||
|
||||
/**
|
||||
* Utilities
|
||||
*/
|
||||
export function createOrRestoreEIP155Wallet() {
|
||||
const mnemonic = localStorage.getItem('WALLET_MNEMONIC')
|
||||
const mnemonic1 = localStorage.getItem('EIP155_MNEMONIC_1')
|
||||
const mnemonic2 = localStorage.getItem('EIP155_MNEMONIC_2')
|
||||
|
||||
if (mnemonic) {
|
||||
wallet1 = Wallet.fromMnemonic(mnemonic, "m/44'/60'/0'/0/0")
|
||||
wallet2 = Wallet.fromMnemonic(mnemonic, "m/44'/60'/0'/0/1")
|
||||
if (mnemonic1 && mnemonic2) {
|
||||
wallet1 = EIP155Lib.init({ mnemonic: mnemonic1 })
|
||||
wallet2 = EIP155Lib.init({ mnemonic: mnemonic2 })
|
||||
} else {
|
||||
wallet1 = Wallet.createRandom()
|
||||
wallet2 = Wallet.fromMnemonic(wallet1.mnemonic.phrase, "m/44'/60'/0'/0/1")
|
||||
wallet1 = EIP155Lib.init({})
|
||||
wallet2 = EIP155Lib.init({})
|
||||
|
||||
// Don't store mnemonic in local storage in a production project!
|
||||
localStorage.setItem('WALLET_MNEMONIC', wallet1.mnemonic.phrase)
|
||||
localStorage.setItem('EIP155_MNEMONIC_1', wallet1.getMnemonic())
|
||||
localStorage.setItem('EIP155_MNEMONIC_2', wallet2.getMnemonic())
|
||||
}
|
||||
|
||||
address1 = wallet1.getAddress()
|
||||
address2 = wallet2.getAddress()
|
||||
|
||||
eip155Wallets = {
|
||||
[wallet1.address]: wallet1,
|
||||
[wallet2.address]: wallet2
|
||||
[address1]: wallet1,
|
||||
[address2]: wallet2
|
||||
}
|
||||
eip155Addresses = Object.keys(eip155Wallets)
|
||||
|
||||
|
@ -18,16 +18,12 @@ export async function createOrRestoreSolanaWallet() {
|
||||
if (secretKey1 && secretKey2) {
|
||||
const secretArray1: number[] = Object.values(JSON.parse(secretKey1))
|
||||
const secretArray2: number[] = Object.values(JSON.parse(secretKey2))
|
||||
|
||||
wallet1 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray1) })
|
||||
wallet2 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray2) })
|
||||
address1 = await wallet1.getAccount()
|
||||
address2 = await wallet2.getAccount()
|
||||
} else {
|
||||
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!
|
||||
localStorage.setItem(
|
||||
'SOLANA_SECRET_KEY_1',
|
||||
@ -39,6 +35,9 @@ export async function createOrRestoreSolanaWallet() {
|
||||
)
|
||||
}
|
||||
|
||||
address1 = await wallet1.getAddress()
|
||||
address2 = await wallet2.getAddress()
|
||||
|
||||
solanaWallets = {
|
||||
[address1]: wallet1,
|
||||
[address2]: wallet2
|
||||
|
Loading…
Reference in New Issue
Block a user