Пример #1
0
        private void btnSignTransact_Click(object sender, EventArgs e)
        {
            Sender   = txtTransSender.Text;
            Receiver = txtTransRecip.Text;
            value    = txtTransValue.Text;
            fee      = "100";
            //dateCreated = "1534495254";
            dateCreated       = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
            data              = "";
            senderPubKey      = pubKeySession;
            minedInBlockIndex = "index";
            TransferSuccess   = "success?";
            var    SignPrivKey = new EthECKey(privKeySession);
            string msg         = Sender + Receiver + value + fee + dateCreated + data + senderPubKey + minedInBlockIndex;

            //string msg = Sender + Receiver + value + fee + dateCreated + data + senderPubKey + transactionDataHash + minedInBlockIndex;
            //string msg = "Sean";
            byte[] msgBytes  = Encoding.UTF8.GetBytes(msg);
            byte[] msgHash   = new Sha3Keccack().CalculateHash(msgBytes);
            var    signature = SignPrivKey.SignAndCalculateV(msgHash);

            transactionDataHash = sha256(msg);


            var walletJsonData = new { From = Sender, To = Receiver, value = value, fee = fee, dateCreated = dateCreated, data = data, senderPubKey = senderPubKey, transactionDataHash = transactionDataHash, senderSignature = signature.V[0] - 27 + signature.R.ToHex() + signature.S.ToHex(), minedInBlockIndex = minedInBlockIndex, TransferSuccess = TransferSuccess };

            JsonTrans         = JsonConvert.SerializeObject(walletJsonData, Formatting.Indented);
            txtTransData.Text = JsonTrans;
        }
Пример #2
0
 public string GetPublicAddress()
 {
   var initaddr = new Sha3Keccack().CalculateHash(GetPubKeyNoPrefix());
   var addr = new byte[initaddr.Length - 12];
   Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
   return new AddressUtil().ConvertToChecksumAddress(addr.ToHex());
 }
Пример #3
0
        private static async Task Main(string[] args)
        {
            try
            {
                CompileContract(args);

                var web3     = CreateWeb3();
                var contract = await DeployContractAsync(web3);

                var function       = contract.GetFunction("callEcRecover");
                var message        = new Sha3Keccack().CalculateHash(Encoding.ASCII.GetBytes("Hello"));
                var messageSigner  = new MessageSigner();
                var ecdsaSignature = messageSigner.SignAndCalculateV(message, PrivateKey);


                var address = await function.CallAsync <string>(new object[]
                {
                    message,
                    ecdsaSignature.V[0],
                    ecdsaSignature.R,
                    ecdsaSignature.S
                });

                Log($"Received: {address.Substring(2).ToLowerInvariant()}; Our: {Address.Substring(2).ToLowerInvariant()} Correct: {string.Equals(address.Substring(2), Address.Substring(2), StringComparison.OrdinalIgnoreCase)}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #4
0
        private string GetTransactionHash(
            string from,
            string to,
            string data,
            BigInteger txFee,
            BigInteger gasPrice,
            BigInteger gasLimit,
            BigInteger nonce,
            string relayHubAddress,
            string relayAddress)
        {
            var keccack256 = new Sha3Keccack();

            var encoder = new IntTypeEncoder();

            return(keccack256.CalculateHashFromHex(
                       RelayPrefix.ToHexUTF8(),
                       from,
                       to,
                       data,
                       encoder.EncodeInt(txFee).ToHex(),
                       encoder.EncodeInt(gasPrice).ToHex(),
                       encoder.EncodeInt(gasLimit).ToHex(),
                       encoder.EncodeInt(nonce).ToHex(),
                       relayHubAddress,
                       relayAddress));
        }
Пример #5
0
        public void ShouldRecoverGethPrefix()
        {
            //signed message using geth 1.5^
            var signature = "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c";
            var text      = "test";
            var hasher    = new Sha3Keccack();
            var hash      = hasher.CalculateHash(text);
            var byteList  = new List <byte>();

            var bytePrefix     = "0x19".HexToByteArray();
            var textBytePrefix = Encoding.UTF8.GetBytes("Ethereum Signed Message:\n" + hash.HexToByteArray().Length);
            var bytesMessage   = hash.HexToByteArray();

            byteList.AddRange(bytePrefix);
            byteList.AddRange(textBytePrefix);
            byteList.AddRange(bytesMessage);
            var hashPrefix2 = hasher.CalculateHash(byteList.ToArray()).ToHex();

            var signer = new MessageSigner();

            var account = signer.EcRecover(hashPrefix2.HexToByteArray(), signature);

            Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c", account.EnsureHexPrefix().ToLower());

            signature = signer.Sign(hashPrefix2.HexToByteArray(),
                                    "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7");

            account = signer.EcRecover(hashPrefix2.HexToByteArray(), signature);

            Assert.Equal("0x12890d2cce102216644c59dae5baed380d84830c".ToLower(), account.EnsureHexPrefix().ToLower());
        }
Пример #6
0
        public async void ShouldSignCorrectly()
        {
            await Task.Yield();

            string privateKey = "0xa07cb7889ab3a164dcc72cb6103f2573c7ef2d4a855810594d2bf25df60bc39e";

            PlasmaCore.Transactions.Transaction transaction = new PlasmaCore.Transactions.Transaction();
            transaction.AddInput(2000, 0, 1);
            transaction.AddOutput("0xf86b5b1c2c8de1ea4dc737c849272340fa3561c5", "0x0000000000000000000000000000000000000000", 123);
            transaction.AddOutput("0xf86b5b1c2c8de1ea4dc737c849272340fa3561c5", "0x0000000000000000000000000000000000000000", 5555308);

            TypedDataTransactionEncoder txEncoder = new TypedDataTransactionEncoder(omgDomain);

            byte[] encodedTx      = txEncoder.EncodeRaw(transaction);
            var    rawHash        = new Sha3Keccack().CalculateHash(encodedTx);
            var    ecKey          = new Nethereum.Signer.EthECKey(privateKey);
            var    ecdsaSignature = ecKey.SignAndCalculateV(rawHash);

            string signature = Nethereum.Signer.EthECDSASignature.CreateStringSignature(ecdsaSignature);

            Assert.Equal("0xed0ff5633cb85aa0f64684759185f8a9f94fd1b654be5942d562bf64f504e3a96a83b90a5e50e50b8a75d4f711d1c0e56066519237dbd94e564084a561b8ba2f1b",
                         signature.EnsureHexPrefix());

            transaction.SetSignature(0, signature.HexToByteArray());

            var signedEncodedTx = txEncoder.EncodeSigned(transaction).ToHex(true);

            Assert.Equal("0xf9012ef843b841ed0ff5633cb85aa0f64684759185f8a9f94fd1b654be5942d562bf64f504e3a96a83b90a5e50e50b8a75d4f711d1c0e56066519237dbd94e564084a561b8ba2f1bd2c58207d08001c3808080c3808080c3808080f8b3eb94f86b5b1c2c8de1ea4dc737c849272340fa3561c59400000000000000000000000000000000000000007bee94f86b5b1c2c8de1ea4dc737c849272340fa3561c59400000000000000000000000000000000000000008354c46ceb94000000000000000000000000000000000000000094000000000000000000000000000000000000000080eb94000000000000000000000000000000000000000094000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000",
                         signedEncodedTx.EnsureHexPrefix());
        }
Пример #7
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);
    }
Пример #8
0
        public static string GetPublicAddress(this ECKey key)
        {
            var initaddr = new Sha3Keccack().CalculateHash(key.GetPubKeyNoPrefix());
            var addr     = new byte[initaddr.Length - 12];

            Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
            return(addr.ToHex());
        }
Пример #9
0
        public void ShouldCalculateHashFromHexForMultipleStrings()
        {
            var keccak = new Sha3Keccack();
            var result = keccak.CalculateHashFromHex("0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae",
                                                     "0x0d57c7d8b54ebf963f94cded8a57a1b109ac7465ada218575473648bf373b90d");

            Assert.Equal("13265b3c8b785f6715b215cb1e6869312588a03afe0076beda8042c2ceb5603b", result);
        }
        public static string GetChannelAddress(string aliceAddress, string bobAddress)
        {
            var sha3 = new Sha3Keccack();

            var output     = sha3.CalculateHashFromHex(aliceAddress, bobAddress);
            var resultHash = "0x" + output;

            return(resultHash);
        }
Пример #11
0
 private void InitialiseInnerServices()
 {
     Eth         = new Eth(Client);
     Shh         = new Shh(Client);
     Net         = new Net(Client);
     Personal    = new Personal(Client);
     Convert     = new UnitConversion();
     sha3Keccack = new Sha3Keccack();
 }
            /// <summary>
            /// Sign transaction with the private key
            /// </summary>
            /// <param name="input">input arguments</param>
            /// <returns>transaction signature string</returns>
            public override Task <string> SignTransaction(byte[] input)
            {
                //CPU-bound
                var ecKey     = new Nethereum.Signer.EthECKey(DecodeLocalKey(), true);
                var rawHash   = new Sha3Keccack().CalculateHash(input);
                var signature = ecKey.SignAndCalculateV(rawHash);

                return(Task.FromResult(Nethereum.Signer.EthECDSASignature.CreateStringSignature(signature)));
            }
Пример #13
0
        public void ShouldEncodeATransactionUsingKeccak256()
        {
            var txRaw =
                "F89D80809400000000000000000000000000000000000000008609184E72A000822710B3606956330C0D630000003359366000530A0D630000003359602060005301356000533557604060005301600054630000000C5884336069571CA07F6EB94576346488C6253197BDE6A7E59DDC36F2773672C849402AA9C402C3C4A06D254E662BF7450DD8D835160CBB053463FED0B53F2CDD7F3EA8731919C8E8CC";
            var txHashB = new Sha3Keccack().CalculateHash(txRaw.HexToByteArray());
            var txHash  = txHashB.ToHex();

            Assert.Equal("4b7d9670a92bf120d5b43400543b69304a14d767cf836a7f6abff4edde092895", txHash);
        }
Пример #14
0
        public ContractCallParser(IEnumerable <Tuple <string, int> > abis)
        {
            var hash = new Sha3Keccack();

            foreach (var tuple in abis)
            {
                var key = BitConverter.ToString(hash.CalculateHash(Encoding.UTF8.GetBytes(tuple.Item1)).Take(4).ToArray()).Replace("-", string.Empty).ToLower();
                _map[key] = tuple.Item2;
            }
        }
Пример #15
0
        public void Data()
        {
            var guid = Guid.Parse("176a82d8-3154-4c76-bab8-441ad43d0de6");

            var strForHash = EthUtils.GuidToByteArray(guid).ToHex();

            var hash = new Sha3Keccack().CalculateHash(strForHash.HexToByteArray());

            var sign = Sign(hash, private_key_a).ToHex();
        }
Пример #16
0
        public static string Sign(ITransactionEncoder txEncoder, Transaction transaction, string address, string privateKey)
        {
            byte[] encodedTx      = txEncoder.EncodeRaw(transaction);
            var    rawHash        = new Sha3Keccack().CalculateHash(encodedTx);
            var    ecKey          = new Nethereum.Signer.EthECKey(privateKey);
            var    ecdsaSignature = ecKey.SignAndCalculateV(rawHash);
            string signature      = Nethereum.Signer.EthECDSASignature.CreateStringSignature(ecdsaSignature);

            transaction.SetSignature(address, signature.HexToByteArray());
            return(txEncoder.EncodeSigned(transaction).ToHex(true));
        }
Пример #17
0
        public static string GetPublicKey(string PrivateKey)
        {
            var key      = new EthECKey(PrivateKey.HexToByteArray(), true);
            var initaddr = new Sha3Keccack().CalculateHash(key.GetPubKeyNoPrefix());
            var addr     = new byte[initaddr.Length - 12];

            Array.Copy(initaddr, 12, addr, 0, initaddr.Length - 12);
            string PublicKey = new AddressUtil().ConvertToChecksumAddress(addr.ToHex());

            return(PublicKey);
        }
Пример #18
0
            public void Bytes32ToString()
            {
                var hashString = new Sha3Keccack().CalculateHash("magic");
                var hashBytes  = Encoding.UTF8.GetBytes(hashString);

                EventLog <List <ParameterOutput> > eventLog = EventLogWithParameter(1, "bytes32", hashBytes);

                var condition = new ParameterEquals(1, hashString);

                Assert.True(condition.IsTrue(eventLog));
            }
Пример #19
0
 protected virtual void InitialiseInnerServices()
 {
     Eth         = new EthApiContractService(Client);
     Shh         = new ShhApiService(Client);
     Net         = new NetApiService(Client);
     Personal    = new PersonalApiService(Client);
     Convert     = new UnitConversion();
     sha3Keccack = new Sha3Keccack();
     OfflineTransactionSigner = new TransactionSigner();
     addressUtil = new AddressUtil();
 }
Пример #20
0
 private void InitialiseInnerServices()
 {
     Eth         = new Eth(Client);
     Shh         = new Shh(Client);
     Net         = new Net(Client);
     Personal    = new Personal(Client);
     Miner       = new Miner(Client);
     DebugGeth   = new DebugGeth(Client);
     Admin       = new Admin(Client);
     Convert     = new UnitConversion();
     sha3Keccack = new Sha3Keccack();
 }
Пример #21
0
        public void SignPromise(EthECKey signingKey)
        {
            if (UserKeys.GetPublicKeyString(signingKey) != FromAddress)
            {
                throw new Exception("You cannot sign someone else's transaction.");
            }
            string promiseHash = CalculateHash();

            byte[] msgBytes = Encoding.UTF8.GetBytes(promiseHash);
            byte[] msgHash  = new Sha3Keccack().CalculateHash(msgBytes);
            Signature = signingKey.SignAndCalculateV(msgHash);
        }
Пример #22
0
        private string GenerateChannelName()
        {
            byte[] randomBytes = new byte[32];
            Random rng         = new Random();

            rng.NextBytes(randomBytes);
            Sha3Keccack hasher = new Sha3Keccack();
            string      key    = Encoding.UTF8.GetString(randomBytes, 0, 32);
            string      hash   = hasher.CalculateHash(key);

            return(hash.Substring(0, 7));
        }
        public byte[] GetHash(Guid id, string coinAddress, string clientAddr, string toAddr, BigInteger amount)
        {
            var strForHash = EthUtils.GuidToByteArray(id).ToHex() +
                             coinAddress.HexToByteArray().ToHex() +
                             clientAddr.HexToByteArray().ToHex() +
                             toAddr.HexToByteArray().ToHex() +
                             EthUtils.BigIntToArrayWithPadding(amount).ToHex();

            var hash = new Sha3Keccack().CalculateHash(strForHash.HexToByteArray());

            return(hash);
        }
Пример #24
0
        public void test5()
        {
            var signature =
                "0x7c87377d7a148ee69ec5ed9be2c87033639b6970acf9a8e165e81cf3dce4516d2bdcc6957eb435447ef9eaa34f541a81f999f0b967bfbfcca1850f58f4cdf5fe1c";
            var text    = "0x492d0fd814940d1375225a7e10905585b72b0a8c;-7440852294285764000;636706243532255384";
            var hasher  = new Sha3Keccack();
            var hash    = hasher.CalculateHash(text);
            var signer  = new EthereumMessageSigner();
            var account = signer.EncodeUTF8AndEcRecover(text, signature);

            Assert.Equal("0x492d0fd814940d1375225a7e10905585b72b0a8c".ToLower(), account.EnsureHexPrefix().ToLower());
        }
Пример #25
0
 public static bool verify(string dataStr, string signStr)
 {
     try
     {
         var sign     = EthECDSASignatureFactory.ExtractECDSASignature(signStr);
         var dataHash = new Sha3Keccack().CalculateHash(Encoding.UTF8.GetBytes(dataStr));
         var pubKey   = EthECKey.RecoverFromSignature(sign, dataHash);
         return(pubKey.Verify(dataHash, sign));
     }catch
     {
         return(false);
     }
 }
Пример #26
0
        public string RecoveryBouncy()
        {
            EthECKey.SignRecoverable = false;
            var signature =
                "0x0976a177078198a261faf206287b8bb93ebb233347ab09a57c8691733f5772f67f398084b30fc6379ffee2cc72d510fd0f8a7ac2ee0162b95dc5d61146b40ffa1c";
            var text    = "test";
            var hasher  = new Sha3Keccack();
            var hash    = hasher.CalculateHash(text);
            var signer  = new EthereumMessageSigner();
            var account = signer.EcRecover(hash.HexToByteArray(), signature);

            return(account);
        }
Пример #27
0
        private byte[] HashPrefixedMessage(byte[] message)
        {
            var byteList       = new List \ \ ();
            var bytePrefix     = "0x19".HexToByteArray();
            var textBytePrefix = Encoding.UTF8.GetBytes("Ethereum Signed Message:\n" + message.Length);

            byteList.AddRange(bytePrefix);
            byteList.AddRange(textBytePrefix);
            byteList.AddRange(message);
            var hash = new Sha3Keccack().CalculateHash(byteList.ToArray());

            return(hash);
        }
Пример #28
0
 private void InitialiseInnerServices()
 {
     Eth         = new Eth(Client);
     Shh         = new Shh(Client);
     Net         = new Net(Client);
     Personal    = new Personal(Client);
     Miner       = new Miner(Client);
     DebugGeth   = new DebugGeth(Client);
     Admin       = new Admin(Client);
     Convert     = new UnitConversion();
     sha3Keccack = new Sha3Keccack();
     OfflineTransactionSigning = new TransactionSigning();
     addressUtil = new AddressUtil();
 }
Пример #29
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());
        }
Пример #30
0
        public void Test22()
        {
            var guid = new Guid("0b67caf2-0b02-4691-ac25-04858b0fa475");

            var strForHash = EthUtils.GuidToByteArray(guid).ToHex() +
                             "0x8c32ad594a6dc17b2e8b40af3df2c3ce1f79cdd4".HexToByteArray().ToHex() +
                             "0x5c711f0bfad342c4691b6e23cedcdf39e3fe03c1".HexToByteArray().ToHex() +
                             "0x33f53d968aee3cfe1ad460457a29827bfff33d8c".HexToByteArray().ToHex() +
                             EthUtils.BigIntToArrayWithPadding(9500).ToHex();

            var hash = new Sha3Keccack().CalculateHash(strForHash.HexToByteArray());

            var sign = Sign(hash, "0x443dac9b6e682a887009cfd00690d0cac1905e494f84d03070174149c2d0cc76").ToHex();

            Assert.AreEqual("6ddfe17c1ff216df529277ff7fc94bff41b6984d3de36d2132f452986743de4c44831c1bd41d58043705a60aa853d6beeb8b2e0dcdb09805c5dd28b7e3e705ce1b", sign);
        }