PartialTx.fromBytes constructor

PartialTx.fromBytes(
  1. Uint8List bytes
)

Implementation

factory PartialTx.fromBytes(Uint8List bytes) {
  if (!PsbtCodec.hasMagic(bytes)) {
    throw FormatException('Invalid PSBT: missing magic bytes');
  }
  final sections = PsbtCodec.decode(bytes);
  if (sections.isEmpty) {
    throw FormatException('Invalid PSBT: no global section');
  }
  final global = sections.first;
  // Look for version key type 0xfb in the global section.
  int psbtVersion = 0;
  for (final kv in global) {
    if (kv.key.isNotEmpty && kv.key[0] == PsbtGlobal.version.keyType) {
      if (kv.value.length >= 4) {
        psbtVersion = kv.value[0] |
            (kv.value[1] << 8) |
            (kv.value[2] << 16) |
            (kv.value[3] << 24);
      }
      break;
    }
  }
  if (psbtVersion == 2) {
    return PartialTxV2.fromBytes(bytes);
  }
  return PartialTxV1.fromBytes(bytes);
}