richardkiss/pycoin

P2TR: unknown expression PUSH_81

C-Otto opened this issue · 6 comments

I realize that P2TR has beta support, and you might be aware of this. Sorry in advance.

I'm using a history.sh example from electrs, which uses pycoin to get the script for a given address. This fails for bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm:

$ ./history.sh --venv bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm
[2022-02-13 07:47:39.924264] INFO: electrum: connecting to localhost:50001
Traceback (most recent call last):
  File "/home/cotto/git/electrs/contrib/history.py", line 159, in <module>
    main()
  File "/home/cotto/git/electrs/contrib/history.py", line 56, in main
    script_hashes = [
  File "/home/cotto/git/electrs/contrib/history.py", line 57, in <listcomp>
    _script_hash(network.parse.address(addr).script())
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/networks/ParseAPI.py", line 364, in address
    or self.p2tr(s)
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/networks/ParseAPI.py", line 263, in p2tr
    return self._bech32m(s, 1, 32, "for_p2tr")
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/networks/ParseAPI.py", line 240, in _bech32m
    script = script_f(decoded_data)
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/networks/ContractAPI.py", line 54, in for_p2tr
    return self.for_info(dict(type="p2tr", synthetic_key=synthetic_key))
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/networks/ContractAPI.py", line 108, in for_info
    return self._script_tools.compile(script_text)
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/vm/ScriptTools.py", line 53, in compile
    v = self.compile_expression(t)
  File "/home/cotto/git/electrs/contrib/.venv/lib/python3.9/site-packages/pycoin/vm/ScriptTools.py", line 36, in compile_expression
    raise SyntaxError("unknown expression %s" % t)
SyntaxError: unknown expression PUSH_81

Can you simplify the example or provide a pointer to history.sh? Reproducing this bug is the first step to solving it.

Sorry, I hoped the stack trace helped you see the entry point. Here's the history.py (called from history.sh): https://github.com/romanz/electrs/blob/master/contrib/history.py

As far as I can see, this just tries to get the script for the given address, using the mainnet BTC chain details:

network.parse.address(addr).script()

Okay, so here's a minimal failure case:

python3 -c 'from pycoin.symbols.btc import network; network.parse.address("bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm")'

Even easier: coinc 'PUSH_81'

Okay, looks like PUSH_81 isn't a real operator. Try this patch and see if it helps:

--- a/pycoin/networks/ContractAPI.py
+++ b/pycoin/networks/ContractAPI.py
@@ -93,7 +93,7 @@ class ContractAPI(object):
         p2pkh_wit=lambda info: "OP_0 %s" % b2h(info.get("hash160")),
         p2sh=lambda info: "OP_HASH160 %s OP_EQUAL" % b2h(info.get("hash160")),
         p2sh_wit=lambda info: "OP_0 %s" % b2h(info.get("hash256")),
-        p2tr=lambda info: "PUSH_81 %s" % b2h(info.get("synthetic_key")),
+        p2tr=lambda info: "OP_1 %s" % b2h(info.get("synthetic_key")),
         multisig=lambda info: "%d %s %d OP_CHECKMULTISIG" % (
             info.get("m"), " ".join(b2h(sk) for sk in info.get("sec_keys")), len(info.get("sec_keys"))),
     )

I now get

% python3 -c 'from pycoin.symbols.btc import network; print(network.parse.address("bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm").script().hex())'
51202fcad7470279652cc5f88b8908678d6f4d57af5627183b03fc8404cb4e16d889

which seems less bad.

Thanks, this looks reasonable to me:

cotto@home:~/git/electrs/contrib (master)$ ./history.sh --venv bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm
[2022-02-14 08:27:03.221573] INFO: electrum: connecting to localhost:50001
[2022-02-14 08:27:03.312897] INFO: electrum: subscribed to 1 scripthashes
[2022-02-14 08:27:03.319916] INFO: electrum: got history of 8 transactions
[2022-02-14 08:27:03.322742] INFO: electrum: loaded 8 transactions
[2022-02-14 08:27:03.324387] INFO: electrum: loaded 4 header timestamps
[2022-02-14 08:27:03.366496] INFO: electrum: loaded 8 merkle proofs
+------------------------------------------------------------------+----------------------+--------+---------------+--------------+--------------+
|                               txid                               |   block timestamp    | height | confirmations | delta (mBTC) | total (mBTC) |
+------------------------------------------------------------------+----------------------+--------+---------------+--------------+--------------+
| 5b79f5d6039c188613342eb13961dd7d1e1a0f90023d3eaed25fc85a29201bb4 | 2021-11-14T05:15:27Z | 709632 |     13613     |      0.07500 |      0.07500 |
| 905ecdf95a84804b192f4dc221cfed4d77959b81ed66013a7e41a6e61e7ed530 | 2021-11-14T05:40:13Z | 709635 |     13610     |     -0.07500 |      0.00000 |
| aaef6c5fdece5f7ff7614173e057148ea9b737a4a25c2b3b06da5b963ab29c4c | 2021-11-14T05:50:37Z | 709636 |     13609     |      0.07500 |      0.07500 |
| 1ec1bc93dcf19ef972fe7083393d188387cff24203d07ad98bcba776e8fd87ac | 2021-11-14T05:50:37Z | 709636 |     13609     |     -0.07500 |      0.00000 |
| 68d0aa3e3afd7311d25e026d026b2190d4814556653c4d01c849bc2b177a6b44 | 2021-11-14T05:50:37Z | 709636 |     13609     |      0.07500 |      0.07500 |
| 94fff3594c1ac2bca92bd1a23544af99e46e5c70726b57181d504f99d65add80 | 2021-11-14T05:50:37Z | 709636 |     13609     |     -0.07500 |      0.00000 |
| 915181151c955214318bff2323e13a3f0d37013333644193b6dac0b31f905b94 | 2021-11-14T05:50:37Z | 709636 |     13609     |      0.07500 |      0.07500 |
| 3bb0608088450c137906a653fb62f180178580587bd02712d7ecafb7385f9c16 | 2021-11-14T05:53:34Z | 709638 |     13607     |     -0.07500 |      0.00000 |
+------------------------------------------------------------------+----------------------+--------+---------------+--------------+--------------+
[2022-02-14 08:27:03.367258] INFO: electrum: tip=000000000000000000048afc60197f3b9c647e1b663f89cedf105b294d201f15, height=723244 @ 2022-02-14T08:24:58Z

This matches the output I get from mempool.space:

https://mempool.space/address/bc1p9l9dw3cz09jje30c3wysseuddax40t6kyuvrkqlusszvknskmzys00k7zm