Esempio n. 1
0
        public static void Verify()
        {
            string            input          = File.ReadAllText("../../Output.json");
            EthereumSignature inputSignature = JsonConvert.DeserializeObject <EthereumSignature>(input);
            string            message        = inputSignature.Msg;

            byte[] messageBytes = Utils.GetBytes(message);
            byte[] messageHash  = new Sha3Keccack().CalculateHash(messageBytes);

            byte[] rawSignature = inputSignature.Sig.HexToByteArray();
            byte[] r            = ArrayUtils.SubArray(rawSignature, 0, 32);
            byte[] s            = ArrayUtils.SubArray(rawSignature, 32, 32);
            int    recId        = rawSignature[rawSignature.Length - 1];

            EthECDSASignature signature = EthECDSASignatureFactory.FromComponents(r, s);

            EthECKey publicKey = EthECKey.RecoverFromSignature(signature, recId, messageHash);

            bool valid = publicKey.GetPublicAddress() == inputSignature.Address;

            Console.WriteLine("{0}", valid ? "Valid" : "Invalid");
        }
Esempio n. 2
0
        public static void Sign()
        {
            string[] input        = File.ReadAllLines("../../Input.txt");
            string   inputKey     = input[0];
            string   inputMessage = input[1];

            EthECKey privateKey = new EthECKey(inputKey);

            byte[] messageBytes = Utils.GetBytes(inputMessage);
            byte[] messageHash  = new Sha3Keccack().CalculateHash(messageBytes);

            EthECDSASignature ethECDSASignature = privateKey.SignAndCalculateV(messageHash);
            EthereumSignature signature         = new EthereumSignature
            {
                Address = privateKey.GetPublicAddress(),
                Msg     = inputMessage,
                Sig     = EncryptionUtils.ToString(ethECDSASignature),
                Version = "1"
            };
            string output = JsonUtils.Serialize(signature);

            File.WriteAllText("../../Output.json", output);
            Console.WriteLine(output);
        }