Skip to content

Commit

Permalink
Updated transaction decomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Oct 4, 2023
1 parent a21fc6f commit ba40432
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions typescript/src/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import bcoin, { TX } from "bcoin"
import bcoin from "bcoin"
import bufio from "bufio"
import { BigNumber, utils } from "ethers"
import { Hex } from "./hex"
import { BitcoinNetwork, toBitcoinJsLibNetwork } from "./bitcoin-network"
import { address, payments } from "bitcoinjs-lib"
import { Transaction as Tx, address, payments } from "bitcoinjs-lib"

/**
* Represents a transaction hash (or transaction ID) as an un-prefixed hex
Expand Down Expand Up @@ -407,46 +407,53 @@ export interface Client {
export function decomposeRawTransaction(
rawTransaction: RawTransaction
): DecomposedRawTransaction {
const toHex = (bufferWriter: any) => {
const toHex = (bufferWriter: any): string => {
return bufferWriter.render().toString("hex")
}

const vectorToRaw = (elements: any[]) => {
const getTxInputVector = (tx: Tx): string => {
const buffer = bufio.write()
buffer.writeVarint(elements.length)
for (const element of elements) {
element.toWriter(buffer)
}
buffer.writeVarint(tx.ins.length)
tx.ins.forEach((input) => {
buffer.writeHash(input.hash)
buffer.writeU32(input.index)
buffer.writeVarBytes(input.script)
buffer.writeU32(input.sequence)
})
return toHex(buffer)
}

const getTxInputVector = (tx: any) => {
return vectorToRaw(tx.inputs)
}

const getTxOutputVector = (tx: any) => {
return vectorToRaw(tx.outputs)
const getTxOutputVector = (tx: Tx): string => {
const buffer = bufio.write()
buffer.writeVarint(tx.outs.length)
tx.outs.forEach((output) => {
buffer.writeI64(output.value)
buffer.writeVarBytes(output.script)
})
return toHex(buffer)
}

const getTxVersion = (tx: any) => {
const getTxVersion = (tx: Tx): string => {
const buffer = bufio.write()
buffer.writeU32(tx.version)
return toHex(buffer)
}

const getTxLocktime = (tx: any) => {
const getTxLocktime = (tx: Tx): string => {
const buffer = bufio.write()
buffer.writeU32(tx.locktime)
return toHex(buffer)
}

const tx = TX.fromRaw(Buffer.from(rawTransaction.transactionHex, "hex"), null)
const transaction = Tx.fromBuffer(
Buffer.from(rawTransaction.transactionHex, "hex")
)

return {
version: getTxVersion(tx),
inputs: getTxInputVector(tx),
outputs: getTxOutputVector(tx),
locktime: getTxLocktime(tx),
version: getTxVersion(transaction),
inputs: getTxInputVector(transaction),
outputs: getTxOutputVector(transaction),
locktime: getTxLocktime(transaction),
}
}

Expand Down

0 comments on commit ba40432

Please sign in to comment.