SchnorrIdentity.fromJSON constructor

SchnorrIdentity.fromJSON(
  1. String json
)

Implementation

factory SchnorrIdentity.fromJSON(String json) {
  final parsed = jsonDecode(json);
  if (parsed is List) {
    if (parsed[0] is String && parsed[1] is String) {
      return SchnorrIdentity.fromParsedJson([parsed[0], parsed[1]]);
    }
    throw ArgumentError.value(
      json,
      'json',
      'JSON must have at least 2 elements',
    );
  } else if (parsed is Map) {
    final publicKey = parsed['publicKey'];
    final dashPublicKey = parsed['_publicKey'];
    final secretKey = parsed['secretKey'];
    final dashPrivateKey = parsed['_privateKey'];
    final pk = publicKey != null
        ? SchnorrPublicKey.fromRaw(Uint8List.fromList(publicKey.data))
        : SchnorrPublicKey.fromDer(Uint8List.fromList(dashPublicKey.data));

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