Merge branch 'main' of github.com:WalletConnect/examples into main

This commit is contained in:
Ben Kremer 2022-03-15 13:46:07 +01:00
commit f676fd0d05
7 changed files with 108 additions and 111 deletions

View File

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

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

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

View File

@ -2,20 +2,30 @@ 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() {
public async getAddress() {
return await this.keypair.publicKey.toBase58()
}
@ -48,8 +58,12 @@ export class Solana {
await tx.sign(this.keypair)
const { signature } = tx.signatures[tx.signatures.length - 1]
if (!tx.signature) {
throw new Error('Missing signature!')
}
return { signature }
const bs58Signature = bs58.encode(tx.signature)
return { signature: bs58Signature }
}
}

View File

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

View File

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

View File

@ -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<string, Solana>
export let wallet1: SolanaLib
export let wallet2: SolanaLib
export let solanaWallets: Record<string, SolanaLib>
export let solanaAddresses: string[]
let address1: string
@ -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 = Solana.init(Uint8Array.from(secretArray1))
wallet2 = Solana.init(Uint8Array.from(secretArray2))
address1 = await wallet1.getAccount()
address2 = await wallet2.getAccount()
wallet1 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray1) })
wallet2 = SolanaLib.init({ secretKey: Uint8Array.from(secretArray2) })
} else {
wallet1 = Solana.init()
wallet2 = Solana.init()
address1 = await wallet1.getAccount()
address2 = await wallet2.getAccount()
wallet1 = SolanaLib.init({})
wallet2 = SolanaLib.init({})
// 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