Exemplo n.º 1
0
        public IHDKey Derive(uint index)
        {
            var childPath = _PathFromRoot.Derive(index);
            var key       = derivationCache.GetOrAdd(childPath, _ => hdKey.Derive(index));

            return(new HDKeyCache(key, childPath, derivationCache));
        }
Exemplo n.º 2
0
 public RootedKeyPath Derive(KeyPath keyPath)
 {
     if (keyPath == null)
     {
         throw new ArgumentNullException(nameof(keyPath));
     }
     return(new RootedKeyPath(MasterFingerprint, KeyPath.Derive(keyPath)));
 }
 /// <summary>
 /// Rebase the keypaths.
 /// If a PSBT updater only know the child HD public key but not the root one, another updater knowing the parent master key it is based on
 /// can rebase the paths.
 /// </summary>
 /// <param name="oldFingerprint">The old fingerprint</param>
 /// <param name="newFingerprint">The new fingerprint of the master key</param>
 /// <param name="newRoot">The root of the KeyPath who had the old fingerprint</param>
 /// <returns></returns>
 public PSBT RebaseKeyPaths(HDFingerprint oldFingerprint, HDFingerprint newFingerprint, KeyPath newRoot)
 {
     if (newRoot == null)
     {
         throw new ArgumentNullException(nameof(newRoot));
     }
     foreach (var o in Inputs.OfType <PSBTCoin>().Concat(Outputs))
     {
         foreach (var keypath in o.HDKeyPaths.ToList())
         {
             if (keypath.Value.Item1 == oldFingerprint)
             {
                 var newKeyPath = newRoot.Derive(keypath.Value.Item2);
                 o.HDKeyPaths.Remove(keypath.Key);
                 o.HDKeyPaths.Add(keypath.Key, Tuple.Create(newFingerprint, newKeyPath));
             }
         }
     }
     return(this);
 }
Exemplo n.º 4
0
        public IHDKey Derive(KeyPath keyPath)
        {
            var key       = masterKey;
            var childPath = new KeyPath();

            foreach (var index in keyPath.Indexes)
            {
                childPath = childPath.Derive(index);
                if (derivationCache.TryGetValue(childPath, out var cachedKey))
                {
                    key = cachedKey;
                    continue;
                }
                key = key.Derive(index);
                if (derivationCache.Count < 256)
                {
                    derivationCache.Add(childPath, key);
                }
            }
            return(key);
        }
Exemplo n.º 5
0
 public RootedKeyPath Derive(uint index)
 {
     return(new RootedKeyPath(MasterFingerprint, KeyPath.Derive(index)));
 }
        /// <summary>
        /// Rebase the keypaths.
        /// If a PSBT updater only know the child HD public key but not the root one, another updater knowing the parent master key it is based on
        /// can rebase the paths.
        /// </summary>
        /// <param name="accountKey">The current account key</param>
        /// <param name="accountKeyPath">The path from the master key to the accountKey</param>
        /// <param name="masterFingerprint">The master key fingerprint</param>
        /// <returns></returns>
        public PSBT RebaseKeyPaths(IHDKey accountKey, KeyPath accountKeyPath, HDFingerprint masterFingerprint)
        {
            if (accountKey == null)
            {
                throw new ArgumentNullException(nameof(accountKey));
            }
            if (accountKeyPath == null)
            {
                throw new ArgumentNullException(nameof(accountKeyPath));
            }
            accountKey = accountKey.AsHDKeyCache();
            var accountKeyFP = accountKey.GetPublicKey().GetHDFingerPrint();

            foreach (var o in HDKeysFor(null, accountKey).GroupBy(c => c.Coin))
            {
                foreach (var keyPath in o)
                {
                    o.Key.HDKeyPaths.Remove(keyPath.PubKey);
                    o.Key.HDKeyPaths.Add(keyPath.PubKey, Tuple.Create(masterFingerprint, accountKeyPath.Derive(keyPath.KeyPath)));
                }
            }
            return(this);
        }