1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-26 08:08:51 +00:00
trezor-firmware/defs/coins/tools/build_coins.py

169 lines
5.5 KiB
Python
Raw Normal View History

2018-04-23 20:15:58 +00:00
#!/usr/bin/env python3
import json
import glob
import re
from hashlib import sha256
2018-04-28 18:14:46 +00:00
from binascii import unhexlify
2018-04-28 20:00:06 +00:00
import ed25519
from PIL import Image
2018-04-28 18:14:46 +00:00
from trezorlib.protobuf import dump_message
from coindef import CoinDef
2018-04-23 20:15:58 +00:00
def check_type(val, types, nullable=False, empty=False, regex=None, choice=None):
2018-04-23 20:15:58 +00:00
# check nullable
if nullable and val is None:
return True
# check empty
try:
if not empty and len(val) == 0:
return False
except TypeError:
pass
# check regex
if regex is not None:
if types is not str:
return False
m = re.match(regex, val)
2018-04-24 14:03:40 +00:00
if not m:
2018-04-23 20:15:58 +00:00
return False
# check choice
if choice is not None:
if val not in choice:
return False
2018-04-23 20:15:58 +00:00
# check type
if isinstance(types, list):
return True in [isinstance(val, t) for t in types]
else:
return isinstance(val, types)
def validate_coin(coin):
2018-04-24 14:03:40 +00:00
assert check_type(coin['coin_name'], str, regex=r'^[A-Z]')
assert check_type(coin['coin_shortcut'], str, regex=r'^[A-Zt][A-Z][A-Z]+$')
assert check_type(coin['coin_label'], str, regex=r'^[A-Z]')
assert check_type(coin['website'], str, regex=r'^http.*[^/]$')
assert check_type(coin['github'], str, regex=r'^https://github.com/.*[^/]$')
2018-04-23 20:15:58 +00:00
assert check_type(coin['maintainer'], str)
assert check_type(coin['curve_name'], str, choice=['secp256k1', 'secp256k1_decred', 'secp256k1_groestl'])
2018-04-23 20:15:58 +00:00
assert check_type(coin['address_type'], int)
assert check_type(coin['address_type_p2sh'], int)
assert coin['address_type'] != coin['address_type_p2sh']
assert check_type(coin['maxfee_kb'], int)
assert check_type(coin['minfee_kb'], int)
assert coin['maxfee_kb'] >= coin['minfee_kb']
2018-04-24 14:03:40 +00:00
assert check_type(coin['hash_genesis_block'], str, regex=r'^[0-9a-f]{64}$')
2018-04-28 18:14:46 +00:00
assert check_type(coin['xprv_magic'], int)
assert check_type(coin['xpub_magic'], int)
assert check_type(coin['xpub_magic_segwit_p2sh'], int, nullable=True)
assert check_type(coin['xpub_magic_segwit_native'], int, nullable=True)
assert coin['xprv_magic'] != coin['xpub_magic']
assert coin['xprv_magic'] != coin['xpub_magic_segwit_p2sh']
2018-04-25 18:12:42 +00:00
assert coin['xprv_magic'] != coin['xpub_magic_segwit_native']
assert coin['xpub_magic'] != coin['xpub_magic_segwit_p2sh']
2018-04-25 18:12:42 +00:00
assert coin['xpub_magic'] != coin['xpub_magic_segwit_native']
assert coin['xpub_magic_segwit_p2sh'] is None or coin['xpub_magic_segwit_native'] is None or coin['xpub_magic_segwit_p2sh'] != coin['xpub_magic_segwit_native']
2018-04-23 20:15:58 +00:00
assert check_type(coin['slip44'], int)
assert check_type(coin['segwit'], bool)
assert check_type(coin['decred'], bool)
assert check_type(coin['fork_id'], int, nullable=True)
2018-04-24 14:03:40 +00:00
assert check_type(coin['force_bip143'], bool)
assert check_type(coin['version_group_id'], int, nullable=True)
2018-04-23 20:15:58 +00:00
assert check_type(coin['default_fee_b'], dict)
assert check_type(coin['dust_limit'], int)
2018-04-24 14:03:40 +00:00
assert check_type(coin['blocktime_seconds'], int)
2018-04-23 20:15:58 +00:00
assert check_type(coin['signed_message_header'], str)
2018-04-24 14:03:40 +00:00
assert check_type(coin['address_prefix'], str, regex=r'^.*:$')
2018-04-23 20:15:58 +00:00
assert check_type(coin['min_address_length'], int)
assert check_type(coin['max_address_length'], int)
assert coin['max_address_length'] >= coin['min_address_length']
2018-04-23 20:15:58 +00:00
assert check_type(coin['bech32_prefix'], str, nullable=True)
assert check_type(coin['cashaddr_prefix'], str, nullable=True)
assert check_type(coin['bitcore'], list, empty=True)
for bc in coin['bitcore']:
assert not bc.endswith('/')
2018-04-23 20:15:58 +00:00
2018-04-28 20:00:06 +00:00
def validate_icon(icon):
assert icon.size == (96, 96)
assert icon.mode == 'RGBA'
2018-04-28 18:14:46 +00:00
class Writer:
def __init__(self):
self.buf = bytearray()
def write(self, buf):
self.buf.extend(buf)
2018-04-28 20:00:06 +00:00
def serialize(coin, icon):
2018-04-28 18:14:46 +00:00
c = dict(coin)
c['signed_message_header'] = c['signed_message_header'].encode()
c['hash_genesis_block'] = unhexlify(c['hash_genesis_block'])
2018-04-28 20:00:06 +00:00
c['icon'] = icon
2018-04-28 18:14:46 +00:00
msg = CoinDef(**c)
w = Writer()
dump_message(w, msg)
return bytes(w.buf)
def sign(data):
h = sha256(data).digest()
sign_key = ed25519.SigningKey(b'A' * 32)
return sign_key.sign(h)
2018-04-28 20:00:06 +00:00
# conversion copied from trezor-core/tools/png2toi
# TODO: extract into common module in python-trezor
def convert_icon(icon):
import struct
import zlib
w, h = 32, 32
icon = icon.resize((w, h), Image.LANCZOS)
# remove alpha channel, replace with black
bg = Image.new('RGBA', icon.size, (0, 0, 0, 255))
icon = Image.alpha_composite(bg, icon)
# process pixels
pix = icon.load()
data = bytes()
for j in range(h):
for i in range(w):
r, g, b, _ = pix[i, j]
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
data += struct.pack('>H', c)
z = zlib.compressobj(level=9, wbits=10)
zdata = z.compress(data) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
return zdata
2018-04-23 20:15:58 +00:00
def process_json(fn):
2018-04-24 14:03:40 +00:00
print(fn, end=' ... ')
2018-04-23 20:15:58 +00:00
j = json.load(open(fn))
2018-04-28 20:00:06 +00:00
i = Image.open(fn.replace('.json', '.png'))
2018-04-23 20:15:58 +00:00
validate_coin(j)
2018-04-28 20:00:06 +00:00
validate_icon(i)
ser = serialize(j, convert_icon(i))
sig = sign(ser)
definition = (sig + ser).hex()
2018-04-24 14:03:40 +00:00
print('OK')
return j, definition
2018-04-23 20:15:58 +00:00
coins = {}
defs = {}
2018-04-28 18:14:46 +00:00
for fn in glob.glob('../*.json'):
c, d = process_json(fn)
2018-04-23 20:15:58 +00:00
n = c['coin_name']
coins[n] = c
defs[n] = d
2018-04-23 20:15:58 +00:00
2018-04-28 18:14:46 +00:00
json.dump(coins, open('../../coins.json', 'w'), indent=4, sort_keys=True)
json.dump(defs, open('../../coindefs.json', 'w'), indent=4, sort_keys=True)