feat: check fromAddressScriptPubKey/toAddressScriptPubKey validation

This commit is contained in:
c4605
2025-04-14 21:49:15 +02:00
parent 6042d0f27d
commit ea641f6053
3 changed files with 163 additions and 7 deletions

View File

@@ -1,8 +1,13 @@
import * as btc from "@scure/btc-signer"
import { equalBytes } from "@scure/btc-signer/utils"
import { broadcastRevealableTransaction } from "../bitcoinUtils/apiHelpers/broadcastRevealableTransaction"
import { createBitcoinPegInRecipients } from "../bitcoinUtils/apiHelpers/createBitcoinPegInRecipients"
import { createRevealTx } from "../bitcoinUtils/apiHelpers/createRevealTx"
import { UTXOSpendable, bitcoinToSatoshi } from "../bitcoinUtils/bitcoinHelpers"
import {
UTXOSpendable,
addressToScriptPubKey,
bitcoinToSatoshi,
} from "../bitcoinUtils/bitcoinHelpers"
import {
BitcoinAddress,
getBTCPegInAddress,
@@ -63,12 +68,12 @@ import {
getChainIdNetworkType,
} from "../utils/types/knownIds"
import { TransferProphet_Fee_Fixed } from "../utils/types/TransferProphet"
import {
ReselectSpendableUTXOsFn_Public,
reselectSpendableUTXOsFactory,
} from "./bridgeFromBitcoin"
import { ChainId, TokenId, isEVMAddress } from "./types"
import { SDKGlobalContext } from "./types.internal"
import {
reselectSpendableUTXOsFactory,
ReselectSpendableUTXOsFn_Public,
} from "./bridgeFromBitcoin"
export type BridgeFromBRC20Input_reselectSpendableNetworkFeeUTXOs =
ReselectSpendableUTXOsFn_Public
@@ -124,6 +129,54 @@ export async function bridgeFromBRC20(
): Promise<BridgeFromBRC20Output> {
const route = await checkRouteValid(ctx, isSupportedBRC20Route, info)
if (
!equalBytes(
info.fromAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.fromAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromBRC20"],
[
{
name: "fromAddressScriptPubKey",
expected: "the scriptPubKey of the fromAddress",
received: "invalid scriptPubKey",
},
],
)
}
if (info.toAddressScriptPubKey != null) {
if (
!equalBytes(
info.toAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.toAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromBRC20"],
[
{
name: "toAddressScriptPubKey",
expected: "the scriptPubKey of the toAddress",
received: "invalid scriptPubKey",
},
],
)
}
}
if (KnownChainId.isBRC20Chain(route.fromChain)) {
if (KnownChainId.isStacksChain(route.toChain)) {
if (

View File

@@ -1,9 +1,11 @@
import * as btc from "@scure/btc-signer"
import { equalBytes } from "@scure/btc-signer/utils"
import { broadcastRevealableTransaction } from "../bitcoinUtils/apiHelpers/broadcastRevealableTransaction"
import { createBitcoinPegInRecipients } from "../bitcoinUtils/apiHelpers/createBitcoinPegInRecipients"
import { createRevealTx } from "../bitcoinUtils/apiHelpers/createRevealTx"
import {
UTXOSpendable,
addressToScriptPubKey,
bitcoinToSatoshi,
excludeUTXOs,
sumUTXO,
@@ -110,6 +112,54 @@ export async function bridgeFromBitcoin(
): Promise<BridgeFromBitcoinOutput> {
const route = await checkRouteValid(ctx, isSupportedBitcoinRoute, info)
if (
!equalBytes(
info.fromAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.fromAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromBitcoin"],
[
{
name: "fromAddressScriptPubKey",
expected: "the scriptPubKey of the fromAddress",
received: "invalid scriptPubKey",
},
],
)
}
if (info.toAddressScriptPubKey != null) {
if (
!equalBytes(
info.toAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.toAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromBitcoin"],
[
{
name: "toAddressScriptPubKey",
expected: "the scriptPubKey of the toAddress",
received: "invalid scriptPubKey",
},
],
)
}
}
if (KnownChainId.isBitcoinChain(route.fromChain)) {
if (KnownChainId.isStacksChain(route.toChain)) {
if (

View File

@@ -1,9 +1,14 @@
import { getOutputDustThreshold } from "@c4/btc-utils"
import * as btc from "@scure/btc-signer"
import { equalBytes } from "@scure/btc-signer/utils"
import { broadcastRevealableTransaction } from "../bitcoinUtils/apiHelpers/broadcastRevealableTransaction"
import { createBitcoinPegInRecipients } from "../bitcoinUtils/apiHelpers/createBitcoinPegInRecipients"
import { createRevealTx } from "../bitcoinUtils/apiHelpers/createRevealTx"
import { UTXOSpendable, bitcoinToSatoshi } from "../bitcoinUtils/bitcoinHelpers"
import {
UTXOSpendable,
addressToScriptPubKey,
bitcoinToSatoshi,
} from "../bitcoinUtils/bitcoinHelpers"
import {
BitcoinAddress,
getBitcoinHardLinkageAddress,
@@ -59,8 +64,8 @@ import {
getChainIdNetworkType,
} from "../utils/types/knownIds"
import {
reselectSpendableUTXOsFactory,
ReselectSpendableUTXOsFn_Public,
reselectSpendableUTXOsFactory,
} from "./bridgeFromBitcoin"
import { getBridgeFeeOutput } from "./bridgeFromBRC20"
import {
@@ -135,6 +140,54 @@ export async function bridgeFromRunes(
): Promise<BridgeFromRunesOutput> {
const route = await checkRouteValid(ctx, isSupportedRunesRoute, info)
if (
!equalBytes(
info.fromAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.fromAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromRunes"],
[
{
name: "fromAddressScriptPubKey",
expected: "the scriptPubKey of the fromAddress",
received: "invalid scriptPubKey",
},
],
)
}
if (info.toAddressScriptPubKey != null) {
if (
!equalBytes(
info.toAddressScriptPubKey,
addressToScriptPubKey(
info.fromChain === KnownChainId.Bitcoin.Mainnet
? btc.NETWORK
: btc.TEST_NETWORK,
info.toAddress,
),
)
) {
throw new InvalidMethodParametersError(
["XLinkSDK", "bridgeFromRunes"],
[
{
name: "toAddressScriptPubKey",
expected: "the scriptPubKey of the toAddress",
received: "invalid scriptPubKey",
},
],
)
}
}
if (KnownChainId.isRunesChain(route.fromChain)) {
if (KnownChainId.isStacksChain(route.toChain)) {
if (