Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sendPsbt to use correct network and dust limit increase #763

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}