DelegationChain.fromJSON constructor

DelegationChain.fromJSON(
  1. dynamic obj
)

Creates a DelegationChain object from a JSON string.

@param json The JSON string to parse.

Implementation

factory DelegationChain.fromJSON(dynamic obj) {
  var json = obj is String
      ? Map<String, dynamic>.from(jsonDecode(obj))
      : Map<String, dynamic>.from(obj);
  if (json["delegations"] is! List) {
    throw 'Invalid delegations.';
  }

  var publicKey = json["publicKey"] as String;
  var delegations = json["delegations"] as List<dynamic>;

  final parsedDelegations = delegations.map((map) {
    var signedDelegation = SignedDelegation.fromMap(map);
    final delegation = signedDelegation.delegation,
        signature = signedDelegation.signature;
    var pubkey = delegation?.pubkey,
        expiration = delegation?.expiration,
        targets = delegation?.targets;

    if (targets != null && (targets is! List)) {
      throw 'Invalid targets.';
    }

    return SignedDelegation.fromMap({
      "delegation": Delegation(
        pubkey!,
        expiration!, // expiration in JSON is an hexa string (See toJSON() below).
        targets,
      ),
      "signature": signature,
    });
  }).toList();

  return DelegationChain(parsedDelegations, blobFromHex(publicKey));
}