Skip to content

Instantly share code, notes, and snippets.

@ryancdotorg
Created December 24, 2014 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancdotorg/9005ce8298e9c1deac3f to your computer and use it in GitHub Desktop.
Save ryancdotorg/9005ce8298e9c1deac3f to your computer and use it in GitHub Desktop.
# need 'from bitcoin import *' in the git version of pybitcointools...
from pybitcointools import *
### OPCODES - just the ones we need
OP_NAME_UPDATE = 3
OP_2DROP = 109
OP_DROP = 117
OP_DUP = 118
OP_HASH160 = 169
OP_EQUALVERIFY = 136
OP_CHECKSIG = 172
def name_update_to_script(name, data, addr):
return serialize_script([
OP_NAME_UPDATE,
hexlify(name),
hexlify(data),
OP_2DROP, OP_DROP,
OP_DUP, OP_HASH160,
b58check_to_hex(addr),
OP_EQUALVERIFY, OP_CHECKSIG
])
def script_to_name_update(script):
# XXX for now be strict about format
opcodes = deserialize_script(script)
name = unhexlify(opcodes[1])
data = unhexlify(opcodes[2])
addr = hex_to_b58check(opcodes[7])
if script == name_update_to_script(name, data, addr):
return AttrDict({'name':name, 'data':data, 'address':addr})
else:
raise ValueError("Malformed NAME_UPDATE script.")
## rpc via bitcoinrpc - it works out of the box with namecoin
def get_last_output(name):
history = filter(lambda x: 'expired' not in x, _nt['rpc'].name_history(name))
txid = sorted(history, key=lambda x: x['expires_in'], reverse=True)[0]['txid']
# we still need the vout
for output in _nt['rpc'].getrawtransaction(txid, 1)['vout']:
if 'nameOp' in output['scriptPubKey']:
if output['scriptPubKey']['nameOp']['name'] == name:
return AttrDict({
'txid': txid,
'vout': output['n'],
'amount': output['value']
})
raise ValueError("Could not find last output for '%s'!" % name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment