Exemplo n.º 1
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());
         }
     }
 }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
0
        public IEnumerable <ExtKey> GetSecrets(string password, params Script[] scripts)
        {
            Key secret  = EncryptedSecret.GetKey(password);
            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);
            }
        }
Exemplo n.º 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 == null && isInternal == null)
         {
             return(HdPubKeys);
         }
         if (keyState != null && isInternal == null)
         {
             return(HdPubKeys.Where(x => x.KeyState == keyState));
         }
         else if (keyState == null)
         {
             return(HdPubKeys.Where(x => x.IsInternal() == isInternal));
         }
         return(HdPubKeys.Where(x => x.KeyState == keyState && x.IsInternal() == isInternal));
     }
 }
Exemplo n.º 5
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;
                if (isInternal)
                {
                    relevantHdPubKeys = HdPubKeys.Where(x => x.IsInternal);
                }
                else
                {
                    relevantHdPubKeys = 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);
            }
        }