encode static method

Uint8List encode(
  1. List<List<PsbtKeyValue>> sections
)

Implementation

static Uint8List encode(List<List<PsbtKeyValue>> sections) {
  var totalSize = psbtMagic.length;
  for (final section in sections) {
    for (final kv in section) {
      totalSize += WireMeasure.varIntSizeOfInt(kv.key.length);
      totalSize += kv.key.length;
      totalSize += WireMeasure.varIntSizeOfInt(kv.value.length);
      totalSize += kv.value.length;
    }
    totalSize += 1; // 0x00 separator
  }

  final out = Uint8List(totalSize);
  final writer = WireWriter(out);
  writer.writeSlice(Uint8List.fromList(psbtMagic));
  for (final section in sections) {
    for (final kv in section) {
      writer.writeVarInt(BigInt.from(kv.key.length));
      writer.writeSlice(kv.key);
      writer.writeVarInt(BigInt.from(kv.value.length));
      writer.writeSlice(kv.value);
    }
    writer.writeUInt8(0x00); // separator
  }
  return out;
}