Bitcoin full node and query server

May 17, 2025

View in context in the libbitcoin Slack #general mirror.

evoskuil05:46
During the meeting we discussed using fabricated scripts for both the key path spend execution and the fall-through success. These are now implemented, trivial, and very optimal.
static script checksig_script() NOEXCEPT
{
    static const script cached{ { { opcode::checksig } } };
    return cached;
}

static script::cptr success_script_ptr() NOEXCEPT
{
    constexpr auto op_success = opcode::reserved_80;
    static_assert(operation::is_success(op_success));
    static const auto cached = to_shared<script>({ { op_success } });
    return cached;
}

static script::cptr checksig_script_ptr() NOEXCEPT
{
    static const auto cached = to_shared<script>(checksig_script());
    return cached;
}        

...

        case script_version::taproot:
        {
            // witness stack : [annex]...
            if (program->size() == hash_size)
            {
                auto stack_size = out_stack->size();

                // If at least two elements, discard annex if present.
                if (drop_annex(*out_stack))
                    --stack_size;

                // p2ts (tapscript, script path spend)
                // witness stack : <control><script>[stack-elements]
                // input script  : (empty)
                // output script : <1> <32-byte-tweaked-public-key>
                if (stack_size > one)
                {
                    // The last stack element is the control block.
                    const auto& control = pop(*out_stack);

                    // Control length must be 33 + 32m, for integer m [0..128].
                    if (!is_valid_control_block(*control))
                        return error::invalid_witness;

                    // The second-to-last stack element is the script.
                    out_script = to_shared<script>(*pop(*out_stack), false);

                    // TODO: DO SOME NASTY SHIT WITH CONTROL AND SCRIPT.
                    // TODO: MUST OBTAIN tapleaf_hash for signature hash.
                    // q is referred to as `taproot output key`.
                    // p is referred to as `taproot internal key`.

                    // Execute script with remaining stack.
                    return error::script_success;
                }

                // p2tr (taproot, key path spend)
                // witness stack : <signature>
                // input script  : (empty)
                // output script : <1> <32-byte-tweaked-public-key>
                if (is_one(stack_size))
                {
                    // Stack element is a signature that must be valid for q.
                    // Program is q, a 32 byte bip340 public key, so push it.
                    out_stack->push_back(program);
                    out_script = checksig_script_ptr();

                    // out_stack  : <public-key><signature>
                    // out_script : <op_checksig>
                    return error::script_success;
                }

                // Fail if witness stack was empty.
                return error::invalid_witness;
            }

            // This op_success script succeeds immediately with bip342 active.
            out_script = success_script_ptr();
            out_stack->clear();

            // Version 1 other than 32 bytes remain unencumbered (success).
            return error::script_success;
        }
(edited)