This commit is contained in:
Ilja 2022-03-15 13:37:48 +02:00
parent dd81e04445
commit b324ed7b34
3 changed files with 21 additions and 80 deletions

View File

@ -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)
}
}

View File

@ -2,17 +2,27 @@ import { Keypair, PublicKey, Transaction, TransactionInstructionCtorFields } fro
import bs58 from 'bs58' import bs58 from 'bs58'
import nacl from 'tweetnacl' import nacl from 'tweetnacl'
export class Solana { /**
* Types
*/
interface IInitArguments {
secretKey?: Uint8Array
}
/**
* Library
*/
export default class SolanaLib {
keypair: Keypair keypair: Keypair
constructor(keypair: Keypair) { constructor(keypair: Keypair) {
this.keypair = keypair this.keypair = keypair
} }
static init(secretKey?: Uint8Array) { static init({ secretKey }: IInitArguments) {
const keypair = secretKey ? Keypair.fromSecretKey(secretKey) : Keypair.generate() const keypair = secretKey ? Keypair.fromSecretKey(secretKey) : Keypair.generate()
return new Solana(keypair) return new SolanaLib(keypair)
} }
public async getAccount() { public async getAccount() {

View File

@ -1,8 +1,8 @@
import { Solana } from '@/lib/Solana' import SolanaLib from '@/lib/SolanaLib'
export let wallet1: Solana export let wallet1: SolanaLib
export let wallet2: Solana export let wallet2: SolanaLib
export let solanaWallets: Record<string, Solana> export let solanaWallets: Record<string, SolanaLib>
export let solanaAddresses: string[] export let solanaAddresses: string[]
let address1: string let address1: string
@ -19,13 +19,13 @@ export async function createOrRestoreSolanaWallet() {
const secretArray1: number[] = Object.values(JSON.parse(secretKey1)) const secretArray1: number[] = Object.values(JSON.parse(secretKey1))
const secretArray2: number[] = Object.values(JSON.parse(secretKey2)) const secretArray2: number[] = Object.values(JSON.parse(secretKey2))
wallet1 = Solana.init(Uint8Array.from(secretArray1)) wallet1 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray1) })
wallet2 = Solana.init(Uint8Array.from(secretArray2)) wallet2 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray2) })
address1 = await wallet1.getAccount() address1 = await wallet1.getAccount()
address2 = await wallet2.getAccount() address2 = await wallet2.getAccount()
} else { } else {
wallet1 = Solana.init() wallet1 = SolanaLib.init({})
wallet2 = Solana.init() wallet2 = SolanaLib.init({})
address1 = await wallet1.getAccount() address1 = await wallet1.getAccount()
address2 = await wallet2.getAccount() address2 = await wallet2.getAccount()
// Don't store secretKey in local storage in a production project! // Don't store secretKey in local storage in a production project!