restoreStateFromMap method

  1. @override
Future<ChannelState> restoreStateFromMap(
  1. Map<String, dynamic> map,
  2. int sequenceNumber
)

Restore state from a map representation Override in subclasses to provide custom map restoration

Implementation

@override
Future<ChannelState> restoreStateFromMap(Map<String, dynamic> map, int sequenceNumber) async {
  DateTime? date(Object? v) => v == null ? null : (v is DateTime ? v : DateTime.parse(v as String));
  final channelId = map['channelId'] as String;
  if (channelId != aggregateId) {
    throw StateError('Snapshot at $sequenceNumber belongs to channel $channelId, not $aggregateId');
  }
  final role = map['role'] as String?;
  return ChannelState(
    channelId: channelId,
    walletId: map['walletId'] as String?,
    status: ChannelStatus.values.byName(map['status'] as String),
    role: role == null ? null : ChannelRole.values.byName(role),
    clientPeerId: map['clientPeerId'] as String?,
    serverPeerId: map['serverPeerId'] as String?,
    clientPubKeyHex: map['clientPubKeyHex'] as String?,
    serverPubKeyHex: map['serverPubKeyHex'] as String?,
    clientAddressB58: map['clientAddressB58'] as String?,
    serverAddressB58: map['serverAddressB58'] as String?,
    derivationIndex: map['derivationIndex'] as int?,
    fundingAmountSats: BigInt.parse(map['fundingAmountSats'] as String),
    fundingTxId: map['fundingTxId'] as String?,
    fundingTxHex: map['fundingTxHex'] as String?,
    fundingOutputIndex: map['fundingOutputIndex'] as int?,
    fundingAncestorTxids: [
      for (final t in map['fundingAncestorTxids'] as List? ?? const []) t as String,
    ],
    // Snapshots written before libspiffy-b83/9f7 have no funding broadcast
    // or signed refund keys.
    fundingBeefHex: map['fundingBeefHex'] as String?,
    fundingInputSats: map['fundingInputSats'] as int?,
    fundingBroadcastAttempts: map['fundingBroadcastAttempts'] as int? ?? 0,
    fundingBroadcastInFlight: map['fundingBroadcastInFlight'] as bool? ?? false,
    fundingBroadcastError: map['fundingBroadcastError'] as String?,
    fundingRecordedInWallet: map['fundingRecordedInWallet'] as bool? ?? false,
    returnLegRecordedInWallet:
        map['returnLegRecordedInWallet'] as bool? ?? false,
    lockTimeUnix: map['lockTimeUnix'] as int?,
    refundTxHex: map['refundTxHex'] as String?,
    refundClientSigHex: map['refundClientSigHex'] as String?,
    refundServerSigHex: map['refundServerSigHex'] as String?,
    signedRefundTxHex: map['signedRefundTxHex'] as String?,
    clientBalanceSats: BigInt.parse(map['clientBalanceSats'] as String),
    serverBalanceSats: BigInt.parse(map['serverBalanceSats'] as String),
    latestSequenceNumber: map['latestSequenceNumber'] as int,
    latestPaymentTxHex: map['latestPaymentTxHex'] as String?,
    latestPaymentTxId: map['latestPaymentTxId'] as String?,
    // Snapshots written before V-149 also hold latestClientSignatureHex,
    // which nothing has read since the client stopped assembling
    // settlements (V-141); it is ignored. PaymentRecordedEvent still
    // carries the signature in the journal.
    context: map['context'] as String?,
    // Snapshots written before libspiffy-bps1 have no marker key.
    counterpartyMarker: map['counterpartyMarker'] as String?,
    createdAt: date(map['createdAt']),
    // Snapshots written before libspiffy-07mx have no claim key; a
    // channel whose claim predates this reads back as unclaimed, which is
    // what the journal said at the time.
    refundClaimedTxId: map['refundClaimedTxId'] as String?,
    closedAt: date(map['closedAt']),
    version: map['version'] as int,
    lastModified: date(map['lastModified']),
  );
}