Пример #1
0
        public static SignatureData SignMessage(byte[] message, ECKeyPair keys, bool needToHash)
        {
            BigInteger publicKey   = keys.PublicKey;
            var        messageHash = needToHash ? Hash.Blake2B(message) : message;
            var        recId       = -1;

            ECDSASignature sig = keys.Sign(messageHash);

            for (int i = 0; i < 4; i++)
            {
                BigInteger k = RecoverFromSignature(i, sig, messageHash);

                if (k != null && k.Equals(publicKey))
                {
                    recId = i;
                    break;
                }
            }

            if (recId == -1)
            {
                throw new FormatException("Sign the data failed.");
            }
            if (recId == 2 || recId == 3)
            {
                throw new InvalidOperationException("Recovery is not valid for VeChain MainNet.");
            }

            byte v = (byte)recId;

            byte[] r = sig.R.ToByteArrayUnsigned().PadLeading(32);
            byte[] s = sig.S.ToByteArrayUnsigned().PadLeading(32);

            return(new SignatureData(v, r, s));
        }