public override async Task SignAsync(CeloTransactionChainId transaction) { var txMessage = new EthereumSignTx { Nonce = transaction.Nonce, GasPrice = transaction.GasPrice, GasLimit = transaction.GasLimit, To = transaction.ReceiveAddress, Value = transaction.Value, AddressNs = GetPath(), ChainId = (uint)new BigInteger(transaction.ChainId) }; if (transaction.Data.Length > 0) { txMessage.DataInitialChunk = transaction.Data; txMessage.DataLength = (uint)transaction.Data.Length; } var signature = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage); if (signature.SignatureS == null || signature.SignatureR == null) { throw new Exception("Signing failure or not accepted"); } transaction.SetSignature(EthECDSASignatureFactory.FromComponents(signature.SignatureR, signature.SignatureS, (byte)signature.SignatureV)); }
public EthECDSASignature GetSignature() { if (!IsSigned()) { return(null); } return(EthECDSASignatureFactory.FromComponents(R, S, V)); }
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); } }
/// <summary> /// Returns HoardID that signed this transaction /// </summary> /// <param name="signatureStr"></param> /// <param name="rlpEncodedTransaction"></param> /// <returns>Signer of this transaction</returns> public static HoardID RecoverHoardIdFromTransaction(string signatureStr, byte[] rlpEncodedTransaction) { if (string.IsNullOrEmpty(signatureStr)) { return(null); } var signature = EthECDSASignatureFactory.ExtractECDSASignature(signatureStr); var rawHash = new Nethereum.Util.Sha3Keccack().CalculateHash(rlpEncodedTransaction); return(new HoardID(EthECKey.RecoverFromSignature(signature, rawHash).GetPublicAddress())); }
public string Recover(string message, byte[] signature, bool preFixed) { var bytes = Encoding.ASCII.GetBytes(message); if (!preFixed) { bytes = this.HashMessage(bytes); } var ecdaSignature = EthECDSASignatureFactory.ExtractECDSASignature(signature.ToHex()); return(Bech32.Encode("io", Hash.Hash160B(EthECKey.RecoverFromSignature(ecdaSignature, bytes).GetPubKey().Slice(1)))); }
private static void Main(string[] args) { string input = File.ReadAllText("../../../Input.json"); EthereumSignature inputSignature = JsonConvert.DeserializeObject <EthereumSignature>(input); byte[] messageBytes = Utils.GetBytes(inputSignature.Message); byte[] messageHash = new Sha3Keccack().CalculateHash(messageBytes); int recId = inputSignature.V.HexToByteArray()[0]; EthECDSASignature signature = EthECDSASignatureFactory.FromComponents( inputSignature.R.HexToByteArray(), inputSignature.S.HexToByteArray()); EthECKey publicKey = EthECKey.RecoverFromSignature(signature, recId, messageHash); Console.WriteLine("Public address: {0}", publicKey.GetPublicAddress()); }
public virtual void RlpDecode() { var decodedTxList = RLP.Decode(rlpEncoded); var transaction = (RLPCollection)decodedTxList[0]; nonce = transaction[0].RLPData; gasPrice = transaction[1].RLPData; gasLimit = transaction[2].RLPData; receiveAddress = transaction[3].RLPData; value = transaction[4].RLPData; data = transaction[5].RLPData; // only parse signature in case tx is signed if (transaction[6].RLPData != null) { var v = transaction[6].RLPData[0]; var r = transaction[7].RLPData; var s = transaction[8].RLPData; signature = EthECDSASignatureFactory.FromComponents(r, s, v); } decoded = true; }
public static void Verify() { string input = File.ReadAllText("../../Output.json"); EthereumSignature inputSignature = JsonConvert.DeserializeObject <EthereumSignature>(input); string message = inputSignature.Msg; byte[] messageBytes = Utils.GetBytes(message); byte[] messageHash = new Sha3Keccack().CalculateHash(messageBytes); byte[] rawSignature = inputSignature.Sig.HexToByteArray(); byte[] r = ArrayUtils.SubArray(rawSignature, 0, 32); byte[] s = ArrayUtils.SubArray(rawSignature, 32, 32); int recId = rawSignature[rawSignature.Length - 1]; EthECDSASignature signature = EthECDSASignatureFactory.FromComponents(r, s); EthECKey publicKey = EthECKey.RecoverFromSignature(signature, recId, messageHash); bool valid = publicKey.GetPublicAddress() == inputSignature.Address; Console.WriteLine("{0}", valid ? "Valid" : "Invalid"); }
private static EthECDSASignature ExtractEcdsaSignature(string signature) { var signatureArray = signature.HexToByteArray(); var v = signatureArray[64]; if ((v == 0) || (v == 1)) { v = (byte)(v + 27); } var r = new byte[32]; Array.Copy(signatureArray, r, 32); var s = new byte[32]; Array.Copy(signatureArray, 32, s, 0, 32); var ecdaSignature = EthECDSASignatureFactory.FromComponents(r, s, v); return(ecdaSignature); }
public override async Task SignAsync(Transaction transaction) { var txMessage = new EthereumSignTx { Nonce = transaction.Nonce, GasPrice = transaction.GasPrice, GasLimit = transaction.GasLimit, To = transaction.ReceiveAddress, Value = transaction.Value, AddressNs = GetPath(), }; if (transaction.Data.Length > 0) { txMessage.DataInitialChunk = transaction.Data; txMessage.DataLength = (uint)transaction.Data.Length; } var signature = await TrezorManager.SendMessageAsync <EthereumTxRequest, EthereumSignTx>(txMessage); transaction.SetSignature(EthECDSASignatureFactory.FromComponents(signature.SignatureR, signature.SignatureS, (byte)signature.SignatureV)); }
private async Task <string> BuildTransactionAsync( string operationId, string masterWalletAddress, string targetWalletAddress, string encodedData) { return(await _transactionRunner.RunWithTransactionAsync(async txContext => { var nonce = await _noncesRepository.GetNextNonceAsync(masterWalletAddress, txContext); var transaction = new Transaction( to: targetWalletAddress, amount: 0, nonce: nonce, gasPrice: _gasPrice, gasLimit: _gasLimit, data: encodedData ); _log.Info ( "Transaction has been built.", new { operationId, masterWalletAddress, nonce } ); try { var(v, r, s) = await _transactionSigner.SignTransactionAsync(masterWalletAddress, transaction.RawHash); var signature = EthECDSASignatureFactory.FromComponents(r: r, s: s, v: v); transaction.SetSignature(signature); #region Logging _log.Info ( "Transaction has been signed.", new { operationId, masterWalletAddress, nonce } ); #endregion } catch (Exception e) { #region Logging _log.Error ( e, "Failed to sign transaction.", new { operationId, masterWalletAddress, nonce } ); #endregion throw; } var transactionData = transaction.GetRLPEncoded().ToHex(true); try { await _operationsRepository.SetTransactionDataAsync( operationId, transactionData, txContext); #region Logging _log.Info ( "Transaction data has been saved.", new { operationId, masterWalletAddress, nonce } ); #endregion } catch (Exception e) { #region Logging _log.Error ( e, "Failed to save transaction data.", new { operationId, masterWalletAddress, nonce } ); #endregion throw; } return transactionData; })); }
public Transaction(byte[] nonce, byte[] gasPrice, byte[] gasLimit, byte[] receiveAddress, byte[] value, byte[] data, byte[] r, byte[] s, byte v) : this(nonce, gasPrice, gasLimit, receiveAddress, value, data) { signature = EthECDSASignatureFactory.FromComponents(r, s, v); }
private async Task <Dictionary <Guid, (string TxData, string TxHash)> > BuildTransactionsAsync( string masterWalletAddress, List <OperationDetails> operations) { var txDict = new Dictionary <Guid, Transaction>(); foreach (var operation in operations) { try { var transaction = _buildTransactionStrategies[operation.Type].Execute ( from: masterWalletAddress, nonce: operation.Nonce, operationId: operation.Id, operationPayloadJson: operation.JsonPayload ); txDict.Add(operation.Id, transaction); } catch (Exception e) { _log.Error ( e, "Failed to build transaction.", new { operation.Id, operation.Type, masterWalletAddress, operation.Nonce } ); throw; } } _log.Info($"{txDict.Count} transactions have been built."); try { var keys = txDict.Keys.ToArray(); var signedData = await _transactionSigner.SignTransactionsAsync( masterWalletAddress, keys.Select(k => txDict[k].RawHash).ToList()); for (int i = 0; i < keys.Length; ++i) { var(v, r, s) = signedData[i]; var signature = EthECDSASignatureFactory.FromComponents(r, s, v); txDict[keys[i]].SetSignature(signature); } _log.Info($"{txDict.Count} transactions have been signed."); } catch (Exception e) { _log.Error(e, $"Failed to sign {txDict.Count} transactions."); throw; } var transactionsDataAndHashes = new Dictionary <Guid, (string, string)>(txDict.Count); foreach (var pair in txDict) { var txData = pair.Value.GetRLPEncoded().ToHex(true); var txHash = $"0x{Sha3Keccack.Current.CalculateHashFromHex(txData)}"; transactionsDataAndHashes.Add(pair.Key, (txData, txHash)); } try { await _operationRepository.SetTransactionsDataAndHashesAsync(transactionsDataAndHashes); _log.Info($"Data for {transactionsDataAndHashes.Count} transactions have been saved."); } catch (Exception e) { _log.Error(e, "Failed to save transactions data."); throw; } return(transactionsDataAndHashes); }
private async Task <(string Data, string Hash)> BuildTransactionAsync( Guid operationId, string operationType, string masterWalletAddress, long nonce, string payloadJson) { Transaction transaction; try { transaction = _buildTransactionStrategies[operationType].Execute ( from: masterWalletAddress, nonce: nonce, operationId: operationId, operationPayloadJson: payloadJson ); #region Logging _log.Info ( "Transaction has been built.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion } catch (Exception e) { #region Logging _log.Error ( e, "Failed to build transaction.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion throw; } try { var(v, r, s) = await _transactionSigner.SignTransactionAsync(masterWalletAddress, transaction.RawHash); var signature = EthECDSASignatureFactory.FromComponents(r: r, s: s, v: v); transaction.SetSignature(signature); #region Logging _log.Info ( "Transaction has been signed.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion } catch (Exception e) { #region Logging _log.Error ( e, "Failed to sign transaction.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion throw; } var transactionData = transaction.GetRLPEncoded().ToHex(true); var transactionHash = $"0x{Sha3Keccack.Current.CalculateHashFromHex(transactionData)}"; try { await _operationRepository.SetTransactionDataAndHashAsync( operationId, transactionData, transactionHash); #region Logging _log.Info ( "Transaction data has been saved.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion } catch (Exception e) { #region Logging _log.Error ( e, "Failed to save transaction data.", new { operationId, operationType, masterWalletAddress, nonce } ); #endregion throw; } return(transactionData, transactionHash); }