SolanaPayRequest.parse constructor

SolanaPayRequest.parse(
  1. String url
)

Implementation

factory SolanaPayRequest.parse(String url) {
  final uri = Uri.tryParse(url);
  if (uri == null) {
    throw const ParseUrlException('Link invalid');
  }

  if (uri.scheme != 'solana') {
    throw const ParseUrlException('Protocol invalid');
  }

  final Ed25519HDPublicKey recipient;
  try {
    if (uri.path.isEmpty) {
      throw const ParseUrlException('Recipient missing');
    }
    recipient = Ed25519HDPublicKey.fromBase58(uri.path);
  } on Object {
    throw const ParseUrlException('Recipient invalid');
  }

  final Decimal? amount;
  final amountParam = uri.queryParameters['amount'];
  if (amountParam == null) {
    amount = null;
  } else {
    try {
      amount = Decimal.parse(amountParam);
    } on Object {
      throw const ParseUrlException('Amount invalid');
    }
  }

  final Ed25519HDPublicKey? splToken;
  final splTokenParam = uri.queryParameters['spl-token'];
  if (splTokenParam == null) {
    splToken = null;
  } else {
    try {
      splToken = Ed25519HDPublicKey.fromBase58(splTokenParam);
    } on Object {
      throw const ParseUrlException('Token invalid');
    }
  }

  final Iterable<Ed25519HDPublicKey>? reference;
  final referenceParam = uri.queryParametersAll['reference'];
  if (referenceParam == null) {
    reference = null;
  } else {
    try {
      reference = referenceParam.map(Ed25519HDPublicKey.fromBase58);
    } on Object {
      throw const ParseUrlException('Reference invalid');
    }
  }

  final String? label = uri.queryParameters['label'];
  final String? message = uri.queryParameters['message'];
  final String? memo = uri.queryParameters['memo'];

  return SolanaPayRequest(
    recipient: recipient,
    amount: amount,
    splToken: splToken,
    reference: reference,
    label: label,
    message: message,
    memo: memo,
  );
}