diff --git a/package.json b/package.json index 580506a..2fb5582 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "@headlessui/tailwindcss": "^0.1.3", "@headlessui/vue": "^1.7.12", "@heroicons/vue": "^2.0.16", - "@metalet/utxo-wallet-service": "0.3.18", + "@metalet/utxo-wallet-service": "0.3.19", "@okxweb3/crypto-lib": "^1.0.1", "@rollup/plugin-alias": "^4.0.3", "@scure/bip39": "^1.2.2", @@ -109,7 +109,7 @@ "vite-svg-loader": "^5.1.0", "vitest": "^0.34.3", "vue-svg-loader": "^0.16.0", - "vue-tsc": "^1.0.24", + "vue-tsc": "^2.1.2", "webextension-polyfill": "^0.10.0", "yarn": "^1.22.21" } diff --git a/src/components/GoToMerge.vue b/src/components/GoToMerge.vue new file mode 100644 index 0000000..dfabbba --- /dev/null +++ b/src/components/GoToMerge.vue @@ -0,0 +1,31 @@ + + + diff --git a/src/lib/actions/btc/sign-psbt.ts b/src/lib/actions/btc/sign-psbt.ts index 4a63031..376391b 100644 --- a/src/lib/actions/btc/sign-psbt.ts +++ b/src/lib/actions/btc/sign-psbt.ts @@ -83,7 +83,7 @@ export async function process({ // psbt.signInput(v.index, keyPair, v.sighashTypes) // }) for (let i = 0; i < options.toSignInputs.length; i++) { - const keyPair = wallet.getSigner(options!.toSignInputs![i].treehash) + const keyPair = wallet.getSigner({ treeHash: options!.toSignInputs![i].treehash }) const v = options.toSignInputs[i] psbt.signInput(v.index, keyPair, v.sighashTypes) } diff --git a/src/lib/actions/btc/transfer.ts b/src/lib/actions/btc/transfer.ts index c8d2f9a..9e85ead 100644 --- a/src/lib/actions/btc/transfer.ts +++ b/src/lib/actions/btc/transfer.ts @@ -28,10 +28,9 @@ export async function process(params: TransferParameters): Promise<{ txId: strin const utxos = await getBtcUtxos(wallet.getAddress(), wallet.getScriptType() === ScriptType.P2PKH, true) const { rawTx } = wallet.signTx(SignType.SEND, { - recipient: params.toAddress, - amount: new Decimal(params.satoshis).toNumber(), - feeRate, utxos, + feeRate, + outputs: [{ address: params.toAddress, satoshis: new Decimal(params.satoshis).toNumber() }], }) if (params.options?.noBroadcast) { diff --git a/src/lib/bridge-utils.ts b/src/lib/bridge-utils.ts index 9fa552b..6e8f2bd 100644 --- a/src/lib/bridge-utils.ts +++ b/src/lib/bridge-utils.ts @@ -540,24 +540,51 @@ export async function mintBtc( try { const { orderId, bridgeAddress, bridgeRuleServiceAddress, bridgeRuleServiceMintFee } = await createPrepayOrderMintBtc({ - amount: mintAmount, - originTokenId, - addressType: scriptType, publicKey, + originTokenId, publicKeySign, publicKeyReceive, publicKeyReceiveSign, + addressType: scriptType, + amount: mintAmount.toString(), }) + const outputs = [ + { + address: bridgeAddress, + satoshis: Number(mintAmount), + }, + ] + + console.log('bridgeRuleServiceAddress', bridgeRuleServiceAddress) + console.log('bridgeRuleServiceMintFee', bridgeRuleServiceMintFee) + + if (bridgeRuleServiceAddress && bridgeRuleServiceMintFee) { + outputs.push({ + address: bridgeRuleServiceAddress, + satoshis: bridgeRuleServiceMintFee, + }) + } + + console.log('outputs', outputs) + const utxos = await getBtcUtxos(btcWallet.getAddress(), btcWallet.getScriptType() === ScriptType.P2PKH, false) - const { rawTx, txId } = btcWallet.signTx(SignType.SEND, { + console.log('utxos', utxos) + console.log('feeRate', feeRate) + console.log('outputs', outputs) + + const { rawTx, txId, txInputs, txOutputs } = btcWallet.signTx(SignType.SEND, { utxos, - amount: mintAmount, - recipient: bridgeAddress, + outputs, feeRate, }) + console.log('txId', txId) + console.log('rawTx', rawTx) + console.log('txInputs', txInputs) + console.log('txOutputs', txOutputs) + await submitPrepayOrderMintBtc({ orderId, txHex: rawTx, @@ -565,6 +592,8 @@ export async function mintBtc( return { txId, recipient: bridgeAddress } } catch (error) { + console.log('error', error) + throw new Error((error as any).message || (error as any).msg) } } @@ -582,16 +611,17 @@ export async function mintMrc20( const publicKeyReceive = mvcWallet.getPublicKey().toString('hex') const publicKeyReceiveSign = mvcWallet.signMessage(publicKeyReceive, 'base64') - const createResp = await createPrepayOrderMintMrc20({ - amount: new Decimal(mintAmount).div(10 ** selectedPair.decimals).toFixed(), - originTokenId: selectedPair.originTokenId, - addressType: scriptType, - publicKey: publicKey, - publicKeySign: publicKeySign, - publicKeyReceive, - publicKeyReceiveSign: publicKeyReceiveSign, - }) - const { orderId, bridgeAddress } = createResp + const { orderId, bridgeAddress, bridgeRuleServiceAddress, bridgeRuleServiceMintFee } = + await createPrepayOrderMintMrc20({ + amount: new Decimal(mintAmount).div(10 ** selectedPair.decimals).toFixed(), + originTokenId: selectedPair.originTokenId, + addressType: scriptType, + publicKey: publicKey, + publicKeySign: publicKeySign, + publicKeyReceive, + publicKeyReceiveSign: publicKeyReceiveSign, + }) + const needRawTx = btcWallet.getScriptType() === ScriptType.P2PKH const utxos = await getBtcUtxos(btcWallet.getAddress(), needRawTx, false) const mrc20Utxos = await getMRC20Utxos(btcWallet.getAddress(), selectedPair.originTokenId, needRawTx) @@ -610,6 +640,13 @@ export async function mintMrc20( }, ]), revealAddr: bridgeAddress, + service: + bridgeRuleServiceAddress && bridgeRuleServiceMintFee + ? { + address: bridgeRuleServiceAddress, + satoshis: bridgeRuleServiceMintFee, + } + : undefined, }) const submitPrepayOrderMintDto = { diff --git a/src/lib/wallets/btc.ts b/src/lib/wallets/btc.ts index 52dfe87..a9b51c7 100644 --- a/src/lib/wallets/btc.ts +++ b/src/lib/wallets/btc.ts @@ -3,13 +3,10 @@ import { getBtcNetwork } from '../network' import { type UTXO } from '@/queries/utxos' import { getCurrentWallet } from '../wallet' import { createPayment } from '../bip32-deriver' -import { Account, getAddressType } from '@/lib/account' +import { Chain } from '@metalet/utxo-wallet-service' import { Payment, Psbt, Transaction } from 'bitcoinjs-lib' -import { Chain, ScriptType } from '@metalet/utxo-wallet-service' import { fetchBtcTxHex, broadcastBTCTx } from '@/queries/transaction' -// TODO: add safe utxo - function calculateFee(psbt: Psbt, feeRate: number): number { const tx = psbt.extractTransaction() @@ -107,11 +104,6 @@ export const split = async (feeRate: number, utxos: UTXO[]) => { let psbt = await buildPsbt(utxos, 0) psbt = await buildPsbt(utxos, Math.ceil(psbt.extractTransaction().virtualSize() * feeRate)) - // console.log('virtualSize', psbt.extractTransaction().virtualSize()) - - // console.log(Math.floor(new Decimal(68971 * 2).div(psbt.extractTransaction().virtualSize()).toNumber())) - - // return { txId: psbt.extractTransaction().getId() } const txId = await broadcastBTCTx(psbt.extractTransaction().toHex()) return { txId } @@ -155,6 +147,4 @@ export const merge = async (feeRate: number, utxos: UTXO[]) => { psbt = await buildPsbt(utxos, total.minus(fee)) return { txId: psbt.extractTransaction().getId(), total, fee, rawTx: psbt.extractTransaction().toHex() } - - // return await this.broadcast(psbt) } diff --git a/src/pages/authorize/BTCTransfer.vue b/src/pages/authorize/BTCTransfer.vue index a4081f7..acb80a5 100644 --- a/src/pages/authorize/BTCTransfer.vue +++ b/src/pages/authorize/BTCTransfer.vue @@ -45,10 +45,9 @@ watch( const address = wallet.getAddress() const utxos = await getBtcUtxos(address, wallet.getScriptType() === ScriptType.P2PKH, true) const { txInputs, txOutputs } = wallet.signTx(SignType.SEND, { - recipient: props.params.toAddress, - amount: new Decimal(props.params.satoshis).div(1e8).toNumber(), - feeRate: _currentFeeRate, utxos, + feeRate: _currentFeeRate, + outputs: [{ address: props.params.toAddress, satoshis: new Decimal(props.params.satoshis).toNumber() }], }) inputs.value = txInputs outputs.value = txOutputs diff --git a/src/pages/settings/components/BTCMerge.vue b/src/pages/settings/components/BTCMerge.vue index 1230f34..7160ebd 100644 --- a/src/pages/settings/components/BTCMerge.vue +++ b/src/pages/settings/components/BTCMerge.vue @@ -1,6 +1,6 @@