From 4b6f2276da7a2f51f59555ee7bf20d9c80473c16 Mon Sep 17 00:00:00 2001 From: Gancho Radkov <43912948+ganchoradkov@users.noreply.github.com> Date: Tue, 5 Nov 2024 12:34:24 +0200 Subject: [PATCH] fix: `sendPsbt` to use correct network and dust limit increase (#763) * fix: `sendPsbt` correct network and dust limit increase * refactor: sets `checkSegwitAlways` to true --- advanced/dapps/react-dapp-v2/src/chains/bip122.ts | 1 + .../react-dapp-v2/src/contexts/JsonRpcContext.tsx | 13 ++++++++++--- advanced/dapps/react-dapp-v2/src/helpers/bip122.ts | 13 ++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/advanced/dapps/react-dapp-v2/src/chains/bip122.ts b/advanced/dapps/react-dapp-v2/src/chains/bip122.ts index 88077f660..b1e490576 100644 --- a/advanced/dapps/react-dapp-v2/src/chains/bip122.ts +++ b/advanced/dapps/react-dapp-v2/src/chains/bip122.ts @@ -2,6 +2,7 @@ import { NamespaceMetadata, ChainMetadata, ChainsMap } from "../helpers"; export const BIP122_MAINNET = "000000000019d6689c085ae165831e93"; export const BIP122_TESTNET = "000000000933ea01ad0ee984209779ba"; +export const BIP122_DUST_LIMIT = "1001"; export const BtcChainData: ChainsMap = { [BIP122_MAINNET]: { diff --git a/advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx b/advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx index eb8d3631f..8891bdcb5 100644 --- a/advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx +++ b/advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx @@ -78,10 +78,12 @@ import { apiGetAddressUtxos, calculateChange, getAvailableBalanceFromUtxos, + isBip122Testnet, isOrdinalAddress, isValidBip122Signature, } from "../helpers/bip122"; import { getAddressFromAccount } from "@walletconnect/utils"; +import { BIP122_DUST_LIMIT } from "../chains/bip122"; /** * Types @@ -1709,7 +1711,7 @@ export function JsonRpcContextProvider({ const req = { account: address, recipientAddress: address, - amount: "550", + amount: BIP122_DUST_LIMIT, }; console.log("request", { method, @@ -1741,19 +1743,24 @@ export function JsonRpcContextProvider({ ): Promise => { const method = DEFAULT_BIP122_METHODS.BIP122_SIGN_PSBT; + const network = isBip122Testnet(chainId) + ? bitcoin.networks.testnet + : bitcoin.networks.bitcoin; + + console.log("network", isBip122Testnet(chainId), network); const utxos = (await apiGetAddressUtxos(address, chainId)) as IUTXO[]; if (!utxos || utxos.length === 0) { throw new Error("No UTXOs found for address: " + address); } const availableBalance = getAvailableBalanceFromUtxos(utxos); // in satoshis - const satoshisToTransfer = 550; + const satoshisToTransfer = parseInt(BIP122_DUST_LIMIT, 10); if (availableBalance < satoshisToTransfer) { throw new Error( "Insufficient balance: " + availableBalance + " satoshis" ); } - const network = bitcoin.networks.testnet; + const psbt = new bitcoin.Psbt({ network }); const utxosToSpend: any[] = []; diff --git a/advanced/dapps/react-dapp-v2/src/helpers/bip122.ts b/advanced/dapps/react-dapp-v2/src/helpers/bip122.ts index 8f178f1be..d743ab86e 100644 --- a/advanced/dapps/react-dapp-v2/src/helpers/bip122.ts +++ b/advanced/dapps/react-dapp-v2/src/helpers/bip122.ts @@ -4,6 +4,7 @@ import BitcoinMessage from "bitcoinjs-message"; import { convertHexToBase64 } from "./utilities"; import { IUTXO } from "./types"; import { BIP122_TESTNET } from "../chains/bip122"; + export async function apiGetBip122AccountBalance( address: string, chainId: string @@ -70,9 +71,19 @@ export async function isValidBip122Signature( ); } - return BitcoinMessage.verify(message, address, convertHexToBase64(signature)); + return BitcoinMessage.verify( + message, + address, + convertHexToBase64(signature), + undefined, + true + ); } export function isOrdinalAddress(address: string) { return address.startsWith("tb1p"); } + +export function isBip122Testnet(chainId: string) { + return chainId.includes(BIP122_TESTNET); +}