Skip to content

Commit 9e02865

Browse files
committed
use scriptPubKeys everywhere instead of scripthashes
related #3
1 parent ad420c2 commit 9e02865

3 files changed

Lines changed: 95 additions & 77 deletions

File tree

docs/protocol-basics.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ All responses received in the stream from and including the server's
7676
response to this call will use its negotiated protocol version.
7777

7878

79+
.. _scriptpubkeys:
7980
.. _script hashes:
8081

8182
Script Hashes
8283
-------------
8384

8485
A :dfn:`script hash` is the hash of the binary bytes of the locking
85-
script (ScriptPubKey), expressed as a hexadecimal string. The hash
86-
function to use is given by the "hash_function" member of
87-
:func:`server.features` (currently :func:`sha256` only). Like for
86+
script (scriptPubKey), expressed as a hexadecimal string. The hash
87+
function to use is :func:`sha256`. Like for
8888
block and transaction hashes, when converting the big-endian binary
8989
hash to a hexadecimal string the least-significant byte appears first,
9090
and the most-significant byte last.
@@ -93,27 +93,28 @@ For example, the legacy Bitcoin address from the genesis block::
9393

9494
1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa
9595

96-
has P2PKH script::
96+
has P2PKH script (scriptPubKey)::
9797

9898
76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac
9999

100100
with SHA256 hash::
101101

102102
6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b
103103

104-
which is sent to the server reversed as::
104+
the scripthash is defined as the reverse of that::
105105

106106
8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161
107107

108-
By subscribing to this hash you can find P2PKH payments to that address.
108+
By subscribing to the scriptPubKey or the scripthash,
109+
you can find P2PKH payments to that address.
109110

110111
One public key, the genesis block public key, among the trillions for
111112
that address is::
112113

113114
04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb
114115
649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f
115116

116-
which has P2PK script::
117+
which has P2PK script (scriptPubKey)::
117118

118119
4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb
119120
649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac
@@ -122,11 +123,12 @@ with SHA256 hash::
122123

123124
3318537dfb3135df9f3d950dbdf8a7ae68dd7c7dfef61ed17963ff80f3850474
124125

125-
which is sent to the server reversed as::
126+
the scripthash is defined as the reverse of that::
126127

127128
740485f380ff6379d11ef6fe7d7cdd68aea7f8bd0d953d9fdf3531fb7d531833
128129

129-
By subscribing to this hash you can find P2PK payments to the genesis
130+
By subscribing to the scriptPubKey or the scripthash,
131+
you can find P2PK payments to the genesis
130132
block public key.
131133

132134
.. note:: The Genesis block coinbase is uniquely unspendable and
@@ -139,10 +141,10 @@ block public key.
139141
Status
140142
------
141143

142-
To calculate the `status` of a :ref:`script hash <script hashes>` (or
143-
address):
144+
To calculate the `status` of a :ref:`scriptPubKey <scriptpubkeys>`
145+
(or :ref:`script hash <script hashes>` or address):
144146

145-
1. Consider all transactions touching the script hash (both those spending
147+
1. Consider all transactions touching the scriptPubKey (both those spending
146148
from it, and those funding it), both confirmed and unconfirmed (in mempool).
147149

148150
2. Order confirmed transactions by increasing height (and position in the
@@ -166,7 +168,7 @@ txid (in network byteorder) is used to arrive at a canonical ordering.
166168
6. Next, with mempool transactions in the specified order, append a similar
167169
string.
168170

169-
7. The :dfn:`status` of the script hash is the :func:`sha256` hash of the
171+
7. The :dfn:`status` of the scriptPubKey is the :func:`sha256` hash of the
170172
full string expressed as a hexadecimal string, or :const:`null` if the
171173
string is empty because there are no transactions.
172174

docs/protocol-changes.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ Changes
194194
* Breaking change for the version negotiation: we now mandate that
195195
the :func:`server.version` message must be the first message sent.
196196
That is, version negotiation must happen before any other messages.
197-
* The status of a scripthash has its definition tightened in a
197+
* The `blockchain.scripthash.*` methods are all replaced with `blockchain.scriptpubkey.*`
198+
methods. Note: `sha256(scriptPubKey) == scripthash`.
199+
* The status of a scripthash/scriptpubkey has its definition tightened in a
198200
backwards-compatible way: mempool txs now have a canonical ordering
199201
defined for the calculation (previously their order was undefined).
200202
* :func:`blockchain.scripthash.get_mempool` previously did not define
@@ -218,8 +220,24 @@ Changes
218220
New methods
219221
-----------
220222

223+
* :func:`blockchain.scriptpubkey.get_balance`
224+
* :func:`blockchain.scriptpubkey.get_history`
225+
* :func:`blockchain.scriptpubkey.get_mempool`
226+
* :func:`blockchain.scriptpubkey.listunspent`
227+
* :func:`blockchain.scriptpubkey.subscribe`
228+
* :func:`blockchain.scriptpubkey.unsubscribe`
221229
* :func:`blockchain.outpoint.subscribe` to subscribe to a transaction
222230
outpoint, and get a notification when it gets spent.
223231
* :func:`blockchain.outpoint.unsubscribe` to unsubscribe from a TXO.
224232
* :func:`blockchain.outpoint.get_status` to get current status of a TXO, without subscribing to changes.
225233
* :func:`blockchain.transaction.broadcast_package` to broadcast a package of transactions (submitpackage).
234+
235+
Removed methods
236+
---------------
237+
238+
* :func:`blockchain.scripthash.get_balance`
239+
* :func:`blockchain.scripthash.get_history`
240+
* :func:`blockchain.scripthash.get_mempool`
241+
* :func:`blockchain.scripthash.listunspent`
242+
* :func:`blockchain.scripthash.subscribe`
243+
* :func:`blockchain.scripthash.unsubscribe`

docs/protocol-methods.rst

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,19 @@ be accepted to the daemon's memory pool.
290290

291291
0.0
292292

293-
blockchain.scripthash.get_balance
294-
=================================
293+
blockchain.scriptpubkey.get_balance
294+
===================================
295295

296-
Return the confirmed and unconfirmed balances of a :ref:`script hash
297-
<script hashes>`.
296+
Return the confirmed and unconfirmed balances of a :ref:`scriptPubKey <scriptpubkeys>`.
298297

299298
**Signature**
300299

301-
.. function:: blockchain.scripthash.get_balance(scripthash)
302-
.. versionadded:: 1.1
300+
.. function:: blockchain.scriptpubkey.get_balance(scriptpubkey)
301+
.. versionadded:: 1.6
303302

304-
*scripthash*
303+
*scriptpubkey*
305304

306-
The script hash as a hexadecimal string.
305+
The scriptPubKey as a hexadecimal string.
307306

308307
**Result**
309308

@@ -322,25 +321,24 @@ Return the confirmed and unconfirmed balances of a :ref:`script hash
322321
"unconfirmed": 23684400
323322
}
324323

325-
blockchain.scripthash.get_history
326-
=================================
324+
blockchain.scriptpubkey.get_history
325+
===================================
327326

328-
Return the confirmed and unconfirmed history of a :ref:`script hash
329-
<script hashes>`.
327+
Return the confirmed and unconfirmed history of a :ref:`scriptPubKey <scriptpubkeys>`.
330328

331329
**Signature**
332330

333-
.. function:: blockchain.scripthash.get_history(scripthash)
334-
.. versionadded:: 1.1
331+
.. function:: blockchain.scriptpubkey.get_history(scriptpubkey)
332+
.. versionadded:: 1.6
335333

336-
*scripthash*
334+
*scriptpubkey*
337335

338-
The script hash as a hexadecimal string.
336+
The scriptPubKey as a hexadecimal string.
339337

340338
**Result**
341339

342340
A list of confirmed transactions in blockchain order, with the
343-
output of :func:`blockchain.scripthash.get_mempool` appended to the
341+
output of :func:`blockchain.scriptpubkey.get_mempool` appended to the
344342
list. Each confirmed transaction is a dictionary with the following
345343
keys:
346344

@@ -352,7 +350,7 @@ Return the confirmed and unconfirmed history of a :ref:`script hash
352350

353351
The transaction hash in hexadecimal.
354352

355-
See :func:`blockchain.scripthash.get_mempool` for how mempool
353+
See :func:`blockchain.scriptpubkey.get_mempool` for how mempool
356354
transactions are returned.
357355

358356
**Result Examples**
@@ -380,27 +378,26 @@ Return the confirmed and unconfirmed history of a :ref:`script hash
380378
}
381379
]
382380

383-
blockchain.scripthash.get_mempool
384-
=================================
381+
blockchain.scriptpubkey.get_mempool
382+
===================================
385383

386-
Return the unconfirmed transactions of a :ref:`script hash <script
387-
hashes>`.
384+
Return the unconfirmed transactions of a :ref:`scriptPubKey <scriptpubkeys>`.
388385

389386
**Signature**
390387

391-
.. function:: blockchain.scripthash.get_mempool(scripthash)
392-
.. versionadded:: 1.1
388+
.. function:: blockchain.scriptpubkey.get_mempool(scriptpubkey)
389+
.. versionadded:: 1.6
393390
.. versionchanged:: 1.6
394391
results must be sorted (previously undefined order)
395392

396-
*scripthash*
393+
*scriptpubkey*
397394

398-
The script hash as a hexadecimal string.
395+
The scriptPubKey as a hexadecimal string.
399396

400397
**Result**
401398

402399
A list of mempool transactions. The order is the same as when computing the
403-
:ref:`status <status>` of the script hash.
400+
:ref:`status <status>` of the scriptPubKey.
404401
Each mempool transaction is a dictionary with the following keys:
405402

406403
* *height*
@@ -428,19 +425,19 @@ hashes>`.
428425
]
429426

430427

431-
blockchain.scripthash.listunspent
432-
=================================
428+
blockchain.scriptpubkey.listunspent
429+
===================================
433430

434-
Return an ordered list of UTXOs sent to a script hash.
431+
Return an ordered list of UTXOs sent to a :ref:`scriptPubKey <scriptpubkeys>`.
435432

436433
**Signature**
437434

438-
.. function:: blockchain.scripthash.listunspent(scripthash)
439-
.. versionadded:: 1.1
435+
.. function:: blockchain.scriptpubkey.listunspent(scriptpubkey)
436+
.. versionadded:: 1.6
440437

441-
*scripthash*
438+
*scriptpubkey*
442439

443-
The script hash as a hexadecimal string.
440+
The scriptPubKey as a hexadecimal string.
444441

445442
**Result**
446443

@@ -501,50 +498,59 @@ Return an ordered list of UTXOs sent to a script hash.
501498

502499
.. _subscribed:
503500

504-
blockchain.scripthash.subscribe
505-
===============================
501+
blockchain.scriptpubkey.subscribe
502+
=================================
506503

507-
Subscribe to a script hash.
504+
Subscribe to a :ref:`scriptPubKey <scriptpubkeys>`.
508505

509506
**Signature**
510507

511-
.. function:: blockchain.scripthash.subscribe(scripthash)
512-
.. versionadded:: 1.1
508+
.. function:: blockchain.scriptpubkey.subscribe(scriptpubkey)
509+
.. versionadded:: 1.6
513510

514-
*scripthash*
511+
*scriptpubkey*
515512

516-
The script hash as a hexadecimal string.
513+
The scriptPubKey as a hexadecimal string.
517514

518515
**Result**
519516

520-
The :ref:`status <status>` of the script hash.
517+
The :ref:`status <status>` of the scriptPubKey.
521518

522519
**Notifications**
523520

524-
The client will receive a notification when the :ref:`status <status>` of the script
525-
hash changes. Its signature is
526-
527-
.. function:: blockchain.scripthash.subscribe(scripthash, status)
521+
The client will receive a notification when the :ref:`status <status>` of the
522+
scriptPubKey changes. Importantly, the notifications use :ref:`script hash <script hashes>`
523+
instead of scriptPubKey. The scripthash corresponds to the scriptPubKey from the
524+
original request.
525+
The client is expected to maintain a mapping scripthash->scriptpubkey, or similar,
526+
to be able to figure out what the notification refers to.
527+
Notably, this way servers do not have to store in memory the scriptpubkey corresponding
528+
to the original request (which can be up to 10 KB in size, as per Bitcoin consensus),
529+
only the scripthash (which is fixed size). Also, this limits upstream bandwidth usage
530+
of servers.
531+
The signature is
532+
533+
.. function:: blockchain.scriptpubkey.subscribe(scripthash, status)
528534
:noindex:
529535

530-
blockchain.scripthash.unsubscribe
531-
=================================
536+
blockchain.scriptpubkey.unsubscribe
537+
===================================
532538

533-
Unsubscribe from a script hash, preventing future notifications if its :ref:`status
539+
Unsubscribe from a scriptPubKey, preventing future notifications if its :ref:`status
534540
<status>` changes.
535541

536542
**Signature**
537543

538-
.. function:: blockchain.scripthash.unsubscribe(scripthash)
539-
.. versionadded:: 1.4.2
544+
.. function:: blockchain.scriptpubkey.unsubscribe(scriptpubkey)
545+
.. versionadded:: 1.6
540546

541-
*scripthash*
547+
*scriptpubkey*
542548

543-
The script hash as a hexadecimal string.
549+
The scriptPubKey as a hexadecimal string.
544550

545551
**Result**
546552

547-
Returns :const:`True` if the scripthash was subscribed to, otherwise :const:`False`.
553+
Returns :const:`True` if the scriptpubkey was subscribed to, otherwise :const:`False`.
548554
Note that :const:`False` might be returned even for something subscribed to earlier,
549555
because the server can drop subscriptions in rare circumstances.
550556

@@ -1188,6 +1194,7 @@ Return a list of features and services supported by the server.
11881194
.. function:: server.features()
11891195
.. versionchanged:: 1.6
11901196
added *method_flavours* field to result
1197+
removed *hash_function* field from result
11911198

11921199
**Result**
11931200

@@ -1224,14 +1231,6 @@ Return a list of features and services supported by the server.
12241231
The hash of the genesis block. This is used to detect if a peer
12251232
is connected to one serving a different network.
12261233

1227-
* *hash_function*
1228-
1229-
The hash function the server uses for :ref:`script hashing
1230-
<script hashes>`. The client must use this function to hash
1231-
pay-to-scripts to produce script hashes to send to the server.
1232-
The default is "sha256". "sha256" is currently the only
1233-
acceptable value.
1234-
12351234
* *server_version*
12361235

12371236
A string that identifies the server software. Should be the same
@@ -1272,7 +1271,6 @@ Return a list of features and services supported by the server.
12721271
"protocol_min": "1.0",
12731272
"pruning": null,
12741273
"server_version": "ElectrumX 1.0.17",
1275-
"hash_function": "sha256",
12761274
"method_flavours": {
12771275
"blockchain.outpoint.subscribe": {"requires_spk_hint": true},
12781276
"blockchain.transaction.get": {"supports_verbose_true": false}

0 commit comments

Comments
 (0)