示例#1
0
        public Identifier Derive_key_id(uint derivation)
        {
            var extkeyNew = Extkey.Derive(Secp, derivation);
            var keyId     = extkeyNew.Identifier(Secp);

            return(keyId);
        }
示例#2
0
        // if we know the derivation index we can just straight to deriving the key
        public SecretKey derived_key_from_index(uint derivation)
        {
            Log.Verbose("Derived Key (fast) by derivation: {derivation}", derivation);
            var extkey = Extkey.Derive(Secp, derivation);

            return(extkey.Key);
        }
示例#3
0
        public SecretKey Derived_key(Identifier keyId)
        {
            Log.Verbose("Derived Key by key_id: {key_id}", keyId);

            // first check our overrides and just return the key if we have one in there
            KeyOverrides.TryGetValue(keyId.HexValue, out var sk);

            if (sk != null)
            {
                Log.Verbose("... Derived Key (using override) key_id: {key_id}", keyId);
                return(sk);
            }

            // then check the derivation cache to see if we have previously derived this key
            // if so use the derivation from the cache to derive the key

            var cache = KeyDerivationCache;

            if (cache.TryGetValue(keyId.HexValue, out var derivation))
            {
                Log.Verbose("... Derived Key (cache hit) key_id: {key_id}, derivation: {derivation}", keyId, derivation);
                return(derived_key_from_index(derivation));
            }


            // otherwise iterate over a large number of derivations looking for our key
            // cache the resulting derivations by key_id for faster lookup later
            // TODO - remove the 10k hard limit and be smarter about batching somehow


            for (uint i = 1; i <= 10000; i++)

            {
                var extkeyNew = Extkey.Derive(Secp, i);
                var ident     = extkeyNew.Identifier(Secp);


                if (!cache.ContainsKey(ident.HexValue))
                {
                    Log.Verbose("... Derived Key (cache miss) key_id: {key_id}, derivation: {derivation}", ident,
                                extkeyNew.NChild);
                    cache.TryAdd(ident.HexValue, extkeyNew.NChild);
                }


                if (ident.HexValue == keyId.HexValue)
                {
                    return(extkeyNew.Key);
                }
            }


            throw new Exception($"KeyDerivation - cannot find extkey for {keyId.HexValue}");
        }