Skip to content

Commit

Permalink
Namecoin: name script support in transaction.get_address_from_output_…
Browse files Browse the repository at this point in the history
…script
  • Loading branch information
JeremyRand committed Oct 1, 2018
1 parent 3c920cb commit 5fc9e66
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
53 changes: 53 additions & 0 deletions electrum_nmc/names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
#
# Electrum-NMC - lightweight Namecoin client
# Copyright (C) 2018 Namecoin Developers
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

def split_name_script(decoded):
# name_new TxOuts look like:
# NAME_NEW (hash) 2DROP (Bitcoin TxOut)
match = [ OP_NAME_NEW, opcodes.OP_PUSHDATA4, opcodes.OP_2DROP ]
if match_decoded(decoded[:len(match)], match):
return {"name_op": OP_NAME_NEW, "address_scriptPubKey": decoded[len(match):]}

# name_firstupdate TxOuts look like:
# NAME_FIRSTUPDATE (name) (rand) (value) 2DROP 2DROP (Bitcoin TxOut)
match = [ OP_NAME_FIRSTUPDATE, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2DROP, opcodes.OP_2DROP ]
if match_decoded(decoded[:len(match)], match):
return {"name_op": OP_NAME_FIRSTUPDATE, "address_scriptPubKey": decoded[len(match):]}

# name_update TxOuts look like:
# NAME_UPDATE (name) (value) 2DROP DROP (Bitcoin TxOut)
match = [ OP_NAME_UPDATE, opcodes.OP_PUSHDATA4, opcodes.OP_PUSHDATA4, opcodes.OP_2DROP, opcodes.OP_DROP ]
if match_decoded(decoded[:len(match)], match):
return {"name_op": OP_NAME_UPDATE, "address_scriptPubKey": decoded[len(match):]}

return {"name_op": None, "address_scriptPubKey": decoded}


from .transaction import match_decoded, opcodes

OP_NAME_NEW = opcodes.OP_1
OP_NAME_FIRSTUPDATE = opcodes.OP_2
OP_NAME_UPDATE = opcodes.OP_3

7 changes: 7 additions & 0 deletions electrum_nmc/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ def get_address_from_output_script(_bytes, *, net=None):
except MalformedBitcoinScript:
decoded = None

# Strip the name prefix if one is present.
decoded = split_name_script(decoded)["address_scriptPubKey"]

# The Genesis Block, self-payments, and pay-by-IP-address payments look like:
# 65 BYTES:... CHECKSIG
match = [ opcodes.OP_PUSHDATA4, opcodes.OP_CHECKSIG ]
Expand Down Expand Up @@ -1285,3 +1288,7 @@ def tx_from_str(txt):
tx_dict = json.loads(str(txt))
assert "hex" in tx_dict.keys()
return tx_dict["hex"]


from .names import split_name_script

0 comments on commit 5fc9e66

Please sign in to comment.