public PSBT SignAll(ExtKey masterKey, SigHash sigHash = SigHash.All)
        {
            if (masterKey == null)
            {
                throw new ArgumentNullException(nameof(masterKey));
            }
            DerivationCache derivation = new DerivationCache(masterKey);

            foreach (var o in Inputs)
            {
                o.TrySign(masterKey, sigHash, derivation);
            }
            return(this);
        }
Ejemplo n.º 2
0
        internal void TrySign(ExtKey masterKey, SigHash sigHash, DerivationCache derivation)
        {
            foreach (var hdk in HDKeyPaths)
            {
                var pubkey  = hdk.Key;
                var keyPath = hdk.Value.Item2;
                var fp      = hdk.Value.Item1;

                if ((fp == masterKey.GetPublicKey().GetHDFingerPrint() || fp == default) &&
                    (derivation.Derive(keyPath) is ExtKey k && k.GetPublicKey() == pubkey))
                {
                    Sign(k.PrivateKey, sigHash);
                }
            }
        }
        /// <summary>
        /// Add keypath information to this PSBT for each input or output involving it
        /// </summary>
        /// <param name="masterKey">The master key of the keypaths</param>
        /// <param name="paths">The path of the public keys with their expected scriptPubKey</param>
        /// <returns>This PSBT</returns>
        public PSBT AddKeyPath(IHDKey masterKey, params Tuple <KeyPath, Script>[] paths)
        {
            if (masterKey == null)
            {
                throw new ArgumentNullException(nameof(masterKey));
            }
            if (paths == null)
            {
                throw new ArgumentNullException(nameof(paths));
            }

            DerivationCache derivationCache = new DerivationCache(masterKey);

            foreach (var path in paths)
            {
                var key = derivationCache.Derive(path.Item1);
                AddKeyPath(masterKey.GetPublicKey().GetHDFingerPrint(), key.GetPublicKey(), path.Item1, path.Item2);
            }
            return(this);
        }
        /// <summary>
        /// Get the balance change if you were signing this transaction.
        /// </summary>
        /// <returns>The balance change</returns>
        public Money GetBalance(IHDKey masterKey)
        {
            if (masterKey == null)
            {
                throw new ArgumentNullException(nameof(masterKey));
            }
            var             masterFP        = masterKey.GetPublicKey().GetHDFingerPrint();
            Money           total           = Money.Zero;
            DerivationCache derivationCache = new DerivationCache(masterKey);

            foreach (var o in Inputs.OfType <PSBTCoin>().Concat(Outputs))
            {
                var amount = o.GetCoin()?.Amount;
                if (amount == null)
                {
                    continue;
                }
                if (o is PSBTInput)
                {
                    amount = -amount;
                }
                foreach (var hdk in o.HDKeyPaths)
                {
                    var pubkey  = hdk.Key;
                    var keyPath = hdk.Value.Item2;
                    var fp      = hdk.Value.Item1;

                    if ((fp == masterKey.GetPublicKey().GetHDFingerPrint() || fp == default) &&
                        (derivationCache.Derive(keyPath).GetPublicKey() == pubkey))
                    {
                        total += amount;
                    }
                }
            }
            return(total);
        }