Example #1
0
        public async Task <(VaspInformation VaspInformation, VaspContractInfo VaspContractInfo)> CreateForNaturalPersonAsync(
            string vaspSmartContractAddress,
            NaturalPersonId[] naturalPersonIds,
            PlaceOfBirth placeOfBirth)
        {
            var vaspContractInfo = await _nodeClientEthereumRpc.GetVaspContractInfoAync(vaspSmartContractAddress);

            var vaspInformation = new VaspInformation(
                vaspContractInfo.Name,
                vaspSmartContractAddress,
                vaspContractInfo.HandshakeKey,//Ensure it is correct
                vaspContractInfo.Address,
                placeOfBirth,
                naturalPersonIds,
                null,
                null);

            return(vaspInformation, vaspContractInfo);
        }
Example #2
0
        public (VaspInformation VaspInformation, VaspContractInfo VaspContractInfo) Create(
            string vaspSmartContractAddress)
        {
            var vaspContractInfo = _nodeClientEthereumRpc
                                   .GetVaspContractInfoAync(vaspSmartContractAddress)
                                   .GetAwaiter()
                                   .GetResult();

            var vaspInformation = new VaspInformation(
                vaspContractInfo.Name,
                vaspSmartContractAddress,
                vaspContractInfo.HandshakeKey,//Ensure it is correct
                vaspContractInfo.Address,
                null,
                null,
                null,
                null);

            return(vaspInformation, vaspContractInfo);
        }
        /// <summary>
        /// Run listener which would process incoming messages.
        /// </summary>
        /// <param name="messageHandler">Handler which authorizes originator's Vasp and processes Transfer Request and Transfer Dispatch Messages</param>
        public void RunListener(IVaspMessageHandler messageHandler)
        {
            lock (_lock)
            {
                if (!_hasStartedListening)
                {
                    _hasStartedListening = true;
                    var token       = _cancellationTokenSource.Token;
                    var taskFactory = new TaskFactory(_cancellationTokenSource.Token);

                    this._listener = taskFactory.StartNew(async(_) =>
                    {
                        var privateKeyId     = await _whisperRpc.RegisterKeyPairAsync(this._handshakeKey.PrivateKey);
                        string messageFilter =
                            await _whisperRpc.CreateMessageFilterAsync(topicHex: _vaspContractInfo.VaspCode.Code,
                                                                       privateKeyId);

                        do
                        {
                            var sessionRequestMessages = await _transportClient.GetSessionMessagesAsync(messageFilter);

                            if (sessionRequestMessages != null &&
                                sessionRequestMessages.Count != 0)
                            {
                                foreach (var message in sessionRequestMessages)
                                {
                                    var sessionRequestMessage = message.Message as SessionRequestMessage;

                                    if (sessionRequestMessage == null)
                                    {
                                        continue;
                                    }

                                    var isAuthorized =
                                        await messageHandler.AuthorizeSessionRequestAsync(sessionRequestMessage.VASP);

                                    if (!isAuthorized)
                                    {
                                        continue;
                                    }

                                    var originatorVaspContractInfo =
                                        await _ethereumRpc.GetVaspContractInfoAync(sessionRequestMessage.VASP
                                                                                   .VaspIdentity);

                                    if (!_signService.VerifySign(message.Payload, message.Signature,
                                                                 originatorVaspContractInfo.SigningKey))
                                    {
                                        continue;
                                    }

                                    var sharedSecret =
                                        this._handshakeKey.GenerateSharedSecretHex(sessionRequestMessage.HandShake
                                                                                   .EcdhPubKey);

                                    var session = new BeneficiarySession(
                                        originatorVaspContractInfo,
                                        this.VaspInfo,
                                        sessionRequestMessage.Message.SessionId,
                                        sessionRequestMessage.HandShake.TopicA,
                                        originatorVaspContractInfo.SigningKey,
                                        sharedSecret,
                                        this._signatureKey,
                                        this._whisperRpc,
                                        messageHandler,
                                        _transportClient,
                                        _signService);

                                    this.NotifySessionCreated(session);
                                    session.OnSessionTermination += this.ProcessSessionTermination;
                                    if (_beneficiarySessionsDict.TryAdd(session.SessionId, session))
                                    {
                                        await session.StartAsync();
                                    }
                                    else
                                    {
                                        await session.TerminateAsync(TerminationMessage.TerminationMessageCode
                                                                     .SessionClosedTransferDeclinedByBeneficiaryVasp);
                                    }
                                }

                                continue;
                            }

                            await Task.Delay(5000, token);
                        } while (!token.IsCancellationRequested);
                    }, token, TaskCreationOptions.LongRunning);
                }
                else
                {
                    throw new Exception("You can start observation only once.");
                }
            }
        }