private DerivationStrategyBase ParseCore(string str) { bool legacy = false; ReadBool(ref str, "legacy", ref legacy); bool p2sh = false; ReadBool(ref str, "p2sh", ref p2sh); bool keepOrder = false; ReadBool(ref str, "keeporder", ref keepOrder); if (!legacy && !_Network.Consensus.SupportSegwit) { throw new FormatException("Segwit is not supported"); } var options = new DerivationStrategyOptions() { KeepOrder = keepOrder, ScriptPubKeyType = legacy ? ScriptPubKeyType.Legacy : p2sh ? ScriptPubKeyType.SegwitP2SH : ScriptPubKeyType.Segwit }; var match = MultiSigRegex.Match(str); if (match.Success) { var sigCount = int.Parse(match.Groups[1].Value); var pubKeys = match.Groups .OfType <Group>() .Skip(2) .SelectMany(g => g.Captures.OfType <Capture>()) .Select(g => new BitcoinExtPubKey(g.Value.Substring(1), Network)) .ToArray(); return(CreateMultiSigDerivationStrategy(pubKeys, sigCount, options)); } else { var key = _Network.Parse <BitcoinExtPubKey>(str); return(CreateDirectDerivationStrategy(key, options)); } }
private DerivationStrategyBase ParseCore(string str) { bool legacy = false; ReadBool(ref str, "legacy", ref legacy); bool p2sh = false; ReadBool(ref str, "p2sh", ref p2sh); bool keepOrder = false; ReadBool(ref str, "keeporder", ref keepOrder); var options = new DerivationStrategyOptions() { KeepOrder = keepOrder, Legacy = legacy, P2SH = p2sh }; var match = MultiSigRegex.Match(str); if (match.Success) { var sigCount = int.Parse(match.Groups[1].Value); var pubKeys = match.Groups .OfType <Group>() .Skip(2) .SelectMany(g => g.Captures.OfType <Capture>()) .Select(g => new BitcoinExtPubKey(g.Value.Substring(1), Network)) .ToArray(); return(CreateMultiSigDerivationStrategy(pubKeys, sigCount, options)); } else { var key = _Network.Parse <BitcoinExtPubKey>(str); return(CreateDirectDerivationStrategy(key, options)); } }
private DerivationStrategyBase ParseCore(string str) { bool legacy = false; bool p2sh = false; bool keepOrder = false; Dictionary <string, bool> optionsDictionary = new Dictionary <string, bool>(5); foreach (Match optionMatch in _OptionRegex.Matches(str)) { var key = optionMatch.Groups[1].Value.ToLowerInvariant(); if (!AuthorizedOptions.Contains(key)) { throw new FormatException($"The option '{key}' is not supported by this network"); } if (!optionsDictionary.TryAdd(key, true)) { throw new FormatException($"The option '{key}' is duplicated"); } } str = _OptionRegex.Replace(str, string.Empty); if (optionsDictionary.Remove("legacy")) { legacy = true; } if (optionsDictionary.Remove("p2sh")) { p2sh = true; } if (optionsDictionary.Remove("keeporder")) { keepOrder = true; } if (!legacy && !_Network.Consensus.SupportSegwit) { throw new FormatException("Segwit is not supported you need to specify option '-[legacy]'"); } if (legacy && p2sh) { throw new FormatException("The option 'legacy' is incompatible with 'p2sh'"); } var options = new DerivationStrategyOptions() { KeepOrder = keepOrder, ScriptPubKeyType = legacy ? ScriptPubKeyType.Legacy : p2sh ? ScriptPubKeyType.SegwitP2SH : ScriptPubKeyType.Segwit, AdditionalOptions = new ReadOnlyDictionary <string, bool>(optionsDictionary) }; var match = MultiSigRegex.Match(str); if (match.Success) { var sigCount = int.Parse(match.Groups[1].Value); var pubKeys = match.Groups .OfType <Group>() .Skip(2) .SelectMany(g => g.Captures.OfType <Capture>()) .Select(g => new BitcoinExtPubKey(g.Value.Substring(1), Network)) .ToArray(); return(CreateMultiSigDerivationStrategy(pubKeys, sigCount, options)); } else { var key = _Network.Parse <BitcoinExtPubKey>(str); return(CreateDirectDerivationStrategy(key, options)); } }
/// <summary> /// Create a multisig derivation strategy from public keys /// </summary> /// <param name="pubKeys">The public keys belonging to the multi sig</param> /// <param name="sigCount">The number of required signature</param> /// <param name="options">Derivation options</param> /// <returns>A multisig derivation strategy</returns> public DerivationStrategyBase CreateMultiSigDerivationStrategy(BitcoinExtPubKey[] pubKeys, int sigCount, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase derivationStrategy = new MultisigDerivationStrategy(sigCount, pubKeys.ToArray(), options.ScriptPubKeyType == ScriptPubKeyType.Legacy, !options.KeepOrder, options.AdditionalOptions); if (options.ScriptPubKeyType == ScriptPubKeyType.Legacy) { return(new P2SHDerivationStrategy(derivationStrategy, false)); } if (!_Network.Consensus.SupportSegwit) { throw new InvalidOperationException("This crypto currency does not support segwit"); } derivationStrategy = new P2WSHDerivationStrategy(derivationStrategy); if (options.ScriptPubKeyType == ScriptPubKeyType.SegwitP2SH) { derivationStrategy = new P2SHDerivationStrategy(derivationStrategy, true); } return(derivationStrategy); }
/// <summary> /// Create a multisig derivation strategy from public keys /// </summary> /// <param name="pubKeys">The public keys belonging to the multi sig</param> /// <param name="sigCount">The number of required signature</param> /// <param name="options">Derivation options</param> /// <returns>A multisig derivation strategy</returns> public DerivationStrategyBase CreateMultiSigDerivationStrategy(ExtPubKey[] pubKeys, int sigCount, DerivationStrategyOptions options = null) { return(CreateMultiSigDerivationStrategy(pubKeys.Select(p => p.GetWif(Network)).ToArray(), sigCount, options)); }
/// <summary> /// Create a single signature derivation strategy from public key /// </summary> /// <param name="publicKey">The public key of the wallet</param> /// <param name="options">Derivation options</param> /// <returns></returns> public DerivationStrategyBase CreateDirectDerivationStrategy(BitcoinExtPubKey publicKey, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase strategy = new DirectDerivationStrategy(publicKey, options.ScriptPubKeyType != ScriptPubKeyType.Legacy, options.AdditionalOptions); if (options.ScriptPubKeyType != ScriptPubKeyType.Legacy && !_Network.Consensus.SupportSegwit) { throw new InvalidOperationException("This crypto currency does not support segwit"); } if (options.ScriptPubKeyType == ScriptPubKeyType.SegwitP2SH) { strategy = new P2SHDerivationStrategy(strategy, true); } return(strategy); }
/// <summary> /// Create a single signature derivation strategy from public key /// </summary> /// <param name="publicKey">The public key of the wallet</param> /// <param name="options">Derivation options</param> /// <returns></returns> public DerivationStrategyBase CreateDirectDerivationStrategy(ExtPubKey publicKey, DerivationStrategyOptions options = null) { return(CreateDirectDerivationStrategy(publicKey.GetWif(Network), options)); }
/// <summary> /// Create a multisig derivation strategy from public keys /// </summary> /// <param name="pubKeys">The public keys belonging to the multi sig</param> /// <param name="sigCount">The number of required signature</param> /// <param name="options">Derivation options</param> /// <returns>A multisig derivation strategy</returns> public DerivationStrategyBase CreateMultiSigDerivationStrategy(BitcoinExtPubKey[] pubKeys, int sigCount, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase derivationStrategy = new MultisigDerivationStrategy(sigCount, pubKeys.ToArray(), options.Legacy) { LexicographicOrder = !options.KeepOrder }; if (options.Legacy) { return(new P2SHDerivationStrategy(derivationStrategy, false)); } derivationStrategy = new P2WSHDerivationStrategy(derivationStrategy); if (options.P2SH) { derivationStrategy = new P2SHDerivationStrategy(derivationStrategy, true); } return(derivationStrategy); }
/// <summary> /// Create a single signature derivation strategy from public key /// </summary> /// <param name="publicKey">The public key of the wallet</param> /// <param name="options">Derivation options</param> /// <returns></returns> public DerivationStrategyBase CreateDirectDerivationStrategy(BitcoinExtPubKey publicKey, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase strategy = new DirectDerivationStrategy(publicKey) { Segwit = !options.Legacy }; if (options.P2SH && !options.Legacy) { strategy = new P2SHDerivationStrategy(strategy, true); } return(strategy); }
/// <summary> /// Create a single signature derivation strategy from public key /// </summary> /// <param name="publicKey">The public key of the wallet</param> /// <param name="options">Derivation options</param> /// <returns></returns> public DerivationStrategyBase CreateDirectDerivationStrategy(BitcoinExtPubKey publicKey, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase strategy = null; #pragma warning disable CS0618 // Type or member is obsolete if (options.ScriptPubKeyType != ScriptPubKeyType.TaprootBIP86) #pragma warning restore CS0618 // Type or member is obsolete { strategy = new DirectDerivationStrategy(publicKey, options.ScriptPubKeyType != ScriptPubKeyType.Legacy, options.AdditionalOptions); if (options.ScriptPubKeyType == ScriptPubKeyType.Segwit && !_Network.Consensus.SupportSegwit) { throw new InvalidOperationException("This crypto currency does not support segwit"); } if (options.ScriptPubKeyType == ScriptPubKeyType.SegwitP2SH) { strategy = new P2SHDerivationStrategy(strategy, true); } } else { if (!_Network.Consensus.SupportTaproot) { throw new InvalidOperationException("This crypto currency does not support taproot"); } strategy = new TaprootDerivationStrategy(publicKey, options.AdditionalOptions); } return(strategy); }
/// <summary> /// Create a single signature derivation strategy from public key /// </summary> /// <param name="publicKey">The public key of the wallet</param> /// <param name="options">Derivation options</param> /// <returns></returns> public DerivationStrategyBase CreateDirectDerivationStrategy(BitcoinExtPubKey publicKey, DerivationStrategyOptions options = null) { options = options ?? new DerivationStrategyOptions(); DerivationStrategyBase strategy = new DirectDerivationStrategy(publicKey) { Segwit = !options.Legacy }; if (!options.Legacy && !_Network.Consensus.SupportSegwit) { throw new InvalidOperationException("This crypto currency does not support segwit"); } if (options.P2SH && !options.Legacy) { strategy = new P2SHDerivationStrategy(strategy, true); } return(strategy); }