btcpayserver/BTCPayServer.Common/BTCPayNetwork.cs

165 lines
5.9 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NBitcoin;
using NBXplorer;
2019-12-24 08:20:44 +01:00
using NBXplorer.Models;
namespace BTCPayServer
{
public enum DerivationType
{
Legacy,
SegwitP2SH,
Segwit
}
public class BTCPayDefaultSettings
{
static BTCPayDefaultSettings()
{
2018-04-19 09:54:25 +02:00
_Settings = new Dictionary<NetworkType, BTCPayDefaultSettings>();
foreach (var chainType in new[] { NetworkType.Mainnet, NetworkType.Testnet, NetworkType.Regtest })
{
var settings = new BTCPayDefaultSettings();
_Settings.Add(chainType, settings);
2018-04-19 09:54:25 +02:00
settings.DefaultDataDirectory = StandardConfiguration.DefaultDataDirectory.GetDirectory("BTCPayServer", NBXplorerDefaultSettings.GetFolderName(chainType));
settings.DefaultExtensionDirectory =
StandardConfiguration.DefaultDataDirectory.GetDirectory("BTCPayServer", "Extensions");
settings.DefaultConfigurationFile = Path.Combine(settings.DefaultDataDirectory, "settings.config");
2018-04-19 09:54:25 +02:00
settings.DefaultPort = (chainType == NetworkType.Mainnet ? 23000 :
chainType == NetworkType.Regtest ? 23002 :
chainType == NetworkType.Testnet ? 23001 : throw new NotSupportedException(chainType.ToString()));
}
}
static readonly Dictionary<NetworkType, BTCPayDefaultSettings> _Settings;
2018-04-19 09:54:25 +02:00
public static BTCPayDefaultSettings GetDefaultSettings(NetworkType chainType)
{
return _Settings[chainType];
}
public string DefaultDataDirectory { get; set; }
public string DefaultExtensionDirectory { get; set; }
public string DefaultConfigurationFile { get; set; }
public int DefaultPort { get; set; }
}
2020-06-28 10:55:27 +02:00
public class BTCPayNetwork : BTCPayNetworkBase
{
2020-06-28 10:55:27 +02:00
public Network NBitcoinNetwork { get { return NBXplorerNetwork?.NBitcoinNetwork; } }
public NBXplorer.NBXplorerNetwork NBXplorerNetwork { get; set; }
public bool SupportRBF { get; internal set; }
public string LightningImagePath { get; set; }
public BTCPayDefaultSettings DefaultSettings { get; set; }
2018-02-12 19:27:36 +01:00
public KeyPath CoinType { get; internal set; }
2020-06-28 10:55:27 +02:00
public Dictionary<uint, DerivationType> ElectrumMapping = new Dictionary<uint, DerivationType>();
2019-12-24 08:20:44 +01:00
public virtual bool WalletSupported { get; set; } = true;
2020-06-28 10:55:27 +02:00
public virtual bool ReadonlyWallet { get; set; } = false;
public int MaxTrackedConfirmation { get; internal set; } = 6;
public string UriScheme { get; internal set; }
2020-03-29 17:28:22 +02:00
public bool SupportPayJoin { get; set; } = false;
public bool SupportLightning { get; set; } = true;
2020-03-29 17:28:22 +02:00
Put Ledger Wallet pairing in a popup, prepare code for Trezor pairing (#836) * Allowing for POS to be displayed at website root * Switching to asp attributes for form post action * Applying default formatting rules on HTML * The destination pays mining fees => Subtract fees from amount * small cleanup (#851) * Part2: Openiddict: Init OpenIddict & Database Migration & Auth Policies (#567) * Part 1: OpenIddict - Minor Changes & Config prep * Part 1: OpenIddict - Minor Changes & Config prep * Part2: Openiddict: Init OpenIddict & Database Migration & Auth Policies * pr changes * pr changes * fix merge * pr fixes * remove config for openid -- no need for it for now * fix compile * fix compile #2 * remove extra ns using * Update Startup.cs * compile * adjust settings a bit * remove duplicate * remove external login provider placeholder html * remove unused directives * regenerate db snapshot model * Remove dynamic policy * Provide Pretty descriptions for payment methods from their handlers (#852) * small cleanup * Provide Pretty descriptions for payment methods from their handlers * remove PrettyMethod() * integration with trezor * rough load xpub from trezor * update deriv scheme trezor * move ledger import to dialog * add import from hw wallet dropdown * Support temporary links for local file system provider (#848) * wip * Support temporary links for local file system provider * pass base url to file services * fix test * do not crash on errors with local filesystem * remove console * fix paranthesis * work on trezor.net integration * pushed non compiling sign wallet code * comment out wallet code * abstract ledger ws in add deriv * Auto stash before merge of "trezor" and "btcpayserver/master" * final add changes * cleanup * improve connectivity and fix e2e tests * fix selenium * add experimental warning for trezor * move import button to right and convert to text link * switch to defer and async scripts in add deriv scheme * make defer not async * more elaborate import trezor dialog * Fix small issues * hide trezor for now
2019-05-25 04:45:36 +02:00
public KeyPath GetRootKeyPath(DerivationType type)
{
KeyPath baseKey;
if (!NBitcoinNetwork.Consensus.SupportSegwit)
{
baseKey = new KeyPath("44'");
}
else
{
switch (type)
{
case DerivationType.Legacy:
baseKey = new KeyPath("44'");
break;
case DerivationType.SegwitP2SH:
baseKey = new KeyPath("49'");
break;
case DerivationType.Segwit:
baseKey = new KeyPath("84'");
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
return baseKey
.Derive(CoinType);
}
2018-04-12 04:48:33 +02:00
2019-05-24 11:42:22 +02:00
public KeyPath GetRootKeyPath()
2018-04-12 04:48:33 +02:00
{
return new KeyPath(NBitcoinNetwork.Consensus.SupportSegwit ? "49'" : "44'")
.Derive(CoinType);
}
public override T ToObject<T>(string json)
{
return NBXplorerNetwork.Serializer.ToObject<T>(json);
}
public override string ToString<T>(T obj)
{
return NBXplorerNetwork.Serializer.ToString(obj);
}
2019-12-24 08:20:44 +01:00
public virtual IEnumerable<(MatchedOutput matchedOutput, OutPoint outPoint)> GetValidOutputs(NewTransactionEvent evtOutputs)
{
return evtOutputs.Outputs.Select(output =>
{
var outpoint = new OutPoint(evtOutputs.TransactionData.TransactionHash, output.Index);
return (output, outpoint);
});
}
public virtual string GenerateBIP21(string cryptoInfoAddress, Money cryptoInfoDue)
{
return $"{UriScheme}:{cryptoInfoAddress}?amount={cryptoInfoDue.ToString(false, true)}";
}
public virtual GetTransactionsResponse FilterValidTransactions(GetTransactionsResponse response)
{
return response;
}
}
public abstract class BTCPayNetworkBase
{
public bool ShowSyncSummary { get; set; } = true;
public string CryptoCode { get; internal set; }
public string BlockExplorerLink { get; internal set; }
public string DisplayName { get; set; }
public int Divisibility { get; set; } = 8;
[Obsolete("Should not be needed")]
public bool IsBTC
{
get
{
return CryptoCode == "BTC";
}
}
public string CryptoImagePath { get; set; }
public string[] DefaultRateRules { get; internal set; } = Array.Empty<string>();
public override string ToString()
{
return CryptoCode;
}
public virtual T ToObject<T>(string json)
{
return NBitcoin.JsonConverters.Serializer.ToObject<T>(json, null);
}
public virtual string ToString<T>(T obj)
{
2019-09-30 10:32:43 +02:00
return NBitcoin.JsonConverters.Serializer.ToString(obj, null);
2018-04-12 04:48:33 +02:00
}
}
}