Example #1
0
 public void AddNewAccount(AccountRegistrationCoreTransaction transaction, CoreAccount account)
 {
     NewAccounts.Add(new AccountRegistrationInfo(transaction, new AccountOperation(account.AccountId, account.AccountKey, transaction.Timestamp)));
 }
        public async Task <HeleusClientResponse> RegisterAccount(string name, Key key, string password)
        {
            if (name.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("Name is empty.", nameof(name));
            }
            if (password.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("Password is empty.", nameof(password));
            }
            if (key == null)
            {
                key = Key.Generate(Protocol.TransactionKeyType);
            }

            if (key.KeyType != Protocol.TransactionKeyType || !key.IsPrivate)
            {
                throw new ArgumentException("Key type is wrong.", nameof(key));
            }

            try
            {
                var transaction = new AccountRegistrationCoreTransaction(key.PublicKey)
                {
                    SignKey = key
                };
                var response = await SendTransaction(transaction, true);

                if (response.TransactionResult == TransactionResultTypes.AlreadyProcessed)
                {
                    var m    = new ClientKeyCheckMessage(key, false);
                    var sent = await SendMessage(0, m);

                    if (sent && await WaitResponse(m) is ClientKeyCheckResponseMessage checkResponse && checkResponse.KeyCheck != null)
                    {
                        var check = checkResponse.KeyCheck;
                        if (check != null && check.ChainId == Protocol.CoreChainId)
                        {
                            var coreAccount = (await DownloadCoreAccount(check.AccountId)).Data;
                            if (coreAccount != null)
                            {
                                if (coreAccount.AccountKey == key.PublicKey)
                                {
                                    var account = await StoreAccount($"{name} ({check.AccountId})", check.AccountId, key, password);

                                    if (CurrentCoreAccount == null)
                                    {
                                        await SetCoreAccount(account, password);
                                    }

                                    return(new HeleusClientResponse(HeleusClientResultTypes.Ok, TransactionResultTypes.Ok, new AccountOperation(account.AccountId, key, 0), 0));
                                }
                            }
                        }
                    }
                }

                var operation = response.Transaction;
                if (operation != null && operation is AccountOperation registration && registration.PublicKey == key.PublicKey)
                {
                    var accountId = registration.AccountId;
                    Log.Trace($"New account registered with id {accountId} and public key {key.PublicKey.HexString} with transaction id {operation.OperationId}.", this);

                    var account = await StoreAccount($"{name} ({accountId})", accountId, key, password);

                    if (CurrentCoreAccount == null)
                    {
                        await SetCoreAccount(account, password);
                    }

                    _ = SendMessage(accountId, new ClientInfoMessage(new ClientInfo(accountId, _clientKey)));
                }
                else
                {
                    Log.Trace($"Account registration failed.", this);
                }

                return(response);
            }
Example #3
0
 public AccountRegistrationInfo(AccountRegistrationCoreTransaction transaction, AccountOperation operation)
 {
     Transaction = transaction;
     Operation   = operation;
 }