Пример #1
0
        /// <summary>
        /// Retrieves the transaction record for a given transaction ID.
        /// </summary>
        /// <param name="transaction">
        /// Transaction identifier of the record
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A transaction record with the specified id, or an exception if not found.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="TransactionException">If the network has no record of the transaction or request has invalid or had missing data.</exception>
        public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null)
        {
            transaction             = RequireInputParameter.Transaction(transaction);
            await using var context = CreateChildContext(configure);
            var transactionId = Protobuf.ToTransactionID(transaction);
            // For the public version of this method, we do not know
            // if the transaction in question has come to consensus so
            // we need to get the receipt first (and wait if necessary).
            var query = new Query
            {
                TransactionGetReceipt = new TransactionGetReceiptQuery
                {
                    TransactionID = transactionId
                }
            };
            await Transactions.ExecuteNetworkRequestWithRetryAsync(context, query, getServerMethod, shouldRetry);

            // The Receipt status returned does notmatter in this case.
            // We may be retrieving a failed record (the status would not equal OK).
            var record = await GetTransactionRecordAsync(context, transactionId);

            var result = new TransactionRecord();

            Protobuf.FillRecordProperties(record, result);
            return(result);
Пример #2
0
        /// <summary>
        /// Retrieves the account records associated with an account that are presently
        /// held within the network.
        /// </summary>
        /// <param name="address">
        /// The Hedera Network Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null)
        {
            address = RequireInputParameter.Address(address);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transfers       = Transactions.CreateCryptoTransferList((payer, -context.FeeLimit), (gateway, context.FeeLimit));
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Get Account Records");
            var query           = new Query
            {
                CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery
                {
                    Header    = Transactions.SignQueryHeader(transactionBody, payer),
                    AccountID = Protobuf.ToAccountID(address)
                }
            };
            var response = await Transactions.ExecuteRequestWithRetryAsync(context, query, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, getResponseCode(response));
            return(response.CryptoGetAccountRecords.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
Пример #3
0
        /// <summary>
        /// Retrieves the account records associated with an account that are presently
        /// held within the network because they exceeded the receive or send threshold
        /// values for autogeneration of records.
        /// </summary>
        /// <param name="address">
        /// The Hedera Network Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetAccountRecordsAsync(Address address, Action <IContext>?configure = null)
        {
            address = RequireInputParameter.Address(address);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                CryptoGetAccountRecords = new CryptoGetAccountRecordsQuery
                {
                    Header    = Transactions.CreateAskCostHeader(),
                    AccountID = Protobuf.ToAccountID(address)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.CryptoGetAccountRecords.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.CryptoGetAccountRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                var precheckCode = getResponseHeader(response)?.NodeTransactionPrecheckCode ?? ResponseCodeEnum.Unknown;
                if (precheckCode != ResponseCodeEnum.Ok)
                {
                    throw new TransactionException("Unable to retrieve transaction records.", Protobuf.FromTransactionId(transactionId), (ResponseCode)precheckCode);
                }
            }
            return(response.CryptoGetAccountRecords.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
Пример #4
0
        /// <summary>
        /// Retrieves the records associated with an contract that are presently
        /// held within the network.
        /// </summary>
        /// <param name="contract">
        /// The Hedera Network Contract Address to retrieve associated records.
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A detailed description of the account.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        public async Task <TransactionRecord[]> GetContractRecordsAsync(Address contract, Action <IContext>?configure = null)
        {
            contract = RequireInputParameter.Contract(contract);
            await using var context = CreateChildContext(configure);
            var query = new Query
            {
                ContractGetRecords = new ContractGetRecordsQuery
                {
                    Header     = Transactions.CreateAskCostHeader(),
                    ContractID = Protobuf.ToContractID(contract)
                }
            };
            var response = await Transactions.ExecuteUnsignedAskRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

            long cost = (long)response.ContractGetRecordsResponse.Header.Cost;

            if (cost > 0)
            {
                var transactionId = Transactions.GetOrCreateTransactionID(context);
                query.ContractGetRecords.Header = await Transactions.CreateAndSignQueryHeaderAsync(context, cost, transactionId);

                response = await Transactions.ExecuteSignedRequestWithRetryAsync(context, query, getRequestMethod, getResponseHeader);

                ValidateResult.ResponseHeader(transactionId, getResponseHeader(response));
            }
            return(response.ContractGetRecordsResponse.Records.Select(record =>
            {
                var result = new TransactionRecord();
                Protobuf.FillRecordProperties(record, result);
                return result;
            }).ToArray());
Пример #5
0
        /// <summary>
        /// Internal implementation for Multi Account Transfer Crypto.
        /// Returns either a receipt or record or throws an exception.
        /// </summary>
        private async Task <TResult> TransferImplementationAsync <TResult>(Dictionary <Account, long> sendAccounts, Dictionary <Address, long> receiveAddresses, Action <IContext>?configure) where TResult : new()
        {
            var transferList = RequireInputParameter.MultiTransfers(sendAccounts, receiveAddresses);
            var context      = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payers          = sendAccounts.Keys.ToArray <ISigner>().Append(RequireInContext.Payer(context)).ToArray();
            var transfers       = Transactions.CreateCryptoTransferList(transferList);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateCryptoTransferTransactionBody(context, transfers, transactionId, "Transfer Crypto");
            var request         = Transactions.SignTransaction(transactionBody, payers);
            var precheck        = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to execute crypto transfer, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransferRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Transfers = Protobuf.FromTransferList(record.TransferList);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #6
0
        /// <summary>
        /// Internal implementation of the update Contract functionality.
        /// </summary>
        private async Task <TResult> UpdateContractImplementationAsync <TResult>(UpdateContractParams updateParameters, Action <IContext>?configure) where TResult : new()
        {
            updateParameters        = RequireInputParameter.UpdateParameters(updateParameters);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer              = RequireInContext.Payer(context);
            var signatory          = Transactions.GatherSignatories(context, updateParameters.Signatory);
            var updateContractBody = new ContractUpdateTransactionBody
            {
                ContractID = Protobuf.ToContractID(updateParameters.Contract)
            };

            if (updateParameters.Expiration.HasValue)
            {
                updateContractBody.ExpirationTime = Protobuf.ToTimestamp(updateParameters.Expiration.Value);
            }
            if (!(updateParameters.Administrator is null))
            {
                updateContractBody.AdminKey = Protobuf.ToPublicKey(updateParameters.Administrator);
            }
            if (updateParameters.RenewPeriod.HasValue)
            {
                updateContractBody.AutoRenewPeriod = Protobuf.ToDuration(updateParameters.RenewPeriod.Value);
            }
            if (!(updateParameters.File is null))
            {
                updateContractBody.FileID = Protobuf.ToFileId(updateParameters.File);
            }
            if (!string.IsNullOrWhiteSpace(updateParameters.Memo))
            {
                updateContractBody.Memo = updateParameters.Memo;
            }
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ContractUpdateInstance = updateContractBody;
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to update Contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord arec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, arec);
            }
            else if (result is TransactionReceipt arcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, arcpt);
            }
            return(result);
Пример #7
0
        /// <summary>
        /// Retrieves the transaction record for a given transaction ID.
        /// </summary>
        /// <param name="transaction">
        /// Transaction identifier of the record
        /// </param>
        /// <param name="configure">
        /// Optional callback method providing an opportunity to modify
        /// the execution configuration for just this method call.
        /// It is executed prior to submitting the request to the network.
        /// </param>
        /// <returns>
        /// A transaction record with the specified id, or an exception if not found.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">If required arguments are missing.</exception>
        /// <exception cref="InvalidOperationException">If required context configuration is missing.</exception>
        /// <exception cref="PrecheckException">If the gateway node create rejected the request upon submission.</exception>
        /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
        public async Task <TransactionRecord> GetTransactionRecordAsync(TxId transaction, Action <IContext>?configure = null)
        {
            transaction = RequireInputParameter.Transaction(transaction);
            var context       = CreateChildContext(configure);
            var transactionId = Protobuf.ToTransactionID(transaction);
            var record        = await GetTransactionRecordAsync(context, transactionId);

            var result = new TransactionRecord();

            Protobuf.FillRecordProperties(record, result);
            return(result);
        }
Пример #8
0
        /// <summary>
        /// Internal helper method implementing the file update service.
        /// </summary>
        public async Task <TResult> UpdateFileImplementationAsync <TResult>(UpdateFileParams updateParameters, Action <IContext>?configure) where TResult : new()
        {
            updateParameters        = RequireInputParameter.UpdateParameters(updateParameters);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer          = RequireInContext.Payer(context);
            var signatory      = Transactions.GatherSignatories(context, updateParameters.Signatory);
            var updateFileBody = new FileUpdateTransactionBody
            {
                FileID = Protobuf.ToFileId(updateParameters.File)
            };

            if (!(updateParameters.Endorsements is null))
            {
                updateFileBody.Keys = Protobuf.ToPublicKeyList(updateParameters.Endorsements);
            }
            if (updateParameters.Contents.HasValue)
            {
                updateFileBody.Contents = ByteString.CopyFrom(updateParameters.Contents.Value.ToArray());
            }
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.FileUpdate = updateFileBody;
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to update file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #9
0
        /// <summary>
        /// Internal implementation for Create Account
        /// Returns either a receipt or record or throws
        /// an exception.
        /// </summary>
        private async Task <TResult> CreateAccountImplementationAsync <TResult>(CreateAccountParams createParameters, Action <IContext>?configure) where TResult : new()
        {
            createParameters = RequireInputParameter.CreateParameters(createParameters);
            var context = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Create Account");
            // Create Account requires just the 32 bits of the public key, without the prefix.
            var publicKeyWithoutPrefix = Keys.ImportPublicEd25519KeyFromBytes(createParameters.PublicKey).Export(KeyBlobFormat.PkixPublicKey).TakeLast(32).ToArray();

            transactionBody.CryptoCreateAccount = new CryptoCreateTransactionBody
            {
                Key = new Proto.Key {
                    Ed25519 = ByteString.CopyFrom(publicKeyWithoutPrefix)
                },
                InitialBalance         = createParameters.InitialBalance,
                SendRecordThreshold    = createParameters.SendThresholdCreateRecord,
                ReceiveRecordThreshold = createParameters.ReceiveThresholdCreateRecord,
                ReceiverSigRequired    = createParameters.RequireReceiveSignature,
                AutoRenewPeriod        = Protobuf.ToDuration(createParameters.AutoRenewPeriod),
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to create account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is AccountReceipt arcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, arcpt);
                arcpt.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            else if (result is AccountRecord arec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, arec);
                arec.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            return(result);
Пример #10
0
        /// <summary>
        /// Internal implementation for Create Account
        /// Returns either a receipt or record or throws
        /// an exception.
        /// </summary>
        private async Task <TResult> CreateAccountImplementationAsync <TResult>(CreateAccountParams createParameters, Action <IContext>?configure) where TResult : new()
        {
            var publicKey = RequireInputParameter.KeysFromEndorsements(createParameters);

            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatories     = Transactions.GatherSignatories(context, createParameters.Signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.CryptoCreateAccount = new CryptoCreateTransactionBody
            {
                Key                    = publicKey,
                InitialBalance         = createParameters.InitialBalance,
                SendRecordThreshold    = createParameters.SendThresholdCreateRecord,
                ReceiveRecordThreshold = createParameters.ReceiveThresholdCreateRecord,
                ReceiverSigRequired    = createParameters.RequireReceiveSignature,
                AutoRenewPeriod        = Protobuf.ToDuration(createParameters.AutoRenewPeriod),
                ProxyAccountID         = createParameters.Proxy is null ? null : Protobuf.ToAccountID(createParameters.Proxy),
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatories);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to create account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CreateAccountRecord arec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, arec);
                arec.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            else if (result is CreateAccountReceipt arcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, arcpt);
                arcpt.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            return(result);
Пример #11
0
        /// <summary>
        /// Internal Create Contract Implementation
        /// </summary>
        public async Task <TResult> CreateContractImplementationAsync <TResult>(CreateContractParams createParameters, Action <IContext>?configure) where TResult : new()
        {
            createParameters        = RequireInputParameter.CreateParameters(createParameters);
            await using var context = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatory       = Transactions.GatherSignatories(context, createParameters.Signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ContractCreateInstance = new ContractCreateTransactionBody
            {
                FileID                = Protobuf.ToFileId(createParameters.File),
                AdminKey              = createParameters.Administrator is null ? null : Protobuf.ToPublicKey(createParameters.Administrator),
                Gas                   = createParameters.Gas,
                InitialBalance        = createParameters.InitialBalance,
                AutoRenewPeriod       = Protobuf.ToDuration(createParameters.RenewPeriod),
                ConstructorParameters = ByteString.CopyFrom(Abi.EncodeArguments(createParameters.Arguments).ToArray()),
                Memo                  = context.Memo ?? ""
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to create contract, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CreateContractRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Contract = Protobuf.FromContractID(receipt.ContractID);
            }
            else if (result is CreateContractReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.Contract = Protobuf.FromContractID(receipt.ContractID);
            }
            return(result);
Пример #12
0
        /// <summary>
        /// Internal implementation of the contract call method.
        /// </summary>
        private async Task <TResult> SubmitMessageImplementationAsync <TResult>(Address topic, ReadOnlyMemory <byte> message, Signatory?signatory, Action <IContext>?configure) where TResult : new()
        {
            topic   = RequireInputParameter.Topic(topic);
            message = RequireInputParameter.Message(message);
            await using var context = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatories     = Transactions.GatherSignatories(context, signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ConsensusSubmitMessage = new ConsensusSubmitMessageTransactionBody
            {
                TopicID = Protobuf.ToTopicID(topic),
                Message = ByteString.CopyFrom(message.Span)
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatories);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Submit Message failed, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is SubmitMessageRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.RunningHash    = receipt.TopicRunningHash?.ToByteArray();
                rec.SequenceNumber = receipt.TopicSequenceNumber;
            }
            else if (result is SubmitMessageReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.RunningHash    = receipt.TopicRunningHash?.ToByteArray();
                rcpt.SequenceNumber = receipt.TopicSequenceNumber;
            }
            return(result);
Пример #13
0
        /// <summary>
        /// Internal implementation of the Create ConsensusTopic service.
        /// </summary>
        private async Task <TResult> CreateTopicImplementationAsync <TResult>(CreateTopicParams createParameters, Action <IContext>?configure) where TResult : new()
        {
            createParameters        = RequireInputParameter.CreateParameters(createParameters);
            await using var context = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatory       = Transactions.GatherSignatories(context, createParameters.Signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.ConsensusCreateTopic = new ConsensusCreateTopicTransactionBody
            {
                Memo             = createParameters.Memo,
                AdminKey         = createParameters.Administrator is null ? null : Protobuf.ToPublicKey(createParameters.Administrator),
                SubmitKey        = createParameters.Participant is null ? null : Protobuf.ToPublicKey(createParameters.Participant),
                AutoRenewPeriod  = Protobuf.ToDuration(createParameters.RenewPeriod),
                AutoRenewAccount = createParameters.RenewAccount is null ? null : Protobuf.ToAccountID(createParameters.RenewAccount)
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to create Consensus Topic, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CreateTopicRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Topic = Protobuf.FromTopicID(receipt.TopicID);
            }
            else if (result is CreateTopicReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.Topic = Protobuf.FromTopicID(receipt.TopicID);
            }
            return(result);
Пример #14
0
        /// <summary>
        /// Internal implementation of the Add Claim service.
        /// </summary>
        public async Task <TResult> AddClaimImplementationAsync <TResult>(Claim claim, Action <IContext>?configure) where TResult : new()
        {
            claim = RequireInputParameter.AddParameters(claim);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Add Claim");

            transactionBody.CryptoAddClaim = new CryptoAddClaimTransactionBody
            {
                Claim = new Proto.Claim
                {
                    AccountID     = Protobuf.ToAccountID(claim.Address),
                    Hash          = ByteString.CopyFrom(claim.Hash.ToArray()),
                    Keys          = Protobuf.ToPublicKeyList(claim.Endorsements),
                    ClaimDuration = Protobuf.ToDuration(claim.ClaimDuration)
                }
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to attach claim, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #15
0
        /// <summary>
        /// Internal implementation of the contract call method.
        /// </summary>
        private async Task <TResult> CallContractImplementationAsync <TResult>(CallContractParams callParmeters, Action <IContext>?configure) where TResult : new()
        {
            callParmeters = RequireInputParameter.CallContractParameters(callParmeters);
            var context         = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Call Contract");

            transactionBody.ContractCall = new ContractCallTransactionBody
            {
                ContractID         = Protobuf.ToContractID(callParmeters.Contract),
                Gas                = callParmeters.Gas,
                Amount             = callParmeters.PayableAmount,
                FunctionParameters = Abi.EncodeFunctionWithArguments(callParmeters.FunctionName, callParmeters.FunctionArgs).ToByteString()
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Contract call failed, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is CallContractRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.Contract   = Protobuf.FromContractID(record.Receipt.ContractID);
                rec.CallResult = Protobuf.FromContractCallResult(record.ContractCallResult);
            }
            else if (result is ContractReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.Contract = Protobuf.FromContractID(receipt.ContractID);
            }
            return(result);
Пример #16
0
        /// <summary>
        /// Internal implementation of the Create File service.
        /// </summary>
        public async Task <TResult> CreateFileImplementationAsync <TResult>(CreateFileParams createParameters, Action <IContext>?configure) where TResult : new()
        {
            createParameters        = RequireInputParameter.CreateParameters(createParameters);
            await using var context = CreateChildContext(configure);
            var gateway         = RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatory       = Transactions.GatherSignatories(context, createParameters.Signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.FileCreate = new FileCreateTransactionBody
            {
                ExpirationTime = Protobuf.ToTimestamp(createParameters.Expiration),
                Keys           = Protobuf.ToPublicKeyList(createParameters.Endorsements),
                Contents       = ByteString.CopyFrom(createParameters.Contents.ToArray()),
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatory);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to create file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is FileRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
                rec.File = Protobuf.FromFileID(receipt.FileID);
            }
            else if (result is FileReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
                rcpt.File = Protobuf.FromFileID(receipt.FileID);
            }
            return(result);
Пример #17
0
        /// <summary>
        /// Internal implementation of the Delete Claim Methods
        /// </summary>
        public async Task <TResult> DeleteClaimImplementationAsync <TResult>(Address address, ReadOnlyMemory <byte> hash, Action <IContext>?configure = null) where TResult : new()
        {
            address = RequireInputParameter.Address(address);
            hash    = RequireInputParameter.Hash(hash);
            var context = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Delete Claim");

            transactionBody.CryptoDeleteClaim = new CryptoDeleteClaimTransactionBody
            {
                AccountIDToDeleteFrom = Protobuf.ToAccountID(address),
                HashToDelete          = ByteString.CopyFrom(hash.ToArray())
            };
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to remove Claim, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #18
0
        /// <summary>
        /// Internal helper function implementing file append services.
        /// </summary>
        public async Task <TResult> AppendFileImplementationAsync <TResult>(AppendFileParams appendParameters, Action <IContext>?configure = null) where TResult : new()
        {
            appendParameters = RequireInputParameter.AppendParameters(appendParameters);
            var context = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payer          = RequireInContext.Payer(context);
            var appendFileBody = new FileAppendTransactionBody
            {
                FileID   = Protobuf.ToFileId(appendParameters.File),
                Contents = ByteString.CopyFrom(appendParameters.Contents.ToArray())
            };
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Append File Content");

            transactionBody.FileAppend = appendFileBody;
            var request  = Transactions.SignTransaction(transactionBody, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to append to file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #19
0
        /// <summary>
        /// Internal helper function implementing the file delete functionality.
        /// </summary>
        public async Task <TResult> DeleteFileImplementationAsync <TResult>(Address fileToDelete, Signatory?signatory, Action <IContext>?configure = null) where TResult : new()
        {
            fileToDelete            = RequireInputParameter.FileToDelete(fileToDelete);
            await using var context = CreateChildContext(configure);
            RequireInContext.Gateway(context);
            var payer           = RequireInContext.Payer(context);
            var signatories     = Transactions.GatherSignatories(context, signatory);
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateTransactionBody(context, transactionId);

            transactionBody.FileDelete = new FileDeleteTransactionBody
            {
                FileID = Protobuf.ToFileId(fileToDelete)
            };
            var request = await Transactions.SignTransactionAsync(transactionBody, signatories);

            var precheck = await Transactions.ExecuteSignedRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to delete file, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is TransactionRecord rec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, rec);
            }
            else if (result is TransactionReceipt rcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, rcpt);
            }
            return(result);
Пример #20
0
        /// <summary>
        /// Internal implementation of the update account functionality.
        /// </summary>
        private async Task <TResult> UpdateAccountImplementationAsync <TResult>(UpdateAccountParams updateParameters, Action <IContext>?configure) where TResult : new()
        {
            updateParameters = RequireInputParameter.UpdateParameters(updateParameters);
            var context = CreateChildContext(configure);

            RequireInContext.Gateway(context);
            var payer             = RequireInContext.Payer(context);
            var updateAccountBody = new CryptoUpdateTransactionBody
            {
                AccountIDToUpdate = Protobuf.ToAccountID(updateParameters.Account)
            };

            if (!(updateParameters.Endorsement is null))
            {
                updateAccountBody.Key = Protobuf.ToPublicKey(updateParameters.Endorsement);
            }
            if (updateParameters.SendThresholdCreateRecord.HasValue)
            {
                updateAccountBody.SendRecordThresholdWrapper = updateParameters.SendThresholdCreateRecord.Value;
            }
            if (updateParameters.ReceiveThresholdCreateRecord.HasValue)
            {
                updateAccountBody.ReceiveRecordThresholdWrapper = updateParameters.ReceiveThresholdCreateRecord.Value;
            }
            if (updateParameters.AutoRenewPeriod.HasValue)
            {
                updateAccountBody.AutoRenewPeriod = Protobuf.ToDuration(updateParameters.AutoRenewPeriod.Value);
            }
            if (updateParameters.Expiration.HasValue)
            {
                updateAccountBody.ExpirationTime = Protobuf.ToTimestamp(updateParameters.Expiration.Value);
            }
            var transactionId   = Transactions.GetOrCreateTransactionID(context);
            var transactionBody = Transactions.CreateEmptyTransactionBody(context, transactionId, "Update Account");

            transactionBody.CryptoUpdateAccount = updateAccountBody;
            var request  = Transactions.SignTransaction(transactionBody, updateParameters.Account, payer);
            var precheck = await Transactions.ExecuteRequestWithRetryAsync(context, request, getRequestMethod, getResponseCode);

            ValidateResult.PreCheck(transactionId, precheck.NodeTransactionPrecheckCode);
            var receipt = await GetReceiptAsync(context, transactionId);

            if (receipt.Status != ResponseCodeEnum.Success)
            {
                throw new TransactionException($"Unable to update account, status: {receipt.Status}", Protobuf.FromTransactionId(transactionId), (ResponseCode)receipt.Status);
            }
            var result = new TResult();

            if (result is AccountRecord arec)
            {
                var record = await GetTransactionRecordAsync(context, transactionId);

                Protobuf.FillRecordProperties(record, arec);
                arec.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            else if (result is AccountReceipt arcpt)
            {
                Protobuf.FillReceiptProperties(transactionId, receipt, arcpt);
                arcpt.Address = Protobuf.FromAccountID(receipt.AccountID);
            }
            return(result);