Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
fix: Made OP_Return as last output, fixed type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelord007 committed Jun 30, 2024
1 parent 076022d commit 54f4936
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
27 changes: 20 additions & 7 deletions app/btcintegration/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ interface ConnectedAddressData {
pubKey: string
}

export type Params = {
contract: string
message: string
amount: number
tss: string
}

declare global {
interface Window {
unisat: any
}
}

const BtcIntegration = () => {
const [contractAddress, setContractAddress] = useState("")
const [message, setMessage] = useState("")
const [amount, setAmount] = useState()
const [amount, setAmount] = useState<number | undefined>()
const [selectedWallet, setSelectedWallet] = useState<Wallet>("XDefi")

const sendTransaction = async () => {
Expand Down Expand Up @@ -50,7 +63,7 @@ const BtcIntegration = () => {
}
}

const callXDefi = async (params) => {
const callXDefi = async (params: Params) => {
if (!window.xfi) return alert("XDEFI wallet not installed")
const wallet = window.xfi
window.xfi.bitcoin.changeNetwork("testnet")
Expand All @@ -71,7 +84,7 @@ const BtcIntegration = () => {
},
],
}
window.xfi.bitcoin.request(tx, (err, res) => {
window.xfi.bitcoin.request(tx, (err: Error, res: Response) => {
if (err) {
return alert(`Couldn't send transaction, ${JSON.stringify(err)}`)
} else if (res) {
Expand All @@ -80,7 +93,7 @@ const BtcIntegration = () => {
})
}

const callUniSat = async (params) => {
const callUniSat = async (params: Params) => {
if (!window.unisat) return alert("Unisat wallet not installed")
try {
await window.unisat.requestAccounts()
Expand All @@ -94,7 +107,7 @@ const BtcIntegration = () => {
}
}

const callXverse = async (params) => {
const callXverse = async (params: Params) => {
const response = await Wallet.request("getAccounts", {
purposes: [AddressPurpose.Payment],
message: "Test app wants to know your addresses!",
Expand Down Expand Up @@ -128,7 +141,7 @@ const BtcIntegration = () => {
type="number"
value={amount}
onChange={(e) => {
setAmount(e.target.value)
setAmount(Number(e.target.value))
}}
placeholder="0"
/>
Expand Down Expand Up @@ -159,7 +172,7 @@ const BtcIntegration = () => {
<div>
<select
onChange={(e) => {
setSelectedWallet(e.target.value)
setSelectedWallet(e.target.value as Wallet)
}}
className="block my-2"
>
Expand Down
8 changes: 5 additions & 3 deletions app/btcintegration/xverse-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { base64, hex } from "@scure/base"
import * as btc from "micro-btc-signer"
import Wallet, { RpcErrorCode } from "sats-connect"

import { Params } from "./page"

const bitcoinTestnet = {
bech32: "tb",
pubKeyHash: 0x6f,
Expand Down Expand Up @@ -32,7 +34,7 @@ async function fetchUtxo(address: string): Promise<any[]> {
async function createTransaction(
publickkey: string,
senderAddress: string,
params
params: Params
) {
const publicKey = hex.decode(publickkey)

Expand Down Expand Up @@ -72,12 +74,12 @@ async function createTransaction(

const opReturn = btc.Script.encode(["RETURN", Buffer.from(memo, "utf8")])

tx.addOutputAddress(recipientAddress, BigInt(params.amount), bitcoinTestnet)
tx.addOutputAddress(changeAddress, BigInt(800), bitcoinTestnet)
tx.addOutput({
script: opReturn,
amount: BigInt(0),
})
tx.addOutputAddress(recipientAddress, BigInt(params.amount), bitcoinTestnet)
tx.addOutputAddress(changeAddress, BigInt(800), bitcoinTestnet)

const psbt = tx.toPSBT(0)

Expand Down

0 comments on commit 54f4936

Please sign in to comment.