Files
bro-sdk/examples/bridgeFrom/BRC20.ts
c4605 f5b572eebd Squashed commit of the following:
commit a9488404b028aaeef5b7ab867a2392a969834fb0
Author: c4605 <bolasblack@gmail.com>
Date:   Mon May 12 23:36:07 2025 +0800

    docs: optimize deps

commit a50d05dca2
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Wed May 7 16:50:43 2025 +0200

    chore: address bro-sdk team review

commit 47fcf33525
Merge: d13a68d 2188881
Author: Sofía <36451685+sofinico@users.noreply.github.com>
Date:   Tue May 6 17:36:14 2025 +0200

    Merge branch 'snapshots/v0.5' into feat/update-v0.5-readme

commit d13a68d4f8
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue May 6 17:18:15 2025 +0200

    fix: add temporal patch to prevent `pnpm docs:watch` from failing

commit fef755490f
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue May 6 17:15:20 2025 +0200

    chore(docs): fix lint errors and remove unsed packages

commit f8739f7f17
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Wed Apr 30 16:17:59 2025 -0300

    docs: Update dev-instructions file.

commit 78de614697
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Wed Apr 30 16:06:12 2025 -0300

    docs: Update Readme to address example location

commit 4592f31a85
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Wed Apr 30 16:05:45 2025 -0300

    docs: Update `Bitcoin.ts` mock

commit 355bec1a9b
Merge: a3f0e9a 4fe7500
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Wed Apr 30 15:56:15 2025 -0300

    Merge branch 'snapshots/v0.5' of github.com:Brotocol-xyz/bro-sdk into feat/update-v0.5-readme

commit a3f0e9ab41
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Tue Apr 29 16:10:23 2025 -0300

    docs: Remove `docs/readmeCodeSnippets.ts`

commit 23f725bfa9
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Tue Apr 29 16:08:55 2025 -0300

    docs: Moved exampels into code-snippets, adressed comments in #20

commit f44e4382b7
Merge: 9809076 52d6e8c
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Tue Apr 29 11:48:05 2025 -0300

    Merged snapshot

commit 9809076820
Merge: f531587 57f8b37
Author: Sofía <36451685+sofinico@users.noreply.github.com>
Date:   Mon Apr 28 14:47:40 2025 +0200

    Merge pull request #2 from CoinFabrik/feat/update-v0.5-readme-runes-brc20

    Docs: Add code snippets for Runes and BRC20

commit 57f8b3790f
Author: Facundo Lerena <faculerena@gmail.com>
Date:   Fri Apr 25 15:29:48 2025 -0300

    Docs: Update code snippets

    Added Runes and BRC20 bridging examples

commit f5315877ee
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Fri Apr 25 17:47:04 2025 +0200

    docs: address peer-review [wip]

commit 12d8f150e9
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue Apr 22 14:07:26 2025 +0200

    docs: update readme basic operations and add table of contents

commit c52e9b834a
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue Apr 22 12:28:42 2025 +0200

    docs: update code snippets

    * Replace `getSupportedRoutes` with `getPossibleRoutes`
    * Ajust readmeCodeSnippets.ts to rebrand where applicable
    * Add note explaining `functionArgs` serialization design for bridge from Stacks
    * Add @types/node dev dependency for readmeCodeSnippets.ts

commit fcddf91a43
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue Apr 22 11:17:49 2025 +0200

    docs: update readme

    Changes include:

    * Removed static token support table and replaced it with SDK methods that dynamically retrieve supported tokens.
    * Replaced the supported chains table with a  bullet-point list for improved readability.
    * Added a "Basic Operations" section to introcude general usage patterns of the SDK.
    * Improved code snippets, with correspondace to the docs/readmeCodeSnippets.ts file for consistency.

commit 029c19922d
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue Apr 22 11:05:19 2025 +0200

    chore: add branded literal type doc

commit 13090aa374
Author: sofinico <nicolettisofia1@gmail.com>
Date:   Tue Apr 22 11:04:53 2025 +0200

    docs: create readme code snippets and dev instructions files

done #20
2025-05-23 11:59:50 +02:00

162 lines
4.6 KiB
TypeScript

// Bridge From BRC20
import {
GetConfirmedSpendableUTXOFn,
reselectSpendableUTXOsFactory,
UTXOBasic,
} from "../../../src/bitcoinHelpers"
import {
BroSDK,
KnownTokenId,
KnownChainId,
BridgeFromBRC20Input,
} from "../../../src/index"
import { Psbt, payments, networks } from "bitcoinjs-lib"
import axios from "axios"
const sdk = new BroSDK()
// For BRC20 provide the tick symbol
const brc20Token: KnownTokenId.BRC20Token = (await sdk.brc20TickToBRC20Token(
KnownChainId.BRC20.Mainnet,
"alex$",
))!
// For EVM tokens provide the contract address
const evmToken: KnownTokenId.EVMToken = (await sdk.evmAddressToEVMToken(
KnownChainId.EVM.Ethereum,
"0x31761a152F1e96F966C041291644129144233b0B",
))!
// Mock functions for key management
const getPublicKey = async (): Promise<string> => {
// This is a mock implementation
// In a real implementation, this would return the user's public key
return "02a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7"
}
const signTx = async (hex: string): Promise<string> => {
// This is a mock implementation
// In a real implementation, this would sign the transaction with the user's private key
return "mock_signature"
}
const publicKey = await getPublicKey()
const { address: senderAddress, output: scriptPubKey } = payments.p2wpkh({
pubkey: Buffer.from(publicKey, "hex"),
network: networks.bitcoin,
})
// Select UTXOs to spend
const reselectSpendableNetworkFeeUTXOs: BridgeFromBRC20Input["reselectSpendableNetworkFeeUTXOs"] =
async (satsToSend, lastTimeSelectedUTXOs) => {
// Example of available UTXOs from a Bitcoin node or API
const availableUTXOs: UTXOBasic[] = [
{
txId: "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
index: 0,
amount: 5000n,
},
{
txId: "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890",
index: 1,
amount: 3000n,
},
{
txId: "7890abcdef1234567890abcdef1234567890abcdef1234567890abcdef123456",
index: 2,
amount: 2000n,
},
]
const getUTXOSpendable: GetConfirmedSpendableUTXOFn = async (
utxo: UTXOBasic,
) => {
// For this example, we'll create a simple UTXOSpendable object
return {
...utxo,
scriptPubKey: scriptPubKey!,
addressType: "p2wpkh",
blockHeight: 800000n, // Example block height
}
}
// Create the reselect function with factory helper
const reselectFn = reselectSpendableUTXOsFactory(
availableUTXOs,
getUTXOSpendable,
)
return reselectFn(satsToSend, lastTimeSelectedUTXOs)
}
const sendTransaction: BridgeFromBRC20Input["sendTransaction"] = async tx => {
/**
* Implementation example for broadcasting a Bitcoin transaction with Axios
*/
const response = await axios.post("https://blockstream.info/api/tx", tx.hex, {
headers: { "Content-Type": "text/plain" },
})
return { txid: response.data }
}
const bridgeFromBRC20Input: BridgeFromBRC20Input = {
fromChain: KnownChainId.BRC20.Mainnet,
fromToken: brc20Token,
toChain: KnownChainId.EVM.Ethereum,
toToken: evmToken,
fromAddress: senderAddress!,
fromAddressScriptPubKey: scriptPubKey!,
toAddress: "0x31751a152F1e95F966C041291644129144233b0B",
inputInscriptionUTXO: {
txId: "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
index: 0,
amount: 1000n,
scriptPubKey: scriptPubKey!,
addressType: "p2wpkh",
},
networkFeeRate: 10n,
reselectSpendableNetworkFeeUTXOs,
networkFeeChangeAddress: senderAddress!,
networkFeeChangeAddressScriptPubKey: scriptPubKey!,
signPsbt: async tx => {
const psbt = Psbt.fromBuffer(Buffer.from(tx.psbt))
for (const index of tx.signBitcoinInputs) {
const signature = await signTx(psbt.toHex())
// Add the signature to the PSBT
psbt.updateInput(index, {
partialSig: [
{
pubkey: Buffer.from(await getPublicKey(), "hex"),
signature: Buffer.from(signature, "hex"),
},
],
})
}
for (const index of tx.signInscriptionInputs) {
const signature = await signTx(psbt.toHex())
// Add the signature to the PSBT
psbt.updateInput(index, {
partialSig: [
{
pubkey: Buffer.from(await getPublicKey(), "hex"),
signature: Buffer.from(signature, "hex"),
},
],
})
}
psbt.finalizeAllInputs()
return { psbt: psbt.toBuffer() }
},
sendTransaction,
}
const result = await sdk.bridgeFromBRC20(bridgeFromBRC20Input)
console.log("Bitcoin Transaction ID:", result.txid)