Skip to content

Commit

Permalink
fix: sendPsbt to use correct network and dust limit increase (#763)
Browse files Browse the repository at this point in the history
* fix: `sendPsbt` correct network and dust limit increase

* refactor: sets `checkSegwitAlways` to true
  • Loading branch information
ganchoradkov authored Nov 5, 2024
1 parent 5aa7b75 commit 4b6f227
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions advanced/dapps/react-dapp-v2/src/chains/bip122.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down
13 changes: 10 additions & 3 deletions advanced/dapps/react-dapp-v2/src/contexts/JsonRpcContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1709,7 +1711,7 @@ export function JsonRpcContextProvider({
const req = {
account: address,
recipientAddress: address,
amount: "550",
amount: BIP122_DUST_LIMIT,
};
console.log("request", {
method,
Expand Down Expand Up @@ -1741,19 +1743,24 @@ export function JsonRpcContextProvider({
): Promise<IFormattedRpcResponse> => {
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[] = [];
Expand Down
13 changes: 12 additions & 1 deletion advanced/dapps/react-dapp-v2/src/helpers/bip122.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

0 comments on commit 4b6f227

Please sign in to comment.