fromRaw static method

BtcTransaction fromRaw(
  1. String raw
)

Instantiates a Transaction from serialized raw hexadacimal data (classmethod)

Implementation

static BtcTransaction fromRaw(String raw) {
  final rawtx = hexToBytes(raw);
  int cursor = 4;
  Uint8List? flag;
  bool hasSegwit = false;
  if (rawtx[4] == 0) {
    flag = Uint8List.fromList(rawtx.sublist(5, 6));
    if (flag[0] == 1) {
      hasSegwit = true;
    }
    cursor += 2;
  }
  final vi = viToInt(rawtx.sublist(cursor, cursor + 9));
  cursor += vi.item2;

  List<TxInput> inputs = [];
  for (int index = 0; index < vi.item1; index++) {
    final inp =
        TxInput.fromRaw(raw: raw, hasSegwit: hasSegwit, cursor: cursor);
    inputs.add(inp.item1);
    cursor = inp.item2;
  }

  List<TxOutput> outputs = [];
  final viOut = viToInt(rawtx.sublist(cursor, cursor + 9));
  cursor += viOut.item2;
  for (int index = 0; index < viOut.item1; index++) {
    final inp =
        TxOutput.fromRaw(raw: raw, hasSegwit: hasSegwit, cursor: cursor);
    outputs.add(inp.item1);
    cursor = inp.item2;
  }
  List<TxWitnessInput> witnesses = [];
  if (hasSegwit) {
    for (int n = 0; n < inputs.length; n++) {
      final wVi = viToInt(rawtx.sublist(cursor, cursor + 9));
      cursor += wVi.item2;
      List<String> witnessesTmp = [];
      for (int n = 0; n < wVi.item1; n++) {
        Uint8List witness = Uint8List(0);
        final wtVi = viToInt(rawtx.sublist(cursor, cursor + 9));
        if (wtVi.item1 != 0) {
          witness = rawtx.sublist(
              cursor + wtVi.item2, cursor + wtVi.item1 + wtVi.item2);
        }
        cursor += wtVi.item1 + wtVi.item2;
        witnessesTmp.add(bytesToHex(witness));
      }
      witnesses.add(TxWitnessInput(stack: witnessesTmp));
    }
  }
  return BtcTransaction(
      inputs: inputs, outputs: outputs, w: witnesses, hasSegwit: hasSegwit);
}