EcdaSignature.signForEth constructor

EcdaSignature.signForEth(
  1. Uint8List message,
  2. Uint8List privateKey, {
  3. int chainId = -1,
})

Implementation

factory EcdaSignature.signForEth(Uint8List message, Uint8List privateKey,
    {int chainId = -1}) {
  final digest = SHA256Digest();
  final signer = ECDSASigner(null, HMac(digest, 64));
  final key = ECPrivateKey(hexToBigInt(dynamicToHex(privateKey)), secp256k1);

  signer.init(true, PrivateKeyParameter(key));
  var sig = signer.generateSignature(message) as ECSignature;

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

  String pubHex =
      dynamicToHex(privateKeyToPublicKey(privateKey, compress: false));

  int recId = getRecid(pubHex, message.toStr(), sig);
  if (recId == -1) {
    throw Exception(
        'Could not construct a recoverable key. This should never happen');
  }

  return EcdaSignature(
      Uint8List.fromList(toBytesPadded(sig.r, 32)),
      Uint8List.fromList(toBytesPadded(sig.s, 32)),
      chainId > 0 ? recId + (chainId * 2 + 35) : recId + 27);
}