示例#1
0
        public static async Task <HeleusClientResponse> RequestRestore(long accountId, string name, string password, Key key)
        {
            HeleusClientResponse result = null;

            if (CurrentCoreAccount != null)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.CoreAccountAlreadyAvailable);
                goto end;
            }

            if (!IsValidPassword(password))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.PasswordError);
                goto end;
            }

            if (_busy)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Busy);
                goto end;
            }
            _busy = true;

            var coreAccountDownload = await Client.DownloadCoreAccount(accountId);

            var coreAccount = coreAccountDownload.Data;

            if (name.IsNullOrWhiteSpace())
            {
                name = $"Heleus Account ({accountId})";
            }

            if (coreAccount != null)
            {
                if (key.PublicKey == coreAccount.AccountKey)
                {
                    var coreAccountStore = new CoreAccountKeyStore(name, accountId, key, password);
                    await coreAccountStore.DecryptKeyAsync(password, true);

                    await Client.StoreAccount(coreAccountStore);

                    CurrentCoreAccount = coreAccountStore;

                    result = new HeleusClientResponse(HeleusClientResultTypes.Ok);
                }
                else
                {
                    result = new HeleusClientResponse(HeleusClientResultTypes.RestoreInvalidSignatureKey);
                }
            }
            else
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.RestoreCoreAccountNotFound);
            }

end:
            await UIApp.PubSub.PublishAsync(new CoreAccountRegisterEvent(result, CurrentCoreAccount));

            if (CurrentCoreAccount != null)
            {
                await UnlockCoreAccount(password);
            }

            if (result.ResultType != HeleusClientResultTypes.Busy)
            {
                _busy = false;
            }

            return(result);
        }
示例#2
0
        public async Task <MessageSentEvent> SendMessage(Chat chat, string text)
        {
            var index         = chat.Index;
            var submitAccount = ServiceNode.GetSubmitAccount <MessageSubmitAccount>(chat.KeyIndex, index);

            if (submitAccount == null)
            {
                submitAccount = GenerateSubmitAccount(index);
            }

            await GenerateSecretExchangeKey(submitAccount, chat.FriendAccountId, chat.FriendKeyIndex);

            var result = await SetSubmitAccount(submitAccount, true);

            if (result != null)
            {
                goto end;
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Ok, (long)ServiceUserCodes.InvalidAttachement);
                goto end;
            }

            var secretKey = submitAccount.DefaultSecretKey;
            var record    = await EncrytpedRecord <MessageRecord> .EncryptRecord(secretKey, new MessageRecord(text));

            var transaction = new DataTransaction(submitAccount.AccountId, submitAccount.ChainId, MessageServiceInfo.MessageDataChainIndex);

            transaction.EnableFeature <PreviousAccountTransaction>(PreviousAccountTransaction.FeatureId);

            var sharedAccountIndex = transaction.EnableFeature <SharedAccountIndex>(SharedAccountIndex.FeatureId);

            sharedAccountIndex.Index = index;

            var data = transaction.EnableFeature <Data>(Data.FeatureId);

            data.AddBinary(MessageServiceInfo.MessageDataIndex, record.ToByteArray());

            var receiver = transaction.EnableFeature <Receiver>(Receiver.FeatureId);

            receiver.AddReceiver(submitAccount.FriendAccountId);

            transaction.EnableFeature(EnforceReceiverFriend.FeatureId);

            result = await _client.SendDataTransaction(transaction, true);

            if (result.TransactionResult == TransactionResultTypes.Ok)
            {
                if (!_chats.ContainsKey(chat.Index))
                {
                    _chats[chat.Index] = chat;
                    await SaveAsync();
                }

                UIApp.Run(() => chat.DownloadMessages(false));
            }

end:

            var @event = new MessageSentEvent(text, index, this, result);
            await UIApp.PubSub.PublishAsync(@event);

            return(@event);
        }
示例#3
0
 public JoinChainEvent(long accountId, int chainId, HeleusClientResponse response)
 {
     AccountId = accountId;
     ChainId   = chainId;
     Response  = response;
 }
示例#4
0
 public TodoListRegistrationEvent(HeleusClientResponse result, long listId) : base(result)
 {
     ListId = listId;
 }
示例#5
0
 public TodoListAccountDeleteEvent(HeleusClientResponse result, long listId, long accountId) : base(result)
 {
     ListId    = listId;
     AccountId = accountId;
 }
示例#6
0
 public TodoListNameChangedEvent(HeleusClientResponse result, long listId) : base(result)
 {
     ListId = listId;
 }
示例#7
0
 public TodoTaskStatusEvent(HeleusClientResponse result, TodoTaskStatusTypes itemStatus) : base(result)
 {
     ItemStatus = itemStatus;
 }
示例#8
0
 public InboxRenameEvent(string title, short keyIndex, MessageNode node, HeleusClientResponse heleusClientResponse) : base(node, heleusClientResponse)
 {
     Title    = title;
     KeyIndex = keyIndex;
 }
示例#9
0
 public MessageSentEvent(HeleusClientResponse result) : base(result)
 {
 }
示例#10
0
 public NoteUploadedEvent(HeleusClientResponse response)
 {
     Response = response;
 }
示例#11
0
 public MessageNodeClientEvent(MessageNode node, HeleusClientResponse heleusClientResponse) : base(heleusClientResponse)
 {
     Node = node;
 }
 public CoreAccountTransferEvent(HeleusClientResponse response)
 {
     Response = response;
 }
示例#13
0
 public UnfriendEvent(long accountId, MessageNode node, HeleusClientResponse heleusClientResponse) : base(node, heleusClientResponse)
 {
     AccountId = accountId;
 }
示例#14
0
 public VerifyUploadEvent(HeleusClientResponse response, ServiceNode serviceNode) : base(serviceNode)
 {
     Response = response;
 }
示例#15
0
        public static async Task UpdateChain(ChainKeyStore chainKey, string password, string chainName, string chainWebsite, WalletClient.ChainKey[] chainKeys, Uri[] endPoints, PurchaseInfo[] purchases, short[] revokedChainKeys, Uri[] revokedEndPoints, int[] revokedPurchases)
        {
            if (CurrentCoreAccount == null)
            {
                return;
            }


            HeleusClientResponse result    = null;
            ChainInfo            chainInfo = null;

            if (_busy)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Busy);
                goto end;
            }
            _busy = true;

            try
            {
                await chainKey.DecryptKeyAsync(password, true);

                await Client.SetChainAccount(chainKey, password);
            }
            catch
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.PasswordError);
                goto end;
            }

            try
            {
                var transaction = Client.NewChainUpdateTransaction(true);
                if (chainKeys != null)
                {
                    foreach (var ck in chainKeys)
                    {
                        transaction.AddChainKey(ck.SignedPublicKey);
                    }
                }
                if (endPoints != null)
                {
                    foreach (var ep in endPoints)
                    {
                        transaction.AddPublicEndpoint(ep.AbsoluteUri);
                    }
                }
                if (purchases != null)
                {
                    foreach (var p in purchases)
                    {
                        transaction.AddPurchase(p);
                    }
                }

                if (revokedChainKeys != null)
                {
                    foreach (var key in revokedChainKeys)
                    {
                        transaction.RevokeChainKey(key);
                    }
                }

                if (revokedEndPoints != null)
                {
                    foreach (var ep in revokedEndPoints)
                    {
                        transaction.RemovePublicEndPoint(ep.AbsoluteUri);
                    }
                }

                if (revokedPurchases != null)
                {
                    foreach (var p in revokedPurchases)
                    {
                        transaction.RemovePurchase(p);
                    }
                }

                transaction.UpdateChainName(chainName);
                transaction.UpdateChainWebsite(chainWebsite);

                if (!await Client.SetTargetChain(Protocol.CoreChainId))
                {
                    result = new HeleusClientResponse(HeleusClientResultTypes.EndpointConnectionError);
                    goto end;
                }

                result = await Client.UpdateChain(transaction, chainKeys);

                if (result.TransactionResult == TransactionResultTypes.Ok)
                {
                    chainInfo = (await Client.DownloadChainInfo((result.Transaction as ChainInfoOperation).ChainId)).Data;
                }
            }
            catch (Exception ex)
            {
                Log.IgnoreException(ex);
            }

end:
            await UIApp.PubSub.PublishAsync(new ChainRegistrationEvent(result, chainInfo));

            if (result.ResultType != HeleusClientResultTypes.Busy)
            {
                _busy = false;
            }
        }
示例#16
0
 public ProfileUploadEvent(long accountId, int chainId, HeleusClientResponse response)
 {
     AccountId = accountId;
     ChainId   = chainId;
     Response  = response;
 }
示例#17
0
        public static async Task <(HeleusClientResponse, PublicServiceAccountKey)> JoinChain(int chainId, long expires, Key accountChainKey, string accountPassword, string chainKeyName = null, string chainKeyPassword = null)
        {
            HeleusClientResponse    result    = null;
            PublicServiceAccountKey publicKey = null;

            if (!HasCoreAccount)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.NoCoreAccount);
                goto end;
            }

            if (_busy)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Busy);
                goto end;
            }

            _busy = true;

            if (!IsCoreAccountUnlocked)
            {
                if (!await CurrentCoreAccount.DecryptKeyAsync(accountPassword, false))
                {
                    result = new HeleusClientResponse(HeleusClientResultTypes.PasswordError);
                    goto end;
                }

                await UnlockCoreAccount(accountPassword);
            }

            if (!await Client.SetTargetChain(chainId))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.EndpointConnectionError);
                goto end;
            }

            var nextKeyIndex = (await Client.DownloadNextServiceAccountKeyIndex(CurrentCoreAccount.AccountId, chainId)).Data;

            if (nextKeyIndex == null)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.EndpointConnectionError);
                goto end;
            }

            if (!nextKeyIndex.IsValid)
            {
                var err = HeleusClientResultTypes.EndpointConnectionError;
                if (nextKeyIndex.ResultType == ResultTypes.AccountNotFound)
                {
                    err = HeleusClientResultTypes.NoCoreAccount;
                }

                result = new HeleusClientResponse(err);
                goto end;
            }

            publicKey = PublicServiceAccountKey.GenerateSignedPublicKey(CurrentCoreAccount.AccountId, chainId, expires, nextKeyIndex.Item, accountChainKey, CurrentCoreAccount.DecryptedKey);
            result    = await Client.JoinChain(chainId, publicKey, accountChainKey, chainKeyName, chainKeyPassword);

end:
            await UIApp.PubSub.PublishAsync(new JoinChainEvent(CurrentCoreAccount == null ? 0 : CurrentCoreAccount.AccountId, chainId, result));

            if (result.ResultType != HeleusClientResultTypes.Busy)
            {
                _busy = false;
            }

            return(result, publicKey);
        }
 public ChainRegistrationEvent(HeleusClientResponse response, ChainInfo chainInfo)
 {
     ChainOperation = response.Transaction as ChainInfoOperation;
     Response       = response;
     ChainInfo      = chainInfo;
 }
示例#19
0
 public NewTodoTaskEvent(HeleusClientResponse result) : base(result)
 {
 }
示例#20
0
 public SubscriptionEvent(HeleusClientResponse result, long accountId, bool subscribed) : base(result)
 {
     AccountId  = accountId;
     Subscribed = subscribed;
 }
示例#21
0
 protected TodoListEvent(HeleusClientResponse result, ServiceNode serviceNode) : base(result)
 {
     ServiceNode = serviceNode;
 }
示例#22
0
 public PurchaseEvent(HeleusClientResponse response)
 {
     Response = response;
 }
示例#23
0
 public TodoListDeletetEvent(HeleusClientResponse result, long listId) : base(result)
 {
     ListId = listId;
 }
示例#24
0
 public RequestRevenueEvent(HeleusClientResponse response, AccountRevenueInfo revenueInfo)
 {
     Response    = response;
     RevenueInfo = revenueInfo;
 }
示例#25
0
 public TodoListInvitationSentEvent(HeleusClientResponse result, long listId) : base(result)
 {
     ListId = listId;
 }
示例#26
0
        public static async Task <HeleusClientResponse> RegisterCoreAccount(string name, string password, Key key)
        {
            HeleusClientResponse result = null;

            if (CurrentCoreAccount != null)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.CoreAccountAlreadyAvailable);
                goto end;
            }

            if (_busy)
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.Busy);
                goto end;
            }
            _busy = true;

            if (!IsValidPassword(password))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.PasswordError);
                goto end;
            }

            if (name.IsNullOrWhiteSpace())
            {
                name = $"Heleus Account";
            }


            if (!await Client.SetTargetChain(Protocol.CoreChainId))
            {
                result = new HeleusClientResponse(HeleusClientResultTypes.EndpointConnectionError);
                goto end;
            }

            try
            {
                result = await Client.RegisterAccount(name, key, password);

                CurrentCoreAccount = Client.GetRegisteredCoreAccount(result);
            }
            catch (Exception ex)
            {
                Log.IgnoreException(ex);
            }

end:
            await UIApp.PubSub.PublishAsync(new CoreAccountRegisterEvent(result, CurrentCoreAccount));

            if (CurrentCoreAccount != null)
            {
                await UnlockCoreAccount(password);
            }

            if (result.ResultType != HeleusClientResultTypes.Busy)
            {
                _busy = false;
            }

            return(result);
        }
示例#27
0
 public SendInvitationEvent(long accountId, MessageNode node, HeleusClientResponse heleusClientResponse) : base(node, heleusClientResponse)
 {
     AccountId = accountId;
 }
示例#28
0
 public MessageSentEvent(string text, Chain.Index index, MessageNode node, HeleusClientResponse heleusClientResponse) : base(node, heleusClientResponse)
 {
     Text  = text;
     Index = index;
 }