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)); }
public async void ShouldDeployForwarder_CloneItUsingFactory_TransferEther() { var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011"; var web3 = _ethereumClientIntegrationFixture.GetWeb3(); //Getting the current Ether balance of the destination, we are going to transfer 0.001 ether var balanceDestination = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); var balanceDestinationEther = Web3.Web3.Convert.FromWei(balanceDestination); var defaultForwarderDeploymentReceipt = await ForwarderService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderDeployment()); var defaultForwaderContractAddress = defaultForwarderDeploymentReceipt.ContractAddress; var defaultForwarderService = new ForwarderService(web3, defaultForwaderContractAddress); await defaultForwarderService.ChangeDestinationRequestAndWaitForReceiptAsync(destinationAddress); var destinationInContract = await defaultForwarderService.DestinationQueryAsync(); Assert.True(destinationInContract.IsTheSameAddress(destinationAddress)); var factoryDeploymentReceipt = await ForwarderFactoryService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderFactoryDeployment()); var factoryAddress = factoryDeploymentReceipt.ContractAddress; var factoryService = new ForwarderFactoryService(web3, factoryDeploymentReceipt.ContractAddress); //New invovice var salt = BigInteger.Parse("12"); var saltHex = new IntTypeEncoder().Encode(salt).ToHex(); var contractCalculatedAddress = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex, defaultForwaderContractAddress); var txnReceipt = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt); var clonedAdress = txnReceipt.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress.IsTheSameAddress(contractCalculatedAddress)); var clonedForwarderService = new ForwarderService(web3, contractCalculatedAddress); var destinationInContractCloned = await clonedForwarderService.DestinationQueryAsync(); Assert.True(destinationInContractCloned.IsTheSameAddress(destinationAddress)); //gas is added due to forwarding var transferEtherReceipt = await web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractCalculatedAddress, 10, null, 4500000); // var forwardedDeposit = transferEtherReceipt.DecodeAllEvents<ForwarderDepositedEventDTO>()[0].Event; var balance = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); Assert.Equal(balanceDestinationEther + 10, Web3.Web3.Convert.FromWei(balance)); var balance2 = await web3.Eth.GetBalance.SendRequestAsync(contractCalculatedAddress); Assert.Equal(0, balance2.Value); }
private static byte[] EncodeData <T>(T structure) { byte[] result = new byte[0]; byte[] part = null; foreach (var prop in structure.GetMemberProperties()) { // TODO: Support array types object val = prop.Item1.GetValue(structure); string memberType = prop.Item2.Type; PropertyInfo propInfo = prop.Item1; if (memberType == "address") { if (!(val is string)) { throw new MemberTypeException(propInfo.Name, memberType, propInfo.PropertyType); } part = new AddressTypeEncoder().Encode(val); } else if (memberType == "bytes") { if (!(val is byte[] value)) { throw new MemberTypeException(propInfo.Name, memberType, propInfo.PropertyType); } part = _keccak.CalculateHash(value); } else if (memberType == "string") { if (!(val is string str)) { throw new MemberTypeException(propInfo.Name, memberType, propInfo.PropertyType); } part = _keccak.CalculateHash(Encoding.UTF8.GetBytes(str)); } else if (memberType == "bool") { if (!(val is bool)) { throw new MemberTypeException(propInfo.Name, memberType, propInfo.PropertyType); } part = new BoolTypeEncoder().Encode(val); } else if (Util.IsValidIntegerAbiType(memberType, out int intSize, out bool signed)) { if (!Util.IsNumber(val)) { throw new MemberTypeException(propInfo.Name, memberType, propInfo.PropertyType); } part = new IntTypeEncoder(signed, (uint)intSize).Encode(val); }
public TransactionInput CreateSetTopScoreTransactionInput(string addressFrom, string addressOwner, string privateKey, BigInteger score, HexBigInteger gas = null, HexBigInteger valueAmount = null) { var numberBytes = new IntTypeEncoder().Encode(score); var sha3 = new Nethereum.Util.Sha3Keccack(); var hash = sha3.CalculateHashFromHex(addressFrom, addressOwner, numberBytes.ToHex()); var signer = new MessageSigner(); var signature = signer.Sign(hash.HexToByteArray(), privateKey); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); var function = GetFunctionSetTopScore(); return(function.CreateTransactionInput(addressFrom, gas, valueAmount, score, ethEcdsa.V, ethEcdsa.R, ethEcdsa.S)); }
public TransactionInput closeRoomInput(string addressFrom, string privateKey, HexBigInteger gas, int id, HexBigInteger valueAmount = null) { var numberBytes = new IntTypeEncoder().Encode(id); var sha3 = new Nethereum.Util.Sha3Keccack(); var hash = sha3.CalculateHashFromHex(addressFrom, numberBytes.ToHex()); var signer = new MessageSigner(); var signature = signer.Sign(hash.HexToByteArray(), privateKey); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); object[] array = new object[] { id }; var function = closeRoomFunction(); return(function.CreateTransactionInput(addressFrom, gas, valueAmount, array)); }
public TransactionInput createUserInput(string addressFrom, string privateKey, HexBigInteger gas, string name, int pin) { var stringBytes = new StringTypeEncoder().Encode(name); var numberBytes = new IntTypeEncoder().Encode(pin); var sha3 = new Nethereum.Util.Sha3Keccack(); var hash = sha3.CalculateHashFromHex(addressFrom, numberBytes.ToHex(), stringBytes.ToHex()); var signer = new MessageSigner(); var signature = signer.Sign(hash.HexToByteArray(), privateKey); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); object[] array = new object[] { name, pin }; var function = createUserFunction(); return(function.CreateTransactionInput(addressFrom, array)); }
public static string Sha3ForCooperative(CooperativeSignDto cooperativeDto) { var sha3 = new Sha3Keccack(); var bob = new IntTypeEncoder() .Encode(StringToBigInteger(cooperativeDto.BobAmountWei)); var alice = new IntTypeEncoder() .Encode(StringToBigInteger(cooperativeDto.AliceAmountWei)); var output = sha3.CalculateHashFromHex(bob.ToHex(), alice.ToHex(), cooperativeDto.ChannelId); var resultHash = "0x" + output; return(resultHash); }
public void ShouldSignHash() { var address1 = "0x6A849e2036A36EB4Cd37b9aFA3c770064899f1A2"; var address2 = "0x12890D2cce102216644c59daE5baed380d84830c"; var numberBytes = new IntTypeEncoder().Encode(1); //Number is a big integer so we need 32 bytes if it was int32 it will be 4 bytes. Using the abi encoder it takes care of padding var sha3 = new Util.Sha3Keccack(); var output = sha3.CalculateHashFromHex(address1, address2, numberBytes.ToHex()); Assert.Equal("0xc11d3d2b8e0c5b8b645b9e7502751352ecaf8c3fdf3a0124dae9c1556fb2ce37", output.EnsureHexPrefix()); var signer = new MessageSigner(); var signature = signer.Sign(output.HexToByteArray(), "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7"); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); var adddress = signer.EcRecover(output.HexToByteArray(), signature); Assert.Equal(adddress, new EthECKey("0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7".HexToByteArray(), true).GetPublicAddress()); }
private byte[] SolidityPack(object[] paramsArray, Parameter[] paramsTypes) { var intTypeEncoder = new IntTypeEncoder(); var cursor = new List <byte>(); for (var i = 0; i < paramsArray.Length; i++) { if (paramsTypes[i].Type == "address") { cursor.AddRange(EncodePacked(paramsArray[i])); } else if (paramsTypes[i].Type == "uint256") { cursor.AddRange(intTypeEncoder.Encode(paramsArray[i])); } } return(cursor.ToArray()); }
public static string Sha3ForAudit(AuditSignDto auditDto) { var sha3 = new Sha3Keccack(); var bob = new IntTypeEncoder() .Encode(StringToBigInteger(auditDto.BobAmountWei)); var alice = new IntTypeEncoder() .Encode(StringToBigInteger(auditDto.AliceAmountWei)); var epoch = new IntTypeEncoder() .Encode(auditDto.EpochId); var output = sha3.CalculateHashFromHex(bob.ToHex(), alice.ToHex(), auditDto.ChannelId, epoch.ToHex()); var resultHash = "0x" + output; return(resultHash); }
private async Task <string> SendEtherAndCreateClone(Web3.Web3 web3, ForwarderFactoryService factoryService, decimal amount, string saltNumber, string factoryAddress, string defaultForwaderContractAddress) { //Lets create new contract to be paid var salt = BigInteger.Parse(saltNumber); //salt id var saltHex = new IntTypeEncoder().Encode(salt).ToHex(); //Calculate the new contract address var contractCalculatedAddress = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex, defaultForwaderContractAddress); //Let's tranfer some ether, with some extra gas to allow forwarding if the smart contract is deployed (UX problem) var transferEtherReceipt = await web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractCalculatedAddress, amount, null, 4500000); var txnReceipt = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt); var clonedAdress = txnReceipt.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress.IsTheSameAddress(contractCalculatedAddress)); return(contractCalculatedAddress); }
public static string Sha3ForReceipts(ReceiptDto receiptDto) { var sha3 = new Sha3Keccack(); var rec = new IntTypeEncoder() .Encode(receiptDto.ReceiptId); var epo = new IntTypeEncoder() .Encode(receiptDto.EpochId); var amo = new IntTypeEncoder() .Encode(StringToBigInteger(receiptDto.AmountWei)); var output = sha3.CalculateHashFromHex(rec.ToHex(), epo.ToHex(), amo.ToHex(), receiptDto.Address); var resultHash = "0x" + output; return(resultHash); }
public async void ShouldDeployForwarder_TransferToken_CloneItUsingFactory_FlushTokensUsinFactory() { var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011"; //Using ropsten infura //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten); var web3 = _ethereumClientIntegrationFixture.GetWeb3(); //Deploy our custom token var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, new ERC20TokenDeployment() { DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Web3.Convert.ToWei(10000) }); var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress); //Deploying first the default forwarder (template for all clones) var defaultForwarderDeploymentReceipt = await ForwarderService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderDeployment()); var defaultForwaderContractAddress = defaultForwarderDeploymentReceipt.ContractAddress; var defaultForwarderService = new ForwarderService(web3, defaultForwaderContractAddress); //initialiasing with the destination address await defaultForwarderService.ChangeDestinationRequestAndWaitForReceiptAsync(destinationAddress); var destinationInContract = await defaultForwarderService.DestinationQueryAsync(); //validate the destination address has been set correctly Assert.True(destinationInContract.IsTheSameAddress(destinationAddress)); //Deploying the factory var factoryDeploymentReceipt = await ForwarderFactoryService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderFactoryDeployment()); var factoryAddress = factoryDeploymentReceipt.ContractAddress; var factoryService = new ForwarderFactoryService(web3, factoryDeploymentReceipt.ContractAddress); //Lets create new salt var salt = BigInteger.Parse("12"); //12 var saltHex = new IntTypeEncoder().Encode(salt).ToHex(); //Calculate the new contract address var contractCalculatedAddress = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex, defaultForwaderContractAddress); //Lets create new salt for another var salt2 = BigInteger.Parse("13"); //13 var saltHex2 = new IntTypeEncoder().Encode(salt2).ToHex(); //Calculate the new contract address var contractCalculatedAddress2 = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex2, defaultForwaderContractAddress); var transferRecipt = await tokenService.TransferRequestAndWaitForReceiptAsync(contractCalculatedAddress, Web3.Web3.Convert.ToWei(0.001)); //Check the balance of the adress we sent.. we have not deployed the smart contract so it should be still the same var balanceContract = await tokenService.BalanceOfQueryAsync(contractCalculatedAddress); Assert.Equal((decimal)0.001, Web3.Web3.Convert.FromWei(balanceContract)); var transferReceipt2 = await tokenService.TransferRequestAndWaitForReceiptAsync(contractCalculatedAddress2, Web3.Web3.Convert.ToWei(0.001)); //Create the clone with the salt to match the address var txnReceipt = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt); var clonedAdress = txnReceipt.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress.IsTheSameAddress(contractCalculatedAddress)); var txnReceipt2 = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt2); var clonedAdress2 = txnReceipt2.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress2.IsTheSameAddress(contractCalculatedAddress2)); //Flushing from the factory var flushAllReceipt = await factoryService.FlushTokensRequestAndWaitForReceiptAsync(new List <string> { contractCalculatedAddress, contractCalculatedAddress2 }, tokenService.ContractHandler.ContractAddress); //////validate balances... for two forwarders of 0.001 + 0.001 var newbalanceDestination = await tokenService.BalanceOfQueryAsync(destinationAddress); Assert.Equal((decimal)0.001 + (decimal)0.001, Web3.Web3.Convert.FromWei(newbalanceDestination)); }
public async void ShouldDeployForwarder_TransferEther_CloneItUsingFactory_FlushEther2ClonesUsingFactory() { var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011"; //Using ropsten infura //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten); var web3 = _ethereumClientIntegrationFixture.GetWeb3(); //Getting the current Ether balance of the destination, we are going to transfer 0.001 ether var balanceDestination = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); var balanceDestinationEther = Web3.Web3.Convert.FromWei(balanceDestination); //Deploying first the default forwarder (template for all clones) var defaultForwarderDeploymentReceipt = await ForwarderService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderDeployment()); var defaultForwaderContractAddress = defaultForwarderDeploymentReceipt.ContractAddress; var defaultForwarderService = new ForwarderService(web3, defaultForwaderContractAddress); //initialiasing with the destination address await defaultForwarderService.ChangeDestinationRequestAndWaitForReceiptAsync(destinationAddress); var destinationInContract = await defaultForwarderService.DestinationQueryAsync(); //validate the destination address has been set correctly Assert.True(destinationInContract.IsTheSameAddress(destinationAddress)); //Deploying the factory var factoryDeploymentReceipt = await ForwarderFactoryService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderFactoryDeployment()); var factoryAddress = factoryDeploymentReceipt.ContractAddress; var factoryService = new ForwarderFactoryService(web3, factoryDeploymentReceipt.ContractAddress); //Lets create new contract to be paid var salt = BigInteger.Parse("12"); //salt id var saltHex = new IntTypeEncoder().Encode(salt).ToHex(); //Calculate the new contract address var contractCalculatedAddress = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex, defaultForwaderContractAddress); //Let's tranfer some ether, with some extra gas to allow forwarding if the smart contract is deployed (UX problem) var transferEtherReceipt = await web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractCalculatedAddress, (decimal)0.001, null, 4500000); //Lets create new contract to be paid var salt2 = BigInteger.Parse("13"); //salt id var saltHex2 = new IntTypeEncoder().Encode(salt2).ToHex(); //Calculate the new contract address var contractCalculatedAddress2 = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex2, defaultForwaderContractAddress); //Let's tranfer some ether, with some extra gas to allow forwarding if the smart contract is deployed (UX problem) var transferEtherReceipt2 = await web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractCalculatedAddress2, (decimal)0.001, null, 4500000); //Create the clone with the salt to match the address var txnReceipt = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt); var clonedAdress = txnReceipt.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress.IsTheSameAddress(contractCalculatedAddress)); //Create the clone2 with the salt to match the address var txnReceipt2 = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt2); var clonedAdress2 = txnReceipt2.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress2.IsTheSameAddress(contractCalculatedAddress2)); //Flushing from the factory var flushAllReceipt = await factoryService.FlushEtherRequestAndWaitForReceiptAsync(new List <string> { contractCalculatedAddress, contractCalculatedAddress2 }); //////validate balances... for two forwarders of 0.001 + 0.001 var newbalanceDestination = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); Assert.Equal((decimal)0.001 + (decimal)0.001 + balanceDestinationEther, Web3.Web3.Convert.FromWei(newbalanceDestination)); }
public async void ShouldDeployForwarder_TransferEther_CloneItUsingFactory_FlushEther() { var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011"; //Using ropsten infura //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten); var web3 = _ethereumClientIntegrationFixture.GetWeb3(); //Getting the current Ether balance of the destination, we are going to transfer 0.001 ether var balanceDestination = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); var balanceDestinationEther = Web3.Web3.Convert.FromWei(balanceDestination); //Deploying first the default forwarder (template for all clones) var defaultForwarderDeploymentReceipt = await ForwarderService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderDeployment()); var defaultForwaderContractAddress = defaultForwarderDeploymentReceipt.ContractAddress; var defaultForwarderService = new ForwarderService(web3, defaultForwaderContractAddress); //initialiasing with the destination address await defaultForwarderService.ChangeDestinationRequestAndWaitForReceiptAsync(destinationAddress); var destinationInContract = await defaultForwarderService.DestinationQueryAsync(); //validate the destination address has been set correctly Assert.True(destinationInContract.IsTheSameAddress(destinationAddress)); //Deploying the factory var factoryDeploymentReceipt = await ForwarderFactoryService.DeployContractAndWaitForReceiptAsync(web3, new ForwarderFactoryDeployment()); var factoryAddress = factoryDeploymentReceipt.ContractAddress; var factoryService = new ForwarderFactoryService(web3, factoryDeploymentReceipt.ContractAddress); //Lets create new invovice to be paid var salt = BigInteger.Parse("12"); //12 our invoice number var saltHex = new IntTypeEncoder().Encode(salt).ToHex(); //Calculate the new contract address var contractCalculatedAddress = CalculateCreate2AddressMinimalProxy(factoryAddress, saltHex, defaultForwaderContractAddress); //Let's tranfer some ether, with some extra gas to allow forwarding if the smart contract is deployed (UX problem) var transferEtherReceipt = await web3.Eth.GetEtherTransferService().TransferEtherAndWaitForReceiptAsync(contractCalculatedAddress, (decimal)0.001, null, 4500000); //Check the balance of the adress we sent.. we have not deployed the smart contract so it should be still the same var balanceContract = await web3.Eth.GetBalance.SendRequestAsync(contractCalculatedAddress); //Assert.Equal((decimal)0.001, Web3.Web3.Convert.FromWei(balanceContract.Value)); //Create the clone with the salt to match the address var txnReceipt = await factoryService.CloneForwarderRequestAndWaitForReceiptAsync(defaultForwaderContractAddress, salt); var clonedAdress = txnReceipt.DecodeAllEvents <ForwarderClonedEventDTO>()[0].Event.ClonedAdress; Assert.True(clonedAdress.IsTheSameAddress(contractCalculatedAddress)); //we should still have the same balance balanceContract = await web3.Eth.GetBalance.SendRequestAsync(contractCalculatedAddress); Assert.Equal((decimal)0.001, Web3.Web3.Convert.FromWei(balanceContract.Value)); //create a service to for cloned forwarder var clonedForwarderService = new ForwarderService(web3, contractCalculatedAddress); var destinationInContractCloned = await clonedForwarderService.DestinationQueryAsync(); //validate the destination address is the same Assert.True(destinationInContractCloned.IsTheSameAddress(destinationAddress)); //Using flush directly in the cloned contract //call flush to get all the ether transferred to destination address var flushReceipt = await clonedForwarderService.FlushRequestAndWaitForReceiptAsync(); balanceContract = await web3.Eth.GetBalance.SendRequestAsync(contractCalculatedAddress); //validate balances... var newbalanceDestination = await web3.Eth.GetBalance.SendRequestAsync(destinationAddress); Assert.Equal((decimal)0.001 + balanceDestinationEther, Web3.Web3.Convert.FromWei(newbalanceDestination)); }
public IntType(string name) : base(name) { Decoder = new IntTypeDecoder(); Encoder = new IntTypeEncoder(); }
public ParametersEncoder() { intTypeEncoder = new IntTypeEncoder(); }
public ParametersEncoder() { intTypeEncoder = new IntTypeEncoder(); attributesToABIExtractor = new AttributesToABIExtractor(); }
public IntType(string name) : base(name) { Decoder = new IntTypeDecoder(IsSigned(CanonicalName)); Encoder = new IntTypeEncoder(IsSigned(CanonicalName), GetSize(CanonicalName)); }