Example #1
0
 public BitcoinExtPubKey GetBitcoinExtPubKey(int?index = null, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null) => GetBitcoinExtKey(index, hdPathType, account).Neuter();
Example #2
0
 public PubKey GetPubKey(int index, HdPathType hdPathType, SafeAccount account)
 {
     return(GetBitcoinExtKey(index, hdPathType, account).PrivateKey.PubKey);
 }
Example #3
0
        public IList <BitcoinAddress> GetFirstNAddresses(AddressType type, int addressCount, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
        {
            var addresses = new List <BitcoinAddress>();

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

            return(addresses);
        }
Example #4
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);
        }
Example #5
0
        public BitcoinExtKey GetBitcoinExtKey(int?index = null, HdPathType hdPathType = HdPathType.Receive, SafeAccount account = null)
        {
            string firstPart = "";

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

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

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

            KeyPath keyPath = new KeyPath(firstPart + lastPart);

            return(ExtKey.Derive(keyPath).GetWif(Network));
        }
Example #6
0
        public BitcoinExtKey FindPrivateKey(BitcoinAddress address, int stopSearchAfterIteration = 100000, SafeAccount account = null)
        {
            foreach (AddressType type in Enum.GetValues(typeof(AddressType)))
            {
                for (int i = 0; i < stopSearchAfterIteration; i++)
                {
                    if (GetAddress(type, i, HdPathType.Receive, account) == address)
                    {
                        return(GetBitcoinExtKey(i, HdPathType.Receive, account));
                    }
                    if (GetAddress(type, i, HdPathType.Change, account) == address)
                    {
                        return(GetBitcoinExtKey(i, HdPathType.Change, account));
                    }
                    if (GetAddress(type, i, HdPathType.NonHardened, account) == address)
                    {
                        return(GetBitcoinExtKey(i, HdPathType.NonHardened, account));
                    }
                }
            }

            throw new KeyNotFoundException(address.ToString());
        }
Example #7
0
 public static string GetPathString(SafeAccount account) => account.PathString;