public async Task CreateVaspForNaturalPerson_Builder() { var builder = new VaspInformationBuilder(NodeClient.EthereumRpc); (VaspInformation vaspInfo, VaspContractInfo vaspContractInfo) = await builder.CreateForNaturalPersonAsync( Settings.VaspSmartContractAddressPerson, Settings.NaturalPersonIds, Settings.PlaceOfBirth); VaspClient vasp = VaspClient.Create( vaspInfo, vaspContractInfo, Settings.PersonHandshakePrivateKeyHex, Settings.PersonSignaturePrivateKeyHex, NodeClient.EthereumRpc, NodeClient.WhisperRpc, _fakeEnsProvider, _signService, NodeClient.TransportClient); // VASP paramaters must be derived from smart contract Assert.NotNull(vasp.VaspInfo.Name); Assert.NotNull(vasp.VaspInfo.VaspIdentity); Assert.NotNull(vasp.VaspInfo.PostalAddress); // VASP natural person parameters should be same what we pass in constructor Assert.True(vasp.VaspInfo.NaturalPersonIds.SequenceEqual(Settings.NaturalPersonIds)); Assert.Equal(vasp.VaspInfo.PlaceOfBirth, Settings.PlaceOfBirth); Assert.Null(vasp.VaspInfo.JuridicalPersonIds); Assert.Null(vasp.VaspInfo.BIC); }
protected override void Load(ContainerBuilder builder) { _fakeEnsProvider = new FakeEnsProvider(); _signService = new WhisperSignService(); _whisperRpc = new WhisperRpc(new Web3(_appSettings.WhisperRpcUri), new WhisperMessageFormatter()); _ethereumRpc = new EthereumRpc(new Web3(_appSettings.EthereumRpcUri)); _transportClient = new TransportClient(_whisperRpc, _signService, new WhisperMessageFormatter()); var vaspInformationBuilder = new VaspInformationBuilder(_ethereumRpc); var(vaspInfo, vaspContractInfo) = vaspInformationBuilder.Create( _appSettings.VaspSmartContractAddress); var originator = VaspClient.Create( vaspInfo, vaspContractInfo, _appSettings.HandshakePrivateKeyHex, _appSettings.SignaturePrivateKeyHex, _ethereumRpc, _whisperRpc, _fakeEnsProvider, _signService, _transportClient); builder.RegisterInstance(vaspInfo); builder.RegisterInstance(vaspContractInfo); builder.RegisterInstance(originator); builder.RegisterType <TransactionsManager>() .SingleInstance() .AsSelf() .AutoActivate(); base.Load(builder); }
public TransactionsManager(VaspClient vaspClient, VaspInformation vaspInformation) { _vaspInformation = vaspInformation; _outgoingTransactions = new List <Core.Models.Transaction>(); _incomingTransactions = new List <Core.Models.Transaction>(); _vaspSessionsManager = new VaspSessionsManager(vaspClient, this); }
public VaspSessionsManager( VaspClient vaspClient, IVaspCallbacks vaspCallbacks) { _vaspClient = vaspClient; _originatorVaspCallbacks = new OriginatorVaspCallbacks( async(message, originatorSession) => { await vaspCallbacks.SessionReplyMessageReceivedAsync(originatorSession.SessionId, message); }, async(message, originatorSession) => { await vaspCallbacks.TransferReplyMessageReceivedAsync(originatorSession.SessionId, message); if (message.Message.MessageCode == "5") //todo: handle properly. { await originatorSession.TerminateAsync(TerminationMessage.TerminationMessageCode.SessionClosedTransferOccured); originatorSession.Wait(); } }, async(message, originatorSession) => { await vaspCallbacks.TransferConfirmationMessageReceivedAsync(originatorSession.SessionId, message); await originatorSession.TerminateAsync(TerminationMessage.TerminationMessageCode.SessionClosedTransferOccured); originatorSession.Wait(); }); IVaspMessageHandler vaspMessageHandler = new VaspMessageHandlerCallbacks( vaspInfo => Task.FromResult(true), async(request, currentSession) => { _beneficiarySessions[currentSession.SessionId] = currentSession as BeneficiarySession; await vaspCallbacks.TransferRequestMessageReceivedAsync(currentSession.SessionId, request); return(null); }, async(dispatch, currentSession) => { await vaspCallbacks.TransferDispatchMessageReceivedAsync(currentSession.SessionId, dispatch); return(null); }); _vaspClient.RunListener(vaspMessageHandler); }
/// <summary> /// C-tor /// </summary> public TransactionsManager( VaspCode vaspCode, string handshakePrivateKeyHex, string signaturePrivateKeyHex, IEthereumRpc ethereumRpc, ISignService signService, IEnsProvider ensProvider, ITransportClient transportClient, ITransactionDataService transactionDataService, ISessionsRepository sessionsRepository, ITransactionsRepository transactionsRepository, IVaspCodeManager vaspCodeManager, VaspInformation vaspInformation, ILoggerFactory loggerFactory) { _vaspClient = VaspClient.Create( vaspCode, handshakePrivateKeyHex, signaturePrivateKeyHex, ethereumRpc, ensProvider, signService, transportClient, loggerFactory); _vaspClient.SessionRequestMessageReceived += SessionRequestMessageReceivedAsync; _vaspClient.SessionReplyMessageReceived += SessionReplyMessageReceivedAsync; _vaspClient.TransferReplyMessageReceived += TransferReplyMessageReceivedAsync; _vaspClient.TransferConfirmationMessageReceived += TransferConfirmationMessageReceivedAsync; _vaspClient.TransferRequestMessageReceived += TransferRequestMessageReceivedAsync; _vaspClient.TransferDispatchMessageReceived += TransferDispatchMessageReceivedAsync; _vaspClient.BeneficiarySessionCreated += BeneficiarySessionCreatedAsync; _transactionDataService = transactionDataService; _sessionsRepository = sessionsRepository; _transactionsRepository = transactionsRepository; _vaspCodeManager = vaspCodeManager; _vaspInformation = vaspInformation; }
public async Task CreateVaspForBank() { var signature = new EthECKey(Settings.PersonSignaturePrivateKeyHex); var handshake = ECDH_Key.ImportKey(Settings.PersonHandshakePrivateKeyHex); var signPub = signature.GetPubKey().ToHex(prefix: true); var handshakePub = handshake.PublicKey; (VaspInformation vaspInfo, VaspContractInfo vaspContractInfo) = await VaspInformationBuilder.CreateForBankAsync( NodeClient.EthereumRpc, Settings.VaspSmartContractAddressBank, Settings.Bic); VaspClient vasp = VaspClient.Create( vaspInfo, vaspContractInfo, Settings.BankHandshakePrivateKeyHex, Settings.BankSignaturePrivateKeyHex, NodeClient.EthereumRpc, NodeClient.WhisperRpc, _fakeEnsProvider, _signService, NodeClient.TransportClient); // VASP paramaters must be derived from smart contract Assert.NotNull(vasp.VaspInfo.Name); Assert.NotNull(vasp.VaspInfo.VaspIdentity); Assert.NotNull(vasp.VaspInfo.PostalAddress); // VASP natural person parameters should be same what we pass in constructor Assert.Equal(vasp.VaspInfo.BIC, Settings.Bic); Assert.Null(vasp.VaspInfo.NaturalPersonIds); Assert.Null(vasp.VaspInfo.PlaceOfBirth); Assert.Null(vasp.VaspInfo.JuridicalPersonIds); }
public void CreateSessionRequest( string beneficiaryName, string originatorName, PostalAddress originatorPostalAddress, PlaceOfBirth placeOfBirth, VirtualAssetType assetType, decimal transferAmount) { (VaspInformation vaspInfoPerson, VaspContractInfo vaspContractInfoPerson) = VaspInformationBuilder.CreateForNaturalPersonAsync( _ethereumRpc, _vaspSmartContractAddressPerson, _naturalPersonIds, placeOfBirth) .GetAwaiter() .GetResult(); VaspClient originator = VaspClient.Create( vaspInfoPerson, vaspContractInfoPerson, _personHandshakePrivateKeyHex, _personSignaturePrivateKeyHex, _ethereumRpc, _whisperRpc, _fakeEnsProvider, _signService, _transportClient); (VaspInformation vaspInfoJuridical, VaspContractInfo vaspContractInfoJuridical) = VaspInformationBuilder.CreateForJuridicalPersonAsync( _ethereumRpc, _vaspSmartContractAddressJuridical, _juridicalPersonIds) .GetAwaiter() .GetResult(); VaspClient beneficiary = VaspClient.Create( vaspInfoJuridical, vaspContractInfoJuridical, _juridicalHandshakePrivateKeyHex, _juridicalSignaturePrivateKeyHex, _ethereumRpc, _whisperRpc, _fakeEnsProvider, _signService, _transportClient); var originatorVaan = VirtualAssetssAccountNumber.Create(vaspInfoPerson.GetVaspCode(), "524ee3fb082809"); var beneficiaryVaan = VirtualAssetssAccountNumber.Create(vaspInfoJuridical.GetVaspCode(), "524ee3fb082809"); var vaspSession = new VaspSessionWrapper( beneficiaryName, originator, beneficiary); VaspSessions[vaspSession.Id] = vaspSession; var originatorDoc = Originator.CreateOriginatorForNaturalPerson( originatorName, originatorVaan, originatorPostalAddress, placeOfBirth, new NaturalPersonId[] { new NaturalPersonId("Id", NaturalIdentificationType.NationalIdentityNumber, Country.List["DE"]), }); vaspSession.StartSession(originatorDoc, beneficiaryVaan, assetType, transferAmount); }
public async Task CreateSessionBetweenVASPs() { int sessionTerminationCounter = 0; SessionTermination sessionTerminationDelegate = @event => { sessionTerminationCounter++; }; (VaspInformation vaspInfoPerson, VaspContractInfo vaspContractInfoPerson) = await VaspInformationBuilder.CreateForNaturalPersonAsync( NodeClient.EthereumRpc, Settings.VaspSmartContractAddressPerson, Settings.NaturalPersonIds, Settings.PlaceOfBirth); VaspClient originator = VaspClient.Create( vaspInfoPerson, vaspContractInfoPerson, Settings.PersonHandshakePrivateKeyHex, Settings.PersonSignaturePrivateKeyHex, NodeClient.EthereumRpc, NodeClient.WhisperRpc, _fakeEnsProvider, _signService, NodeClient.TransportClient); (VaspInformation vaspInfoJuridical, VaspContractInfo vaspContractInfoJuridical) = await VaspInformationBuilder.CreateForJuridicalPersonAsync( NodeClient.EthereumRpc, Settings.VaspSmartContractAddressJuridical, Settings.JuridicalIds); VaspClient beneficiary = VaspClient.Create( vaspInfoJuridical, vaspContractInfoJuridical, Settings.JuridicalHandshakePrivateKeyHex, Settings.JuridicalSignaturePrivateKeyHex, NodeClient.EthereumRpc, NodeClient.WhisperRpc, _fakeEnsProvider, _signService, NodeClient.TransportClient); var originatorVaan = VirtualAssetssAccountNumber.Create(vaspInfoPerson.GetVaspCode(), "524ee3fb082809"); var beneficiaryVaan = VirtualAssetssAccountNumber.Create(vaspInfoJuridical.GetVaspCode(), "524ee3fb082809"); IVaspMessageHandler messageHandler = new VaspMessageHandlerCallbacks( (vaspInfo) => { return(Task.FromResult(true)); }, (request, currentSession) => { var message = new TransferReplyMessage(currentSession.SessionId, TransferReplyMessage.TransferReplyMessageCode.TransferAccepted, request.Originator, new Beneficiary("Mr. Test", request.Beneficiary.VAAN), new TransferReply( request.Transfer.VirtualAssetType, request.Transfer.TransferType, request.Transfer.Amount, "0x0"), request.VASP); return(Task.FromResult(message)); }, (dispatch, currentSession) => { var message = new TransferConfirmationMessage(currentSession.SessionId, TransferConfirmationMessage.TransferConfirmationMessageCode.TransferConfirmed, dispatch.Originator, dispatch.Beneficiary, dispatch.Transfer, dispatch.Transaction, dispatch.VASP); return(Task.FromResult(message)); }); beneficiary.RunListener(messageHandler); originator.SessionTerminated += sessionTerminationDelegate; beneficiary.SessionTerminated += sessionTerminationDelegate; // change enum to string and add constants //beneficiary.TransferRequest += request => new TransferReply(VirtualAssetType.ETH, TransferType.BlockchainTransfer, "10", "1223"); //beneficiary.TransferDispatch += message => new TransferConfirmationMessage(); var originatorDoc = Originator.CreateOriginatorForNaturalPerson( "Test van der Test", originatorVaan, new PostalAddress( "StreetX", 44, "AddressLineX", "510051", "TownX", Country.List["DE"]), new PlaceOfBirth(DateTime.Today.AddYears(-30), "TownX", Country.List["DE"]), new NaturalPersonId[] { new NaturalPersonId("Id", NaturalIdentificationType.NationalIdentityNumber, Country.List["DE"]), }); var session = await originator.CreateSessionAsync(originatorDoc, beneficiaryVaan); var transferReply = await session.TransferRequestAsync(new TransferInstruction() { VirtualAssetTransfer = new VirtualAssetTransfer() { TransferType = TransferType.BlockchainTransfer, VirtualAssetType = VirtualAssetType.ETH, TransferAmount = "1000000000000000000" } }); var transferConformation = await session.TransferDispatchAsync(transferReply.Transfer, new Transaction("0xtxhash", DateTime.UtcNow, "0x0...a")); Assert.Equal(1, originator.GetActiveSessions().Count); Assert.True(originator.GetActiveSessions().First() is OriginatorSession); Assert.Equal(1, beneficiary.GetActiveSessions().Count); Assert.True(beneficiary.GetActiveSessions().First() is BeneficiarySession); await session.TerminateAsync(TerminationMessage.TerminationMessageCode.SessionClosedTransferOccured); session.Wait(); originator.Dispose(); beneficiary.Dispose(); Assert.Equal(2, sessionTerminationCounter); _testOutputHelper.WriteLine("End of test"); }
//[Fact] public async Task ConnectionBetweenTwoClientsSuccessful() { var vaspClient1 = VaspClient.Create(_settings1); var vaspClient2 = VaspClient.Create(_settings2); var sessionRequestSemaphore = new SemaphoreSlim(0, 1); var sessionReplySemaphore = new SemaphoreSlim(0, 1); var transferRequestSemaphore = new SemaphoreSlim(0, 1); var transferReplySemaphore = new SemaphoreSlim(0, 1); var transferDispatchSemaphore = new SemaphoreSlim(0, 1); var transferConfirmSemaphore = new SemaphoreSlim(0, 1); var terminationSemaphore = new SemaphoreSlim(0, 1); var sessionRequestReceived = false; var sessionReplyReceived = false; var transferRequestReceived = false; var transferReplyReceived = false; var transferDispatchReceived = false; var transferConfirmReceived = false; var terminationReceived = false; string session1 = null; vaspClient1.OriginatorSessionApprovedEvent += async approved => { sessionReplyReceived = true; sessionReplySemaphore.Release(); var transferRequest = new TransferRequest { Transfer = new Transfer { Amount = 10 } }; await vaspClient1.SendApplicationMessageAsync(session1, MessageType.TransferRequest, JObject.FromObject(transferRequest)); }; vaspClient1.ApplicationMessageReceivedEvent += async received => { if (received.Type == MessageType.TransferReply) { transferReplyReceived = true; transferReplySemaphore.Release(); var transferReply = received.Body.ToObject <TransferReply>(); Assert.Equal(TransferReplyMessageCode.TransferAccepted, transferReply.Code); var transferDispatch = new TransferDispatch { Transfer = new Transaction { TransactionHash = "txhash" } }; await vaspClient1.SendApplicationMessageAsync(session1, MessageType.TransferDispatch, JObject.FromObject(transferDispatch)); } if (received.Type == MessageType.TransferConfirmation) { transferConfirmReceived = true; transferConfirmSemaphore.Release(); var transferConfirm = received.Body.ToObject <TransferConfirm>(); Assert.Equal(TransactionConfirmCode.TransferConfirmed, transferConfirm.Code); await vaspClient1.SessionTerminateAsync(session1); } }; string session2 = null; vaspClient2.BeneficiarySessionCreatedEvent += async created => { sessionRequestReceived = true; sessionRequestSemaphore.Release(); session2 = created.SessionId; await vaspClient2.SessionReplyAsync(session2, true); }; vaspClient2.SessionTerminatedEvent += terminated => { terminationReceived = true; terminationSemaphore.Release(); return(Task.CompletedTask); }; vaspClient2.ApplicationMessageReceivedEvent += async received => { if (received.Type == MessageType.TransferRequest) { transferRequestReceived = true; transferRequestSemaphore.Release(); var transferRequest = received.Body.ToObject <TransferRequest>(); Assert.Equal(10, transferRequest.Transfer.Amount); var transferReply = new TransferReply { Code = TransferReplyMessageCode.TransferAccepted }; await vaspClient2.SendApplicationMessageAsync(session2, MessageType.TransferReply, JObject.FromObject(transferReply)); } if (received.Type == MessageType.TransferDispatch) { transferDispatchReceived = true; transferDispatchSemaphore.Release(); var transferDispatch = received.Body.ToObject <TransferDispatch>(); Assert.Equal("txhash", transferDispatch.Transfer.TransactionHash); var transferConfirm = new TransferConfirm { Code = TransactionConfirmCode.TransferConfirmed }; await vaspClient2.SendApplicationMessageAsync(session2, MessageType.TransferConfirmation, JObject.FromObject(transferConfirm)); } }; session1 = await vaspClient1.CreateSessionAsync(_settings2.VaspId); await Task.WhenAny( Task.Delay(TimeSpan.FromMinutes(2)), Task.WhenAll( sessionRequestSemaphore.WaitAsync(), sessionReplySemaphore.WaitAsync(), transferRequestSemaphore.WaitAsync(), transferReplySemaphore.WaitAsync(), transferDispatchSemaphore.WaitAsync(), transferConfirmSemaphore.WaitAsync(), terminationSemaphore.WaitAsync())); Assert.True(sessionRequestReceived, "Session request message was not delivered"); Assert.True(sessionReplyReceived, "Session reply message was not delivered"); Assert.True(transferRequestReceived, "Transfer request message was not delivered"); Assert.True(transferReplyReceived, "Transfer reply message was not delivered"); Assert.True(transferDispatchReceived, "Transfer dispatch message was not delivered"); Assert.True(transferConfirmReceived, "Transfer confirm message was not delivered"); Assert.True(terminationReceived, "Termination message was not delivered"); }