Skip to content

Commit

Permalink
refactor: replace duplicate code to generate inputs
Browse files Browse the repository at this point in the history
also, update fn that generates input
  • Loading branch information
iamcrazycoder committed Aug 4, 2023
1 parent 3742b12 commit 53dc8d3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 172 deletions.
173 changes: 31 additions & 142 deletions packages/sdk/src/inscription/instant-buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {
AddressTypes,
calculateTxFee,
calculateTxFeeWithRate,
createTransaction,
getAddressesFromPublicKey,
getNetwork,
InputType,
OrditApi,
processInput,
toXOnly
processInput
} from ".."
import { Network } from "../config/types"

Expand Down Expand Up @@ -102,31 +101,16 @@ export async function generateBuyerPsbt({

let totalInput = 0

const witnessScripts: Buffer[] = []
const usedUTXOTxIds: string[] = []
for (let i = 0; i < 2; i++) {
const dummyUtxo = dummyUtxos[i]
const { rawTx } = await OrditApi.fetchTx({ txId: dummyUtxo.txid, network, hex: true })
if (!rawTx) {
throw new Error("Failed to get raw transaction for id: " + dummyUtxo.txid)
}

if (format !== "p2tr") {
for (const output in rawTx.outs) {
try {
rawTx.setWitness(parseInt(output), [])
} catch {}
}
}
if (usedUTXOTxIds.includes(dummyUtxo.txid)) continue

const p2shInputRedeemScript: any = {}
const p2shInputWitnessUTXO: any = {}
const input = await processInput({ utxo: dummyUtxo, pubKey: publicKey, network })

const input = processInput(dummyUtxo, publicKey, network)

psbt.addInput({
...input,
...p2shInputWitnessUTXO,
...p2shInputRedeemScript
})
usedUTXOTxIds.push(input.hash)
psbt.addInput(input)
totalInput += dummyUtxo.sats
}

Expand Down Expand Up @@ -154,48 +138,23 @@ export async function generateBuyerPsbt({

for (let i = 0; i < spendableUTXOs.length; i++) {
const utxo = spendableUTXOs[i]
if (usedUTXOTxIds.includes(utxo.txid)) continue

const { rawTx } = await OrditApi.fetchTx({ txId: utxo.txid, network, hex: true })

if (format !== "p2tr") {
for (const output in rawTx?.outs) {
try {
rawTx.setWitness(parseInt(output), [])
} catch {}
}
}
const input = await processInput({ utxo, pubKey: publicKey, network })
input.witnessUtxo?.script && witnessScripts.push(input.witnessUtxo?.script)

const input: any = {
hash: utxo.txid,
index: utxo.n,
nonWitnessUtxo: rawTx?.toBuffer(),
sequence: 0xfffffffd // Needs to be at least 2 below max int value to be RBF
}

if (pubKeyType === "taproot") {
const xKey = toXOnly(Buffer.from(publicKey, "hex"))
const p2tr = createTransaction(xKey, "p2tr", network)

input.tapInternalKey = toXOnly(Buffer.from(publicKey, "hex"))
input.witnessUtxo = {
script: p2tr.output!,
value: utxo.sats
}
}

psbt.addInput({
...input
})
usedUTXOTxIds.push(input.hash)

psbt.addInput(input)
totalInput += utxo.sats
}

// const fee = calculateTxFeeWithRate(psbt.txInputs.length, psbt.txOutputs.length, feeRate);
const fee = calculateTxFee({
totalInputs: psbt.txInputs.length,
totalOutputs: psbt.txOutputs.length,
satsPerByte: feeRate,
type: pubKeyType
type: pubKeyType,
additional: { witnessScripts }
})

const totalOutput = psbt.txOutputs.reduce((partialSum, a) => partialSum + a.value, 0)
Expand Down Expand Up @@ -238,28 +197,7 @@ export async function generateDummyUtxos({

for (let i = 0; i < spendableUTXOs.length; i++) {
const utxo = spendableUTXOs[i]
const { rawTx } = await OrditApi.fetchTx({ txId: utxo.txid, network, hex: true })
if (!rawTx) {
throw new Error("Failed to get raw transaction for id: " + utxo.txid)
}

const input: any = {
hash: utxo.txid,
index: utxo.n,
nonWitnessUtxo: rawTx.toBuffer(),
sequence: 0xfffffffd // Needs to be at least 2 below max int value to be RBF
}

if (pubKeyType === "taproot") {
const xKey = toXOnly(Buffer.from(publicKey, "hex"))
const p2tr = createTransaction(xKey, "p2tr", network)

input.tapInternalKey = toXOnly(Buffer.from(publicKey, "hex"))
input.witnessUtxo = {
script: p2tr.output!,
value: utxo.sats
}
}
const input = await processInput({ utxo, pubKey: publicKey, network })

psbt.addInput(input)

Expand Down Expand Up @@ -313,13 +251,11 @@ export async function getSellerInputsOutputs({
receiveAddress,
publicKey,
pubKeyType = "taproot",
network = "testnet",
side = "seller"
network = "testnet"
}: GenerateSellerInstantBuyPsbtOptions) {
const format = addressNameToType[pubKeyType]
const address = getAddressesFromPublicKey(publicKey, network, format)[0]

const inputs = []
const inputs: InputType[] = []
const outputs = []

const { totalUTXOs, unspendableUTXOs } = await OrditApi.fetchUnspentUTXOs({
Expand All @@ -328,69 +264,23 @@ export async function getSellerInputsOutputs({
type: "all"
})
if (!totalUTXOs) {
throw new Error("No UTXOs found.")
throw new Error("No UTXOs found")
}

let found = false

for (let i = 0; i < unspendableUTXOs.length; i++) {
const unspendableUTXO = unspendableUTXOs[i]
if (unspendableUTXO.inscriptions!.find((v: any) => v.outpoint == inscriptionOutPoint)) {
if (unspendableUTXO.inscriptions!.length > 1) {
throw new Error("Multiple inscriptions! Please split them first.")
}
const { rawTx } = await OrditApi.fetchTx({ txId: unspendableUTXO.txid, network, hex: true })
if (!rawTx) {
throw new Error("Failed to get raw transaction for id: " + unspendableUTXO.txid)
}

if (format !== "p2tr") {
for (const output in rawTx.outs) {
try {
rawTx.setWitness(parseInt(output), [])
} catch {}
}
}

const options: any = {}

const data: any = {
hash: unspendableUTXO.txid,
index: unspendableUTXO.n,
nonWitnessUtxo: rawTx.toBuffer(),
sequence: 0xfffffffd // Needs to be at least 2 below max int value to be RBF
}
const postage = unspendableUTXO.sats

if (side === "seller") {
options.sighashType = bitcoin.Transaction.SIGHASH_SINGLE | bitcoin.Transaction.SIGHASH_ANYONECANPAY
}

if (format === "p2tr") {
const xKey = toXOnly(Buffer.from(publicKey, "hex"))
const p2tr = createTransaction(xKey, "p2tr", network)

data.tapInternalKey = toXOnly(Buffer.from(publicKey, "hex"))
data.witnessUtxo = {
script: p2tr.output!,
value: postage
}
}

inputs.push({
...data,
...options
})
outputs.push({ address: receiveAddress, value: price + postage })

found = true
break
}
const utxo = unspendableUTXOs.find((utxo) => utxo.inscriptions?.find((i) => i.outpoint === inscriptionOutPoint))
if (!utxo) {
throw new Error("Inscription not found")
}

if (!found) {
throw new Error("inscription not found.")
}
const input = await processInput({
utxo,
pubKey: publicKey,
network,
sighashType: bitcoin.Transaction.SIGHASH_SINGLE | bitcoin.Transaction.SIGHASH_ANYONECANPAY
})

inputs.push(input)
outputs.push({ address: receiveAddress, value: price + utxo.sats })

return { inputs, outputs }
}
Expand All @@ -415,7 +305,6 @@ export interface GenerateSellerInstantBuyPsbtOptions {
publicKey: string
pubKeyType?: AddressFormats
network?: Network
side?: "seller" | "buyer"
}

export interface GenerateBuyerInstantBuyPsbtOptions {
Expand Down
Loading

0 comments on commit 53dc8d3

Please sign in to comment.