sign function

Uint8List sign(
  1. String message,
  2. BinaryBlob secretKey
)

Implementation

Uint8List sign(String message, BinaryBlob secretKey) {
  final blob = message.plainToU8a(useDartEncode: true);
  final digest = SHA256Digest();
  final signer = ECDSASigner(digest, HMac(digest, 64));
  final key = ECPrivateKey(bytesToUnsignedInt(secretKey), 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 sign(message, secretKey);
  }
  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]);
}