bytes property

Uint8List get bytes
override

Implementation

Uint8List get bytes {
  final inputBuffers = inputs.map((input) => input.bytes);
  const inputLengthByte = 1;
  final inputsByteLength = inputBuffers.fold(
    0,
    (prev, buffer) => prev + buffer.length,
  );

  final outputBuffers = outputs.map((output) => output.bytes);
  const outputLengthByte = 1;
  final outputsByteLength = outputBuffers.fold(
    0,
    (prev, buffer) => prev + buffer.length,
  );

  var txByteLength = 4 +
      inputLengthByte +
      inputsByteLength +
      outputLengthByte +
      outputsByteLength +
      4;

  if (hasWitness) {
    txByteLength += 1; // Segwit Flag
    txByteLength += 1; // Segwit Marker

    txByteLength += segwitInputs.fold(
      0,
      (prev, input) {
        assert(input.isSegwit);
        return prev + input.wittnessScript.length;
      },
    );
    txByteLength += nonSegwitInputs.length; // Empty Script
  }

  ///
  /// Construct Buffer
  ///
  final buffer = Uint8List(txByteLength);
  var offset = 0;

  /// Version
  offset += buffer.bytes.writeUint32(offset, version);

  /// Segwit Flag
  if (hasWitness) {
    offset += buffer.bytes.writeUint8(offset, SEGWIT_MARKER);
    offset += buffer.bytes.writeUint8(offset, SEGWIT_FLAG);
  }

  /// Inputs
  offset += buffer.bytes.writeVarInt(offset, inputs.length);
  for (final input in inputs) {
    offset += buffer.writeSlice(offset, input.bytes);
  }

  /// Outputs
  offset += buffer.bytes.writeVarInt(offset, outputs.length);
  for (final output in outputs) {
    offset += buffer.writeSlice(offset, output.bytes);
  }

  /// Witness
  if (hasWitness)
    for (final input in inputs) {
      if (input.isSegwit) {
        offset += buffer.writeSlice(offset, input.wittnessScript);
        continue;
      }

      offset += buffer.bytes.writeUint8(offset, 0x00); // Empty Script
    }

  /// Locktime
  offset += buffer.bytes.writeUint32(offset, lockTime);

  return buffer;
}