示例#1
0
        public async Task TestContactsAreCachedAndOnTangleShouldExcludeRejectedContactsAndSetCache()
        {
            var pubKeyAddress          = Seed.Random().Value;
            var contactRequestAddress  = new Address(Seed.Random().Value);
            var rejectedContactAddress = Seed.Random().Value;
            var storedContactAddress   = Seed.Random().Value;
            var keyPair = InMemoryContactRepository.NtruKeyPair;

            var contactRepository = new InMemoryContactRepository();
            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            await contactRepository.AddContactAsync(rejectedContactAddress, false, pubKeyAddress);

            var rejectedContactBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = rejectedContactAddress, PublicKeyAddress = rejectedContactAddress, Rejected = true
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var approvedContactBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = storedContactAddress, PublicKeyAddress = storedContactAddress
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var requestBundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(
                    new Contact {
                ChatAddress = storedContactAddress, PublicKeyAddress = storedContactAddress, Request = true, Name = "Requester"
            },
                    keyPair.PublicKey,
                    keyPair.PublicKey).Payload);

            var messenger = new InMemoryMessenger();

            messenger.SentMessages.Add(new Message(rejectedContactBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));
            messenger.SentMessages.Add(new Message(approvedContactBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));
            messenger.SentMessages.Add(new Message(requestBundle.Transactions.Aggregate(new TryteString(), (current, transaction) => current.Concat(transaction.Fragment)), contactRequestAddress));

            var interactor = new GetContactsInteractor(contactRepository, messenger, NtruEncryption.Key);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress = new Address(pubKeyAddress),
                RequestAddress   = contactRequestAddress,
                KeyPair          = keyPair
            });

            Assert.AreEqual(ResponseCode.Success, response.Code);
            Assert.AreEqual(1, response.ApprovedContacts.Count);
            Assert.AreEqual(1, response.PendingContactRequests.Count);
        }
        /// <inheritdoc />
        public override async Task <AcceptContactResponse> ExecuteAsync(AcceptContactRequest request)
        {
            try
            {
                var contactDetails = new Contact
                {
                    Name             = request.UserName,
                    ImagePath        = request.UserImagePath,
                    ChatAddress      = request.ChatAddress.Value,
                    ChatKeyAddress   = request.ChatKeyAddress.Value,
                    ContactAddress   = request.UserContactAddress.Value,
                    PublicKeyAddress = request.UserPublicKeyAddress.Value,
                    Rejected         = false,
                    Request          = false,
                    PublicKey        = null
                };

                // Generate chat pass salt here so we exit the interactor when it fails, before sending something
                var chatPasSalt = await this.GetChatPasswordSalt(request.ChatKeyAddress, request.UserKeyPair);

                var contactInformation = await this.Repository.LoadContactInformationByAddressAsync(request.ContactPublicKeyAddress);

                var contactExchange = ContactExchange.Create(contactDetails, contactInformation.PublicKey, request.UserKeyPair.PublicKey);

                await this.SendContactDetails(contactExchange.Payload, contactInformation);

                await this.ExchangeKey(contactDetails, contactInformation.PublicKey, chatPasSalt);

                await this.Repository.AddContactAsync(request.ChatAddress.Value, true, contactDetails.PublicKeyAddress);

                return(new AcceptContactResponse {
                    Code = ResponseCode.Success
                });
            }
            catch (MessengerException exception)
            {
                return(new AcceptContactResponse {
                    Code = exception.Code
                });
            }
            catch (Exception)
            {
                return(new AcceptContactResponse {
                    Code = ResponseCode.UnkownException
                });
            }
        }
示例#3
0
        public async Task TestContactsAreContainedCachedAndApproved()
        {
            var pubKeyAddress         = Seed.Random().Value;
            var contactRequestAddress = new Address(Seed.Random().Value);
            var storedContactAddress  = Seed.Random().Value;
            var keyPair = InMemoryContactRepository.NtruKeyPair;

            var contactRepository = new InMemoryContactRepository();
            var transactionCache  = new MemoryTransactionCache();

            var bundle = CreateBundle(
                contactRequestAddress,
                ContactExchange.Create(new Contact {
                ChatAddress = storedContactAddress, Rejected = true
            }, keyPair.PublicKey, keyPair.PublicKey).Payload);

            bundle.Transactions.ForEach(async t => await transactionCache.SaveTransactionAsync(new TransactionCacheItem
            {
                Address           = contactRequestAddress,
                TransactionHash   = t.Hash,
                TransactionTrytes = t.ToTrytes()
            }));

            await contactRepository.AddContactAsync(storedContactAddress, true, pubKeyAddress);

            var iotaRepositoryMock = new Mock <IIotaRepository>();

            iotaRepositoryMock.Setup(i => i.FindTransactionsByAddressesAsync(It.IsAny <List <Address> >())).ReturnsAsync(
                new TransactionHashList {
                Hashes = new List <Hash>(bundle.Transactions.Select(t => t.Hash))
            });
            iotaRepositoryMock.Setup(i => i.GetTrytesAsync(It.IsAny <List <Hash> >())).ReturnsAsync(new List <TransactionTrytes>());

            var interactor = new GetContactsInteractor(contactRepository, new TangleMessenger(iotaRepositoryMock.Object, transactionCache), NtruEncryption.Key);
            var response   = await interactor.ExecuteAsync(
                new GetContactsRequest
            {
                PublicKeyAddress = new Address(pubKeyAddress),
                RequestAddress   = contactRequestAddress,
                KeyPair          = keyPair
            });

            Assert.AreEqual(1, response.ApprovedContacts.Count);
        }
示例#4
0
        /// <inheritdoc />
        public override async Task <AddContactResponse> ExecuteAsync(AddContactRequest request)
        {
            try
            {
                var requesterDetails = new Contact
                {
                    ChatAddress      = Seed.Random().ToString(),
                    ChatKeyAddress   = Seed.Random().ToString(),
                    Name             = request.Name,
                    ImagePath        = request.ImagePath,
                    ContactAddress   = request.RequestAddress.Value,
                    Request          = true,
                    Rejected         = false,
                    PublicKey        = null,
                    PublicKeyAddress = request.PublicKeyAddress.Value
                };

                var contactInformation = await this.Repository.LoadContactInformationByAddressAsync(request.ContactAddress);

                var contactExchange = ContactExchange.Create(requesterDetails, contactInformation.PublicKey, request.UserPublicKey);

                await this.SendContactDetails(contactExchange.Payload, contactInformation);

                await this.ExchangeKey(requesterDetails, contactInformation.PublicKey, GetChatPasSalt());

                await this.Repository.AddContactAsync(requesterDetails.ChatAddress, true, requesterDetails.PublicKeyAddress);

                return(new AddContactResponse {
                    Code = ResponseCode.Success
                });
            }
            catch (MessengerException exception)
            {
                return(new AddContactResponse {
                    Code = exception.Code
                });
            }
            catch (Exception)
            {
                return(new AddContactResponse {
                    Code = ResponseCode.UnkownException
                });
            }
        }