示例#1
0
    static void Main()
    {
        //var privKey = EthECKey.GenerateKey();
        var privKey = new EthECKey("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a");

        byte[] pubKeyCompressed = new ECKey(privKey.GetPrivateKeyAsBytes(), true).GetPubKey(true);
        Console.WriteLine("Private key: {0}", privKey.GetPrivateKey().Substring(4));
        Console.WriteLine("Public key: {0}", privKey.GetPubKey().ToHex().Substring(2));
        Console.WriteLine("Public key (compressed): {0}", pubKeyCompressed.ToHex());

        Console.WriteLine();

        string msg = "Message for signing";

        byte[] msgBytes  = Encoding.UTF8.GetBytes(msg);
        byte[] msgHash   = new Sha3Keccack().CalculateHash(msgBytes);
        var    signature = privKey.SignAndCalculateV(msgHash);

        Console.WriteLine("Msg: {0}", msg);
        Console.WriteLine("Msg hash: {0}", msgHash.ToHex());
        Console.WriteLine("Signature: [v = {0}, r = {1}, s = {2}]",
                          signature.V[0] - 27, signature.R.ToHex(), signature.S.ToHex());

        Console.WriteLine();

        var pubKeyRecovered = EthECKey.RecoverFromSignature(signature, msgHash);

        Console.WriteLine("Recovered pubKey: {0}", pubKeyRecovered.GetPubKey().ToHex().Substring(2));

        bool validSig = pubKeyRecovered.Verify(msgHash, signature);

        Console.WriteLine("Signature valid? {0}", validSig);
    }
示例#2
0
        public void ShouldEncodeATransactionUsingKeccak256()
        {
            var txRaw =
                "F89D80809400000000000000000000000000000000000000008609184E72A000822710B3606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C5884336069571CA07F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4A06D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC";
            var txHashB = new Sha3Keccack().CalculateHash(txRaw.HexToByteArray());
            var txHash  = txHashB.ToHex();

            Assert.Equal("4b7d9670a92bf120d5b43400543b69304a14d767cf836a7f6abff4edde092895", txHash);
        }
示例#3
0
        public void GeneratorPublicKeyByPrivateKeyTest()
        {
            var publicKey = AccountUtils.GeneratorPublicKeyByPrivateKey("25aa95ed437f8efaf37cf849a5a6ba212308d5d735105e03e38410542bf1d5ff");

            Assert.True(publicKey == "0x6a3b8f69e6860c1ad417944ae4d262930cf23ba0d1ee40ed09a4f165a2642be766901d0bd1b1d0510e0b9976ac314e961910a145073c21fdcb8cdaf8f4fbee56");
            var initaddr = new Sha3Keccack().CalculateHash(publicKey.Substring(publicKey.StartsWith("0x") ? 2 : 0).HexToByteArray());
            var addr     = new byte[initaddr.Length - 12];

            Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
            var address = AccountUtils.ConvertToChecksumAddress(addr.ToHex());

            //initaddr.ToHex() 长度64,截取后面40位得到就是地址
            Assert.Equal(initaddr.ToHex().Substring(24, 40), address.RemoveHexPrefix().ToLower());
        }