1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 6 // The Solver functions are used by policy and the wallet, but not consensus.
7 8 #ifndef LIMENKA_SCRIPT_SOLVER_H
9 #define LIMENKA_SCRIPT_SOLVER_H
10 11 #include <attributes.h>
12 #include <script/script.h>
13 #include <span.h>
14 15 #include <string>
16 #include <optional>
17 #include <utility>
18 #include <vector>
19 20 class CPubKey;
21 22 enum class TxoutType {
23 NONSTANDARD,
24 // 'standard' transaction types:
25 ANCHOR, //!< anyone can spend script
26 PUBKEY,
27 PUBKEYHASH,
28 SCRIPTHASH,
29 MULTISIG,
30 NULL_DATA, //!< unspendable OP_RETURN script that carries data
31 WITNESS_V0_SCRIPTHASH,
32 WITNESS_V0_KEYHASH,
33 WITNESS_V1_TAPROOT,
34 WITNESS_V3_SPKHASH,
35 WITNESS_V4_BPCT, // bulletproof confidential transaction
36 WITNESS_UNKNOWN, //!< Only for Witness versions not already defined above
37 };
38 39 /** Get the name of a TxoutType as a string */
40 std::string GetTxnOutputType(TxoutType t);
41 42 constexpr bool IsPushdataOp(opcodetype opcode)
43 {
44 return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
45 }
46 47 /**
48 * Parse a scriptPubKey and identify script type for standard scripts. If
49 * successful, returns script type and parsed pubkeys or hashes, depending on
50 * the type. For example, for a P2SH script, vSolutionsRet will contain the
51 * script hash, for P2PKH it will contain the key hash, etc.
52 *
53 * @param[in] scriptPubKey Script to parse
54 * @param[out] vSolutionsRet Vector of parsed pubkeys and hashes
55 * @return The script type. TxoutType::NONSTANDARD represents a failed solve.
56 */
57 TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet);
58 59 /** Generate a P2PK script for the given pubkey. */
60 CScript GetScriptForRawPubKey(const CPubKey& pubkey);
61 62 /** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise.
63 * The keyspans refer to bytes in the passed script. */
64 std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
65 66 /** Generate a multisig script. */
67 CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys, bool fSorted=false);
68 69 #endif // LIMENKA_SCRIPT_SOLVER_H
70