Skip to content

Commit

Permalink
chore: remove reference to account.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AricRedemption committed Oct 26, 2023
1 parent 8ff12b0 commit e883d47
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 80 deletions.
2 changes: 2 additions & 0 deletions src/data/extension-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
updateBtcPath,
migrateV2,
needsMigrationV2,
getAddressType,
} from '@/lib/account'

export default {
Expand All @@ -17,6 +18,7 @@ export default {
addAccount,
getAddress,
getPrivateKey,
getAddressType,
updateBtcPath,
migrateV2,
needsMigrationV2,
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/btc/sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ECPairFactory from 'ecpair'
import { crypto } from 'bitcoinjs-lib'
import { getPrivateKey } from '@/lib/account'
// import { getPrivateKey } from '@/lib/account'
import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs'
import { raise } from '@/lib/helpers'
import { createEmit } from '@/lib/emitters'

const ECPair = ECPairFactory(ecc)

Expand All @@ -13,7 +14,8 @@ export async function process(message: string): Promise<string> {
throw new TypeError('The message to be signed must be a string.')
}

const privateKey = await getPrivateKey('btc')
// const privateKey = await getPrivateKey('btc')
const privateKey = await createEmit<string>('getPrivateKey')('btc')
const keyPair = ECPair.fromWIF(privateKey)
const hash = crypto.sha256(Buffer.from(message))
const signature = keyPair.sign(hash)
Expand Down
37 changes: 18 additions & 19 deletions src/lib/actions/btc/sign-psbt.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import ECPairFactory from 'ecpair'
import { getPrivateKey, getAddressType, getPublicKey, getAddress } from '@/lib/account'
import bitcoin, { address as PsbtAddress, networks } from 'bitcoinjs-lib'
// import { getPrivateKey, getAddressType, getPublicKey, getAddress } from '@/lib/account'
import { Psbt, payments, address as PsbtAddress, networks, Transaction } from 'bitcoinjs-lib'
import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs'
import { getNetwork } from '@/lib/network'
import { createEmit } from '@/lib/emitters'

const ECPair = ECPairFactory(ecc)

Expand Down Expand Up @@ -34,15 +35,14 @@ export interface SignPsbtOptions {
toSignInputs?: UserToSignInput[]
}

export async function process(
psbt: bitcoin.Psbt,
toSignInputs: ToSignInput[],
autoFinalized: boolean
): Promise<bitcoin.Psbt> {
export async function process(psbt: Psbt, toSignInputs: ToSignInput[], autoFinalized: boolean): Promise<Psbt> {
const networkType = await getNetwork()
const pubkey = await getPublicKey('btc')
const addressType = await getAddressType('btc')
const privateKey = await getPrivateKey('btc')
// const pubkey = await getPublicKey('btc')
const pubkey = await createEmit<string>('getPublicKey')('btc')
// const addressType = await getAddressType('btc')
const addressType = await createEmit<string>('getAddressType')('btc')
// const privateKey = await getPrivateKey('btc')
const privateKey = await createEmit<string>('getPrivateKey')('btc')
const psbtNetwork = networkType === 'mainnet' ? networks.bitcoin : networks.testnet

const keyPair = ECPair.fromWIF(privateKey)
Expand All @@ -60,7 +60,7 @@ export async function process(
// Special measures taken for compatibility with certain applications.
if (isNotSigned && isP2TR && lostInternalPubkey) {
const tapInternalKey = toXOnly(Buffer.from(pubkey, 'hex'))
const { output } = bitcoin.payments.p2tr({
const { output } = payments.p2tr({
internalPubkey: tapInternalKey,
network: psbtNetwork,
})
Expand All @@ -85,9 +85,11 @@ export async function process(
return psbt
}

const formatOptionsToSignInputs = async (_psbt: string | bitcoin.Psbt, options?: SignPsbtOptions) => {
const pubkey = await getPublicKey('btc')
const btcAddress = await getAddress('btc')
const formatOptionsToSignInputs = async (_psbt: string | Psbt, options?: SignPsbtOptions) => {
// const pubkey = await getPublicKey('btc')
const pubkey = await createEmit<string>('getPublicKey')('btc')
// const btcAddress = await getAddress('btc')
const btcAddress = await createEmit<string>('getAddress')('btc')

let toSignInputs: ToSignInput[] = []
if (options && options.toSignInputs) {
Expand Down Expand Up @@ -122,18 +124,15 @@ const formatOptionsToSignInputs = async (_psbt: string | bitcoin.Psbt, options?:
const networkType = await getNetwork()
const psbtNetwork = networkType === 'mainnet' ? networks.bitcoin : networks.testnet

const psbt =
typeof _psbt === 'string'
? bitcoin.Psbt.fromHex(_psbt as string, { network: psbtNetwork })
: (_psbt as bitcoin.Psbt)
const psbt = typeof _psbt === 'string' ? Psbt.fromHex(_psbt as string, { network: psbtNetwork }) : (_psbt as Psbt)
psbt.data.inputs.forEach((v, index) => {
let script: any = null
let value = 0
if (v.witnessUtxo) {
script = v.witnessUtxo.script
value = v.witnessUtxo.value
} else if (v.nonWitnessUtxo) {
const tx = bitcoin.Transaction.fromBuffer(v.nonWitnessUtxo)
const tx = Transaction.fromBuffer(v.nonWitnessUtxo)
const output = tx.outs[psbt.txInputs[index].index]
script = output.script
value = output.value
Expand Down
10 changes: 7 additions & 3 deletions src/lib/actions/connect.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { getAddress, getCurrentAccount } from '../account'
import { Account } from '../account'
import connector from '../connector'
import { createEmit } from '@/lib/emitters'
// import { getAddress, getCurrentAccount } from '../account'

export async function process(params: any, host: string) {
const account = await getCurrentAccount()
// const account = await getCurrentAccount()
const account = await createEmit<Account>('getCurrentAccount')()

if (!account) {
return { address: '', txid: '' }
}

await connector.connect(account.id, host)

const address = await getAddress()
// const address = await getAddress()
const address = await createEmit<string>('getAddress')()

return { address }
}
7 changes: 5 additions & 2 deletions src/lib/actions/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { getCurrentAccount } from '../account'
// import { getCurrentAccount } from '../account'
import { Account } from '../account'
import connector from '../connector'
import { createEmit } from '@/lib/emitters'

export async function process(params: any, host: string) {
const account = await getCurrentAccount()
// const account = await getCurrentAccount()
const account = await createEmit<Account>('getCurrentAccount')()
if (!account) {
return { address: '', txid: '' }
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/ecies-decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { mvc } from 'meta-contract'
import { getPrivateKey } from '../account'
// import { getPrivateKey } from '../account'
import { eciesDecrypt } from '../crypto'
import { createEmit } from '../emitters'

export async function process(params: any, host: string) {
const { encrypted } = params

const wif = await getPrivateKey()
// const wif = await getPrivateKey()
const wif = await createEmit<string>('getPrivateKey')()
const privateKeyObj = mvc.PrivateKey.fromWIF(wif)

const message = eciesDecrypt(encrypted, privateKeyObj)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/ecies-encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { mvc } from 'meta-contract'
import { getCurrentAccount, getPrivateKey } from '../account'
// import { getCurrentAccount, getPrivateKey } from '../account'
import { eciesEncrypt } from '../crypto'
import { createEmit } from '../emitters'

export async function process(params: any, host: string) {
const { message } = params

const wif = await getPrivateKey()
// const wif = await getPrivateKey()
const wif = await createEmit<string>('getPrivateKey')()
const privateKeyObj = mvc.PrivateKey.fromWIF(wif)

const encrypted = eciesEncrypt(message, privateKeyObj)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/merge.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { API_NET, API_TARGET, Wallet } from 'meta-contract'
import { getNetwork } from '../network'
import { getAddress, getPrivateKey } from '../account'
// import { getAddress, getPrivateKey } from '../account'
import { FEEB } from '@/data/config'
import { getApiHost } from '../host'
import { createEmit } from '../emitters'

export async function process() {
const network: API_NET = (await getNetwork()) as API_NET
const purse = await getPrivateKey("mvc")
// const purse = await getPrivateKey("mvc")
const purse = await createEmit<string>('getPrivateKey')('mvc')
const apiHost = await getApiHost()

const wallet = new Wallet(purse, network, FEEB, API_TARGET.MVC, apiHost)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/sign-message.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { mvc } from 'meta-contract'

import { getPrivateKey } from '../account'
// import { getPrivateKey } from '../account'
import { signMessage } from '../crypto'
import { createEmit } from '../emitters'

export async function process(params: any, host: string) {
const wif = await getPrivateKey()
// const wif = await getPrivateKey()
const wif = await createEmit<string>('getPrivateKey')()
const privateKeyObj = mvc.PrivateKey.fromWIF(wif)
const signature = signMessage(params.message, privateKeyObj, params.encoding)

Expand Down
7 changes: 5 additions & 2 deletions src/lib/actions/sign-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { getAddress, getCurrentAccount } from '../account'
// import { getAddress, getCurrentAccount } from '../account'
import { Account } from '../account'
import { signTransaction } from '../crypto'
import { createEmit } from '../emitters'
import { getNetwork } from '../network'

export async function process(params: any, host: string) {
const account = await getCurrentAccount()
const network = await getNetwork()
// const account = await getCurrentAccount()
const account = await createEmit<Account>('getCurrentAccount')()

const signature = await signTransaction(account!, network, params.transaction)

Expand Down
7 changes: 5 additions & 2 deletions src/lib/actions/sign-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { getCurrentAccount } from '../account'
// import { getCurrentAccount } from '../account'
import { Account } from '../account'
import { signTransactions } from '../crypto'
import { createEmit } from '../emitters'
import { getNetwork } from '../network'

export async function process(params: any, host: string) {
const account = await getCurrentAccount()
const network = await getNetwork()
// const account = await getCurrentAccount()
const account = await createEmit<Account>('getCurrentAccount')()

const signingTransactions = params.transactions
const signedTransactions = await signTransactions(account!, network, signingTransactions)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/switch-network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getAddress } from '../account'
// import { getAddress } from '../account'
import { createEmit } from '../emitters'
import { setNetwork, getNetwork } from '../network'

export async function process(params: any, host: string) {
Expand All @@ -12,7 +13,8 @@ export async function process(params: any, host: string) {
switched = 'mainnet'
}

const address = await getAddress()
// const address = await getAddress()
const address = await createEmit<string>('getAddress')()

return {
status: 'ok',
Expand Down
12 changes: 8 additions & 4 deletions src/lib/actions/transfer-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { API_NET, API_TARGET, FtManager } from 'meta-contract'
import { getNetwork } from '../network'
import { getCurrentAccount, getAddress, getPrivateKey } from '../account'
// import { getCurrentAccount, getAddress, getPrivateKey } from '../account'
import { FEEB } from '@/data/config'
import { createEmit } from '@/lib/emitters'
import { METASV_HOST, METASV_TESTNET_HOST } from '@/data/hosts'

export async function process({
Expand All @@ -17,7 +18,8 @@ export async function process({
}[]
}) {
const network: API_NET = (await getNetwork()) as API_NET
const purse = await getPrivateKey()
// const purse = await getPrivateKey()
const purse = await createEmit<string>('getPrivateKey')()
const apiHost = network === API_NET.MAIN ? METASV_HOST : METASV_TESTNET_HOST

const ftManager = new FtManager({
Expand All @@ -28,7 +30,8 @@ export async function process({
apiHost,
})
// Pick the largest utxo from wallet to pay the transaction
const selfAddress = await getAddress()
// const selfAddress = await getAddress()
const selfAddress = await createEmit<string>('getAddress')()
const largestUtxo = await ftManager.api
.getUnspents(selfAddress)
.then((utxos) => {
Expand Down Expand Up @@ -69,7 +72,8 @@ export async function estimate({
}[]
}) {
const network: API_NET = (await getNetwork()) as API_NET
const purse = await getPrivateKey()
// const purse = await getPrivateKey()
const purse = await createEmit<string>('getPrivateKey')()
const apiHost = network === API_NET.MAIN ? METASV_HOST : METASV_TESTNET_HOST

const ftManager = new FtManager({
Expand Down
12 changes: 8 additions & 4 deletions src/lib/actions/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { API_NET, API_TARGET, FtManager, TxDecoder, Wallet, mvc } from 'meta-contract'
import { API_NET, API_TARGET, FtManager, Wallet, mvc } from 'meta-contract'
import { getNetwork } from '../network'
import { getAddress, getPrivateKey } from '../account'
// import { getAddress, getPrivateKey } from '../account'
import { FEEB } from '@/data/config'
import { getApiHost } from '../host'
import { createEmit } from '@/lib/emitters'

export type Receiver = {
address: string
Expand All @@ -16,8 +17,11 @@ export type TransferTask = {
export async function process({ tasks, broadcast = true }: { tasks: TransferTask[]; broadcast?: boolean }) {
console.log('here')
const network: API_NET = (await getNetwork()) as API_NET
const purse = await getPrivateKey("mvc")
const address = (await getAddress())!
// const purse = await getPrivateKey("mvc")
const purse = await createEmit<string>('getPrivateKey')()

// const address = (await getAddress())!
const address = await createEmit<string>('getAddress')()
const apiHost = await getApiHost()

const wallet = new Wallet(purse, network, FEEB, API_TARGET.MVC, apiHost)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/actions/verify-signature.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { mvc } from 'meta-contract'
import { getPrivateKey } from '../account'
// import { getPrivateKey } from '../account'
import { verifySignature } from '../crypto'
import { createEmit } from '@/lib/emitters'

export async function process(params: any, host: string) {
const { message, signature, encoding } = params

const wif = await getPrivateKey()
// const wif = await getPrivateKey()
const wif = await createEmit<string>('getPrivateKey')()
const publicKey = mvc.PrivateKey.fromWIF(wif).toPublicKey()

const verified = verifySignature(message, signature, publicKey, encoding)
Expand Down
6 changes: 5 additions & 1 deletion src/lib/network.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref } from 'vue'
import storage from './storage'
import { networks } from 'bitcoinjs-lib'
import { createEmit } from '@/data/event-actions'

export type Network = 'mainnet' | 'testnet'
Expand All @@ -10,9 +11,12 @@ export async function setNetwork(_network: Network) {
await storage.set('network', _network)
network.value = _network
createEmit('networkChanged')(_network)
console.log("createEmit('networkChanged')", _network)
}

export async function getNetwork(): Promise<Network> {
return network.value
}

export async function getBtcNetwork(): Promise<Network> {
return network.value
}
23 changes: 0 additions & 23 deletions src/pages/Dev.vue

This file was deleted.

Loading

0 comments on commit e883d47

Please sign in to comment.