Skip to content

feature: blockchain.scriptpubkey.* endpoints #3

Closed

Description

@Davidson-Souza

I would like to get some feedback on a new endpoint for the Electrum Protocol that takes scriptpubkeys instead of script hashes.

Rationale

I'm working on a project that has an integrated Electrum Server, but doesn't index all addresses. It's meant for single users that want a server for their wallet, and only their wallet and to find older transactions, we use BIP-0158 Compact Block Filters, but those filters don't index script hashes, only scriptpubkeys. For this reason, I need to ask the user to provide the wallet xpub or output descriptor before being able to use Floresta. This is a bad UX, I would prefer everything to work out-of-the box with no extra configuration.

It gets even harder when you consider second layer protocols, like Lightning. Where we can't have something easy like output descriptors; the addresses we need to follow depend on the peer's public keys, which we don't know until we open a channel, and is unique per channel. If we can give the scriptpubkey it doesn't matter that those addresses aren't HD, simply use the filters to find any historical transactions.

Spec

I thought about mirroring the endpoints in blockchain.scripthash, but with scriptpubkeys. This won't have the same problem as the older blockchain.address. Scripts are general, and don't depend on the address type, so they are future-proof for new soft-forks. In fact, if those endpoints use the same data as blockchain.scripthash, so if any soft-fork breaks those new endpoints, it also breaks the old ones.

Talking with @SomberNight back in Prague, we've decided it would be better to limit the script sizes for DoS prevention. The Bitcoin consensus limits scriptpubkeys to 10,000 bytes (anything above that is unspendable), but this is way above the needed. If we assume that users will only care about standard addresses type, 200 bytes cover all of them. (*)

  • n-of-3 bare multisig: If we use uncompressed pks (65 bytes) we have <t> OP_PUSH65 <pk1> OP_PUSH65 <pk1> OP_PUSH65 <pk1> 3 OP_CHECKMULTISIG (198 bytes)
  • pay to pubkey: again, using uncompressed pk: OP_PUSH65 <pk> OP_CHECKMULTISIG (67 bytes)
  • pay to pk hash: OP_DUP OP_HASH160 OP_PUSH20 <hash> OP_EQUALVERIFY OP_CHECKSIG (25 bytes)
  • pay to script hash: OP_HASH160 OP_PUSHBYTES_20 <hash> OP_EQUAL (23 bytes)
  • pay to witness script hash: OP_0 OP_PUSHBYTES32 <hash> (34 bytes)
  • pay to witness pk hash: OP_0 OP_PUSHBYTES20 <hash> (22 bytes)
  • pay to tr: OP_1 OP_PUSHBYTES32 <hash> (34 bytes)
  • OP_RETURN: OP_RETURN OP_PUSHDATA1 <bytes> <thing>

If we were to limit it to 200 bytes, we would cover them. But we can assume that users of things like Floresta or EPS are just using a common address scheme like pkh or one of the segwit addresses. So 64 bytes are more than enough. If the user wants something more fancy, just use the old blockchain.scripthash endpoints.

Implementation

It should be trivial to implement this with the current indexers, you just need to hash the spk passed as parameter and lookup as usual. No re-indexing is required here. There's also no need to change wallets that don't want to implement these, as the old ones are still there. But if you'd like to, it's trivial to swap endpoints.

(*) someone please check this math, I did this from the top of my head and might be wrong by a few bytes.

Activity

  1. deleted a comment from Jostino on Oct 23, 2024
  2. SomberNight commented on Feb 21, 2025

    @SomberNight
    Member

    Talking with @SomberNight back in Prague, we've decided it would be better to limit the script sizes for DoS prevention

    I thought about this more and I think instead of limiting the script sizes, another approach would be to keep using scripthashes in the notifications only.

    See
    9e02865 from #2
    SomberNight@9e02865#diff-ffa7e37c618564f8c58d9072b5ef234e6e9b2cdf1d71d16be7dc356146cb475fR521

    blockchain.scriptpubkey.subscribe
    =================================
    
    Subscribe to a :ref:`scriptPubKey <scriptpubkeys>`.
    
    **Signature**
    
      .. function:: blockchain.scriptpubkey.subscribe(scriptpubkey)
      .. versionadded:: 1.6
    
      *scriptpubkey*
    
        The scriptPubKey as a hexadecimal string.
    
    **Result**
    
      The :ref:`status <status>` of the scriptPubKey.
    
    **Notifications**
    
      The client will receive a notification when the :ref:`status <status>` of the
      scriptPubKey changes. Importantly, the notifications use :ref:`script hash <script hashes>`
      instead of scriptPubKey. The scripthash corresponds to the scriptPubKey from the
      original request.
      The client is expected to maintain a mapping scripthash->scriptpubkey, or similar,
      to be able to figure out what the notification refers to.
      Notably, this way servers do not have to store in memory the scriptpubkey corresponding
      to the original request (which can be up to 10 KB in size, as per Bitcoin consensus),
      only the scripthash (which is fixed size). Also, this limits upstream bandwidth usage
      of servers.
      The signature is
    
        .. function:: blockchain.scriptpubkey.subscribe(scripthash, status)
          :noindex:
    
  3. Davidson-Souza commented on Feb 25, 2025

    @Davidson-Souza
    Author

    Hi @SomberNight.

    I thought about this more and I think instead of limiting the script sizes, another approach would be to keep using scripthashes in the notifications only.

    See
    9e02865 from #2
    SomberNight@9e02865#diff-ffa7e37c618564f8c58d9072b5ef234e6e9b2cdf1d71d16be7dc356146cb475fR521

    If wallet implementors don't have a problem with keeping this map scriptHashes -> scriptPubkeys around, this approach is perfect for me. It does solve the original problem and still makes it simple to reason about resource usage.

  4. added this to the protocol 1.7 milestone on May 22, 2026
  5. SomberNight commented on Jul 1, 2026

    @SomberNight
    Member

    done in #2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions