Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
np-utils scripthash endianness fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Aug 14, 2018
1 parent e7e463d commit 3876c83
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Expand Up @@ -2,6 +2,12 @@
History
=======

0.4.12-dev (in progress)
------------------------
* `np-utils --address-to-scripthash` outputs now little-endian and big-endian scripthashes
* `np-utils --scripthash-to-address` detects input endianness and converts accordingly


0.4.11 (2018-07-05)
-------------------
* Added ``Size()`` method to `ECPoint` and `Fixed8` class.
Expand Down
13 changes: 13 additions & 0 deletions README.rst
Expand Up @@ -36,6 +36,19 @@ Library for working with NEO related data in Python, without database dependenci
"address": "AHVvg26CNz1vxteJfeHy4R8P4VN8SydCM6"
}
$ np-utils --address-to-scripthash AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y
Scripthash big endian: 0xe9eed8dc39332032dc22e5d6e86332c50327ba23
Scripthash little endian: 23ba2703c53263e8d6e522dc32203339dcd8eee9
Scripthash neo-python format: b'#\xba\'\x03\xc52c\xe8\xd6\xe5"\xdc2 39\xdc\xd8\xee\xe9'
$ np-utils --scripthash-to-address 0xe9eed8dc39332032dc22e5d6e86332c50327ba23
AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y
$ np-utils --scripthash-to-address 23ba2703c53263e8d6e522dc32203339dcd8eee9
Detected little endian scripthash. Converting to big endian for internal use.
Big endian scripthash: 0xe9eed8dc39332032dc22e5d6e86332c50327ba23
AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y
Getting started
---------------
Expand Down
12 changes: 11 additions & 1 deletion neocore/bin/cli.py
Expand Up @@ -7,6 +7,7 @@
import base58
import hashlib
import json
import binascii

from Crypto import Random

Expand Down Expand Up @@ -39,6 +40,12 @@ def address_to_scripthash(address):

def scripthash_to_address(scripthash):
try:
if scripthash[0:2] != "0x":
# litle endian. convert to big endian now.
print("Detected little endian scripthash. Converting to big endian for internal use.")
scripthash_bytes = binascii.unhexlify(scripthash)
scripthash = "0x%s" % binascii.hexlify(scripthash_bytes[::-1]).decode("utf-8")
print("Big endian scripthash:", scripthash)
return Crypto.ToAddress(UInt160.ParseString(scripthash))
except Exception as e:
raise ConversionError("Wrong format")
Expand Down Expand Up @@ -77,7 +84,10 @@ def main():
if args.address_to_scripthash:
try:
scripthash = address_to_scripthash(args.address_to_scripthash[0])
print('Script Hash: 0x{} -- Python format: {!r}'.format(scripthash[::-1].hex(), scripthash))
print("Scripthash big endian: 0x{}".format(scripthash[::-1].hex()))
print("Scripthash little endian: {}".format(scripthash.hex(), scripthash))
print("Scripthash neo-python format: {!r}".format(scripthash))

except ConversionError as e:
print(e)
exit(1)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Expand Up @@ -18,7 +18,7 @@ def test_scripthash_to_address(self):
address = cli.scripthash_to_address(scripthash)
self.assertEqual(address, "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y")

scripthash = "e9eed8dc39332032dc22e5d6e86332c50327ba23"
scripthash = "23ba2703c53263e8d6e522dc32203339dcd8eee9"
address = cli.scripthash_to_address(scripthash)
self.assertEqual(address, "AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y")

Expand Down

0 comments on commit 3876c83

Please sign in to comment.