예제 #1
0
        /// <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="newRoot">The KeyPath with the fingerprint of the new root key</param>
        /// <returns>This PSBT</returns>
        public PSBT RebaseKeyPaths(IHDKey accountKey, RootedKeyPath newRoot)
        {
            if (accountKey == null)
            {
                throw new ArgumentNullException(nameof(accountKey));
            }
            if (newRoot == null)
            {
                throw new ArgumentNullException(nameof(newRoot));
            }
            accountKey = accountKey.AsHDKeyCache();
            var accountKeyFP = accountKey.GetPublicKey().GetHDFingerPrint();

            foreach (var o in HDKeysFor(accountKey).GroupBy(c => c.Coin))
            {
                foreach (var keyPath in o)
                {
                    o.Key.HDKeyPaths.Remove(keyPath.PubKey);
                    o.Key.HDKeyPaths.Add(keyPath.PubKey, newRoot.Derive(keyPath.RootedKeyPath.KeyPath));
                }
            }
            foreach (var xpub in GlobalXPubs.ToList())
            {
                if (xpub.Key.ExtPubKey.PubKey == accountKey.GetPublicKey())
                {
                    GlobalXPubs.Remove(xpub.Key);
                    GlobalXPubs.Add(xpub.Key, newRoot.Derive(xpub.Value.KeyPath));
                }
            }
            return(this);
        }