Beispiel #1
0
        // GetP2wpkh pubKey.WitHash.ScriptPubKey.GetDestinationAddress(Network);
        // GetP2pkhAddress pubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
        // GetP2shOverP2wpkhAddress pubKey.WitHash.ScriptPubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
        public BitcoinAddress GetAddress(AddressType type, int index, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
        {
            BitcoinAddress cachedAddress = SafeCache.FirstOrDefault(x => x.Key.Item1 == type && x.Key.Item2 == index && x.Key.Item3 == hdPathType && x.Key.Item4 == account).Value;

            if (cachedAddress != null)
            {
                return(cachedAddress);
            }

            PubKey         pubKey  = GetPubKey(index, hdPathType, account);
            BitcoinAddress address = null;

            if (type == AddressType.Pay2WitnessPublicKeyHash)
            {
                address = pubKey.WitHash.ScriptPubKey.GetDestinationAddress(Network);
            }
            else if (type == AddressType.Pay2PublicKeyHash)
            {
                address = pubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
            }
            else if (type == AddressType.Pay2ScriptHashOverPay2WitnessPublicKeyHash)
            {
                address = pubKey.WitHash.ScriptPubKey.Hash.ScriptPubKey.GetDestinationAddress(Network);
            }
            else
            {
                throw new NotSupportedException(type.ToString());
            }

            SafeCache.TryAdd(new Tuple <AddressType, int, HdPathType, SafeAccount>(type, index, hdPathType, account), address);
            return(address);
        }
Beispiel #2
0
        public BitcoinExtKey GetPrivateKey(int index, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
        {
            string firstPart = "";

            if (account != null)
            {
                firstPart += Hierarchy.GetPathString(account) + "/";
            }

            firstPart += Hierarchy.GetPathString(hdPathType);
            string lastPart;

            if (hdPathType == HdPathType.NonHardened)
            {
                lastPart = $"/{index}";
            }
            else
            {
                lastPart = $"/{index}'";
            }

            KeyPath keyPath = new KeyPath(firstPart + lastPart);

            return(ExtKey.Derive(keyPath).GetWif(Network));
        }
Beispiel #3
0
        public IList <BitcoinAddress> GetFirstNAddresses(int addressCount, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
        {
            var addresses = new List <BitcoinAddress>();

            for (var i = 0; i < addressCount; i++)
            {
                addresses.Add(GetAddress(i, hdPathType, account));
            }

            return(addresses);
        }
Beispiel #4
0
        public HashSet <BitcoinAddress> GetFirstNAddresses(int addressCount, HdPathType hdPathType = HdPathType.Receive)
        {
            var addresses = new HashSet <BitcoinAddress>();

            for (var i = 0; i < addressCount; i++)
            {
                addresses.Add(GetAddress(i, hdPathType));
            }

            return(addresses);
        }
Beispiel #5
0
        private void UpdateSafeTrackingByHdPathType(HdPathType hdPathType)
        {
            if (TracksDefaultSafe)
            {
                UpdateSafeTrackingByPath(hdPathType);
            }

            foreach (var acc in SafeAccounts)
            {
                UpdateSafeTrackingByPath(hdPathType, acc);
            }
        }
Beispiel #6
0
        public static string GetPathString(HdPathType type)
        {
            switch (type)
            {
            case HdPathType.Stealth:
                return("0'");

            case HdPathType.Receive:
                return("1'");

            case HdPathType.Change:
                return("2'");

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Beispiel #7
0
        public BitcoinExtKey GetBitcoinExtKey(int?index = null, HdPathType hdPathType = HdPathType.Receive)
        {
            var firstPart = "";

            firstPart += Hierarchy.GetPathString(hdPathType);
            string lastPart;

            if (index == null)
            {
                lastPart = "";
            }
            else
            {
                lastPart = $"/{index}'";
            }

            var keyPath = new KeyPath(firstPart + lastPart);

            return(ExtKey.Derive(keyPath).GetWif(Network));
        }
Beispiel #8
0
        internal BitcoinExtKey GetPrivateKey(int index, HdPathType hdPathType = HdPathType.Receive)
        {
            string startPath;

            if (hdPathType == HdPathType.Receive)
            {
                startPath = ReceiveHdPath;
            }
            else if (hdPathType == HdPathType.Change)
            {
                startPath = ChangeHdPath;
            }
            else
            {
                throw new Exception("HdPathType not exists");
            }

            var keyPath = new KeyPath(startPath + "/" + index + "'");

            return(_seedPrivateKey.Derive(keyPath).GetWif(_network));
        }
Beispiel #9
0
        internal BitcoinExtKey GetPrivateKey(int index, HdPathType hdPathType = HdPathType.Receive)
        {
            KeyPath keyPath;

            if (hdPathType == HdPathType.Receive)
            {
                keyPath = new KeyPath($"{ReceiveHdPath}/{index}'");
            }
            else if (hdPathType == HdPathType.Change)
            {
                keyPath = new KeyPath($"{ChangeHdPath}/{index}'");
            }
            else if (hdPathType == HdPathType.NonHardened)
            {
                keyPath = new KeyPath($"{NonHardenedHdPath}/{index}");
            }
            else
            {
                throw new Exception("HdPathType not exists");
            }

            return(ExtKey.Derive(keyPath).GetWif(Network));
        }
Beispiel #10
0
        public static string GetPathString(HdPathType type)
        {
            switch (type)
            {
            case HdPathType.Stealth:
                return("0'");

            case HdPathType.Receive:
                return("1'");

            case HdPathType.Change:
                return("2'");

            case HdPathType.NonHardened:
                return("3");

            case HdPathType.Account:
                return("4'");                        // special

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Beispiel #11
0
        private void UpdateSafeTrackingByPath(HdPathType hdPathType, SafeAccount account = null)
        {
            int i          = 0;
            var cleanCount = 0;

            while (true)
            {
                Script scriptPubkey = account == null?Safe.GetAddress(i, hdPathType).ScriptPubKey : Safe.GetAddress(i, hdPathType, account).ScriptPubKey;

                Tracker.TrackedScriptPubKeys.Add(scriptPubkey);

                // if clean elevate cleancount and if max reached don't look for more
                if (Tracker.IsClean(scriptPubkey))
                {
                    cleanCount++;
                    if (cleanCount > MaxCleanAddressCount)
                    {
                        return;
                    }
                }

                i++;
            }
        }
Beispiel #12
0
 public PubKey GetPubKey(int index, HdPathType hdPathType, SafeAccount account)
 {
     return(GetBitcoinExtKey(index, hdPathType, account).PrivateKey.PubKey);
 }
Beispiel #13
0
 public BitcoinExtPubKey GetBitcoinExtPubKey(int?index = null, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null) => GetBitcoinExtKey(index, hdPathType, account).Neuter();
Beispiel #14
0
 public BitcoinExtPubKey GetBitcoinExtPubKey(int?index = null, HdPathType hdPathType = HdPathType.Receive)
 {
     return(GetBitcoinExtKey(index, hdPathType).Neuter());
 }
Beispiel #15
0
 public BitcoinAddress GetAddress(int index, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
 {
     return(GetPrivateKey(index, hdPathType, account).ScriptPubKey.GetDestinationAddress(Network));
 }
Beispiel #16
0
 public BitcoinAddress GetAddress(int index, HdPathType hdPathType = HdPathType.Receive)
 {
     return(GetBitcoinExtKey(index, hdPathType).ScriptPubKey.GetDestinationAddress(Network));
 }
Beispiel #17
0
        public IEnumerable <Script> GetUnusedScriptPubKeys(SafeAccount account = null, HdPathType hdPathType = HdPathType.Receive)
        {
            AssertAccount(account);

            HashSet <Script> scriptPubKeys = new HashSet <Script>();
            int i = 0;

            while (true)
            {
                Script scriptPubkey = account == null?Safe.GetAddress(i, hdPathType).ScriptPubKey : Safe.GetAddress(i, hdPathType, account).ScriptPubKey;

                if (Tracker.IsClean(scriptPubkey))
                {
                    scriptPubKeys.Add(scriptPubkey);

                    if (scriptPubKeys.Count >= MaxCleanAddressCount)
                    {
                        return(scriptPubKeys);
                    }
                }
                i++;
            }
        }