sign function

MsgSignature sign(
  1. Uint8List messageHash,
  2. Uint8List privateKey
)

Implementation

MsgSignature sign(Uint8List messageHash, Uint8List privateKey) {
  var digest = new SHA256Digest();
  var signer = new ECDSASigner(null, new HMac(digest, 64));
  var key = new ECPrivateKey(bytesToInt(privateKey), params);

  signer.init(true, new PrivateKeyParameter(key));
  ECSignature sig = signer.generateSignature(messageHash);

  if (sig.s.compareTo(_halfCurveOrder) > 0) {
    var canonicalisedS = params.n - sig.s;
    sig = new ECSignature(sig.r, canonicalisedS);
  }

  var publicKey = bytesToInt(publicKeyFromPrivateSign(privateKey));

  var recId = -1;
  for (var i = 0; i < 4; i++) {
    var k = _recoverFromSignature(i, sig, messageHash, params);
    if (k == publicKey) {
      recId = i;
      break;
    }
  }

  if (recId == -1) {
    throw new Exception("Could not construct a recoverable key. This should never happen");
  }

  return new MsgSignature(Uint8List.fromList(toBytesPadded(sig.r, 32)),
      Uint8List.fromList(toBytesPadded(sig.s, 32)), recId);
}