Ejemplo n.º 1
0
 internal HDKeyManager(ExtPubKey extPubKey, KeyPath accountKeyPath, uint minGapLimit = AbsoluteMinGapLimit)
 {
     AccountKeyPath = accountKeyPath ?? DefaultAccountKeyPath;
     MinGapLimit    = minGapLimit;
     ExtPubKey      = extPubKey;
     ExternalKeys   = new KeysRegistry(extPubKey.Derive(0), AccountKeyPath.Derive(0), MinGapLimit);
     InternalKeys   = new KeysRegistry(extPubKey.Derive(1), AccountKeyPath.Derive(1), MinGapLimit);
 }
Ejemplo n.º 2
0
        private HDPubKey DerivePubKeyFromIndex(int index, string knownBy)
        {
            var path     = new KeyPath((uint)index);
            var fullPath = AccountKeyPath.Derive((uint)index);
            var pubKey   = ExtPubKey.Derive(path).PubKey;
            var hdPubKey = new HDPubKey(pubKey, fullPath, knownBy);

            return(hdPubKey);
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
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);
            }
        }