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);
            }
        }
示例#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
            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);
            }
        }
示例#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
            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);
            }
        }