示例#1
0
        public IEnumerable <ExtKey> GetSecrets(string password, params Script[] scripts)
        {
            Key secret;

            try
            {
                secret = EncryptedSecret.GetKey(password);
            }
            catch (SecurityException ex)
            {
                throw new SecurityException("Invalid password.", ex);
            }
            var extKey  = new ExtKey(secret, ChainCode);
            var extKeys = new List <ExtKey>();

            lock (HdPubKeysLock)
            {
                foreach (HdPubKey key in HdPubKeys.Where(x =>
                                                         scripts.Contains(x.GetP2wpkhScript()) ||
                                                         scripts.Contains(x.GetP2shOverP2wpkhScript()) ||
                                                         scripts.Contains(x.GetP2pkhScript()) ||
                                                         scripts.Contains(x.GetP2pkScript())))
                {
                    ExtKey ek = extKey.Derive(key.FullKeyPath);
                    extKeys.Add(ek);
                }
                return(extKeys);
            }
        }
        public HdPubKey GenerateNewKey(SmartLabel label, KeyState keyState, bool isInternal, bool toFile = true)
        {
            // BIP44-ish derivation scheme
            // m / purpose' / coin_type' / account' / change / address_index
            var change = isInternal ? 1 : 0;

            lock (HdPubKeysLock)
            {
                HdPubKey[] relevantHdPubKeys = HdPubKeys.Where(x => x.IsInternal == isInternal).ToArray();

                KeyPath path = new KeyPath($"{change}/0");
                if (relevantHdPubKeys.Any())
                {
                    int largestIndex         = relevantHdPubKeys.Max(x => x.Index);
                    var smallestMissingIndex = largestIndex;
                    var present = new bool[largestIndex + 1];
                    for (int i = 0; i < relevantHdPubKeys.Length; ++i)
                    {
                        present[relevantHdPubKeys[i].Index] = true;
                    }
                    for (int i = 1; i < present.Length; ++i)
                    {
                        if (!present[i])
                        {
                            smallestMissingIndex = i - 1;
                            break;
                        }
                    }

                    path = relevantHdPubKeys[smallestMissingIndex].NonHardenedKeyPath.Increment();
                }

                var fullPath = AccountKeyPath.Derive(path);
                var pubKey   = ExtPubKey.Derive(path).PubKey;

                var hdPubKey = new HdPubKey(pubKey, fullPath, label, keyState);
                HdPubKeys.Add(hdPubKey);
                lock (HdPubKeyScriptBytesLock)
                {
                    HdPubKeyScriptBytes.Add(hdPubKey.P2wpkhScript.ToCompressedBytes());
                }

                lock (ScriptHdPubKeyMapLock)
                {
                    ScriptHdPubKeyMap.Add(hdPubKey.P2wpkhScript, hdPubKey);
                }

                if (toFile)
                {
                    ToFile();
                }

                return(hdPubKey);
            }
        }
示例#3
0
        public HdPubKey GenerateNewKey(string label, KeyState keyState, bool isInternal, bool toFile = true)
        {
            // BIP44-ish derivation scheme
            // m / purpose' / coin_type' / account' / change / address_index
            var change = isInternal ? 1 : 0;

            lock (HdPubKeysLock)
            {
                IEnumerable <HdPubKey> relevantHdPubKeys = isInternal ? HdPubKeys.Where(x => x.IsInternal) : HdPubKeys.Where(x => !x.IsInternal);

                KeyPath path;
                if (relevantHdPubKeys.Any())
                {
                    int        largestIndex   = relevantHdPubKeys.Max(x => x.Index);
                    List <int> missingIndexes = Enumerable.Range(0, largestIndex).Except(relevantHdPubKeys.Select(x => x.Index)).ToList();
                    if (missingIndexes.Any())
                    {
                        int smallestMissingIndex = missingIndexes.Min();
                        path = relevantHdPubKeys.First(x => x.Index == (smallestMissingIndex - 1)).NonHardenedKeyPath.Increment();
                    }
                    else
                    {
                        path = relevantHdPubKeys.First(x => x.Index == largestIndex).NonHardenedKeyPath.Increment();
                    }
                }
                else
                {
                    path = new KeyPath($"{change}/0");
                }

                var fullPath = AccountKeyPath.Derive(path);
                var pubKey   = ExtPubKey.Derive(path).PubKey;

                var hdPubKey = new HdPubKey(pubKey, fullPath, label, keyState);
                HdPubKeys.Add(hdPubKey);
                lock (HdPubKeyScriptBytesLock)
                {
                    HdPubKeyScriptBytes.Add(hdPubKey.P2wpkhScript.ToCompressedBytes());
                }

                lock (ScriptHdPubKeyMapLock)
                {
                    ScriptHdPubKeyMap.Add(hdPubKey.P2wpkhScript, hdPubKey);
                }

                if (toFile)
                {
                    ToFile();
                }

                return(hdPubKey);
            }
        }
示例#4
0
 public IEnumerable <HdPubKey> GetKeys(KeyState?keyState = null, bool?isInternal = null)
 {
     // BIP44-ish derivation scheme
     // m / purpose' / coin_type' / account' / change / address_index
     lock (HdPubKeysLock)
     {
         if (keyState is null && isInternal is null)
         {
             return(HdPubKeys);
         }
         if (isInternal is null && !(keyState is null))
         {
             return(HdPubKeys.Where(x => x.KeyState == keyState));
         }
示例#5
0
 public IEnumerable <HdPubKey> GetKeys(Func <HdPubKey, bool> wherePredicate)
 {
     // BIP44-ish derivation scheme
     // m / purpose' / coin_type' / account' / change / address_index
     lock (HdPubKeysLock)
     {
         if (wherePredicate is null)
         {
             return(HdPubKeys.ToList());
         }
         else
         {
             return(HdPubKeys.Where(wherePredicate).ToList());
         }
     }
 }
示例#6
0
        public HdPubKey GenerateNewKey(string label, KeyState keyState, bool isInternal, bool toFile = true)
        {
            // BIP44-ish derivation scheme
            // m / purpose' / coin_type' / account' / change / address_index
            lock (HdPubKeysLock)
            {
                var change = isInternal ? 1 : 0;

                IEnumerable <HdPubKey> relevantHdPubKeys;
                if (isInternal)
                {
                    relevantHdPubKeys = HdPubKeys.Where(x => x.IsInternal());
                }
                else
                {
                    relevantHdPubKeys = HdPubKeys.Where(x => !x.IsInternal());
                }

                KeyPath path;
                if (!relevantHdPubKeys.Any())
                {
                    path = new KeyPath($"{change}/0");
                }
                else
                {
                    path = relevantHdPubKeys.OrderBy(x => x.GetIndex()).Last().GetNonHardenedKeyPath().Increment();
                }

                var fullPath = AccountKeyPath.Derive(path);
                var pubKey   = ExtPubKey.Derive(path).PubKey;

                var hdPubKey = new HdPubKey(pubKey, fullPath, label, keyState);
                HdPubKeys.Add(hdPubKey);

                if (toFile)
                {
                    ToFile();
                }

                return(hdPubKey);
            }
        }