NodeWithAddress.fromJson constructor

NodeWithAddress.fromJson(
  1. Map json
)

Implementation

factory NodeWithAddress.fromJson(Map json) {
  final type = json['type'] as int;

  if (type == 2) {
    return EmptyNode();
  }

  if (json
      case {
        'type': int type,
        'address': String address,
        'derivationPath': String derivationPath,
        'addresses': Map addresses,
        'walletPurpose': int? walletPurpose,
        'publicKey': String publicKey,
      }) {
    return switch (type) {
      0 => ReceiveNode(
          address: address,
          derivationPath: derivationPath,
          addresses: {
            for (final MapEntry(:key, :value) in addresses.entries)
              AddressType.fromIndex(int.parse(key as String)): value as String
          },
          walletPurpose: walletPurpose != null
              ? HDWalletPurpose.values[walletPurpose]
              : null,
          publicKey: publicKey,
        ),
      1 => ChangeNode(
          address: address,
          derivationPath: derivationPath,
          addresses: {
            for (final MapEntry(:key, :value) in addresses.entries)
              AddressType.fromIndex(int.parse(key as String)): value as String
          },
          walletPurpose: walletPurpose != null
              ? HDWalletPurpose.values[walletPurpose]
              : null,
          publicKey: publicKey,
        ),
      _ => throw UnimplementedError(),
    };
  }

  throw UnimplementedError();
}