Refactor CosmosLib
This commit is contained in:
parent
cdbf9af7a6
commit
4d593758e7
66
wallets/react-wallet-v2/src/lib/CosmosLib.ts
Normal file
66
wallets/react-wallet-v2/src/lib/CosmosLib.ts
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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) {
|
@ -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<string, Cosmos>
|
||||
export let wallet1: CosmosLib
|
||||
export let wallet2: CosmosLib
|
||||
export let cosmosWallets: Record<string, CosmosLib>
|
||||
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 = {
|
||||
|
Loading…
Reference in New Issue
Block a user