DelegationChain.fromJSON constructor

DelegationChain.fromJSON(
  1. dynamic obj
)

Creates a DelegationChain object from a JSON string.

Implementation

factory DelegationChain.fromJSON(dynamic obj) {
  final json = obj is String
      ? Map<String, dynamic>.from(jsonDecode(obj))
      : Map<String, dynamic>.from(obj);
  if (json['delegations'] is! List) {
    throw ArgumentError('Invalid delegations type.', json['delegations']);
  }
  final publicKey = json['publicKey'] as String;
  final delegations = json['delegations'] as List<dynamic>;
  final parsedDelegations = delegations.map((map) {
    final signedDelegation = SignedDelegation.fromJson(map);
    final delegation = signedDelegation.delegation,
        signature = signedDelegation.signature;
    final pubkey = delegation?.pubkey,
        expiration = delegation?.expiration,
        targets = delegation?.targets;
    return SignedDelegation.fromJson({
      'delegation': Delegation(
        pubkey!,
        // expiration in JSON is a hex string.
        expiration!,
        targets,
      ),
      'signature': signature,
    });
  }).toList();
  return DelegationChain(parsedDelegations, blobFromHex(publicKey));
}