コード例 #1
0
        public async Task SignTransactionAsync__Should_Return_V_R_and_S()
        {
            var address = await _client.CreateWalletAsync();

            var rawTxHash = new byte[32];

            var(v, r, s) = await _client.SignTransactionAsync(address, rawTxHash);

            v.Length
            .Should()
            .Be(1);

            r.Length
            .Should()
            .Be(32);

            s.Length
            .Should()
            .Be(32);
        }
コード例 #2
0
        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;
            }));
        }
コード例 #3
0
        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);
        }