EC8RawTransaction.fromHex constructor

EC8RawTransaction.fromHex(
  1. String hex
)

Implementation

factory EC8RawTransaction.fromHex(String hex) {
  final buffer = hex.hexToBytes;

  var offset = 0;

  /// Version
  final (version, length) = buffer.bytes.readUint32(offset);
  offset += length;

  /// Inputs
  final (inputLength, inputLengthByteLength) =
      buffer.bytes.readVarInt(offset);
  offset += inputLengthByteLength;

  final inputs = <EC8Input>[];
  for (int i = 0; i < inputLength; i++) {
    final input = EC8Input.fromBuffer(buffer.sublist(offset));
    offset += input.size;
    inputs.add(input);
  }

  /// Outputs
  final (outputLength, outputLengthByteLength) =
      buffer.bytes.readVarInt(offset);
  offset += outputLengthByteLength;

  final outputs = <EC8Output>[];

  for (int i = 0; i < outputLength; i++) {
    final output = EC8Output.fromBuffer(buffer.sublist(offset));
    offset += output.size;
    outputs.add(output);
  }

  /// Fee (is ignored since it is calculated from inputs and outputs)
  final (_, feeOffset) = buffer.bytes.readUint64(offset);
  offset += feeOffset;

  /// Weight (is ignored since it is calculated)
  final (_, weightOffset) = buffer.bytes.readUint32(offset);
  offset += weightOffset;

  /// ValidFrom
  final (validFrom, validFromOffset) = buffer.bytes.readUint64(offset);
  offset += validFromOffset;

  /// ValidUntil
  final (validUntil, validUntilOffset) = buffer.bytes.readUint32(offset);
  offset += validUntilOffset;

  return EC8RawTransaction(
    version: version,
    inputs: inputs,
    outputs: outputs,
    validFrom: validFrom,
    validUntil: validUntil,
  );
}