예제 #1
0
        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 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();
                DerivationStrategyBase derivationStrategy = new MultisigDerivationStrategy(sigCount, pubKeys)
                {
                    LexicographicOrder = !keepOrder
                };
                if (legacy)
                {
                    return(new P2SHDerivationStrategy(derivationStrategy));
                }

                derivationStrategy = new P2WSHDerivationStrategy(derivationStrategy);
                if (p2sh)
                {
                    derivationStrategy = new P2SHDerivationStrategy(derivationStrategy);
                }
                return(derivationStrategy);
            }
            else
            {
                var key = _Network.Parse <BitcoinExtPubKey>(str);
                DerivationStrategyBase strategy = new DirectDerivationStrategy(key)
                {
                    Segwit = !legacy
                };
                if (p2sh && !legacy)
                {
                    strategy = new P2SHDerivationStrategy(strategy);
                }
                return(strategy);
            }
        }
예제 #2
0
        /// <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);
        }
예제 #3
0
        /// <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);
        }