Ed25519KeyIdentity.fromJSON constructor

Ed25519KeyIdentity.fromJSON(
  1. String json
)

Implementation

factory Ed25519KeyIdentity.fromJSON(String json) {
  final parsed = jsonDecode(json);
  if (parsed is List) {
    if (parsed[0] is String && parsed[1] is String) {
      return Ed25519KeyIdentity.fromParsedJson([parsed[0], parsed[1]]);
    }
    throw ArgumentError.value(
      json,
      'json',
      'JSON must have at least 2 elements',
    );
  }
  if (parsed is Map) {
    final reParsed = jsonDecode(json);
    final publicKey = reParsed['publicKey']?.cast<int>();
    final dashPublicKey = reParsed['_publicKey']?.cast<int>();
    final secretKey = reParsed['secretKey']?.cast<int>();
    final dashPrivateKey = reParsed['_privateKey']?.cast<int>();

    final pk = publicKey != null
        ? Ed25519PublicKey.fromRaw(Uint8List.fromList(publicKey))
        : Ed25519PublicKey.fromDer(Uint8List.fromList(dashPublicKey!));

    if (publicKey != null && secretKey != null) {
      return Ed25519KeyIdentity(
        pk,
        Uint8List.fromList(secretKey),
      );
    }
    if (dashPublicKey != null && dashPrivateKey != null) {
      return Ed25519KeyIdentity(
        pk,
        Uint8List.fromList(dashPrivateKey),
      );
    }
  }
  throw ArgumentError.value(jsonEncode(json), 'json', 'Invalid json');
}