Compare commits
228 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb8d10a0b9 | ||
|
|
b19665152d | ||
|
|
561c676758 | ||
|
|
6699e73ed6 | ||
|
|
c5dfbf336e | ||
|
|
e74a996424 | ||
|
|
96c3747ad9 | ||
|
|
c7fe58200f | ||
|
|
8e3051e61b | ||
|
|
d84d4bdf7f | ||
|
|
003c261909 | ||
|
|
4dc82b81c5 | ||
|
|
d36ead30a7 | ||
|
|
4cb2a23900 | ||
|
|
b3faa84cf3 | ||
|
|
b413816797 | ||
|
|
048b59c261 | ||
|
|
7de6647e65 | ||
|
|
f4a837f36f | ||
|
|
140d2c206a | ||
|
|
50c1651cba | ||
|
|
a497084b34 | ||
|
|
051a27fd86 | ||
|
|
124982bd94 | ||
|
|
97f682115c | ||
|
|
39bd13bb25 | ||
|
|
2ef1e353e7 | ||
|
|
8334969f93 | ||
|
|
21ecf986f1 | ||
|
|
cb6fa66300 | ||
|
|
32f21e5306 | ||
|
|
5d0b771a36 | ||
|
|
435ebf8909 | ||
|
|
323f7e8481 | ||
|
|
2d7911bc95 | ||
|
|
ce7a70bb99 | ||
|
|
9b4fb02241 | ||
|
|
aa68a90224 | ||
|
|
236f416fc6 | ||
|
|
36fbd57209 | ||
|
|
28c2bac049 | ||
|
|
2c6ace2a95 | ||
|
|
a9d5a2fadb | ||
|
|
215d311113 | ||
|
|
e598cc10d2 | ||
|
|
25383e196b | ||
|
|
d5c84fa491 | ||
|
|
05b64186fb | ||
|
|
a75c275c10 | ||
|
|
1ca2119138 | ||
|
|
0138d9712b | ||
|
|
19db0dff46 | ||
|
|
f21a4ac701 | ||
|
|
0295dd1e87 | ||
|
|
1293f03182 | ||
|
|
d56356d2f3 | ||
|
|
ded2313463 | ||
|
|
e7c48e2db7 | ||
|
|
ddfc647300 | ||
|
|
5902da4212 | ||
|
|
774007e5c7 | ||
|
|
c47efa0b43 | ||
|
|
c8fee0e18f | ||
|
|
c3b73c7e4d | ||
|
|
0e7a7a5d70 | ||
|
|
08a3329eff | ||
|
|
5b29c62fc6 | ||
|
|
8ad099bcce | ||
|
|
2fba3da43e | ||
|
|
9649294bbf | ||
|
|
c555df3054 | ||
|
|
78ca88b030 | ||
|
|
7ffaa89f55 | ||
|
|
d80adaada3 | ||
|
|
2c7f7ef780 | ||
|
|
e1d82f0350 | ||
|
|
4dd0b4450c | ||
|
|
e258d6b19f | ||
|
|
be14704843 | ||
|
|
4744d574e4 | ||
|
|
a085305b4b | ||
|
|
f09094087c | ||
|
|
9b950cf46d | ||
|
|
bebefb5f48 | ||
|
|
079b67f421 | ||
|
|
4efeddf080 | ||
|
|
951ae61b25 | ||
|
|
58946b1427 | ||
|
|
a2eb4d0194 | ||
|
|
33fde4b51f | ||
|
|
a82ea4221b | ||
|
|
19858588db | ||
|
|
de1a6a0adc | ||
|
|
f55e390859 | ||
|
|
ba945afb3a | ||
|
|
9efb53b93a | ||
|
|
c108c4e9b2 | ||
|
|
f705b2c187 | ||
|
|
875248fc23 | ||
|
|
d05a1a5c3c | ||
|
|
a91aa41173 | ||
|
|
987b2d4169 | ||
|
|
41672156c4 | ||
|
|
eb61e7c08f | ||
|
|
6ee85be74a | ||
|
|
7589e94202 | ||
|
|
df946a441a | ||
|
|
80c4ad5189 | ||
|
|
9f64f17b49 | ||
|
|
205151a83d | ||
|
|
12c599e5cb | ||
|
|
ba491bfccd | ||
|
|
9d9c61c9f2 | ||
|
|
b067106fcf | ||
|
|
fc6158db3b | ||
|
|
6bc7024244 | ||
|
|
67f39bf73d | ||
|
|
cf06bb1a7c | ||
|
|
d653113492 | ||
|
|
61a8daa1b5 | ||
|
|
834d87e8ae | ||
|
|
c85c03e8e7 | ||
|
|
5464d508e4 | ||
|
|
037d280c6b | ||
|
|
739d303956 | ||
|
|
b725d48860 | ||
|
|
c357f67b46 | ||
|
|
8dcc42d867 | ||
|
|
6d7b95e089 | ||
|
|
85924bd786 | ||
|
|
4a8c73e805 | ||
|
|
5a2586e597 | ||
|
|
a65ef7e340 | ||
|
|
0b58b61b07 | ||
|
|
cd207bc0dc | ||
|
|
9ef7b0b2b5 | ||
|
|
b725299b1d | ||
|
|
12c1ea59b3 | ||
|
|
62d6494cb0 | ||
|
|
2e46509b10 | ||
|
|
b9979f7bfd | ||
|
|
48b2084f54 | ||
|
|
52f6f5c4ba | ||
|
|
240ff51c7e | ||
|
|
ea6cfaab25 | ||
|
|
40d561f199 | ||
|
|
9d9b099a31 | ||
|
|
bb83f20fb2 | ||
|
|
6e8db6282a | ||
|
|
1e3b66ca6d | ||
|
|
f3f9a07cb0 | ||
|
|
b20b149b1c | ||
|
|
d9f672804c | ||
|
|
b2e5b14dd6 | ||
|
|
6638105e69 | ||
|
|
36878ec7c0 | ||
|
|
afa340ccb8 | ||
|
|
0c8d9bd136 | ||
|
|
88786b5fe5 | ||
|
|
f9f1d2d2f3 | ||
|
|
090bbd464a | ||
|
|
061fab086b | ||
|
|
d5ec6f7e0d | ||
|
|
dff978f556 | ||
|
|
fda441854f | ||
|
|
c725444347 | ||
|
|
a90a44cb19 | ||
|
|
d0442693b2 | ||
|
|
cfc6e95012 | ||
|
|
58cbbfa5f8 | ||
|
|
7a755d356b | ||
|
|
ee63b10f96 | ||
|
|
4e3b832395 | ||
|
|
e44094ceb1 | ||
|
|
d6cd796ae5 | ||
|
|
d043f0bf88 | ||
|
|
f66e971c0a | ||
|
|
a5a34f3681 | ||
|
|
f41f6efe4c | ||
|
|
5adc78eede | ||
|
|
5f3edaa0eb | ||
|
|
1a9f972dab | ||
|
|
c0c6083069 | ||
|
|
afc65f0f15 | ||
|
|
5118869219 | ||
|
|
bd937ae9e3 | ||
|
|
2f5f53531e | ||
|
|
1921f19e66 | ||
|
|
87dd78f3ab | ||
|
|
8bad5e1105 | ||
|
|
a17697a011 | ||
|
|
dcd79d104b | ||
|
|
3b92f4b4ed | ||
|
|
b996143abc | ||
|
|
07e0ccd284 | ||
|
|
0549ecd94c | ||
|
|
cc6065d7df | ||
|
|
832eec1400 | ||
|
|
651b2bf181 | ||
|
|
2e626e448b | ||
|
|
2b66b0b26b | ||
|
|
dca7bd02c5 | ||
|
|
9bd4864e4a | ||
|
|
94377e964c | ||
|
|
2cb8e9c02b | ||
|
|
d8e43af4a4 | ||
|
|
76d52a4be2 | ||
|
|
51f7724f3c | ||
|
|
fe37f28713 | ||
|
|
17684a2a44 | ||
|
|
2b284733a6 | ||
|
|
19babb809b | ||
|
|
624dd99654 | ||
|
|
110d287e94 | ||
|
|
caee4b70f5 | ||
|
|
104c2074df | ||
|
|
53ea6b9b76 | ||
|
|
6da6edd27f | ||
|
|
0100da72f0 | ||
|
|
fb75d110f5 | ||
|
|
e02637c850 | ||
|
|
80d753db54 | ||
|
|
3340ca6cde | ||
|
|
a7432b868a | ||
|
|
caf0f6b691 | ||
|
|
409f3a01c0 | ||
|
|
797863aa79 | ||
|
|
6314829956 |
@ -2,50 +2,61 @@ version: 2
|
||||
jobs:
|
||||
build:
|
||||
machine:
|
||||
docker_layer_caching: true
|
||||
docker_layer_caching: false
|
||||
steps:
|
||||
- checkout
|
||||
|
||||
test:
|
||||
machine:
|
||||
docker_layer_caching: true
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
command: |
|
||||
# cd BTCPayServer.Tests
|
||||
# docker-compose down --v
|
||||
# docker-compose build
|
||||
# docker-compose run tests
|
||||
|
||||
# publish jobs require $DOCKERHUB_REPO, $DOCKERHUB_USER, $DOCKERHUB_PASS defined
|
||||
publish_docker_linuxamd64:
|
||||
machine:
|
||||
docker_layer_caching: true
|
||||
docker_layer_caching: false
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
command: |
|
||||
LATEST_TAG=${CIRCLE_TAG:1} #trim v from tag
|
||||
if [ -z "$LATEST_TAG" ]
|
||||
then
|
||||
LATEST_TAG="latest"
|
||||
fi
|
||||
#
|
||||
sudo docker build --pull -t $DOCKERHUB_REPO:$LATEST_TAG-amd64 -f BtcTransmuter/Dockerfile.linuxamd64 .
|
||||
sudo docker build --pull -t $DOCKERHUB_REPO:$LATEST_TAG-amd64 -f Dockerfiles/amd64.Dockerfile .
|
||||
sudo docker login --username=$DOCKERHUB_USER --password=$DOCKERHUB_PASS
|
||||
sudo docker push $DOCKERHUB_REPO:$LATEST_TAG-amd64
|
||||
|
||||
publish_docker_linuxarm:
|
||||
publish_docker_linuxarm32:
|
||||
machine:
|
||||
docker_layer_caching: true
|
||||
docker_layer_caching: false
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
command: |
|
||||
sudo docker run --rm --privileged multiarch/qemu-user-static:register --reset
|
||||
LATEST_TAG=${CIRCLE_TAG:1} #trim v from tag
|
||||
if [ -z "$LATEST_TAG" ]
|
||||
then
|
||||
LATEST_TAG="latest"
|
||||
fi
|
||||
#
|
||||
sudo docker build --pull -t $DOCKERHUB_REPO:$LATEST_TAG-arm32v7 -f BtcTransmuter/Dockerfile.linuxarm32v7 .
|
||||
sudo docker build --pull -t $DOCKERHUB_REPO:$LATEST_TAG-arm32v7 -f Dockerfiles/arm32v7.Dockerfile .
|
||||
sudo docker login --username=$DOCKERHUB_USER --password=$DOCKERHUB_PASS
|
||||
sudo docker push $DOCKERHUB_REPO:$LATEST_TAG-arm32v7
|
||||
|
||||
publish_docker_linuxarm64:
|
||||
machine:
|
||||
docker_layer_caching: false
|
||||
steps:
|
||||
- checkout
|
||||
- run:
|
||||
command: |
|
||||
sudo docker run --rm --privileged multiarch/qemu-user-static:register --reset
|
||||
LATEST_TAG=${CIRCLE_TAG:1} #trim v from tag
|
||||
if [ -z "$LATEST_TAG" ]
|
||||
then
|
||||
LATEST_TAG="latest"
|
||||
fi
|
||||
#
|
||||
sudo docker build --pull -t $DOCKERHUB_REPO:$LATEST_TAG-arm64v8 -f Dockerfiles/arm64v8.Dockerfile .
|
||||
sudo docker login --username=$DOCKERHUB_USER --password=$DOCKERHUB_PASS
|
||||
sudo docker push $DOCKERHUB_REPO:$LATEST_TAG-arm64v8
|
||||
publish_docker_multiarch:
|
||||
machine:
|
||||
enabled: true
|
||||
@ -60,39 +71,62 @@ jobs:
|
||||
sudo docker login --username=$DOCKERHUB_USER --password=$DOCKERHUB_PASS
|
||||
#
|
||||
LATEST_TAG=${CIRCLE_TAG:1} #trim v from tag
|
||||
sudo docker manifest create --amend $DOCKERHUB_REPO:$LATEST_TAG $DOCKERHUB_REPO:$LATEST_TAG-amd64 $DOCKERHUB_REPO:$LATEST_TAG-arm32v7
|
||||
if [ -z "$LATEST_TAG" ]
|
||||
then
|
||||
LATEST_TAG="latest"
|
||||
fi
|
||||
sudo docker manifest create --amend $DOCKERHUB_REPO:$LATEST_TAG $DOCKERHUB_REPO:$LATEST_TAG-amd64 $DOCKERHUB_REPO:$LATEST_TAG-arm32v7 $DOCKERHUB_REPO:$LATEST_TAG-arm64v8
|
||||
sudo docker manifest annotate $DOCKERHUB_REPO:$LATEST_TAG $DOCKERHUB_REPO:$LATEST_TAG-amd64 --os linux --arch amd64
|
||||
sudo docker manifest annotate $DOCKERHUB_REPO:$LATEST_TAG $DOCKERHUB_REPO:$LATEST_TAG-arm32v7 --os linux --arch arm --variant v7
|
||||
sudo docker manifest annotate $DOCKERHUB_REPO:$LATEST_TAG $DOCKERHUB_REPO:$LATEST_TAG-arm64v8 --os linux --arch arm64 --variant v8
|
||||
sudo docker manifest push $DOCKERHUB_REPO:$LATEST_TAG -p
|
||||
|
||||
trigger_docs_build:
|
||||
machine:
|
||||
enabled: true
|
||||
image: circleci/classic:201808-01
|
||||
steps:
|
||||
- run:
|
||||
command: |
|
||||
curl -X POST -H "Authorization: token $GH_PAT" -H "Accept: application/vnd.github.everest-preview+json" -H "Content-Type: application/json" https://api.github.com/repos/btcpayserver/btcpayserver-doc/dispatches --data '{"event_type": "build_docs"}'
|
||||
workflows:
|
||||
version: 2
|
||||
build_and_test:
|
||||
jobs:
|
||||
- test
|
||||
|
||||
publish:
|
||||
jobs:
|
||||
- trigger_docs_build:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
||||
# only act on version tags
|
||||
tags:
|
||||
only: /v[0-9]+(\.[0-9]+)*/
|
||||
- publish_docker_linuxamd64:
|
||||
filters:
|
||||
# ignore any commit on any branch by default
|
||||
branches:
|
||||
ignore: /.*/
|
||||
only: master
|
||||
# only act on version tags
|
||||
tags:
|
||||
only: /v[0-9]+(\.[0-9]+)*/
|
||||
- publish_docker_linuxarm:
|
||||
- publish_docker_linuxarm32:
|
||||
filters:
|
||||
branches:
|
||||
ignore: /.*/
|
||||
only: master
|
||||
tags:
|
||||
only: /v[0-9]+(\.[0-9]+)*/
|
||||
- publish_docker_linuxarm64:
|
||||
filters:
|
||||
branches:
|
||||
only: master
|
||||
tags:
|
||||
only: /v[0-9]+(\.[0-9]+)*/
|
||||
- publish_docker_multiarch:
|
||||
requires:
|
||||
- publish_docker_linuxamd64
|
||||
- publish_docker_linuxarm
|
||||
- publish_docker_linuxarm32
|
||||
- publish_docker_linuxarm64
|
||||
filters:
|
||||
branches:
|
||||
ignore: /.*/
|
||||
only: master
|
||||
tags:
|
||||
only: /v[0-9]+(\.[0-9]+)*/
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -303,3 +303,4 @@ BtcTransmuter\.Extension\.BtcPayServer/obj/
|
||||
BtcTransmuter/Extensions/
|
||||
/BtcTransmuter/Extensions/
|
||||
/BtcTransmuter/mydb.db_old
|
||||
dest
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@ -0,0 +1,16 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace BTCTransmuter.Extension.Tor.Configuration
|
||||
{
|
||||
public class BTCTransmuterTorOptions
|
||||
{
|
||||
public string TorrcFile { get; }
|
||||
public string TransmuterHiddenServiceName { get; }
|
||||
|
||||
public BTCTransmuterTorOptions(IConfiguration configuration)
|
||||
{
|
||||
TorrcFile = configuration.GetValue<string>(nameof(TorrcFile), null);
|
||||
TransmuterHiddenServiceName = configuration.GetValue(nameof(TransmuterHiddenServiceName), "BTCTransmuter");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCTransmuter.Extension.Tor.Configuration;
|
||||
using BTCTransmuter.Extension.Tor.Services;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace BTCTransmuter.Extension.Tor.HostedServices
|
||||
{
|
||||
public class TorServicesHostedService : IHostedService
|
||||
{
|
||||
private readonly BTCTransmuterTorOptions _btcTransmuterTorOptions;
|
||||
private readonly TorServices _torServices;
|
||||
|
||||
public TorServicesHostedService(BTCTransmuterTorOptions btcTransmuterTorOptions, TorServices torServices)
|
||||
{
|
||||
_btcTransmuterTorOptions = btcTransmuterTorOptions;
|
||||
_torServices = torServices;
|
||||
}
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_btcTransmuterTorOptions.TorrcFile))
|
||||
{
|
||||
_ = ContinuouslyMonitorTorServices(cancellationToken);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task ContinuouslyMonitorTorServices(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
_torServices.Refresh();
|
||||
await Task.Delay(TimeSpan.FromMinutes(2), cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
BTCTransmuter.Extension.Tor/Models/TorService.cs
Normal file
9
BTCTransmuter.Extension.Tor/Models/TorService.cs
Normal file
@ -0,0 +1,9 @@
|
||||
namespace BTCTransmuter.Extension.Tor.Models
|
||||
{
|
||||
public class TorService
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string OnionHost { get; set; }
|
||||
public int VirtualPort { get; set; }
|
||||
}
|
||||
}
|
||||
91
BTCTransmuter.Extension.Tor/Services/TorServices.cs
Normal file
91
BTCTransmuter.Extension.Tor/Services/TorServices.cs
Normal file
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCTransmuter.Extension.Tor.Configuration;
|
||||
using BTCTransmuter.Extension.Tor.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BTCTransmuter.Extension.Tor.Services
|
||||
{
|
||||
public class TorServices
|
||||
{
|
||||
private readonly BTCTransmuterTorOptions _transmuterTorOptions;
|
||||
private readonly ILogger<TorServices> _logger;
|
||||
|
||||
public TorServices(BTCTransmuterTorOptions transmuterTorOptions, ILogger<TorServices> logger)
|
||||
{
|
||||
_transmuterTorOptions = transmuterTorOptions;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public TorService[] Services { get; internal set; } = Array.Empty<TorService>();
|
||||
|
||||
public TorService TransmuterTorService
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(_transmuterTorOptions.TransmuterHiddenServiceName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Services.SingleOrDefault(service =>
|
||||
service.Name.Equals(_transmuterTorOptions.TransmuterHiddenServiceName,
|
||||
StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
}
|
||||
|
||||
internal void Refresh()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_transmuterTorOptions.TorrcFile) || !File.Exists(_transmuterTorOptions.TorrcFile))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_transmuterTorOptions.TorrcFile))
|
||||
_logger.LogWarning("Torrc file is not found");
|
||||
Services = Array.Empty<TorService>();
|
||||
return;
|
||||
}
|
||||
List<TorService> result = new List<TorService>();
|
||||
try
|
||||
{
|
||||
var torrcContent = File.ReadAllText(_transmuterTorOptions.TorrcFile);
|
||||
if (!Torrc.TryParse(torrcContent, out var torrc))
|
||||
{
|
||||
_logger.LogWarning("Torrc file could not be parsed");
|
||||
Services = Array.Empty<TorService>();
|
||||
return;
|
||||
}
|
||||
|
||||
var services = torrc.ServiceDirectories.SelectMany(d => d.ServicePorts.Select(p => (Directory: new DirectoryInfo(d.DirectoryPath), VirtualPort: p.VirtualPort)))
|
||||
.Select(d => (ServiceName: d.Directory.Name,
|
||||
ReadingLines: System.IO.File.ReadAllLines(Path.Combine(d.Directory.FullName, "hostname")),
|
||||
VirtualPort: d.VirtualPort))
|
||||
.ToArray();
|
||||
foreach (var service in services)
|
||||
{
|
||||
try
|
||||
{
|
||||
var onionHost = (service.ReadingLines)[0].Trim();
|
||||
var torService = new TorService()
|
||||
{
|
||||
Name = service.ServiceName,
|
||||
OnionHost = $"http://{onionHost}",
|
||||
VirtualPort = service.VirtualPort
|
||||
};
|
||||
result.Add(torService);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Error while reading hidden service {service.ServiceName} configuration");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Error while reading torrc file");
|
||||
}
|
||||
Services = result.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
98
BTCTransmuter.Extension.Tor/Services/Torrc.cs
Normal file
98
BTCTransmuter.Extension.Tor/Services/Torrc.cs
Normal file
@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
|
||||
namespace BTCTransmuter.Extension.Tor.Services
|
||||
{
|
||||
public class Torrc
|
||||
{
|
||||
public static bool TryParse(string str, out Torrc value)
|
||||
{
|
||||
value = null;
|
||||
List<HiddenServiceDir> serviceDirectories = new List<HiddenServiceDir>();
|
||||
var lines = str.Split(new char[] { '\n' });
|
||||
HiddenServiceDir currentDirectory = null;
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (HiddenServiceDir.TryParse(line, out var dir))
|
||||
{
|
||||
serviceDirectories.Add(dir);
|
||||
currentDirectory = dir;
|
||||
}
|
||||
else if (HiddenServicePortDefinition.TryParse(line, out var portDef) && currentDirectory != null)
|
||||
{
|
||||
currentDirectory.ServicePorts.Add(portDef);
|
||||
}
|
||||
}
|
||||
value = new Torrc() { ServiceDirectories = serviceDirectories };
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<HiddenServiceDir> ServiceDirectories { get; set; } = new List<HiddenServiceDir>();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
foreach(var serviceDir in ServiceDirectories)
|
||||
{
|
||||
builder.AppendLine(serviceDir.ToString());
|
||||
foreach (var port in serviceDir.ServicePorts)
|
||||
builder.AppendLine(port.ToString());
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public class HiddenServiceDir
|
||||
{
|
||||
public static bool TryParse(string str, out HiddenServiceDir serviceDir)
|
||||
{
|
||||
serviceDir = null;
|
||||
if (!str.Trim().StartsWith("HiddenServiceDir ", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
var parts = str.Split(new char[] { ' ', '\t' }, StringSplitOptions.None);
|
||||
if (parts.Length != 2)
|
||||
return false;
|
||||
serviceDir = new HiddenServiceDir() { DirectoryPath = parts[1].Trim() };
|
||||
return true;
|
||||
}
|
||||
|
||||
public string DirectoryPath { get; set; }
|
||||
public List<HiddenServicePortDefinition> ServicePorts { get; set; } = new List<HiddenServicePortDefinition>();
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"HiddenServiceDir {DirectoryPath}";
|
||||
}
|
||||
}
|
||||
public class HiddenServicePortDefinition
|
||||
{
|
||||
public static bool TryParse(string str, out HiddenServicePortDefinition portDefinition)
|
||||
{
|
||||
portDefinition = null;
|
||||
if (!str.Trim().StartsWith("HiddenServicePort ", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
var parts = str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length != 3)
|
||||
return false;
|
||||
if (!int.TryParse(parts[1].Trim(), out int virtualPort))
|
||||
return false;
|
||||
var addressPort = parts[2].Trim().Split(new []{':'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (addressPort.Length != 2)
|
||||
return false;
|
||||
if (!int.TryParse(addressPort[1].Trim(), out int port))
|
||||
return false;
|
||||
if (!IPAddress.TryParse(addressPort[0].Trim(), out IPAddress address))
|
||||
return false;
|
||||
portDefinition = new HiddenServicePortDefinition() { VirtualPort = virtualPort, Endpoint = new IPEndPoint(address, port) };
|
||||
return true;
|
||||
}
|
||||
public int VirtualPort { get; set; }
|
||||
public IPEndPoint Endpoint { get; set; }
|
||||
public override string ToString()
|
||||
{
|
||||
return $"HiddenServicePort {VirtualPort} {Endpoint}";
|
||||
}
|
||||
}
|
||||
}
|
||||
22
BTCTransmuter.Extension.Tor/TorBtcTransmuterExtension.cs
Normal file
22
BTCTransmuter.Extension.Tor/TorBtcTransmuterExtension.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using BtcTransmuter.Abstractions.Extensions;
|
||||
using BTCTransmuter.Extension.Tor.Configuration;
|
||||
using BTCTransmuter.Extension.Tor.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace BTCTransmuter.Extension.Tor
|
||||
{
|
||||
public class TorBtcTransmuterExtension : BtcTransmuterExtension
|
||||
{
|
||||
public override string Name => "Tor Plugin";
|
||||
public override string Description => "Allows you to access Transmuter over Tor";
|
||||
public override string MenuPartial => "TorOnionLinkPartial";
|
||||
|
||||
public override void Execute(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddSingleton<BTCTransmuterTorOptions>();
|
||||
serviceCollection.AddSingleton<TorServices>();
|
||||
base.Execute(serviceCollection);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
@inject BTCTransmuter.Extension.Tor.Services.TorServices TorServices
|
||||
@if (TorServices.TransmuterTorService != null)
|
||||
{
|
||||
<li class="nav-item">
|
||||
<a href="@TorServices.TransmuterTorService.OnionHost" class="nav-link">Onion</a>
|
||||
</li>
|
||||
}
|
||||
@ -81,6 +81,8 @@ namespace BtcTransmuter.Abstractions.Actions
|
||||
{
|
||||
return BadRequest("what you tryin to pull bro");
|
||||
}
|
||||
|
||||
modelResult.ToSave.ExternalService = externalService;
|
||||
}
|
||||
|
||||
await _recipeManager.AddOrUpdateRecipeAction(modelResult.ToSave);
|
||||
|
||||
@ -1,15 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ExtCore.Mvc.Infrastructure" Version="4.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.2.0" />
|
||||
<PackageReference Include="NetCore.AutoRegisterDi" Version="1.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||
<PackageReference Include="wilx.System.Linq.Dynamic.Core" Version="2.1.0" />
|
||||
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" />
|
||||
<PackageReference Include="McMaster.NETCore.Plugins.Mvc" Version="1.3.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="3.1.4" />
|
||||
<PackageReference Include="NetCore.AutoRegisterDi" Version="2.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.1.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Data\BtcTransmuter.Data.csproj" />
|
||||
|
||||
@ -3,7 +3,6 @@ namespace BtcTransmuter
|
||||
public enum DatabaseType
|
||||
{
|
||||
Sqlite,
|
||||
Postgres,
|
||||
SqlServer
|
||||
Postgres
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace BtcTransmuter
|
||||
{
|
||||
public interface IBtcTransmuterOptions
|
||||
{
|
||||
string ExtensionsDir { get; set; }
|
||||
string DatabaseConnectionString { get; set; }
|
||||
string DataProtectionDir { get; set; }
|
||||
DatabaseType DatabaseType { get; set; }
|
||||
bool UseDatabaseColumnEncryption { get; set; }
|
||||
bool DisableInternalAuth { get; set; }
|
||||
Uri BTCPayAuthServer { get; set; }
|
||||
}
|
||||
}
|
||||
@ -4,17 +4,17 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using BtcTransmuter.Abstractions.Actions;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Abstractions.Helpers;
|
||||
using BtcTransmuter.Abstractions.Triggers;
|
||||
using ExtCore.Infrastructure;
|
||||
using ExtCore.Infrastructure.Actions;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NetCore.AutoRegisterDi;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Extensions
|
||||
{
|
||||
public abstract class BtcTransmuterExtension: ExtensionBase, IExtension, IConfigureAction, IConfigureServicesAction
|
||||
public abstract class BtcTransmuterExtension: IExtension
|
||||
{
|
||||
|
||||
public abstract string Name { get; }
|
||||
@ -25,6 +25,7 @@ namespace BtcTransmuter.Abstractions.Extensions
|
||||
public virtual string Authors { get; } = string.Empty;
|
||||
public virtual string HeaderPartial { get; }
|
||||
public virtual string MenuPartial { get; }
|
||||
public virtual IEnumerable<JsonConverter> JsonConverters { get; set; }
|
||||
|
||||
public IEnumerable<IActionDescriptor> Actions => GetInstancesOfTypeInOurAssembly<IActionDescriptor>();
|
||||
public IEnumerable<ITriggerDescriptor> Triggers => GetInstancesOfTypeInOurAssembly<ITriggerDescriptor>();
|
||||
@ -42,10 +43,6 @@ namespace BtcTransmuter.Abstractions.Extensions
|
||||
Execute(serviceCollection);
|
||||
}
|
||||
|
||||
int IConfigureServicesAction.Priority => 0;
|
||||
|
||||
int IConfigureAction.Priority=> 0;
|
||||
|
||||
public virtual void Execute(IServiceCollection serviceCollection)
|
||||
{
|
||||
RegisterInstances(serviceCollection, new Type[]
|
||||
@ -55,6 +52,7 @@ namespace BtcTransmuter.Abstractions.Extensions
|
||||
typeof(ITriggerDescriptor),
|
||||
typeof(ITriggerHandler),
|
||||
typeof(IExternalServiceDescriptor),
|
||||
typeof(TransmuterInterpolationTypeProvider),
|
||||
});
|
||||
RegisterInstances(serviceCollection, new Type[]
|
||||
{
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Extensions
|
||||
{
|
||||
public static class ControllerExtensions
|
||||
{
|
||||
|
||||
public static bool IsApi(this Controller controller)
|
||||
{
|
||||
return controller.HttpContext.IsApi();
|
||||
}
|
||||
|
||||
public static bool IsApi(this HttpContext context)
|
||||
{
|
||||
if (context.Items.TryGetValue("API", out var val))
|
||||
{
|
||||
return val is true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void SetIsApi(this HttpContext context, bool val)
|
||||
{
|
||||
context.Items.TryAdd("API", val);
|
||||
}
|
||||
public static IActionResult ViewOrJson<T>(this Controller controller, string viewName, T payload)
|
||||
{
|
||||
if (controller.IsApi())
|
||||
{
|
||||
return controller.Json(payload);
|
||||
}
|
||||
|
||||
return controller.View(viewName, payload);
|
||||
}
|
||||
public static IActionResult ViewOrJson<T>(this Controller controller, T payload)
|
||||
{
|
||||
if (controller.IsApi())
|
||||
{
|
||||
return controller.Json(payload);
|
||||
}
|
||||
|
||||
return controller.View(payload);
|
||||
}
|
||||
public static IActionResult ViewOrBadRequest<T>(this Controller controller, T payload, bool usePayloadInBadRequest = false)
|
||||
{
|
||||
if (controller.IsApi())
|
||||
{
|
||||
return usePayloadInBadRequest ? controller.BadRequest(payload) : controller.BadRequest(controller.ModelState);
|
||||
}
|
||||
|
||||
return controller.View(payload);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Extensions
|
||||
{
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static HashSet<T> ToHashSet<T>(
|
||||
this IEnumerable<T> source,
|
||||
IEqualityComparer<T> comparer = null)
|
||||
{
|
||||
return new HashSet<T>(source, comparer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,29 +1,15 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Extensions
|
||||
{
|
||||
public static class ModelStateExtensions
|
||||
{
|
||||
public static void AddModelError<TModel, TProperty>(
|
||||
this ModelStateDictionary modelState,
|
||||
Expression<Func<TModel, TProperty>> ex,
|
||||
string message
|
||||
)
|
||||
{
|
||||
var key = ExpressionHelper.GetExpressionText(ex);
|
||||
modelState.AddModelError(key, message);
|
||||
}
|
||||
|
||||
public static void AddModelError<TModel, TProperty>(this TModel source,
|
||||
Expression<Func<TModel, TProperty>> ex,
|
||||
public static void AddModelError<TModel>(this TModel source,
|
||||
string name,
|
||||
string message,
|
||||
ModelStateDictionary modelState)
|
||||
{
|
||||
var key = ExpressionHelper.GetExpressionText(ex);
|
||||
modelState.AddModelError(key, message);
|
||||
modelState.AddModelError(name, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
16
BtcTransmuter.Abstractions/Extensions/RequestExtensions.cs
Normal file
16
BtcTransmuter.Abstractions/Extensions/RequestExtensions.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Extensions
|
||||
{
|
||||
public static class RequestExtensions
|
||||
{
|
||||
public static string GetAbsoluteRoot(this HttpRequest request)
|
||||
{
|
||||
return string.Concat(
|
||||
request.Scheme,
|
||||
"://",
|
||||
request.Host.ToUriComponent(),
|
||||
request.PathBase.ToUriComponent());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -19,15 +19,15 @@ namespace BtcTransmuter.Abstractions.ExternalServices
|
||||
public abstract string Name { get; }
|
||||
public abstract string Description { get; }
|
||||
public abstract string ViewPartial { get; }
|
||||
protected abstract string ControllerName { get; }
|
||||
public abstract string ControllerName { get; }
|
||||
|
||||
|
||||
public T GetData()
|
||||
public virtual T GetData()
|
||||
{
|
||||
return _data.Get<T>();
|
||||
}
|
||||
|
||||
public void SetData(T data)
|
||||
public virtual void SetData(T data)
|
||||
{
|
||||
_data.Set(data);
|
||||
}
|
||||
|
||||
@ -35,7 +35,7 @@ namespace BtcTransmuter.Abstractions.ExternalServices
|
||||
{
|
||||
return result.Error;
|
||||
}
|
||||
|
||||
|
||||
return View(await BuildViewModel(result.Data));
|
||||
}
|
||||
|
||||
@ -117,7 +117,7 @@ namespace BtcTransmuter.Abstractions.ExternalServices
|
||||
protected async Task<bool> IsAdmin()
|
||||
{
|
||||
var user = await _userManager.GetUserAsync(User);
|
||||
return await _userManager.IsInRoleAsync(user, "Admin");
|
||||
return user!= null && await _userManager.IsInRoleAsync(user, "Admin");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Dynamic.Core;
|
||||
using System.Linq.Dynamic.Core.CustomTypeProviders;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text.RegularExpressions;
|
||||
using BtcTransmuter.Abstractions.Extensions;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Helpers
|
||||
{
|
||||
public class InterpolationHelper
|
||||
public class TransmuterInterpolationTypeProvider : DefaultDynamicLinqCustomTypeProvider
|
||||
{
|
||||
private readonly Type[] _types;
|
||||
|
||||
public TransmuterInterpolationTypeProvider(params Type[] types)
|
||||
{
|
||||
_types = types;
|
||||
}
|
||||
public override HashSet<Type> GetCustomTypes()
|
||||
{
|
||||
return _types.ToHashSet();
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class InterpolationTypeProvider : DefaultDynamicLinqCustomTypeProvider {
|
||||
private readonly IEnumerable<DefaultDynamicLinqCustomTypeProvider> _typeProviders;
|
||||
|
||||
public InterpolationTypeProvider(IEnumerable<TransmuterInterpolationTypeProvider> typeProviders)
|
||||
{
|
||||
_typeProviders = typeProviders;
|
||||
ParsingConfig.Default.CustomTypeProvider = this;
|
||||
|
||||
}
|
||||
|
||||
public override HashSet<Type> GetCustomTypes()
|
||||
{
|
||||
return _typeProviders.SelectMany(provider => provider.GetCustomTypes()).ToHashSet();
|
||||
}
|
||||
}
|
||||
public static class InterpolationHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// https://dotnetfiddle.net/MoqJFk
|
||||
/// </summary>
|
||||
@ -15,15 +48,25 @@ namespace BtcTransmuter.Abstractions.Helpers
|
||||
{
|
||||
try
|
||||
{
|
||||
var parameterExpressions =
|
||||
data.Select(pair => Expression.Parameter(pair.Value.GetType(), pair.Key)).ToList();
|
||||
|
||||
return Regex.Replace(value, @"{{(.+?)}}",
|
||||
match =>
|
||||
{
|
||||
var parameterExpressions =
|
||||
data.Select(pair => Expression.Parameter(pair.Value.GetType(), pair.Key)).ToArray();
|
||||
var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(parameterExpressions, null,
|
||||
match.Groups[1].Value);
|
||||
|
||||
return (e.Compile().DynamicInvoke(data.Values.ToArray()) ?? "").ToString();
|
||||
var processed = match.Groups[1].Value;
|
||||
try
|
||||
{
|
||||
var e = DynamicExpressionParser.ParseLambda(parameterExpressions.ToArray(), null,
|
||||
processed);
|
||||
|
||||
return (e.Compile().DynamicInvoke(data.Values.ToArray()) ?? "").ToString();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return processed;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
catch (Exception)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Data;
|
||||
@ -24,6 +24,7 @@ namespace BtcTransmuter.Abstractions.Recipes
|
||||
Task RemoveRecipeTrigger(string recipeTriggerId);
|
||||
Task RemoveRecipeActionGroup(string recipeActionGroupId);
|
||||
Task<string> GetRecipeName(string recipeId);
|
||||
|
||||
Task<Recipe> CloneRecipe(string recipeId, bool enable, string newName = null);
|
||||
|
||||
}
|
||||
}
|
||||
10
BtcTransmuter.Abstractions/Settings/ISettingsManager.cs
Normal file
10
BtcTransmuter.Abstractions/Settings/ISettingsManager.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BtcTransmuter.Abstractions.Settings
|
||||
{
|
||||
public interface ISettingsManager
|
||||
{
|
||||
Task<T> GetSettings<T>(string key);
|
||||
Task SaveSettings<T>(string key,T settings);
|
||||
}
|
||||
}
|
||||
@ -20,7 +20,7 @@ namespace BtcTransmuter.Abstractions.Triggers
|
||||
public abstract string Name { get; }
|
||||
public abstract string Description { get; }
|
||||
public abstract string ViewPartial { get; }
|
||||
protected abstract string ControllerName { get; }
|
||||
public abstract string ControllerName { get; }
|
||||
|
||||
public Task<IActionResult> EditData(RecipeTrigger data)
|
||||
{
|
||||
@ -55,7 +55,7 @@ namespace BtcTransmuter.Abstractions.Triggers
|
||||
|
||||
var triggerData = await GetTriggerData(trigger);
|
||||
|
||||
if (typeof(TTriggerParameters).IsAssignableFrom(typeof(IUseExternalService)) &&
|
||||
if (typeof(IUseExternalService).IsAssignableFrom(typeof(TTriggerData)) &&
|
||||
((IUseExternalService) triggerData).ExternalServiceId != recipeTrigger.ExternalServiceId)
|
||||
{
|
||||
return false;
|
||||
|
||||
23
BtcTransmuter.Abstractions/Validations.cs
Normal file
23
BtcTransmuter.Abstractions/Validations.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BtcTransmuter.Abstractions
|
||||
{
|
||||
public class EnsureMinimumElementsAttribute : ValidationAttribute
|
||||
{
|
||||
private readonly int _minElements;
|
||||
public EnsureMinimumElementsAttribute(int minElements)
|
||||
{
|
||||
_minElements = minElements;
|
||||
}
|
||||
|
||||
public override bool IsValid(object value)
|
||||
{
|
||||
if (value is IList list)
|
||||
{
|
||||
return list.Count >= _minElements;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,21 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" />
|
||||
<!--<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.6" />-->
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Migrations" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using BtcTransmuter.Data.Encryption;
|
||||
using BtcTransmuter.Data.Models;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public abstract class BaseEntity : IHasJsonData
|
||||
public abstract class BaseEntity :BaseIdEntity, IHasJsonData
|
||||
{
|
||||
public string Id { get; set; }
|
||||
[Encrypted] public string DataJson { get; set; }
|
||||
}
|
||||
}
|
||||
10
BtcTransmuter.Data/Entities/BaseIdEntity.cs
Normal file
10
BtcTransmuter.Data/Entities/BaseIdEntity.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public abstract class BaseIdEntity
|
||||
{
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public string Id { get; set; }
|
||||
}
|
||||
}
|
||||
@ -2,9 +2,8 @@ using System.Collections.Generic;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public class Recipe
|
||||
public class Recipe : BaseIdEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
@ -13,7 +12,13 @@ namespace BtcTransmuter.Data.Entities
|
||||
public RecipeTrigger RecipeTrigger { get; set; }
|
||||
public List<RecipeAction> RecipeActions { get; set; } // executes all actions
|
||||
|
||||
public List<RecipeActionGroup> RecipeActionGroups { get; set; } // executes actions in groups. Action in a group are executed syncrhonously and depending if the previous action executes
|
||||
public List<RecipeActionGroup>
|
||||
RecipeActionGroups
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} // executes actions in groups. Action in a group are executed syncrhonously and depending if the previous action executes
|
||||
|
||||
public List<RecipeInvocation> RecipeInvocations { get; set; } // log
|
||||
}
|
||||
}
|
||||
@ -13,8 +13,11 @@ namespace BtcTransmuter.Data.Entities
|
||||
public Recipe Recipe { get; set; }
|
||||
public ExternalServiceData ExternalService { get; set; }
|
||||
public RecipeActionGroup RecipeActionGroup { get; set; }
|
||||
public List<RecipeInvocation> RecipeInvocations { get; set; }
|
||||
public int Order { get; set; } = 0;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{ActionId} {(ExternalService == null ? string.Empty : $"using service {ExternalService.Name}")}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public class RecipeActionGroup
|
||||
public class RecipeActionGroup : BaseIdEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string RecipeId { get; set; }
|
||||
|
||||
public Recipe Recipe { get; set; }
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public class RecipeInvocation
|
||||
public class RecipeInvocation: BaseIdEntity
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string RecipeId { get; set; }
|
||||
public string RecipeActionId { get; set; }
|
||||
|
||||
public string RecipeAction { get; set; }
|
||||
public string TriggerDataJson { get; set; }
|
||||
public string ActionResult { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
public Recipe Recipe { get; set; }
|
||||
public RecipeAction RecipeAction { get; set; }
|
||||
}
|
||||
}
|
||||
7
BtcTransmuter.Data/Entities/Settings.cs
Normal file
7
BtcTransmuter.Data/Entities/Settings.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public class Settings: BaseEntity
|
||||
{
|
||||
public string Key { get; set; }
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using BtcTransmuter.Data.Encryption;
|
||||
using BtcTransmuter.Data.Models;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace BtcTransmuter.Data.Entities
|
||||
{
|
||||
public class User : IdentityUser
|
||||
public class User : IdentityUser, IHasJsonData
|
||||
{
|
||||
public List<Recipe> Recipes { get; set; }
|
||||
|
||||
[Encrypted] public string DataJson { get; set; }
|
||||
}
|
||||
|
||||
public class UserBlob
|
||||
{
|
||||
public bool BasicAuth { get; set; }
|
||||
public BTCPayAuthDetails BTCPayAuthDetails { get; set; } = new BTCPayAuthDetails();
|
||||
}
|
||||
|
||||
public class BTCPayAuthDetails
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Abstractions.Triggers;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Data.Models;
|
||||
using BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer;
|
||||
using BtcTransmuter.Extension.BtcPayServer.HostedServices;
|
||||
using BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Moq;
|
||||
using NBitcoin;
|
||||
using NBitpayClient;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Tests
|
||||
{
|
||||
public class BtcPayInvoiceWatcherHostedServiceTests : BaseTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task CanDetectInvoiceChanges()
|
||||
{
|
||||
var externalServiceManager = new Mock<IExternalServiceManager>();
|
||||
var triggerDispatcher = new Mock<ITriggerDispatcher>();
|
||||
|
||||
var externalServices = new List<ExternalServiceData>()
|
||||
{
|
||||
new ExternalServiceData()
|
||||
{
|
||||
Type = BtcPayServerService.BtcPayServerServiceType,
|
||||
Id = "A",
|
||||
Name = "A",
|
||||
DataJson = JsonConvert.SerializeObject(new BtcPayServerExternalServiceData())
|
||||
}
|
||||
};
|
||||
var invoices = new List<BtcPayInvoice>()
|
||||
{
|
||||
new BtcPayInvoice()
|
||||
{
|
||||
Id = "A-A",
|
||||
Status = Invoice.STATUS_NEW,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now,
|
||||
|
||||
}
|
||||
};
|
||||
ITrigger dispatchedTrigger = null;
|
||||
|
||||
triggerDispatcher.Setup(dispatcher => dispatcher.DispatchTrigger(It.IsAny<ITrigger>()))
|
||||
.Callback<ITrigger>((x) => { dispatchedTrigger = x; }).Returns(Task.CompletedTask);
|
||||
|
||||
|
||||
var mockBitPay = new TestBitpay();
|
||||
mockBitPay.Invoices = () => invoices.ToArray();
|
||||
|
||||
var btcpayserviceMock = new Mock<BtcPayServerService>();
|
||||
btcpayserviceMock.Setup(serverService => serverService.ConstructClient())
|
||||
.Returns(() => mockBitPay);
|
||||
|
||||
btcpayserviceMock.Setup(serverService => serverService.CheckAccess()).ReturnsAsync(() => true);
|
||||
btcpayserviceMock.Setup(serverService => serverService.GetData()).Returns(() => externalServices[0].Get<BtcPayServerExternalServiceData>());
|
||||
|
||||
externalServiceManager
|
||||
.Setup(manager => manager.GetExternalServicesData(It.IsAny<ExternalServicesDataQuery>()))
|
||||
.ReturnsAsync(() => externalServices);
|
||||
|
||||
var flag = 0;
|
||||
externalServiceManager
|
||||
.Setup(manager => manager.UpdateInternalData(It.IsAny<string>(), It.IsAny<object>()))
|
||||
.Callback<string, object>((key, data) =>
|
||||
{
|
||||
externalServices[0].Set(data);
|
||||
flag++;
|
||||
externalServiceManager.Raise(manager => manager.ExternalServiceDataUpdated+=null, new UpdatedItem<ExternalServiceData>()
|
||||
{
|
||||
Item = externalServices[0],
|
||||
Action = UpdatedItem<ExternalServiceData>.UpdateAction.Updated
|
||||
});
|
||||
})
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
|
||||
var service =
|
||||
new TestBtcPayInvoiceWatcherHostedService(externalServiceManager.Object, triggerDispatcher.Object);
|
||||
|
||||
service.ConvertData = data => btcpayserviceMock.Object;
|
||||
|
||||
await service.StartAsync(CancellationToken.None);
|
||||
|
||||
while (flag == 0)
|
||||
{
|
||||
await Task.Delay(200);
|
||||
}
|
||||
externalServiceManager.Verify(
|
||||
manager => manager.GetExternalServicesData(It.IsAny<ExternalServicesDataQuery>()), Times.Once);
|
||||
|
||||
triggerDispatcher.Verify(dispatcher => dispatcher.DispatchTrigger(It.IsAny<ITrigger>()), Times.Once);
|
||||
var invoice = Assert.IsType<InvoiceStatusChangedTrigger>(dispatchedTrigger).Data.Invoice;
|
||||
Assert.Equal(invoices[0].Id, invoice.Id);
|
||||
Assert.Equal(invoices[0].Status, invoice.Status);
|
||||
invoices[0].Status = Invoice.STATUS_PAID;
|
||||
while (flag == 1)
|
||||
{
|
||||
await Task.Delay(200);
|
||||
}
|
||||
|
||||
|
||||
triggerDispatcher.Verify(dispatcher => dispatcher.DispatchTrigger(It.IsAny<ITrigger>()), Times.Exactly(2));
|
||||
|
||||
invoice = Assert.IsType<InvoiceStatusChangedTrigger>(dispatchedTrigger).Data.Invoice;
|
||||
Assert.Equal(invoices[0].Id, invoice.Id);
|
||||
Assert.Equal(invoices[0].Status, invoice.Status);
|
||||
}
|
||||
|
||||
public class TestBtcPayInvoiceWatcherHostedService : BtcPayInvoiceWatcherHostedService
|
||||
{
|
||||
public TestBtcPayInvoiceWatcherHostedService(IExternalServiceManager externalServiceManager,
|
||||
ITriggerDispatcher triggerDispatcher) : base(externalServiceManager, triggerDispatcher)
|
||||
{
|
||||
}
|
||||
|
||||
public Func<ExternalServiceData, BtcPayServerService> ConvertData = null;
|
||||
|
||||
protected override BtcPayServerService GetServiceFromData(ExternalServiceData externalServiceData)
|
||||
{
|
||||
return ConvertData == null
|
||||
? base.GetServiceFromData(externalServiceData)
|
||||
: ConvertData(externalServiceData);
|
||||
}
|
||||
|
||||
protected override TimeSpan CheckInterval { get; } = TimeSpan.FromSeconds(2);
|
||||
}
|
||||
|
||||
|
||||
public class TestBitpay : Bitpay
|
||||
{
|
||||
public TestBitpay(Key ecKey, Uri envUrl) : base(ecKey, envUrl)
|
||||
{
|
||||
}
|
||||
|
||||
public TestBitpay(): base(new Key(), new Uri("https://gozo.com"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Func<Invoice[]> Invoices;
|
||||
|
||||
|
||||
public override Task<T[]> GetInvoicesAsync<T>(DateTime? dateStart = null, DateTime? dateEnd = null)
|
||||
{
|
||||
if (Invoices != null)
|
||||
{
|
||||
return Task.FromResult(Invoices.Invoke().Select(invoice => (T) invoice).ToArray());
|
||||
}
|
||||
return base.GetInvoicesAsync<T>(dateStart, dateEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Xunit;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Tests
|
||||
{
|
||||
public class
|
||||
BtcPayServerServiceTests : BaseExternalServiceTest<BtcPayServerService, BtcPayServerExternalServiceData>
|
||||
{
|
||||
protected override BtcPayServerService GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any() ?? false)
|
||||
{
|
||||
return new BtcPayServerService((ExternalServiceData) setupArgs.First());
|
||||
}
|
||||
|
||||
return new BtcPayServerService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="Moq" Version="4.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.BtcPayServer\BtcTransmuter.Extension.BtcPayServer.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Tests.Base\BtcTransmuter.Tests.Base.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,15 @@
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Extension.BtcPayServer.Actions.GetInvoice;
|
||||
using BtcTransmuter.Extension.BtcPayServer.HostedServices;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Xunit;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Tests
|
||||
{
|
||||
public class
|
||||
GetInvoiceDataActionHandlerTests : BaseActionTest<GetInvoiceDataActionHandler, GetInvoiceData, BtcPayInvoice>
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using BtcTransmuter.Extension.BtcPayServer.Actions.GetPaymentsFromInvoice;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using NBitpayClient;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Tests
|
||||
{
|
||||
public class
|
||||
GetPaymentsFromInvoiceDataActionHandlerTests : BaseActionTest<GetPaymentsFromInvoiceDataActionHandler, GetPaymentsFromInvoiceData, List<InvoicePaymentInfo>>
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,306 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.BtcPayServer.HostedServices;
|
||||
using BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using NBitpayClient;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Tests
|
||||
{
|
||||
public class
|
||||
InvoiceStatusChangedTriggerHandlerTests : BaseTriggerTest<InvoiceStatusChangedTriggerHandler,
|
||||
InvoiceStatusChangedTriggerData,
|
||||
InvoiceStatusChangedTriggerParameters>
|
||||
{
|
||||
[Fact]
|
||||
public async Task IsTriggered_ExecutesCorrectly()
|
||||
{
|
||||
var handler = GetTriggerHandlerInstance();
|
||||
|
||||
//any status combination
|
||||
var parameters = new InvoiceStatusChangedTriggerParameters()
|
||||
{
|
||||
Conditions = InvoiceStatusChangedController.AllowedStatuses.Select(item => new InvoiceStatusChangeCondition()
|
||||
{
|
||||
Status = item.Value,
|
||||
ExceptionStatuses = InvoiceStatusChangedController.AllowedExceptionStatus.Select(item2 => item2.Value).ToList()
|
||||
}).ToList()
|
||||
};
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_NEW,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
//the external service id is not the same, if it triggers you are in deep shit
|
||||
Assert.False(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "B",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_NEW,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_PAID,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_INVALID,
|
||||
ExceptionStatus = new JValue(Invoice.EXSTATUS_PAID_PARTIAL),
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
//only paid status with no exception statuses
|
||||
parameters.Conditions = new List<InvoiceStatusChangeCondition>()
|
||||
{
|
||||
new InvoiceStatusChangeCondition()
|
||||
{
|
||||
Status = Invoice.STATUS_PAID,
|
||||
ExceptionStatuses = new List<string>()
|
||||
{
|
||||
Invoice.EXSTATUS_FALSE
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_PAID,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
Assert.False(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_PAID,
|
||||
ExceptionStatus = Invoice.EXSTATUS_PAID_PARTIAL,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
Assert.False(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_NEW,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
parameters.Conditions = new List<InvoiceStatusChangeCondition>()
|
||||
{
|
||||
new InvoiceStatusChangeCondition()
|
||||
{
|
||||
Status = Invoice.STATUS_PAID,
|
||||
ExceptionStatuses = new List<string>()
|
||||
{
|
||||
Invoice.EXSTATUS_FALSE
|
||||
}
|
||||
},
|
||||
new InvoiceStatusChangeCondition()
|
||||
{
|
||||
Status = Invoice.STATUS_INVALID,
|
||||
ExceptionStatuses = new List<string>()
|
||||
{
|
||||
"paidLate"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Assert.False(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_INVALID,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
Assert.False(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_PAID,
|
||||
ExceptionStatus = Invoice.EXSTATUS_PAID_OVER,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_PAID,
|
||||
ExceptionStatus = Invoice.EXSTATUS_FALSE,
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
Assert.True(await handler.IsTriggered(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
ExternalServiceId = "A",
|
||||
Invoice = new BtcPayInvoice()
|
||||
{
|
||||
Status = BtcPayInvoice.STATUS_INVALID,
|
||||
ExceptionStatus = "paidLate",
|
||||
CurrentTime = DateTimeOffset.Now,
|
||||
InvoiceTime = DateTimeOffset.Now,
|
||||
ExpirationTime = DateTimeOffset.Now
|
||||
}
|
||||
}
|
||||
},
|
||||
new RecipeTrigger()
|
||||
{
|
||||
TriggerId = handler.TriggerId,
|
||||
ExternalServiceId = "A",
|
||||
DataJson = JsonConvert.SerializeObject(parameters)
|
||||
}));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -22,8 +22,9 @@ namespace BtcTransmuter.Extension.BtcPayServer.Actions.GetPaymentsFromInvoice
|
||||
{
|
||||
private readonly IExternalServiceManager _externalServiceManager;
|
||||
|
||||
private static readonly SelectListItem[] PaymentTypes =
|
||||
public static readonly SelectListItem[] PaymentTypes =
|
||||
{
|
||||
new SelectListItem("Any", ""),
|
||||
new SelectListItem("On-Chain", "BTCLike"),
|
||||
new SelectListItem("Off-Chain", "LightningLike")
|
||||
};
|
||||
|
||||
@ -35,11 +35,12 @@ namespace BtcTransmuter.Extension.BtcPayServer.Actions.GetPaymentsFromInvoice
|
||||
var client = service.ConstructClient();
|
||||
var invoice = await client.GetInvoiceAsync<BtcPayInvoice>(invoiceId);
|
||||
|
||||
var payments = invoice.CryptoInfo.SingleOrDefault(info => info.CryptoCode.Equals(actionData.CryptoCode))?
|
||||
.Payments.Where(x =>
|
||||
var payments = invoice.CryptoInfo.Where(info => info.CryptoCode.Equals(actionData.CryptoCode))
|
||||
.SelectMany(info => info.Payments)
|
||||
.Where(x =>
|
||||
string.IsNullOrEmpty(actionData.PaymentType) ||
|
||||
x.PaymentType.Equals(actionData.PaymentType))
|
||||
.ToList() ?? new List<InvoicePaymentInfo>();
|
||||
.ToList();
|
||||
|
||||
return new BtcPayServerActionHandlerResult<List<InvoicePaymentInfo>>()
|
||||
{
|
||||
|
||||
@ -1,24 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="NBitcoin" Version="4.1.1.99" />
|
||||
<PackageReference Include="NBitpayClient" Version="1.0.0.34" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.4" />
|
||||
<PackageReference Include="NBitcoin" Version="5.0.40" />
|
||||
<PackageReference Include="NBitpayClient" Version="1.0.0.38" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Styles\**;Views\**;Scripts\**" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
||||
@ -6,7 +6,8 @@ namespace BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer
|
||||
{
|
||||
public class BtcPayServerExternalServiceData
|
||||
{
|
||||
[Required][Display(Name = "BtcPay Host Url")] public Uri Server { get; set; }
|
||||
[Required][Display(Name = "BtcPay Host Url")]
|
||||
[Validation.Uri] public Uri Server { get; set; }
|
||||
|
||||
public string Seed { get; set; }
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer
|
||||
public override string Name => "BtcPayServer External Service";
|
||||
public override string Description => "BtcPayServer External Service to be able to interact with its services";
|
||||
public override string ViewPartial => "ViewBtcPayServerExternalService";
|
||||
protected override string ControllerName => "BtcPayServer";
|
||||
public override string ControllerName => "BtcPayServer";
|
||||
|
||||
|
||||
public BtcPayServerService() : base()
|
||||
@ -30,7 +30,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer
|
||||
{
|
||||
}
|
||||
|
||||
public Bitpay ConstructClient()
|
||||
public virtual Bitpay ConstructClient()
|
||||
{
|
||||
try
|
||||
{
|
||||
@ -45,10 +45,17 @@ namespace BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> CheckAccess()
|
||||
public virtual async Task<bool> CheckAccess()
|
||||
{
|
||||
var client = ConstructClient();
|
||||
return client != null && await client.TestAccessAsync(Facade.Merchant);
|
||||
try
|
||||
{
|
||||
var client = ConstructClient();
|
||||
return client != null && await client.TestAccessAsync(Facade.Merchant);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetPairingUrl(string label)
|
||||
@ -69,7 +76,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer
|
||||
catch (Exception)
|
||||
{
|
||||
var data = GetData();
|
||||
return new Uri(data.Server, "api-tokens").ToString();
|
||||
return Uri.TryCreate(data.Server, "api-tokens", out var result) ? result.ToString() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
private readonly IExternalServiceManager _externalServiceManager;
|
||||
private readonly ITriggerDispatcher _triggerDispatcher;
|
||||
private ConcurrentDictionary<string, BtcPayServerService> _externalServices;
|
||||
protected virtual TimeSpan CheckInterval { get; } = TimeSpan.FromMinutes(1);
|
||||
|
||||
public BtcPayInvoiceWatcherHostedService(IExternalServiceManager externalServiceManager,
|
||||
ITriggerDispatcher triggerDispatcher)
|
||||
@ -39,19 +40,24 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
_externalServices = new ConcurrentDictionary<string, BtcPayServerService>(
|
||||
externalServices
|
||||
.Select(service =>
|
||||
new KeyValuePair<string, BtcPayServerService>(service.Id, new BtcPayServerService(service))));
|
||||
new KeyValuePair<string, BtcPayServerService>(service.Id, GetServiceFromData(service))));
|
||||
|
||||
_externalServiceManager.ExternalServiceDataUpdated += ExternalServiceManagerOnExternalServiceUpdated;
|
||||
_ = Loop(cancellationToken);
|
||||
}
|
||||
|
||||
protected virtual BtcPayServerService GetServiceFromData(ExternalServiceData externalServiceData)
|
||||
{
|
||||
return new BtcPayServerService(externalServiceData);
|
||||
}
|
||||
|
||||
private async Task Loop(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var tasks = _externalServices.Select(CheckInvoiceChangeInService);
|
||||
await Task.WhenAll(tasks);
|
||||
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
|
||||
await Task.Delay(CheckInterval, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@ -75,7 +81,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
|
||||
var client = service.ConstructClient();
|
||||
|
||||
var invoices = await client.GetInvoicesAsync<BtcPayInvoice>(data.PairedDate);
|
||||
var invoices = await client.GetInvoicesAsync<BtcPayInvoice>(data.PairedDate, null);
|
||||
|
||||
foreach (var invoice in invoices)
|
||||
{
|
||||
@ -86,7 +92,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
{
|
||||
if (data.MonitoredInvoiceStatuses[invoice.Id] != invoice.Status)
|
||||
{
|
||||
await _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
|
||||
_ = _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
@ -98,7 +104,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
}
|
||||
else
|
||||
{
|
||||
await _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
|
||||
_ = _triggerDispatcher.DispatchTrigger(new InvoiceStatusChangedTrigger()
|
||||
{
|
||||
Data = new InvoiceStatusChangedTriggerData()
|
||||
{
|
||||
@ -112,8 +118,8 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
data.MonitoredInvoiceStatuses.AddOrReplace(invoice.Id, invoice.Status);
|
||||
}
|
||||
|
||||
|
||||
service.SetData(data);
|
||||
|
||||
await _externalServiceManager.UpdateInternalData(key, data);
|
||||
}
|
||||
catch (Exception e)
|
||||
@ -137,13 +143,13 @@ namespace BtcTransmuter.Extension.BtcPayServer.HostedServices
|
||||
switch (e.Action)
|
||||
{
|
||||
case UpdatedItem<ExternalServiceData>.UpdateAction.Added:
|
||||
_externalServices.TryAdd(e.Item.Id, new BtcPayServerService(e.Item));
|
||||
_externalServices.TryAdd(e.Item.Id, GetServiceFromData(e.Item));
|
||||
break;
|
||||
case UpdatedItem<ExternalServiceData>.UpdateAction.Removed:
|
||||
_externalServices.TryRemove(e.Item.Id, out var _);
|
||||
break;
|
||||
case UpdatedItem<ExternalServiceData>.UpdateAction.Updated:
|
||||
_externalServices.TryUpdate(e.Item.Id, new BtcPayServerService(e.Item), null);
|
||||
_externalServices.TryUpdate(e.Item.Id, GetServiceFromData(e.Item), null);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.Extensions;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Abstractions.Recipes;
|
||||
using BtcTransmuter.Abstractions.Triggers;
|
||||
@ -22,18 +26,34 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
{
|
||||
private readonly IExternalServiceManager _externalServiceManager;
|
||||
|
||||
private readonly SelectListItem[] AllowedStatuses = new SelectListItem[]
|
||||
public static readonly SelectListItem[] AllowedStatuses = new SelectListItem[]
|
||||
{
|
||||
new SelectListItem() {Text = "Any Status", Value = null},
|
||||
new SelectListItem() {Text = "New", Value = Invoice.STATUS_NEW},
|
||||
new SelectListItem() {Text = "Paid", Value = Invoice.STATUS_PAID},
|
||||
new SelectListItem() {Text = "Invalid", Value = Invoice.STATUS_INVALID},
|
||||
new SelectListItem() {Text = "Confirmed", Value = Invoice.STATUS_CONFIRMED},
|
||||
new SelectListItem() {Text = "Complete", Value = Invoice.STATUS_COMPLETE}
|
||||
new SelectListItem() {Text = "Complete", Value = Invoice.STATUS_COMPLETE},
|
||||
new SelectListItem() {Text = "Expired", Value = "expired"},
|
||||
};
|
||||
|
||||
public static SelectListItem[] AllowedExceptionStatus = new SelectListItem[]
|
||||
{
|
||||
new SelectListItem() {Text = "None", Value = Invoice.EXSTATUS_FALSE},
|
||||
new SelectListItem() {Text = "Paid partially", Value = Invoice.EXSTATUS_PAID_PARTIAL},
|
||||
new SelectListItem() {Text = "Paid over", Value = Invoice.EXSTATUS_PAID_OVER},
|
||||
new SelectListItem() {Text = "Paid late", Value = "paidLate"},
|
||||
new SelectListItem() {Text = "Marked", Value = "marked"},
|
||||
};
|
||||
|
||||
|
||||
public InvoiceStatusChangedController(IRecipeManager recipeManager, UserManager<User> userManager,
|
||||
public static SelectListItem[] GetAvailableStatuses(string[] usedKeys)
|
||||
{
|
||||
return AllowedStatuses.Where(item => !usedKeys.Contains(item.Value)).ToArray();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public InvoiceStatusChangedController(IRecipeManager recipeManager, UserManager<User> userManager,
|
||||
IMemoryCache memoryCache, IExternalServiceManager externalServiceManager) : base(recipeManager, userManager,
|
||||
memoryCache, externalServiceManager)
|
||||
{
|
||||
@ -56,8 +76,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
nameof(ExternalServiceData.Name), data.ExternalServiceId),
|
||||
RecipeId = data.RecipeId,
|
||||
ExternalServiceId = data.ExternalServiceId,
|
||||
Status = fromData.Status,
|
||||
Statuses = new SelectList(AllowedStatuses, "Value", "Text", fromData.Status),
|
||||
Conditions = fromData.Conditions,
|
||||
};
|
||||
}
|
||||
|
||||
@ -65,7 +84,7 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
BuildModel(
|
||||
InvoiceStatusChangedTriggerViewModel viewModel, RecipeTrigger mainModel)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
if (viewModel.Action != "EditData" || !ModelState.IsValid)
|
||||
{
|
||||
var btcPayServices = await _externalServiceManager.GetExternalServicesData(
|
||||
new ExternalServicesDataQuery()
|
||||
@ -73,12 +92,29 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
Type = new[] {BtcPayServerService.BtcPayServerServiceType},
|
||||
UserId = GetUserId()
|
||||
});
|
||||
|
||||
|
||||
viewModel.Statuses = new SelectList(AllowedStatuses, "Value", "Text", viewModel.Status);
|
||||
viewModel.ExternalServices = new SelectList(btcPayServices, nameof(ExternalServiceData.Id),
|
||||
nameof(ExternalServiceData.Name), viewModel.ExternalServiceId);
|
||||
return (null, viewModel);
|
||||
|
||||
|
||||
if (viewModel.Action.StartsWith("add-condition", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
viewModel.Conditions.Add(new InvoiceStatusChangeCondition()
|
||||
{
|
||||
Status = GetAvailableStatuses(viewModel.Conditions.Select(condition => condition.Status).ToArray()).FirstOrDefault()?.Value?? Invoice.STATUS_NEW,
|
||||
ExceptionStatuses = new List<string>()
|
||||
{
|
||||
Invoice.EXSTATUS_FALSE
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (viewModel.Action.StartsWith("remove-condition", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
var index = int.Parse(viewModel.Action.Substring(viewModel.Action.IndexOf(":", StringComparison.InvariantCultureIgnoreCase) + 1));
|
||||
viewModel.Conditions.RemoveAt(index);
|
||||
}
|
||||
|
||||
return (null, viewModel);
|
||||
}
|
||||
|
||||
mainModel.ExternalServiceId = viewModel.ExternalServiceId;
|
||||
@ -88,10 +124,10 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
|
||||
public class InvoiceStatusChangedTriggerViewModel : InvoiceStatusChangedTriggerParameters
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public string RecipeId { get; set; }
|
||||
public SelectList ExternalServices { get; set; }
|
||||
[Required][Display(Name = "BtcPay Service")] public string ExternalServiceId { get; set; }
|
||||
public SelectList Statuses { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,11 +2,13 @@ using System;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.Triggers;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using NBitpayClient;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
{
|
||||
public class InvoiceStatusChangedTriggerHandler : BaseTriggerHandler<InvoiceStatusChangedTriggerData,
|
||||
InvoiceStatusChangedTriggerParameters>
|
||||
InvoiceStatusChangedTriggerParameters>
|
||||
{
|
||||
public override string TriggerId => new InvoiceStatusChangedTrigger().Id;
|
||||
public override string Name => "BtcPayServer Invoice Status Change";
|
||||
@ -15,19 +17,30 @@ namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
"Trigger a recipe by detecting a status change of an invoice in a btcpay external service.";
|
||||
|
||||
public override string ViewPartial => "ViewInvoiceStatusChangedTrigger";
|
||||
protected override string ControllerName => "InvoiceStatusChanged";
|
||||
public override string ControllerName => "InvoiceStatusChanged";
|
||||
|
||||
protected override Task<bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
|
||||
InvoiceStatusChangedTriggerData triggerData,
|
||||
InvoiceStatusChangedTriggerParameters parameters)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameters.Status))
|
||||
|
||||
var exceptionStatus =Invoice.EXSTATUS_FALSE;
|
||||
if (triggerData.Invoice.ExceptionStatus.Type == JTokenType.String)
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
exceptionStatus = triggerData.Invoice.ExceptionStatus.Value<string>();
|
||||
}
|
||||
var status = triggerData.Invoice.Status;
|
||||
|
||||
foreach (var condition in parameters.Conditions)
|
||||
{
|
||||
if (condition.Status.Equals(status, StringComparison.InvariantCultureIgnoreCase) &&
|
||||
condition.ExceptionStatuses.Contains(exceptionStatus))
|
||||
{
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult(triggerData.Invoice.Status.Equals(parameters.Status,
|
||||
StringComparison.InvariantCultureIgnoreCase));
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using BtcTransmuter.Abstractions;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged
|
||||
{
|
||||
public class InvoiceStatusChangedTriggerParameters
|
||||
{
|
||||
public string Status { get; set; }
|
||||
public List<InvoiceStatusChangeCondition> Conditions { get; set; } = new List<InvoiceStatusChangeCondition>();
|
||||
}
|
||||
|
||||
public class InvoiceStatusChangeCondition
|
||||
{
|
||||
[Required]
|
||||
public string Status { get; set; }
|
||||
[Display(Name = "Additional Statuses")]
|
||||
[Required]
|
||||
[EnsureMinimumElements(1, ErrorMessage = "At least one additional status is required")]
|
||||
public List<string> ExceptionStatuses { get; set; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Globalization;
|
||||
|
||||
namespace BtcTransmuter.Extension.BtcPayServer.Validation
|
||||
{
|
||||
public class UriAttribute : ValidationAttribute
|
||||
{
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
var str = value == null ? null : Convert.ToString(value, CultureInfo.InvariantCulture);
|
||||
Uri uri;
|
||||
bool valid = string.IsNullOrWhiteSpace(str) || Uri.TryCreate(str, UriKind.Absolute, out uri);
|
||||
|
||||
if (!valid)
|
||||
{
|
||||
return new ValidationResult(ErrorMessage);
|
||||
}
|
||||
return ValidationResult.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,14 +20,47 @@
|
||||
<span class="text-danger">There are no BtcPay external services available to create this action. <a asp-action="CreateExternalService" asp-controller="ExternalServices">Create one</a></span>
|
||||
}
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Status" class="control-label"></label>
|
||||
<select asp-for="Status" asp-items="Model.Statuses" class="form-control"></select>
|
||||
<span asp-validation-for="Status" class="text-danger"></span>
|
||||
|
||||
<div class="list-group mb-2">
|
||||
<div class="list-group-item ">
|
||||
<h5 class="mb-1">Status Conditions</h5>
|
||||
</div>
|
||||
@for (var index = 0; index < Model.Conditions.Count; index++)
|
||||
{
|
||||
var statusCondition = Model.Conditions[index];
|
||||
<div class="list-group-item justify-content-between align-items-center">
|
||||
<div class="row">
|
||||
|
||||
<div class="form-group col-sm-12 col-md-6">
|
||||
<label asp-for="Conditions[index].Status" class="control-label"></label>
|
||||
<select asp-for="Conditions[index].Status"
|
||||
asp-items="@(new SelectList(BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged.InvoiceStatusChangedController.AllowedStatuses, "Value", "Text", statusCondition.Status))" class="form-control">
|
||||
</select>
|
||||
|
||||
<span asp-validation-for="Conditions[index].Status" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group col-sm-12 col-md-6">
|
||||
<label asp-for="Conditions[index].ExceptionStatuses" class="control-label"></label>
|
||||
<select asp-for="Conditions[index].ExceptionStatuses"
|
||||
asp-items="@(new SelectList(BtcTransmuter.Extension.BtcPayServer.Triggers.InvoiceStatusChanged.InvoiceStatusChangedController.AllowedExceptionStatus, "Value", "Text", statusCondition.ExceptionStatuses))"
|
||||
multiple="multiple"
|
||||
class="form-control">
|
||||
</select>
|
||||
<span asp-validation-for="Conditions[index].ExceptionStatuses" class="text-danger"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<button type="submit" name="action" value="@($"remove-condition:{index}")" class="btn btn-danger">Remove</button>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
<div class="list-group-item ">
|
||||
<button type="submit" name="action" value="add-condition" class="btn btn-secondary">Add</button>
|
||||
</div>
|
||||
</div>
|
||||
<input type="hidden" asp-for="RecipeId" />
|
||||
<div class="mt-2">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<button type="submit" name="" value="" class="btn btn-primary">Save</button>
|
||||
<a asp-action="EditRecipe" asp-controller="Recipes" class="btn btn-secondary" asp-route-id="@Model.RecipeId">Back to recipe</a>
|
||||
</div>
|
||||
</form>
|
||||
@ -5,5 +5,5 @@
|
||||
var data = Model.Get<GetPaymentsFromInvoiceData>();
|
||||
}
|
||||
<div>
|
||||
Get <kbd>@data.CryptoCode @data.PaymentType </kbd>of BtcPay invoice <kbd>@data.InvoiceId</kbd>
|
||||
Get <kbd>@data.CryptoCode @data.PaymentType</kbd>payments of BTCPay invoice <kbd>@data.InvoiceId</kbd>
|
||||
</div>
|
||||
@ -6,10 +6,15 @@
|
||||
}
|
||||
|
||||
<div>
|
||||
Btcpayserver Invoice Status Change
|
||||
@if (!string.IsNullOrEmpty(data.Status))
|
||||
{
|
||||
<span>to @data.Status</span>
|
||||
}
|
||||
|
||||
Btcpayserver Invoice Status Change to
|
||||
<kbd>
|
||||
@if (data.Conditions.Any())
|
||||
{
|
||||
@string.Join(" or ", data.Conditions.Select(condition => $"{condition.Status}({string.Join(" or ", condition.ExceptionStatuses)})"))
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Not configured</span>
|
||||
}
|
||||
</kbd>
|
||||
</div>
|
||||
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="Moq" Version="4.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Tests.Base\BtcTransmuter.Tests.Base.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,15 @@
|
||||
using BtcTransmuter.Extension.Webhook.Triggers.DynamicServiceMarker;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.DynamicServices.Tests
|
||||
{
|
||||
public class DynamicServiceMarkerTriggerHandlerTests : BaseTriggerTest<DynamicServiceMarkerTriggerHandler,
|
||||
DynamicServiceMarkerTriggerData,
|
||||
DynamicServiceMarkerTriggerParameters>
|
||||
{
|
||||
protected override DynamicServiceMarkerTriggerHandler GetTriggerHandlerInstance(params object[] setupArgs)
|
||||
{
|
||||
return new DynamicServiceMarkerTriggerHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.Actions;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Abstractions.Helpers;
|
||||
using BtcTransmuter.Abstractions.Recipes;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.BtcPayServer.ExternalServices.BtcPayServer;
|
||||
using BtcTransmuter.Extension.DynamicService.ExternalServices.DynamicService;
|
||||
using BtcTransmuter.Extension.Lightning.ExternalServices.NBXplorerWallet;
|
||||
using BtcTransmuter.Extension.NBXplorer.Services;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
namespace BtcTransmuter.Extension.DynamicServices.Tests
|
||||
{
|
||||
public class
|
||||
DynamicServiceServiceTests : BaseExternalServiceTest<DynamicServiceService, DynamicServiceExternalServiceData>
|
||||
{
|
||||
public override Task CanSerializeData()
|
||||
{
|
||||
ConfigureDependencyHelper();
|
||||
using (var scope = DependencyHelper.ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var x = GetExternalService();
|
||||
var instance = new DynamicServiceExternalServiceData();
|
||||
var externalService = GetExternalService(new ExternalServiceData()
|
||||
{
|
||||
Type = x.ExternalServiceType
|
||||
},
|
||||
scope.ServiceProvider.GetRequiredService<IRecipeManager>(),
|
||||
scope.ServiceProvider.GetRequiredService<IActionDispatcher>(),
|
||||
scope.ServiceProvider.GetRequiredService<IExternalServiceManager>()
|
||||
|
||||
);
|
||||
externalService.SetData(instance);
|
||||
externalService.GetData();
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
protected override DynamicServiceService GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any()?? false)
|
||||
{
|
||||
|
||||
return new DynamicServiceService(
|
||||
(ExternalServiceData) setupArgs.First(o => o is ExternalServiceData),
|
||||
(IRecipeManager) setupArgs.First(o => o is IRecipeManager),
|
||||
(IActionDispatcher) setupArgs.First(o => o is IActionDispatcher),
|
||||
(IExternalServiceManager) setupArgs.First(o => o is IExternalServiceManager));
|
||||
}
|
||||
return new DynamicServiceService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Styles\**;Views\**;Scripts\**" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -24,7 +24,7 @@ namespace BtcTransmuter.Extension.DynamicService.ExternalServices.DynamicService
|
||||
"DynamicService External Service to be able to resolve other external services using actions";
|
||||
|
||||
public override string ViewPartial => "ViewDynamicServiceExternalService";
|
||||
protected override string ControllerName => "DynamicService";
|
||||
public override string ControllerName => "DynamicService";
|
||||
|
||||
|
||||
public DynamicServiceService() : base()
|
||||
|
||||
@ -15,7 +15,7 @@ namespace BtcTransmuter.Extension.Webhook.Triggers.DynamicServiceMarker
|
||||
"Used to mark a recipe to be sued for a Dynamic External Service";
|
||||
|
||||
public override string ViewPartial => "ViewDynamicServiceMarkerTrigger";
|
||||
protected override string ControllerName => "DynamicServiceMarker";
|
||||
public override string ControllerName => "DynamicServiceMarker";
|
||||
|
||||
|
||||
protected override Task<bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="Moq" Version="4.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.Email\BtcTransmuter.Extension.Email.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Tests.Base\BtcTransmuter.Tests.Base.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
20
BtcTransmuter.Extension.Email.Tests/ImapServiceTests.cs
Normal file
20
BtcTransmuter.Extension.Email.Tests/ImapServiceTests.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System.Linq;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.Email.ExternalServices.Imap;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Xunit;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Tests
|
||||
{
|
||||
public class ImapServiceTests: BaseExternalServiceTest<ImapService, ImapExternalServiceData>{
|
||||
|
||||
protected override ImapService GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any() ?? false)
|
||||
{
|
||||
return new ImapService((ExternalServiceData) setupArgs.First());
|
||||
}
|
||||
return new ImapService();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
BtcTransmuter.Extension.Email.Tests/Pop3ServiceTests.cs
Normal file
19
BtcTransmuter.Extension.Email.Tests/Pop3ServiceTests.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.Linq;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.Email.ExternalServices.Pop3;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Tests
|
||||
{
|
||||
public class Pop3ServiceTests: BaseExternalServiceTest<Pop3Service, Pop3ExternalServiceData>{
|
||||
|
||||
protected override Pop3Service GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any() ?? false)
|
||||
{
|
||||
return new Pop3Service((ExternalServiceData) setupArgs.First());
|
||||
}
|
||||
return new Pop3Service();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
using BtcTransmuter.Extension.Email.Triggers.ReceivedEmail;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Tests
|
||||
{
|
||||
public class ReceivedEmailTriggerHandlerTests : BaseTriggerTest<ReceivedEmailTriggerHandler,
|
||||
ReceivedEmailTriggerData,
|
||||
ReceivedEmailTriggerParameters>
|
||||
{
|
||||
protected override ReceivedEmailTriggerHandler GetTriggerHandlerInstance(params object[] setupArgs)
|
||||
{
|
||||
return new ReceivedEmailTriggerHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using BtcTransmuter.Extension.Email.Actions.SendEmail;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Tests
|
||||
{
|
||||
public class SendEmailDataActionHandlerTests : BaseActionTest<SendEmailDataActionHandler, SendEmailData, string>
|
||||
{
|
||||
protected override SendEmailDataActionHandler GetActionHandlerInstance(params object[] setupArgs)
|
||||
{
|
||||
return new SendEmailDataActionHandler();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
BtcTransmuter.Extension.Email.Tests/SmtpServiceTests.cs
Normal file
19
BtcTransmuter.Extension.Email.Tests/SmtpServiceTests.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.Linq;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.Email.ExternalServices.Smtp;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Tests
|
||||
{
|
||||
public class SmtpServiceTests: BaseExternalServiceTest<SmtpService, SmtpExternalServiceData>{
|
||||
|
||||
protected override SmtpService GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any() ?? false)
|
||||
{
|
||||
return new SmtpService((ExternalServiceData) setupArgs.First());
|
||||
}
|
||||
return new SmtpService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.Actions;
|
||||
@ -11,6 +13,8 @@ using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using MimeKit;
|
||||
using MimeKit.Text;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.Actions.SendEmail
|
||||
{
|
||||
@ -44,15 +48,16 @@ namespace BtcTransmuter.Extension.Email.Actions.SendEmail
|
||||
Subject = fromData.Subject,
|
||||
To = fromData.To,
|
||||
From = fromData.From,
|
||||
IsHTML = fromData.IsHTML,
|
||||
ExternalServices = new SelectList(services, nameof(ExternalServiceData.Id),
|
||||
nameof(ExternalServiceData.Name), from.ExternalServiceId),
|
||||
};
|
||||
}
|
||||
|
||||
protected override async Task<(RecipeAction ToSave, SendEmailViewModel showViewModel)> BuildModel(
|
||||
SendEmailViewModel viewModel, RecipeAction mainModel)
|
||||
{
|
||||
if (ModelState.IsValid)
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
mainModel.ExternalServiceId = viewModel.ExternalServiceId;
|
||||
mainModel.Set<SendEmailData>(viewModel);
|
||||
|
||||
@ -10,5 +10,6 @@ namespace BtcTransmuter.Extension.Email.Actions.SendEmail
|
||||
public string To { get; set; }
|
||||
public string Body { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public bool IsHTML { get; set; } = false;
|
||||
}
|
||||
}
|
||||
@ -34,7 +34,7 @@ namespace BtcTransmuter.Extension.Email.Actions.SendEmail
|
||||
{
|
||||
InternetAddress.Parse(InterpolateString(actionData.To, data))
|
||||
}, InterpolateString(actionData.Subject, data),
|
||||
new TextPart(TextFormat.Plain)
|
||||
new TextPart(actionData.IsHTML ? TextFormat.Html : TextFormat.Plain)
|
||||
{
|
||||
Text = InterpolateString(actionData.Body, data)
|
||||
});
|
||||
|
||||
@ -1,22 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MailKit" Version="2.1.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Styles\**;Views\**;Scripts\**" />
|
||||
<PackageReference Include="MailKit" Version="2.6.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
</Project>
|
||||
|
||||
@ -14,7 +14,7 @@ namespace BtcTransmuter.Extension.Email.ExternalServices.Imap
|
||||
public override string Name => "Imap External Service";
|
||||
public override string Description => "Imap External Service to be able to analyze incoming email as a trigger";
|
||||
public override string ViewPartial => "ViewImapExternalService";
|
||||
protected override string ControllerName => "Imap";
|
||||
public override string ControllerName => "Imap";
|
||||
|
||||
|
||||
public ImapService() : base()
|
||||
|
||||
@ -14,8 +14,8 @@ namespace BtcTransmuter.Extension.Email.ExternalServices.Pop3
|
||||
public override string Name => "Pop3 External Service";
|
||||
public override string Description => "Pop3 External Service to be able to analyze incoming email as a trigger";
|
||||
public override string ViewPartial => "ViewPop3ExternalService";
|
||||
|
||||
protected override string ControllerName => "Pop3";
|
||||
|
||||
public override string ControllerName => "Pop3";
|
||||
|
||||
|
||||
public Pop3Service() : base()
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
@ -6,12 +9,14 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using MimeKit;
|
||||
using MimeKit.Text;
|
||||
|
||||
namespace BtcTransmuter.Extension.Email.ExternalServices.Smtp
|
||||
{
|
||||
[Route("email-plugin/external-services/smtp")]
|
||||
[Authorize]
|
||||
public class SmtpController : BaseExternalServiceController<SmtpExternalServiceData>
|
||||
public class SmtpController : BaseExternalServiceController<EditSmtpExternalServiceViewModel>
|
||||
{
|
||||
public SmtpController(IExternalServiceManager externalServiceManager, UserManager<User> userManager,
|
||||
IMemoryCache memoryCache) : base(externalServiceManager, userManager, memoryCache)
|
||||
@ -20,23 +25,77 @@ namespace BtcTransmuter.Extension.Email.ExternalServices.Smtp
|
||||
|
||||
protected override string ExternalServiceType => SmtpService.SmtpExternalServiceType;
|
||||
|
||||
protected override Task<SmtpExternalServiceData> BuildViewModel(ExternalServiceData data)
|
||||
protected override Task<EditSmtpExternalServiceViewModel> BuildViewModel(ExternalServiceData data)
|
||||
{
|
||||
return Task.FromResult(new SmtpService(data).GetData());
|
||||
var serviceData = new SmtpService(data).GetData();
|
||||
return Task.FromResult(new EditSmtpExternalServiceViewModel()
|
||||
{
|
||||
Password = serviceData.Password,
|
||||
Port = serviceData.Port,
|
||||
Server = serviceData.Server,
|
||||
Username = serviceData.Username,
|
||||
SSL = serviceData.SSL
|
||||
});
|
||||
}
|
||||
|
||||
protected override Task<(ExternalServiceData ToSave, SmtpExternalServiceData showViewModel)> BuildModel(
|
||||
SmtpExternalServiceData viewModel, ExternalServiceData mainModel)
|
||||
protected override async Task<(ExternalServiceData ToSave, EditSmtpExternalServiceViewModel showViewModel)>
|
||||
BuildModel(
|
||||
EditSmtpExternalServiceViewModel viewModel, ExternalServiceData mainModel)
|
||||
{
|
||||
if (ModelState.IsValid && !string.IsNullOrEmpty(viewModel.TestEmail))
|
||||
{
|
||||
var data = new ExternalServiceData();
|
||||
data.Set(viewModel);
|
||||
data.Type = ExternalServiceType;
|
||||
var smtpService = new SmtpService(data);
|
||||
var error = await SendTestEmail(smtpService, viewModel.TestEmail);
|
||||
if (string.IsNullOrEmpty(error))
|
||||
{
|
||||
viewModel.TestEmail = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
ModelState.AddModelError(nameof(viewModel.TestEmail),error);
|
||||
}
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return Task.FromResult<(ExternalServiceData ToSave, SmtpExternalServiceData showViewModel)>((null,
|
||||
viewModel));
|
||||
return (null, viewModel);
|
||||
}
|
||||
|
||||
mainModel.Set(viewModel);
|
||||
return Task.FromResult<(ExternalServiceData ToSave, SmtpExternalServiceData showViewModel)>((mainModel,
|
||||
null));
|
||||
return (mainModel, null);
|
||||
}
|
||||
|
||||
|
||||
private async Task<string> SendTestEmail(SmtpService service, string email)
|
||||
{
|
||||
try
|
||||
{
|
||||
await service.SendEmail(new MimeMessage(new List<InternetAddress>()
|
||||
{
|
||||
InternetAddress.Parse(email)
|
||||
}, new List<InternetAddress>()
|
||||
{
|
||||
InternetAddress.Parse(email)
|
||||
}, "BTCTransmuter Test Email", new TextPart(TextFormat.Plain)
|
||||
{
|
||||
Text = "Just testing your email setup for BTC Transmuter (ノ◕ヮ◕)ノ*:・゚✧"
|
||||
}));
|
||||
return null;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return e.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class EditSmtpExternalServiceViewModel : SmtpExternalServiceData
|
||||
{
|
||||
[EmailAddress]
|
||||
[Display(Name = "Send test email from and to this address to check if your settings are valid")]
|
||||
public string TestEmail { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ namespace BtcTransmuter.Extension.Email.ExternalServices.Smtp
|
||||
public override string Name => "SMTP External Service";
|
||||
public override string Description => "SMTP External Service to be able to send emails as an action";
|
||||
public override string ViewPartial => "ViewSmtpExternalService";
|
||||
protected override string ControllerName => "Smtp";
|
||||
public override string ControllerName => "Smtp";
|
||||
|
||||
|
||||
public SmtpService() : base()
|
||||
|
||||
@ -73,7 +73,7 @@ namespace BtcTransmuter.Extension.Email.HostedServices
|
||||
await inbox.OpenAsync(FolderAccess.ReadOnly);
|
||||
|
||||
var emailIds =
|
||||
await inbox.SearchAsync(new DateSearchQuery(SearchTerm.SentAfter,
|
||||
await inbox.SearchAsync(new DateSearchQuery(SearchTerm.DeliveredAfter,
|
||||
data.LastCheck.GetValueOrDefault(data.PairedDate)));
|
||||
|
||||
|
||||
@ -90,7 +90,7 @@ namespace BtcTransmuter.Extension.Email.HostedServices
|
||||
ExternalServiceId = service.Key
|
||||
}
|
||||
};
|
||||
await _triggerDispatcher.DispatchTrigger(trigger);
|
||||
_ = _triggerDispatcher.DispatchTrigger(trigger);
|
||||
}
|
||||
|
||||
data.LastCheck = DateTime.Now;
|
||||
|
||||
@ -87,7 +87,7 @@ namespace BtcTransmuter.Extension.Email.HostedServices
|
||||
ExternalServiceId = service.Key
|
||||
}
|
||||
};
|
||||
await _triggerDispatcher.DispatchTrigger(trigger);
|
||||
_ = _triggerDispatcher.DispatchTrigger(trigger);
|
||||
}
|
||||
|
||||
data.LastCheck = DateTime.Now;
|
||||
|
||||
@ -21,7 +21,7 @@ namespace BtcTransmuter.Extension.Email.Triggers.ReceivedEmail
|
||||
"Trigger a recipe by receiving a specifically formatted email through a pop3 or imap external service.";
|
||||
|
||||
public override string ViewPartial => "ViewReceivedEmailTrigger";
|
||||
protected override string ControllerName => "ReceivedEmail";
|
||||
public override string ControllerName => "ReceivedEmail";
|
||||
|
||||
protected override Task<bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
|
||||
ReceivedEmailTriggerData triggerData,
|
||||
|
||||
@ -39,7 +39,11 @@
|
||||
<textarea asp-for="Body" class="form-control"></textarea>
|
||||
<span asp-validation-for="Body" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label asp-for="IsHTML" class="control-label"></label>
|
||||
<input type="checkbox" asp-for="IsHTML" class="form-check"/>
|
||||
<span asp-validation-for="IsHTML" class="text-danger"></span>
|
||||
</div>
|
||||
<input type="hidden" asp-for="RecipeId"/>
|
||||
<div class="mt-2">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
|
||||
@ -13,6 +13,6 @@
|
||||
}
|
||||
@if (!string.IsNullOrEmpty(data.Body))
|
||||
{
|
||||
<span>with body <kbd>@data.Body</kbd></span>
|
||||
<span>with body <kbd>@data.Body</kbd>@(data.IsHTML? "(in HTML)": "")</span>
|
||||
}
|
||||
</div>
|
||||
@ -1,4 +1,4 @@
|
||||
@model BtcTransmuter.Extension.Email.ExternalServices.Smtp.SmtpExternalServiceData
|
||||
@model BtcTransmuter.Extension.Email.ExternalServices.Smtp.EditSmtpExternalServiceViewModel
|
||||
@{
|
||||
ViewData["Title"] = "Edit Smtp External Service Data";
|
||||
}
|
||||
@ -33,6 +33,11 @@
|
||||
<input type="checkbox" asp-for="SSL" class="form-check"/>
|
||||
<span asp-validation-for="SSL" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="TestEmail" class="control-label"></label>
|
||||
<input type="text" asp-for="TestEmail" class="form-control"/>
|
||||
<span asp-validation-for="TestEmail" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="mt-2">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a asp-action="GetServices" asp-controller="ExternalServices" class="btn btn-secondary">Back to list</a>
|
||||
|
||||
@ -1,19 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
|
||||
<PackageReference Include="Moq" Version="4.14.1" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.Exchange\BtcTransmuter.Extension.Exchange.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Tests.Base\BtcTransmuter.Tests.Base.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
using BtcTransmuter.Extension.Exchange.Triggers.CheckExchangeBalance;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
{
|
||||
public class CheckExchangeBalanceTriggerHandlerTests :
|
||||
BaseTriggerTest<CheckExchangeBalanceTriggerHandler, CheckExchangeBalanceTriggerData,
|
||||
CheckExchangeBalanceTriggerParameters>
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -1,18 +1,21 @@
|
||||
using System;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Data.Models;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Extension.Exchange.ExternalServices.Exchange;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Xunit;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
{
|
||||
public class ExchangeServiceTests
|
||||
public class ExchangeServiceTests:BaseExternalServiceTest<ExchangeService,ExchangeExternalServiceData >
|
||||
{
|
||||
[Fact]
|
||||
public void ExchangeService_GetAvailableExchanges()
|
||||
public async Task ExchangeService_GetAvailableExchanges()
|
||||
{
|
||||
Assert.True(ExchangeService.GetAvailableExchanges().Any());
|
||||
Assert.True((await ExchangeService.GetAvailableExchanges()).Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -23,7 +26,7 @@ namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
_ = new ExchangeService(new ExternalServiceData()
|
||||
_ = GetExternalService(new ExternalServiceData()
|
||||
{
|
||||
Type = "invalid"
|
||||
});
|
||||
@ -47,24 +50,44 @@ namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExchangeService_CanConstructClient()
|
||||
public async Task ExchangeService_CanConstructClient()
|
||||
{
|
||||
var data = new ExchangeExternalServiceData()
|
||||
var InvalidData = new ExchangeExternalServiceData()
|
||||
{
|
||||
PublicKey = "test",
|
||||
PairedDate = DateTime.Now
|
||||
PairedDate = DateTime.Now,
|
||||
};
|
||||
var externalServiceData = new ExternalServiceData()
|
||||
{
|
||||
Type = ExchangeService.ExchangeServiceType,
|
||||
Name = "something"
|
||||
};
|
||||
externalServiceData.Set(data);
|
||||
externalServiceData.Set(InvalidData);
|
||||
|
||||
var exchangeService = new ExchangeService(externalServiceData);
|
||||
var exchangeService = GetExternalService(externalServiceData);
|
||||
await Assert.ThrowsAnyAsync<Exception>(async () => await exchangeService.ConstructClient());
|
||||
|
||||
|
||||
var validData = new ExchangeExternalServiceData()
|
||||
{
|
||||
PublicKey = "test",
|
||||
PairedDate = DateTime.Now,
|
||||
ExchangeName = "Binance",
|
||||
PrivateKey = "aa"
|
||||
};
|
||||
externalServiceData.Set(validData);
|
||||
|
||||
Assert.NotNull(exchangeService.ConstructClient());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected override ExchangeService GetExternalService(params object[] setupArgs)
|
||||
{
|
||||
if (setupArgs?.Any()?? false)
|
||||
{
|
||||
return new ExchangeService((ExternalServiceData) setupArgs.First());
|
||||
}
|
||||
return new ExchangeService();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using BtcTransmuter.Abstractions.Actions;
|
||||
using BtcTransmuter.Extension.Exchange.Actions.GetExchangeBalance;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
{
|
||||
public class GetExchangeBalanceDataActionHandlerTests :BaseActionTest<GetExchangeBalanceDataActionHandler, GetExchangeBalanceData, decimal>
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using BtcTransmuter.Extension.Exchange.Actions.GetExchangeRate;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using ExchangeSharp;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
{
|
||||
public class GetExchangeRateDataActionHandlerTests :
|
||||
BaseActionTest<GetExchangeRateDataActionHandler, GetExchangeRateData, ExchangeTicker>
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
using BtcTransmuter.Extension.Exchange.Actions.PlaceOrder;
|
||||
using BtcTransmuter.Tests.Base;
|
||||
using ExchangeSharp;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Tests
|
||||
{
|
||||
public class PlaceOrderDataActionHandlerTests :
|
||||
BaseActionTest<PlaceOrderDataActionHandler,PlaceOrderData, ExchangeOrderResult>
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -30,6 +30,17 @@ namespace BtcTransmuter.Extension.Exchange.Actions.GetExchangeBalance
|
||||
_externalServiceManager = externalServiceManager;
|
||||
}
|
||||
|
||||
[HttpGet("symbols/{externalServiceId}")]
|
||||
public async Task<string[]> GetAvailableMarketSymbols(string externalServiceId)
|
||||
{
|
||||
var serviceData =
|
||||
await _externalServiceManager.GetExternalServiceData(externalServiceId, GetUserId());
|
||||
var exchangeService = new ExchangeService(serviceData);
|
||||
var symbols = await (await exchangeService.ConstructClient()).GetCurrenciesAsync();
|
||||
|
||||
return symbols.Keys.ToArray();
|
||||
}
|
||||
|
||||
protected override async Task<GetExchangeBalanceViewModel> BuildViewModel(RecipeAction from)
|
||||
{
|
||||
var services = await _externalServiceManager.GetExternalServicesData(new ExternalServicesDataQuery()
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.Actions;
|
||||
using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.DynamicServices;
|
||||
using BtcTransmuter.Extension.Exchange.ExternalServices.Exchange;
|
||||
using ExchangeSharp;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Actions.GetExchangeBalance
|
||||
{
|
||||
@ -28,10 +28,13 @@ namespace BtcTransmuter.Extension.Exchange.Actions.GetExchangeBalance
|
||||
|
||||
var externalService = await recipeAction.GetExternalService();
|
||||
var exchangeService = new ExchangeService(externalService);
|
||||
var client = exchangeService.ConstructClient();
|
||||
var client = await exchangeService.ConstructClient();
|
||||
|
||||
var result = await client.GetAmountsAsync();
|
||||
var amount = result.ContainsKey(actionData.Asset) ? result[actionData.Asset] : 0;
|
||||
|
||||
var matched = result
|
||||
.FirstOrDefault(pair => pair.Key.Equals(actionData.Asset, StringComparison.InvariantCultureIgnoreCase));
|
||||
var amount = matched.Value;
|
||||
|
||||
return new TypedActionHandlerResult<decimal>()
|
||||
{
|
||||
|
||||
@ -55,7 +55,7 @@ namespace BtcTransmuter.Extension.Exchange.Actions.GetExchangeRate
|
||||
var serviceData =
|
||||
await _externalServiceManager.GetExternalServiceData(viewModel.ExternalServiceId, GetUserId());
|
||||
var exchangeService = new ExchangeService(serviceData);
|
||||
var symbols = (await exchangeService.ConstructClient().GetMarketSymbolsAsync()).ToArray();
|
||||
var symbols = (await (await exchangeService.ConstructClient()).GetMarketSymbolsAsync()).ToArray();
|
||||
if (symbols.Contains(viewModel.MarketSymbol))
|
||||
{
|
||||
mainModel.ExternalServiceId = viewModel.ExternalServiceId;
|
||||
|
||||
@ -26,7 +26,7 @@ namespace BtcTransmuter.Extension.Exchange.Actions.GetExchangeRate
|
||||
{
|
||||
var externalService = await recipeAction.GetExternalService();
|
||||
var exchangeService = new ExchangeService(externalService);
|
||||
var client = exchangeService.ConstructClient();
|
||||
var client = await exchangeService.ConstructClient();
|
||||
|
||||
var result = await client.GetTickerAsync(actionData.MarketSymbol);
|
||||
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@ -30,6 +32,15 @@ namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
_externalServiceManager = externalServiceManager;
|
||||
}
|
||||
|
||||
[HttpGet("symbols/{externalServiceId}")]
|
||||
public async Task<string[]> GetAvailableMarketSymbols(string externalServiceId)
|
||||
{
|
||||
var serviceData =
|
||||
await _externalServiceManager.GetExternalServiceData(externalServiceId, GetUserId());
|
||||
var exchangeService = new ExchangeService(serviceData);
|
||||
return (await (await exchangeService.ConstructClient()).GetMarketSymbolsAsync()).ToArray();
|
||||
}
|
||||
|
||||
protected override async Task<PlaceOrderViewModel> BuildViewModel(RecipeAction from)
|
||||
{
|
||||
var services = await _externalServiceManager.GetExternalServicesData(new ExternalServicesDataQuery()
|
||||
@ -68,7 +79,7 @@ namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
var serviceData =
|
||||
await _externalServiceManager.GetExternalServiceData(viewModel.ExternalServiceId, GetUserId());
|
||||
var exchangeService = new ExchangeService(serviceData);
|
||||
var symbols = (await exchangeService.ConstructClient().GetMarketSymbolsAsync()).ToArray();
|
||||
var symbols = (await (await exchangeService.ConstructClient()).GetMarketSymbolsAsync()).ToArray();
|
||||
if (symbols.Contains(viewModel.MarketSymbol))
|
||||
{
|
||||
mainModel.ExternalServiceId = viewModel.ExternalServiceId;
|
||||
|
||||
@ -6,13 +6,14 @@ using BtcTransmuter.Data.Entities;
|
||||
using BtcTransmuter.Extension.DynamicServices;
|
||||
using BtcTransmuter.Extension.Exchange.ExternalServices.Exchange;
|
||||
using ExchangeSharp;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
{
|
||||
public class PlaceOrderDataActionHandler : BaseActionHandler<PlaceOrderData, ExchangeOrderResult>
|
||||
{
|
||||
public override string ActionId => "PlaceOrder";
|
||||
public override string Name => "Place Order";
|
||||
public override string ActionId => "PlaceOrder";
|
||||
public override string Name => "Place order on an Exchange";
|
||||
|
||||
public override string Description =>
|
||||
"Place an order on an exchange";
|
||||
@ -20,13 +21,12 @@ namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
public override string ViewPartial => "ViewPlaceOrderAction";
|
||||
public override string ControllerName => "PlaceOrder";
|
||||
|
||||
|
||||
protected override async Task<TypedActionHandlerResult<ExchangeOrderResult>> Execute(Dictionary<string, object> data, RecipeAction recipeAction,
|
||||
PlaceOrderData actionData)
|
||||
{
|
||||
var externalService = await recipeAction.GetExternalService();
|
||||
var exchangeService = new ExchangeService(externalService);
|
||||
var client = exchangeService.ConstructClient();
|
||||
var client = await exchangeService.ConstructClient();
|
||||
var orderRequest = new ExchangeOrderRequest()
|
||||
{
|
||||
MarketSymbol = actionData.MarketSymbol,
|
||||
@ -36,13 +36,14 @@ namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
StopPrice = Convert.ToDecimal(InterpolateString(actionData.StopPrice, data)),
|
||||
IsBuy = actionData.IsBuy,
|
||||
IsMargin = actionData.IsMargin,
|
||||
ShouldRoundAmount = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
var result = await client.PlaceOrderAsync(orderRequest);
|
||||
var result = await client.PlaceOrderAsync(orderRequest);
|
||||
System.Threading.Thread.Sleep(500);
|
||||
result = await client.GetOrderDetailsAsync(result.OrderId);
|
||||
result = await client.GetOrderDetailsAsync(result.OrderId, orderRequest.MarketSymbol);
|
||||
return new TypedActionHandlerResult<ExchangeOrderResult>()
|
||||
{
|
||||
Executed = true,
|
||||
@ -57,9 +58,9 @@ namespace BtcTransmuter.Extension.Exchange.Actions.PlaceOrder
|
||||
{
|
||||
Executed = false,
|
||||
Result =
|
||||
$"Could not place order because {e.Message}"
|
||||
$"Could not place order because {e.Message}. Order details: {JsonConvert.SerializeObject(orderRequest)}"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,25 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
|
||||
<PackageReference Include="DigitalRuby.ExchangeSharp" Version="0.6.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BtcTransmuter.Abstractions\BtcTransmuter.Abstractions.csproj" />
|
||||
<ProjectReference Include="..\BtcTransmuter.Extension.DynamicServices\BtcTransmuter.Extension.DynamicServices.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Styles\**;Views\**;Scripts\**" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<UpToDateCheckInput Remove="Views\SendEmail\EditData.cshtml" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Custom.DigitalRuby.ExchangeSharp" Version="0.9.0" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -25,10 +25,10 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
|
||||
protected override string ExternalServiceType => ExchangeService.ExchangeServiceType;
|
||||
|
||||
protected override Task<EditExchangeExternalServiceDataViewModel> BuildViewModel(ExternalServiceData data)
|
||||
protected override async Task<EditExchangeExternalServiceDataViewModel> BuildViewModel(ExternalServiceData data)
|
||||
{
|
||||
return Task.FromResult(new EditExchangeExternalServiceDataViewModel(new ExchangeService(data).GetData(),
|
||||
ExchangeService.GetAvailableExchanges()));
|
||||
return new EditExchangeExternalServiceDataViewModel(new ExchangeService(data).GetData(),
|
||||
await ExchangeService.GetAvailableExchanges());
|
||||
}
|
||||
|
||||
protected override async
|
||||
@ -41,7 +41,7 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return (null,
|
||||
new EditExchangeExternalServiceDataViewModel(viewModel, ExchangeService.GetAvailableExchanges()));
|
||||
new EditExchangeExternalServiceDataViewModel(viewModel, await ExchangeService.GetAvailableExchanges()));
|
||||
}
|
||||
|
||||
//current External Service data
|
||||
@ -50,11 +50,11 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
|
||||
if (!await exchangeService.TestAccess())
|
||||
{
|
||||
ModelState.AddModelError(String.Empty, "Could not connect with current settings");
|
||||
|
||||
ModelState.AddModelError(String.Empty,
|
||||
"Could not connect with current settings. Transmuter tests against fetching your balance amount from the exchange so you would need to enable that option if available");
|
||||
|
||||
return (null,
|
||||
new EditExchangeExternalServiceDataViewModel(viewModel, ExchangeService.GetAvailableExchanges()));
|
||||
new EditExchangeExternalServiceDataViewModel(viewModel, await ExchangeService.GetAvailableExchanges()));
|
||||
}
|
||||
|
||||
return (externalServiceData, null);
|
||||
|
||||
@ -14,7 +14,7 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
public override string Name => "Exchange External Service";
|
||||
public override string Description => "Integrate from a wide variety of cryptocurrency exchanges";
|
||||
public override string ViewPartial => "ViewExchangeExternalService";
|
||||
protected override string ControllerName => "Exchange";
|
||||
public override string ControllerName => "Exchange";
|
||||
|
||||
|
||||
public ExchangeService() : base()
|
||||
@ -25,17 +25,17 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
{
|
||||
}
|
||||
|
||||
public static IExchangeAPI[] GetAvailableExchanges()
|
||||
public static async Task<IExchangeAPI[]> GetAvailableExchanges()
|
||||
{
|
||||
return ExchangeAPI.GetExchangeAPIs();
|
||||
return await ExchangeAPI.GetExchangeAPIsAsync();
|
||||
}
|
||||
|
||||
|
||||
public ExchangeAPI ConstructClient()
|
||||
public async Task<ExchangeAPI> ConstructClient()
|
||||
{
|
||||
var data = GetData();
|
||||
|
||||
var result = ExchangeAPI.GetExchangeAPI(data.ExchangeName);
|
||||
var result = await ExchangeAPI.GetExchangeAPIAsync(data.ExchangeName);
|
||||
if (result is ExchangeAPI api)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(data.OverrideUrl))
|
||||
@ -52,7 +52,7 @@ namespace BtcTransmuter.Extension.Exchange.ExternalServices.Exchange
|
||||
|
||||
public async Task<bool> TestAccess()
|
||||
{
|
||||
var client = ConstructClient();
|
||||
var client = await ConstructClient();
|
||||
if (client == null)
|
||||
{
|
||||
return false;
|
||||
|
||||
@ -52,11 +52,11 @@ namespace BtcTransmuter.Extension.NBXplorer.HostedServices
|
||||
await Task.WhenAll(exchangeExternalServices.Select(async data =>
|
||||
{
|
||||
var exchangeService = new ExchangeService(data);
|
||||
var client = exchangeService.ConstructClient();
|
||||
var client = await exchangeService.ConstructClient();
|
||||
var amounts = await client.GetAmountsAsync();
|
||||
foreach (var keyValuePair in amounts)
|
||||
{
|
||||
await _triggerDispatcher.DispatchTrigger(new CheckExchangeBalanceTrigger()
|
||||
_ = _triggerDispatcher.DispatchTrigger(new CheckExchangeBalanceTrigger()
|
||||
{
|
||||
Data = new CheckExchangeBalanceTriggerData()
|
||||
{
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BtcTransmuter.Abstractions.ExternalServices;
|
||||
using BtcTransmuter.Abstractions.Recipes;
|
||||
@ -30,6 +31,18 @@ namespace BtcTransmuter.Extension.Exchange.Triggers.CheckExchangeBalance
|
||||
_externalServiceManager = externalServiceManager;
|
||||
}
|
||||
|
||||
[HttpGet("symbols/{externalServiceId}")]
|
||||
public async Task<string[]> GetAvailableMarketSymbols(string externalServiceId)
|
||||
{
|
||||
var serviceData =
|
||||
await _externalServiceManager.GetExternalServiceData(externalServiceId, GetUserId());
|
||||
var exchangeService = new ExchangeService(serviceData);
|
||||
var symbols = await (await exchangeService.ConstructClient()).GetCurrenciesAsync();
|
||||
|
||||
return symbols.Keys.ToArray();
|
||||
}
|
||||
|
||||
|
||||
protected override async Task<CheckExchangeBalanceViewModel> BuildViewModel(RecipeTrigger data)
|
||||
{
|
||||
var innerData = data.Get<CheckExchangeBalanceTriggerParameters>();
|
||||
|
||||
@ -15,7 +15,7 @@ namespace BtcTransmuter.Extension.Exchange.Triggers.CheckExchangeBalance
|
||||
"Trigger a recipe by checking if the balance of an asset on an exchange ";
|
||||
|
||||
public override string ViewPartial => "ViewCheckExchangeBalanceTrigger";
|
||||
protected override string ControllerName => "CheckExchangeBalance";
|
||||
public override string ControllerName => "CheckExchangeBalance";
|
||||
|
||||
|
||||
protected override Task<bool> IsTriggered(ITrigger trigger, RecipeTrigger recipeTrigger,
|
||||
|
||||
@ -22,7 +22,7 @@
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Asset" class="control-label"></label>
|
||||
<input asp-for="Asset" class="form-control"/>
|
||||
<input asp-for="Asset" class="form-control autocomplete" data-datasrc="availableMarketSymbols"/>
|
||||
<span asp-validation-for="Asset" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
@ -41,4 +41,31 @@
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a asp-action="EditRecipe" asp-controller="Recipes" class="btn btn-secondary" asp-route-id="@Model.RecipeId">Back to recipe</a>
|
||||
</div>
|
||||
</form>
|
||||
</form>
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
var actionUrlMapping = @Json.Serialize(Model.ExternalServices.ToDictionary(item => item.Value, item => @Url.Action("GetAvailableMarketSymbols", new { ExternalServiceId = item.Value })));;
|
||||
var availableMarketSymbols = [];
|
||||
$(document).ready(function(){
|
||||
$("#ExternalServiceId").on("input", populateAvailableMarketSymbols);
|
||||
function populateAvailableMarketSymbols(){
|
||||
var value = $("#ExternalServiceId").val();
|
||||
if(!value){
|
||||
availableMarketSymbols = [];
|
||||
}else{
|
||||
$.ajax({
|
||||
url: actionUrlMapping[value],
|
||||
success: function(response){
|
||||
availableMarketSymbols = response;
|
||||
},
|
||||
error: function(){
|
||||
availableMarketSymbols = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
populateAvailableMarketSymbols();
|
||||
})
|
||||
</script>
|
||||
@ -22,12 +22,12 @@
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Asset" class="control-label"></label>
|
||||
<input asp-for="Asset" class="form-control"/>
|
||||
<input asp-for="Asset" class="form-control autocomplete" data-datasrc="availableMarketSymbols" />
|
||||
<span asp-validation-for="Asset" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
|
||||
<input type="hidden" asp-for="RecipeId"/>
|
||||
<input type="hidden" asp-for="RecipeId" />
|
||||
<div class="mt-2">
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
<a asp-action="EditRecipe" asp-controller="Recipes" class="btn btn-secondary" asp-route-id="@Model.RecipeId">Back to recipe</a>
|
||||
@ -35,6 +35,33 @@
|
||||
</form>
|
||||
@await Component.InvokeAsync("RecipeActionFooter", new
|
||||
{
|
||||
recipeId = @Model.RecipeId,
|
||||
recipeId = @Model.RecipeId,
|
||||
recipeActionIdInGroupBeforeThisOne = @Model.RecipeActionIdInGroupBeforeThisOne
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
<script>
|
||||
|
||||
var actionUrlMapping = @Json.Serialize(Model.ExternalServices.ToDictionary(item => item.Value, item => @Url.Action("GetAvailableMarketSymbols", new { ExternalServiceId = item.Value })));;
|
||||
var availableMarketSymbols = [];
|
||||
$(document).ready(function(){
|
||||
$("#ExternalServiceId").on("input", populateAvailableMarketSymbols);
|
||||
function populateAvailableMarketSymbols(){
|
||||
var value = $("#ExternalServiceId").val();
|
||||
if(!value){
|
||||
availableMarketSymbols = [];
|
||||
}else{
|
||||
$.ajax({
|
||||
url: actionUrlMapping[value],
|
||||
success: function(response){
|
||||
availableMarketSymbols = response;
|
||||
},
|
||||
error: function(){
|
||||
availableMarketSymbols = [];
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
populateAvailableMarketSymbols();
|
||||
})
|
||||
</script>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user