signInput method

String signInput(
  1. Uint8List txDigest, {
  2. int sigHash = SIGHASH_ALL,
})

sign transaction digest and returns the signature.

Implementation

String signInput(Uint8List txDigest, {int sigHash = SIGHASH_ALL}) {
  final priveBytes = toBytes();
  Uint8List signature = ec.signDer(txDigest, priveBytes);

  int attempt = 1;
  int lengthR = signature[3];

  while (lengthR == 33) {
    final attemptBytes = bytes32FromInt(attempt);
    signature = ec.signDer(txDigest, priveBytes, entery: attemptBytes);
    attempt += 1;
    lengthR = signature[3];

    if (attempt > 20) {
      throw Exception("wrong !!!!! sign must implanet");
    }
  }
  int derPrefix = signature[0];
  int lengthTotal = signature[1];
  int derTypeInt = signature[2];
  lengthR = signature[3];
  Uint8List R = Uint8List.sublistView(signature, 4, 4 + lengthR);
  int lengthS = signature[5 + lengthR];
  Uint8List S = Uint8List.sublistView(signature, 5 + lengthR + 1);
  BigInt sAsBigint = bytesToInt(S);
  Uint8List newS;
  if (lengthS == 33) {
    final BigInt prime = BigInt.parse(
        'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141',
        radix: 16);
    final newSAsBigint = prime - sAsBigint;
    newS = hexToBytes(newSAsBigint.toRadixString(16).padLeft(64, '0'));
    lengthS -= 1;
    lengthTotal -= 1;
  } else {
    newS = S;
  }

  signature = Uint8List.fromList([
    derPrefix,
    lengthTotal,
    derTypeInt,
    lengthR,
    ...R,
    derTypeInt,
    lengthS,
    ...newS,
  ]);
  signature = Uint8List.fromList([...signature, sigHash]);
  return bytesToHex(signature);
}