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.$2;

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

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