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); }
/// <summary> /// Encodes data according to EIP-712, hashes it and signs with <paramref name="key"/>. /// Matches the signature produced by eth_signTypedData_v4 /// </summary> public string SignTypedDataV4(TypedData typedData, EthECKey key) { var encodedData = EncodeTypedData(typedData); var signature = key.SignAndCalculateV(Sha3Keccack.Current.CalculateHash(encodedData)); return(EthECDSASignature.CreateStringSignature(signature)); }
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; }
/// <summary> /// Signs using a predefined typed data schema and converts and encodes the provide the message value /// </summary> public string SignTypedDataV4 <T, TDomain>(T message, TypedData <TDomain> typedData, EthECKey key) where TDomain : IDomain { var encodedData = EncodeTypedData(message, typedData); var signature = key.SignAndCalculateV(Sha3Keccack.Current.CalculateHash(encodedData)); return(EthECDSASignature.CreateStringSignature(signature)); }
public byte[] Sign(byte[] data) { var key = new EthECKey(this.PrivateKey); var signed = key.SignAndCalculateV(this.HashMessage(data)); var result = new byte[signed.To64ByteArray().Length + 1]; signed.To64ByteArray().CopyTo(result, 0); result[result.Length - 1] = (byte)(signed.V[0] - 27); return(result); }
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); }
private byte[] Sign(byte[] hash, string privateKey) { var key = new EthECKey(privateKey.HexToByteArray(), true); EthECDSASignature signature = key.SignAndCalculateV(hash); //ToByteArrayUnsigned var r = signature.R.ToHex(); var s = signature.S.ToHex(); var v = new[] { signature.V }.ToHex(); var arr = (r + s + v).HexToByteArray(); return(arr); }
public async Task SignOld() { string hex = ""; string privateKey = ""; var secret = new EthECKey(privateKey);; var hash = hex.HexToByteArray(); var signature = secret.SignAndCalculateV(hash); string r = signature.R.Length == 32 ? signature.R.ToHex() : "00" + signature.R.ToHex(); string s = signature.S.Length == 32 ? signature.S.ToHex() : "00" + signature.S.ToHex(); string v = new[] { signature.V }.ToHex(); var arrHex = (r + s + v); Trace.TraceInformation(arrHex); }
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); }
private static void Main(string[] args) { //EthECKey privateKey = EthECKey.GenerateKey(); EthECKey privateKey = new EthECKey("97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a"); string message = "exercise-cryptography"; byte[] messageBytes = Utils.GetBytes(message); byte[] messageHash = new Sha3Keccack().CalculateHash(messageBytes); EthECDSASignature signature = privateKey.SignAndCalculateV(messageHash); EthereumSignature result = new EthereumSignature { Signature = EncryptionUtils.ToString(signature), V = string.Format("0x{0:X2}", EthECKey.GetRecIdFromV(signature.V)), R = signature.R.ToHex(true), S = signature.S.ToHex(true) }; string output = JsonUtils.Serialize(result); File.WriteAllText("../../../Output.json", output); Console.WriteLine(output); }
public EthECDSASignature MessageSigningRec() { EthECKey.SignRecoverable = true; return(key.SignAndCalculateV(message)); }
public EthECDSASignature MessageSigning() => key.SignAndCalculateV(message);