All files / src/utility parseTxScriptOffsets.ts

100% Statements 21/21
100% Branches 0/0
100% Functions 1/1
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3157x             57x     6x 6x 6x   6x 6x 6x 7x 7x 7x 7x   6x 6x 13x 13x 13x 13x   6x  
import { Utils as SdkUtils } from "@bsv/sdk"
 
export interface TxScriptOffsets {
  inputs: { vin: number, offset: number, length: number }[]
  outputs: { vout: number, offset: number, length: number }[]
}
 
export function parseTxScriptOffsets(rawTx: number[])
: TxScriptOffsets
{
  const br = new SdkUtils.Reader(rawTx)
  const inputs: { vin: number, offset: number, length: number }[] = []
  const outputs: { vout: number, offset: number, length: number }[] = []
 
  br.pos += 4 // version
  const inputsLength = br.readVarIntNum()
  for (let i = 0; i < inputsLength; i++) {
    br.pos += 36 // txid and vout
    const scriptLength = br.readVarIntNum()
    inputs.push({ vin: i, offset: br.pos, length: scriptLength })
    br.pos += scriptLength + 4 // script and sequence
  }
  const outputsLength = br.readVarIntNum()
  for (let i = 0; i < outputsLength; i++) {
    br.pos += 8 // satoshis
    const scriptLength = br.readVarIntNum()
    outputs.push({ vout: i, offset: br.pos, length: scriptLength })
    br.pos += scriptLength
  }
  return { inputs, outputs }
}