Ejemplo n.º 1
0
        internal IEnumerable <Script> GetDestinations(Script redeemScript)
        {
            ScriptTemplate scriptTemplate = this.network.StandardScriptsRegistry.GetTemplateFromScriptPubKey(redeemScript);

            if (scriptTemplate != null)
            {
                // We need scripts suitable for matching to HDAddress.ScriptPubKey.
                switch (scriptTemplate.Type)
                {
                case TxOutType.TX_PUBKEYHASH:
                    yield return(redeemScript);

                    break;

                case TxOutType.TX_PUBKEY:
                    yield return(PayToPubkeyTemplate.Instance.ExtractScriptPubKeyParameters(redeemScript).Hash.ScriptPubKey);

                    break;

                case TxOutType.TX_SCRIPTHASH:
                    yield return(PayToScriptHashTemplate.Instance.ExtractScriptPubKeyParameters(redeemScript).ScriptPubKey);

                    break;

                case TxOutType.TX_SEGWIT:
                    TxDestination txDestination = PayToWitTemplate.Instance.ExtractScriptPubKeyParameters(this.network, redeemScript);
                    if (txDestination != null)
                    {
                        yield return(new KeyId(txDestination.ToBytes()).ScriptPubKey);
                    }
                    break;

                default:
                    if (this.scriptAddressReader is ScriptDestinationReader scriptDestinationReader)
                    {
                        foreach (TxDestination destination in scriptDestinationReader.GetDestinationFromScriptPubKey(this.network, redeemScript))
                        {
                            yield return(destination.ScriptPubKey);
                        }
                    }
                    else
                    {
                        string        address     = this.scriptAddressReader.GetAddressFromScriptPubKey(this.network, redeemScript);
                        TxDestination destination = ScriptDestinationReader.GetDestinationForAddress(address, this.network);
                        if (destination != null)
                        {
                            yield return(destination.ScriptPubKey);
                        }
                    }

                    break;
                }
            }
        }
        /// <summary>
        /// Adds a spendable cold staking transaction to a wallet.
        /// </summary>
        /// <param name="wallet">Wallet to add the transaction to.</param>
        /// <returns>The spendable transaction that was added to the wallet.</returns>
        private Transaction AddSpendableColdstakingTransactionToWallet(Wallet.Wallet wallet)
        {
            // Get first unused cold staking address.
            this.coldStakingManager.GetOrCreateColdStakingAccount(wallet.Name, true, walletPassword);
            HdAddress address = this.coldStakingManager.GetFirstUnusedColdStakingAddress(wallet.Name, true);

            TxDestination hotPubKey  = BitcoinAddress.Create(hotWalletAddress1, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);
            TxDestination coldPubKey = BitcoinAddress.Create(coldWalletAddress2, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);

            var scriptPubKey = new Script(OpcodeType.OP_DUP, OpcodeType.OP_HASH160, OpcodeType.OP_ROT, OpcodeType.OP_IF,
                                          OpcodeType.OP_CHECKCOLDSTAKEVERIFY, Op.GetPushOp(hotPubKey.ToBytes()), OpcodeType.OP_ELSE, Op.GetPushOp(coldPubKey.ToBytes()),
                                          OpcodeType.OP_ENDIF, OpcodeType.OP_EQUALVERIFY, OpcodeType.OP_CHECKSIG);

            var transaction = this.Network.CreateTransaction();

            transaction.Outputs.Add(new TxOut(Money.Coins(101), scriptPubKey));

            address.Transactions.Add(new TransactionData()
            {
                Hex          = transaction.ToHex(this.Network),
                Amount       = transaction.Outputs[0].Value,
                Id           = transaction.GetHash(),
                BlockHeight  = 0,
                Index        = 0,
                IsCoinBase   = false,
                IsCoinStake  = false,
                IsPropagated = true,
                BlockHash    = this.Network.GenesisHash,
                ScriptPubKey = scriptPubKey
            });

            return(transaction);
        }
        /// <summary>
        /// Adds a spendable cold staking transaction to a normal account, as oppose to dedicated special account.
        /// </summary>
        /// <param name="wallet">Wallet to add the transaction to.</param>
        /// <returns>The spendable transaction that was added to the wallet.</returns>
        private Transaction AddSpendableColdstakingTransactionToNormalWallet(Wallet.Wallet wallet, bool script = false)
        {
            // This will always be added to the secondary address.
            HdAddress address = wallet.GetAllAddresses().ToArray()[1];

            var transaction = this.Network.CreateTransaction();

            // Use the normal wallet address here.
            TxDestination hotPubKey  = BitcoinAddress.Create(address.Address, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);
            TxDestination coldPubKey = BitcoinAddress.Create(coldWalletAddress2, wallet.Network).ScriptPubKey.GetDestination(wallet.Network);

            var scriptPubKey = new Script(OpcodeType.OP_DUP, OpcodeType.OP_HASH160, OpcodeType.OP_ROT, OpcodeType.OP_IF,
                                          OpcodeType.OP_CHECKCOLDSTAKEVERIFY, Op.GetPushOp(hotPubKey.ToBytes()), OpcodeType.OP_ELSE, Op.GetPushOp(coldPubKey.ToBytes()),
                                          OpcodeType.OP_ENDIF, OpcodeType.OP_EQUALVERIFY, OpcodeType.OP_CHECKSIG);

            transaction.Outputs.Add(new TxOut(Money.Coins(202), script ? scriptPubKey.WitHash.ScriptPubKey : scriptPubKey));

            address.Transactions.Add(new TransactionData()
            {
                Hex             = transaction.ToHex(this.Network),
                Amount          = transaction.Outputs[0].Value,
                Id              = transaction.GetHash(),
                BlockHeight     = 0,
                Index           = 0,
                IsCoinBase      = false,
                IsCoinStake     = false,
                IsColdCoinStake = true,
                IsPropagated    = true,
                BlockHash       = this.Network.GenesisHash,
                ScriptPubKey    = script ? scriptPubKey.WitHash.ScriptPubKey : scriptPubKey,
            });

            return(transaction);
        }