P2PK constructor

P2PK(
  1. PaymentArg _a, [
  2. PaymentOpts? opts
])

Implementation

P2PK(this._a, [PaymentOpts? opts]) {
  if (_a.input == null &&
      _a.output == null &&
      _a.pubkey == null &&
      _a.input == null &&
      _a.signature == null) {
    throw Exception('Not enough data');
  }

  opts = PaymentOpts(
      validate: opts?.validate ?? true,
      allowIncomplete: opts?.allowIncomplete);

  if (_a.pubkey != null && !ecc.isPoint(_a.pubkey!)) {
    throw ArgumentError.value(_a.pubkey, 'pubkey', 'Invalid Public Key.');
  }

  if (_a.signature != null &&
      !bscript.script.isCanonicalScriptSignature(_a.signature!)) {
    throw ArgumentError.value(
        _a.signature, 'signature', 'Signature must be canonical script.');
  }

  if (opts.validate == true) {
    if (_a.output != null) {
      if (_a.output![_a.output!.length - 1] != OPS['OP_CHECKSIG']!) {
        throw Exception('Output is invalid');
      }
      if (!ecc.isPoint(pubkey!)) {
        throw Exception('Output pubkey is invalid');
      }
      if (_a.pubkey != null && _a.pubkey! != pubkey!) {
        throw Exception('Pubkey mismatch');
      }
    }

    if (_a.signature != null) {
      if (_a.input != null && _a.input! != input!) {
        throw Exception('Signature mismatch');
      }
    }

    if (_a.input != null) {
      if (_chunks!.length != 1) throw Exception('Input is invalid');
      if (!bscript.script.isCanonicalScriptSignature(signature!)) {
        throw Exception('Input has invalid signature');
      }
    }
  }

  _assign(_a);
}