mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-02-23 07:04:26 +01:00
Fix #18, fix electrum format not recognizing standard p2pkh on testnet
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<Version>1.0.0.39</Version>
|
||||
<Version>1.0.0.40</Version>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="Build\dockerfiles\**" />
|
||||
|
||||
@@ -262,7 +262,7 @@ namespace BTCPayServer.Controllers
|
||||
//var p2wsh = 0x2aa7ed3U;
|
||||
Dictionary<uint, string[]> electrumMapping = new Dictionary<uint, string[]>();
|
||||
//Source https://github.com/spesmilo/electrum/blob/9edffd17542de5773e7284a8c8a2673c766bb3c3/lib/bitcoin.py
|
||||
var standard = 0x0488b21eU;
|
||||
var standard = _Network == Network.Main ? 0x0488b21eU : 0x043587cf;
|
||||
electrumMapping.Add(standard, new[] { "legacy" });
|
||||
var p2wpkh_p2sh = 0x049d7cb2U;
|
||||
electrumMapping.Add(p2wpkh_p2sh, new string[] { "p2sh" });
|
||||
@@ -280,7 +280,7 @@ namespace BTCPayServer.Controllers
|
||||
for (int i = 0; i < 4; i++)
|
||||
data[i] = standardPrefix[i];
|
||||
|
||||
derivationScheme = new BitcoinExtPubKey(Encoders.Base58Check.EncodeData(data), Network.Main).ToNetwork(_Network).ToString();
|
||||
derivationScheme = new BitcoinExtPubKey(Encoders.Base58Check.EncodeData(data), _Network).ToString();
|
||||
foreach (var label in labels)
|
||||
{
|
||||
derivationScheme = derivationScheme + $"-[{label}]";
|
||||
|
||||
@@ -17,61 +17,6 @@ namespace BTCPayServer.Services.Rates
|
||||
}
|
||||
public class CoinAverageRateProvider : IRateProvider
|
||||
{
|
||||
public class RatesJson
|
||||
{
|
||||
public class RateJson
|
||||
{
|
||||
public string Code
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
public decimal Rate
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
}
|
||||
|
||||
[JsonProperty("rates")]
|
||||
public JObject RatesInternal
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
[JsonIgnore]
|
||||
public List<RateJson> Rates
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public Dictionary<string, decimal> RatesByCurrency
|
||||
{
|
||||
get; set;
|
||||
}
|
||||
|
||||
public decimal GetRate(string currency)
|
||||
{
|
||||
if (!RatesByCurrency.TryGetValue(currency.ToUpperInvariant(), out decimal currUSD))
|
||||
throw new RateUnavailableException(currency);
|
||||
|
||||
if (!RatesByCurrency.TryGetValue("BTC", out decimal btcUSD))
|
||||
throw new RateUnavailableException(currency);
|
||||
|
||||
return currUSD / btcUSD;
|
||||
}
|
||||
public void CalculateDictionary()
|
||||
{
|
||||
RatesByCurrency = new Dictionary<string, decimal>();
|
||||
Rates = new List<RateJson>();
|
||||
foreach (var rate in RatesInternal.OfType<JProperty>())
|
||||
{
|
||||
var rateJson = new RateJson();
|
||||
rateJson.Code = rate.Name;
|
||||
rateJson.Rate = decimal.Parse(rate.Value["rate"].Value<string>(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint);
|
||||
RatesByCurrency.Add(rate.Name, rateJson.Rate);
|
||||
Rates.Add(rateJson);
|
||||
}
|
||||
}
|
||||
}
|
||||
static HttpClient _Client = new HttpClient();
|
||||
|
||||
public string Market
|
||||
@@ -80,13 +25,20 @@ namespace BTCPayServer.Services.Rates
|
||||
} = "global";
|
||||
public async Task<decimal> GetRateAsync(string currency)
|
||||
{
|
||||
RatesJson rates = await GetRatesCore();
|
||||
return rates.GetRate(currency);
|
||||
var rates = await GetRatesCore();
|
||||
return GetRate(rates, currency);
|
||||
}
|
||||
|
||||
private async Task<RatesJson> GetRatesCore()
|
||||
private decimal GetRate(Dictionary<string, decimal> rates, string currency)
|
||||
{
|
||||
var resp = await _Client.GetAsync("https://apiv2.bitcoinaverage.com/constants/exchangerates/" + Market);
|
||||
if (rates.TryGetValue(currency, out decimal result))
|
||||
return result;
|
||||
throw new RateUnavailableException(currency);
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, decimal>> GetRatesCore()
|
||||
{
|
||||
var resp = await _Client.GetAsync($"https://apiv2.bitcoinaverage.com/indices/{Market}/ticker/short");
|
||||
using (resp)
|
||||
{
|
||||
|
||||
@@ -97,19 +49,25 @@ namespace BTCPayServer.Services.Rates
|
||||
if ((int)resp.StatusCode == 403)
|
||||
throw new CoinAverageException("Unauthorized access to the API, premium plan needed");
|
||||
resp.EnsureSuccessStatusCode();
|
||||
var rates = JsonConvert.DeserializeObject<RatesJson>(await resp.Content.ReadAsStringAsync());
|
||||
rates.CalculateDictionary();
|
||||
return rates;
|
||||
var rates = JObject.Parse(await resp.Content.ReadAsStringAsync());
|
||||
return rates.Properties()
|
||||
.Where(p => p.Name.StartsWith("BTC", StringComparison.OrdinalIgnoreCase))
|
||||
.ToDictionary(p => p.Name.Substring(3, 3), p => ToDecimal(p.Value["last"]));
|
||||
}
|
||||
}
|
||||
|
||||
private decimal ToDecimal(JToken token)
|
||||
{
|
||||
return decimal.Parse(token.Value<string>(), System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint);
|
||||
}
|
||||
|
||||
public async Task<ICollection<Rate>> GetRatesAsync()
|
||||
{
|
||||
RatesJson rates = await GetRatesCore();
|
||||
return rates.Rates.Select(o => new Rate()
|
||||
var rates = await GetRatesCore();
|
||||
return rates.Select(o => new Rate()
|
||||
{
|
||||
Currency = o.Code,
|
||||
Value = rates.GetRate(o.Code)
|
||||
Currency = o.Key,
|
||||
Value = o.Value
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user