A vertical slice of P2SH in ElectrumSV

Let’s confirm how P2SH scripts work in the context of ElectrumSV. This is a vertical slice from the wallet down to the node through the indexing server, examining the part each plays.

The node: Bitcoin SV

What do nodes do with transactions that include P2SH scripts?

A P2SH output script, which has to match the fixed standard structure defined in BIP16, contains the 160 bit hash of the script. It takes one item of data, hashes it and clips the hash to 160 bits. If the the data was the expected data based on the hash matching, then the spend succeeds.

A P2SH input script also has a standard structure. It can only be push data operations. The last piece of data is the serialised script. The data preceding it is never passed to the output, but is instead passed to the deserialised script which is expected to execute successfully. If it does, the serialised script is then passed to the output script, and if that also executes successfully, the spend is valid.

Reference: interpreter.cpp in the bitcoin-sv github repository.

The indexing server: ElectrumX

At this time, ElectrumX mostly deals with the needs of Bitcoin Core. This means that the blockchain isn’t intended for more than superficial use, and so the indexing is limited to complete script contents. There’s no nuances of analysing what is in the script. As long as it is spendable, ElectrumX hashes it and indexes all references to scripts that match the hash.

Reference: coins.py in the ElectrumX github repository.

The wallet: ElectrumSV

A wallet knows the addresses it will have incoming payments on, and currently it registers for a designated number of these. It does so by constructing the output scripts these addresses are expected to feature in, and passes the hashes of these to ElectrumX as the things it should be notified about.

That’s pretty much it. It gets notified of unspent outputs that feature an address it is interested in. It doesn’t need to evaluate scripts, because if a block is valid, then the scripts must have been evaluated successfully already. And if there is some shenanigans going on there, then a reorg will eventually clear things up. Spends are just inputs that refer to an output of interest, the contents of the input script are irrelevant.

ElectrumSV only ever touches on P2SH in the context of multi-signature scripts, so it will never ask to be notified about other kinds of P2SH transactions.

Reference: wallet.py in the ElectrumSV github repository.

Final thoughts

It should be obvious at this point that bare multisig support should in theory be just a superficial change to the non-UI parts of ElectrumSV. ElectrumSV can make a script hash of each bare multisig script. ElectrumX already deals in script hashes, so it can notify ElectrumSV about these. The node is already doing the node things with bare multisig scripts.

The big change is likely to be making ElectrumSV handle scripts instead of addresses, and that extends down below the UI.

Useful links

Written by

ElectrumSV developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store