signSecp256k1 function

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

Implementation

Uint8List signSecp256k1(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), secp256k1Params);

  signer.init(true, p_api.PrivateKeyParameter(key));
  ECSignature sig = signer.generateSignature(blob) as ECSignature;
  if (sig.s.compareTo(_halfCurveOrder) > 0) {
    final canonicalizedS = secp256k1Params.n - sig.s;
    sig = ECSignature(sig.r, canonicalizedS);
  }
  if (sig.r == sig.s) {
    return signSecp256k1(message, secretKey);
  }
  Uint8List rU8a = sig.r.toU8a();
  Uint8List 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]);
}