Exemplo n.º 1
0
        public Transaction SignTransaction(KeyPath keyPath, ICoin[] signedCoins, Transaction[] parents, Transaction transaction)
        {
            using(Transport.Lock())
            {
                var pubkey = GetWalletPubKey(keyPath).UncompressedPublicKey.Compress();
                var parentsById = parents.ToDictionary(p => p.GetHash());
                var coinsByPrevout = signedCoins.ToDictionary(c => c.Outpoint);

                List<TrustedInput> trustedInputs = new List<TrustedInput>();
                foreach(var input in transaction.Inputs)
                {
                    Transaction parent;
                    parentsById.TryGetValue(input.PrevOut.Hash, out parent);
                    if(parent == null)
                        throw new KeyNotFoundException("Parent transaction " + input.PrevOut.Hash + " not found");
                    trustedInputs.Add(GetTrustedInput(parent, (int)input.PrevOut.N));
                }

                var inputs = trustedInputs.ToArray();

                transaction = transaction.Clone();

                foreach(var input in transaction.Inputs)
                {
                    ICoin previousCoin = null;
                    coinsByPrevout.TryGetValue(input.PrevOut, out previousCoin);

                    if(previousCoin != null)
                        input.ScriptSig = previousCoin.GetScriptCode();
                }

                bool newTransaction = true;
                foreach(var input in transaction.Inputs.AsIndexedInputs())
                {
                    ICoin coin = null;
                    if(!coinsByPrevout.TryGetValue(input.PrevOut, out coin))
                        continue;

                    UntrustedHashTransactionInputStart(newTransaction, input, inputs);
                    newTransaction = false;

                    UntrustedHashTransactionInputFinalizeFull(transaction.Outputs);

                    var sig = UntrustedHashSign(keyPath, null, transaction.LockTime, SigHash.All);
                    input.ScriptSig = PayToPubkeyHashTemplate.Instance.GenerateScriptSig(sig, pubkey);
                    ScriptError error;
                    if(!Script.VerifyScript(coin.TxOut.ScriptPubKey, transaction, (int)input.Index, Money.Zero, out error))
                        return null;
                }

                return transaction;
            }
        }