sign static method

Envelope sign(
  1. Envelope envelope,
  2. SecretKey key
)

Implementation

static Envelope sign(Envelope envelope, SecretKey key) {
  final hash = envelope.signingHash();
  final sig = RecoverableEcdsaSig.sign(hash, key.bytes);

  final r = _padTo32(sig.bytes.sublist(0, 32));
  final s = _padTo32(sig.bytes.sublist(32, 64));
  final recId = sig.recId;

  int v;
  switch (envelope.kind) {
    case EnvelopeKind.legacy:
      // EIP-155: v = chainId * 2 + 35 + recId
      v = (envelope.chainId * BigInt.two + BigInt.from(35) + BigInt.from(recId)).toInt();
      break;
    case EnvelopeKind.eip2930:
    case EnvelopeKind.eip1559:
    case EnvelopeKind.eip4844:
    case EnvelopeKind.eip7702:
      // Typed transactions use yParity directly (0 or 1).
      v = recId;
      break;
  }

  return envelope.withSignature(v: v, r: r, s: s);
}