/// <summary> /// Sign a prroof that a free service provider provided free content to a Witness. Should be signed by wallet owner as Witness /// </summary> /// <param name="offchainTransaction"></param> /// <returns></returns> public SignedOffchainFreeServiceTransaction signFreeServiceProviderMbs(SignedOffchainFreeServiceTransaction offchainTransaction) { var signer = new MessageSigner(); var encoder = new ABIEncode(); ABIValue[] ABIValues = new ABIValue[] { new ABIValue("uint", offchainTransaction.epoch), new ABIValue("address", offchainTransaction.freeServiceProvider), new ABIValue("address", offchainTransaction.validator), new ABIValue("uint256", offchainTransaction.freeMb) }; var payloadEncoded = encoder.GetABIEncodedPacked(ABIValues); var proof = Sha3Keccack.Current.CalculateHash(payloadEncoded); offchainTransaction.h = proof; var signedTx = signer.Sign(offchainTransaction.h, _wallet.PK); var signature = MessageSigner.ExtractEcdsaSignature(signedTx); offchainTransaction.v = signature.V.FirstOrDefault(); offchainTransaction.r = signature.R; offchainTransaction.s = signature.S; if (offchainTransaction.s.Length < 32) { var tmpS = offchainTransaction.s.ToList(); tmpS.Insert(0, 0); offchainTransaction.s = tmpS.ToArray(); } offchainTransaction.signer = _wallet.address; return(offchainTransaction); }
/// <summary> /// Sign an Offchain payment as a Peer /// </summary> /// <param name="offchainTransaction">The transaction to be signed</param> /// <returns>The Transaction signed by wallet owner as Peer </returns> public SignedOffchainTransaction signOffchainPayment(SignedOffchainTransaction offchainTransaction) { var signer = new MessageSigner(); var encoder = new ABIEncode(); ABIValue[] ABIValues = new ABIValue[] { new ABIValue("address", offchainTransaction.beneficiary), new ABIValue("bytes32", offchainTransaction.nonce), new ABIValue("uint256", offchainTransaction.amount), new ABIValue("uint", offchainTransaction.fee) }; var payloadEncoded = encoder.GetABIEncodedPacked(ABIValues); var proof = Sha3Keccack.Current.CalculateHash(payloadEncoded); offchainTransaction.h = proof; var signedTx = signer.Sign(offchainTransaction.h, _wallet.PK); var signature = MessageSigner.ExtractEcdsaSignature(signedTx); offchainTransaction.v = signature.V.FirstOrDefault(); offchainTransaction.r = signature.R; offchainTransaction.s = signature.S; if (offchainTransaction.s.Length < 32) { var tmpS = offchainTransaction.s.ToList(); tmpS.Insert(0, 0); offchainTransaction.s = tmpS.ToArray(); } offchainTransaction.signer = _wallet.address; return(offchainTransaction); }
internal Order CreateOrder(OrderType orderType, BigInteger expires, BigInteger price, BigInteger amount) { var amountBigNum = orderType == OrderType.Buy ? amount / price : amount; var amountBaseBigNum = amount * price; var contractAddr = Config.AddressEtherDelta; var tokenGet = orderType == OrderType.Buy ? Config.Token : ZeroToken; var tokenGive = orderType == OrderType.Sell ? Config.Token : ZeroToken; var amountGet = orderType == OrderType.Buy ? amountBigNum : amountBaseBigNum; var amountGive = orderType == OrderType.Sell ? amountBigNum : amountBaseBigNum; var orderNonce = new Random().Next(); var plainData = new object[] { Config.AddressEtherDelta, tokenGive, amountGet, tokenGive, amountGive, expires, orderNonce }; var prms = new[] { new Parameter("address", 1), new Parameter("address", 1), new Parameter("uint256", 1), new Parameter("address", 1), new Parameter("uint256", 1), new Parameter("uint256", 1), new Parameter("uint256", 1) }; var pe = new ParametersEncoder(); var data = pe.EncodeParameters(prms, plainData); var ms = new MessageSigner(); var signature = ms.HashAndSign(data, Config.PrivateKey); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); var order = new Order { AmountGet = new HexBigInteger(amountGet), AmountGive = new HexBigInteger(amountGive), TokenGet = tokenGet, TokenGive = tokenGive, ContractAddr = contractAddr, Expires = expires, Nonce = orderNonce, User = Config.User, V = ethEcdsa.V, R = ethEcdsa.R.ToHex(true), S = ethEcdsa.S.ToHex(true) }; return(order); }
public EthECDSASignature Sign(string contract, string makerAddress, string makerToken, string takerToken, string makerAmount, string takerAmount, string expires, string nonce) { var plainData = new Object[] { contract, takerToken, BigInteger.Parse(takerAmount), makerToken, BigInteger.Parse(makerAmount), BigInteger.Parse(expires), BigInteger.Parse(nonce) }; var prms = new[] { new Parameter("address", 1), new Parameter("address", 1), new Parameter("uint256", 1), new Parameter("address", 1), new Parameter("uint256", 1), new Parameter("uint256", 1), new Parameter("uint256", 1) }; var data = SolidityPack(plainData, prms); var keystoreCrypto = new KeyStoreCrypto(); //for etherDelta its SHA256, for IDEX just change with SHA3 var hashed = keystoreCrypto.CalculateSha256Hash(data); var signer = new EthereumMessageSigner(); var newHash = signer.HashPrefixedMessage(hashed); var signatureRaw = signer.SignAndCalculateV(newHash, _userDataProvider.PrivateKey); var signature = EthECDSASignature.CreateStringSignature(signatureRaw); var probe = signer.EcRecover(hashed, signature); var ethEcdsa = MessageSigner.ExtractEcdsaSignature(signature); if (probe == makerAddress) { return(ethEcdsa); } //depending on usage, but it would be better to throw exc here if fails return(null); //throw new Exception("Signing failed"); }
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 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()); }
/// <summary> /// Sign an Offchain Payemnt as Relayer using Wallet private key /// </summary> /// <param name="offchainTransaction">The already signed by Peer Offchain Payment payload</param> /// <returns>The transaction signed by Wallet owner as Relayer</returns> public async Task <SignedOffchainTransaction> relayerSignOffchainPayment(SignedOffchainTransaction offchainTransaction) { //Check that already signed payload is valid var validSignature = await checkOffchainSignature(offchainTransaction); if (!validSignature) { throw new Exception("Invalid signature"); } var signer = new MessageSigner(); var encoder = new ABIEncode(); ABIValue[] ABIValues = new ABIValue[] { new ABIValue("bytes32", offchainTransaction.h), new ABIValue("uint", offchainTransaction.txUntilBlock) }; var payloadEncoded = encoder.GetABIEncodedPacked(ABIValues); var proof = Sha3Keccack.Current.CalculateHash(payloadEncoded); offchainTransaction.rh = proof; var signedTx = signer.Sign(offchainTransaction.rh, _wallet.PK); var signature = MessageSigner.ExtractEcdsaSignature(signedTx); offchainTransaction.rv = signature.V.FirstOrDefault(); offchainTransaction.rr = signature.R; offchainTransaction.rs = signature.S; if (offchainTransaction.rs.Length < 32) { var tmpS = offchainTransaction.rs.ToList(); tmpS.Insert(0, 0); offchainTransaction.rs = tmpS.ToArray(); } offchainTransaction.relayerId = _wallet.address; return(offchainTransaction); }