Exemple #1
0
        public async Task <PublicServiceAccountKey> CheckServiceAccountKey(Key key, bool addWatch)
        {
            var m    = new ClientKeyCheckMessage(key, addWatch);
            var sent = await SendMessage(0, m);

            //Log.Trace("Sent CheckForChainAccountKey: " + sent);

            if (sent && await WaitResponse(m) is ClientKeyCheckResponseMessage checkResponse && checkResponse.KeyCheck != null)
            {
                return(await GetServiceAccountKey(key, checkResponse));
            }

            return(null);
        }
Exemple #2
0
        async Task OnKeyCheck(ClientKeyCheckMessage message, ClientConnection connection)
        {
            if (message.AddWatch)
            {
                _keyWatches.Add(message.KeyUniqueIdentifier, connection.ConnectionId);
            }

            var check = _chainManager.CoreChain.BlockStorage.History.GetRegistrationJoinHistory(message.KeyUniqueIdentifier);

            if (message.RequestCode != 0)
            {
                await connection.Send(new ClientKeyCheckResponseMessage(message.RequestCode, check) { SignKey = _configuration.LocaleNodePrivateKey });
            }
        }
        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);
            }