示例#1
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);
示例#2
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);
 internal CryptoCreateTransactionBody(Hashgraph.CreateAccountParams createParameters) : this()
 {
     if (createParameters is null)
     {
         throw new ArgumentNullException(nameof(createParameters), "The create parameters are missing. Please check that the argument is not null.");
     }
     if (createParameters.Endorsement is null)
     {
         throw new ArgumentOutOfRangeException(nameof(createParameters), "The Endorsement for the account is missing, it is required.");
     }
     if (createParameters.AutoAssociationLimit < 0 || createParameters.AutoAssociationLimit > 1000)
     {
         throw new ArgumentOutOfRangeException(nameof(createParameters.AutoAssociationLimit), "The maximum number of auto-associaitons must be between zero and 1000.");
     }
     Key                           = new Key(createParameters.Endorsement);
     InitialBalance                = createParameters.InitialBalance;
     ReceiverSigRequired           = createParameters.RequireReceiveSignature;
     AutoRenewPeriod               = new Duration(createParameters.AutoRenewPeriod);
     ProxyAccountID                = createParameters.Proxy is null ? null : new AccountID(createParameters.Proxy);
     Memo                          = createParameters.Memo ?? string.Empty;
     MaxAutomaticTokenAssociations = createParameters.AutoAssociationLimit;
 }
示例#4
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 = new TransactionBody(context, transactionId);

            transactionBody.CryptoCreateAccount = new CryptoCreateTransactionBody
            {
                Key                 = publicKey,
                InitialBalance      = createParameters.InitialBalance,
                ReceiverSigRequired = createParameters.RequireReceiveSignature,
                AutoRenewPeriod     = new Duration(createParameters.AutoRenewPeriod),
                ProxyAccountID      = createParameters.Proxy is null ? null : new AccountID(createParameters.Proxy),
            };
            var receipt = await transactionBody.SignAndExecuteWithRetryAsync(signatories, context);

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

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

                record.FillProperties(rec);
            }
            else if (result is CreateAccountReceipt rcpt)
            {
                receipt.FillProperties(transactionId, rcpt);
            }
            return(result);
        }
示例#5
0
 /// <summary>
 /// Creates a new network account with a given initial balance
 /// and other values as indicated in the create parameters.
 /// </summary>
 /// <param name="createParameters">
 /// The account creation parameters, includes the initial balance,
 /// public key and values associated with the new account.
 /// </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 a description of the newly created account
 /// and record information.
 /// </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="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public Task <CreateAccountRecord> CreateAccountWithRecordAsync(CreateAccountParams createParameters, Action <IContext>?configure = null)
 {
     return(CreateAccountImplementationAsync <CreateAccountRecord>(createParameters, configure));
 }
示例#6
0
 /// <summary>
 /// Creates a new network account with a given initial balance
 /// and other values as indicated in the create parameters.
 /// </summary>
 /// <param name="createParameters">
 /// The account creation parameters, includes the initial balance,
 /// public key and values associated with the new account.
 /// </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 a description of the newly created account
 /// and record information.
 /// </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="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public async Task <CreateAccountRecord> CreateAccountWithRecordAsync(CreateAccountParams createParameters, Action <IContext>?configure = null)
 {
     return(new CreateAccountRecord(await ExecuteTransactionAsync(new CryptoCreateTransactionBody(createParameters), configure, true, createParameters.Signatory).ConfigureAwait(false)));
 }
示例#7
0
 /// <summary>
 /// Creates a new network account with a given initial balance
 /// and other values as indicated in the create parameters.
 /// </summary>
 /// <param name="createParameters">
 /// The account creation parameters, includes the initial balance,
 /// public key and values associated with the new account.
 /// </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 recipt with a description of the newly created 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>
 /// <exception cref="ConsensusException">If the network was unable to come to consensus before the duration of the transaction expired.</exception>
 /// <exception cref="TransactionException">If the network rejected the create request as invalid or had missing data.</exception>
 public Task <AccountReceipt> CreateAccountAsync(CreateAccountParams createParameters, Action <IContext>?configure = null)
 {
     return(CreateAccountImplementationAsync <AccountReceipt>(createParameters, configure));
 }