sign method

  1. @override
Future<Uint8List> sign(
  1. Uint8List blob
)
override

Signs a blob of data, with this identity's private key. @param blob - challenge to sign with this identity's secretKey, producing a signature

Implementation

@override
Future<Uint8List> sign(Uint8List blob) async {
  final digest = SHA256Digest();
  final signer = ECDSASigner(digest, HMac(digest, 64));

  final key = ECPrivateKey(bytesToUnsignedInt(_privateKey), params);

  signer.init(true, p_api.PrivateKeyParameter(key));
  var sig = signer.generateSignature(blob) as ECSignature;
  if (sig.s.compareTo(_halfCurveOrder) > 0) {
    final canonicalisedS = params.n - sig.s;
    sig = ECSignature(sig.r, canonicalisedS);
  }
  if (sig.r == sig.s) {
    return await sign(blob);
  }
  var rU8a = sig.r.toU8a();
  var sU8a = sig.s.toU8a();
  if (rU8a.length < 32) {
    rU8a = Uint8List.fromList([0, ...rU8a]);
  }
  if (sU8a.length < 32) {
    sU8a = Uint8List.fromList([0, ...sU8a]);
  }

  return u8aConcat([rU8a, sU8a]);
}