1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

defs: move tools to def/coins/tools

This commit is contained in:
Pavol Rusnak 2018-04-28 19:14:46 +01:00
parent 7f2f37c62e
commit 81ca42a749
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
5 changed files with 122 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

@ -13,7 +13,7 @@ python:
script:
- python coins-check.py
- cd defs/coins && python combine.py
- cd defs/coins/tools && python build_coins.py
notifications:
webhooks:

View File

@ -5,6 +5,10 @@ import glob
import re
from hashlib import sha256
import ed25519
from binascii import unhexlify
from trezorlib.protobuf import dump_message
from coindef import CoinDef
def check_type(val, types, nullable=False, empty=False, regex=None, choice=None):
@ -50,10 +54,10 @@ def validate_coin(coin):
assert check_type(coin['minfee_kb'], int)
assert coin['maxfee_kb'] >= coin['minfee_kb']
assert check_type(coin['hash_genesis_block'], str, regex=r'^[0-9a-f]{64}$')
assert check_type(coin['xprv_magic'], str, regex=r'^[0-9a-f]{8}$')
assert check_type(coin['xpub_magic'], str, regex=r'^[0-9a-f]{8}$')
assert check_type(coin['xpub_magic_segwit_p2sh'], str, regex=r'^[0-9a-f]{8}$', nullable=True)
assert check_type(coin['xpub_magic_segwit_native'], str, regex=r'^[0-9a-f]{8}$', nullable=True)
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']
assert coin['xprv_magic'] != coin['xpub_magic_segwit_native']
@ -80,9 +84,23 @@ def validate_coin(coin):
assert not bc.endswith('/')
class Writer:
def __init__(self):
self.buf = bytearray()
def write(self, buf):
self.buf.extend(buf)
def serialize(coin):
# TODO: replace with protobuf serialization
return json.dumps(coin).encode()
c = dict(coin)
c['signed_message_header'] = c['signed_message_header'].encode()
c['hash_genesis_block'] = unhexlify(c['hash_genesis_block'])
msg = CoinDef(**c)
w = Writer()
dump_message(w, msg)
return bytes(w.buf)
def sign(data):
@ -104,12 +122,12 @@ def process_json(fn):
coins = {}
defs = {}
for fn in glob.glob('*.json'):
for fn in glob.glob('../*.json'):
c, d = process_json(fn)
n = c['coin_name']
coins[n] = c
defs[n] = d
json.dump(coins, open('../coins.json', 'w'), indent=4, sort_keys=True)
json.dump(defs, open('../coindefs.json', 'w'), indent=4, sort_keys=True)
json.dump(coins, open('../../coins.json', 'w'), indent=4, sort_keys=True)
json.dump(defs, open('../../coindefs.json', 'w'), indent=4, sort_keys=True)

View File

@ -1,5 +1,6 @@
syntax = "proto2";
message CoinDef {
{
optional string coin_name = 1;
optional string coin_shortcut = 2;
optional string coin_label = 3;

View File

@ -0,0 +1,91 @@
# Automatically generated by pb2py
from trezorlib import protobuf as p
class CoinDef(p.MessageType):
FIELDS = {
1: ('coin_name', p.UnicodeType, 0),
2: ('coin_shortcut', p.UnicodeType, 0),
3: ('coin_label', p.UnicodeType, 0),
4: ('curve_name', p.UnicodeType, 0),
5: ('address_type', p.UVarintType, 0),
6: ('address_type_p2sh', p.UVarintType, 0),
7: ('maxfee_kb', p.UVarintType, 0),
8: ('minfee_kb', p.UVarintType, 0),
9: ('signed_message_header', p.BytesType, 0),
10: ('hash_genesis_block', p.BytesType, 0),
11: ('xprv_magic', p.UVarintType, 0),
12: ('xpub_magic', p.UVarintType, 0),
13: ('xpub_magic_segwit_p2sh', p.UVarintType, 0),
14: ('xpub_magic_segwit_native', p.UVarintType, 0),
15: ('bech32_prefix', p.UnicodeType, 0),
16: ('cashaddr_prefix', p.UnicodeType, 0),
17: ('slip44', p.UVarintType, 0),
18: ('segwit', p.BoolType, 0),
19: ('decred', p.BoolType, 0),
20: ('fork_id', p.UVarintType, 0),
21: ('force_bip143', p.BoolType, 0),
22: ('dust_limit', p.UVarintType, 0),
23: ('address_prefix', p.UnicodeType, 0),
24: ('min_address_length', p.UVarintType, 0),
25: ('max_address_length', p.UVarintType, 0),
26: ('icon', p.BytesType, 0),
}
def __init__(
self,
coin_name: str = None,
coin_shortcut: str = None,
coin_label: str = None,
curve_name: str = None,
address_type: int = None,
address_type_p2sh: int = None,
maxfee_kb: int = None,
minfee_kb: int = None,
signed_message_header: bytes = None,
hash_genesis_block: bytes = None,
xprv_magic: int = None,
xpub_magic: int = None,
xpub_magic_segwit_p2sh: int = None,
xpub_magic_segwit_native: int = None,
bech32_prefix: str = None,
cashaddr_prefix: str = None,
slip44: int = None,
segwit: bool = None,
decred: bool = None,
fork_id: int = None,
force_bip143: bool = None,
dust_limit: int = None,
address_prefix: str = None,
min_address_length: int = None,
max_address_length: int = None,
icon: bytes = None,
**kwargs,
):
self.coin_name = coin_name
self.coin_shortcut = coin_shortcut
self.coin_label = coin_label
self.curve_name = curve_name
self.address_type = address_type
self.address_type_p2sh = address_type_p2sh
self.maxfee_kb = maxfee_kb
self.minfee_kb = minfee_kb
self.signed_message_header = signed_message_header
self.hash_genesis_block = hash_genesis_block
self.xprv_magic = xprv_magic
self.xpub_magic = xpub_magic
self.xpub_magic_segwit_p2sh = xpub_magic_segwit_p2sh
self.xpub_magic_segwit_native = xpub_magic_segwit_native
self.bech32_prefix = bech32_prefix
self.cashaddr_prefix = cashaddr_prefix
self.slip44 = slip44
self.segwit = segwit
self.decred = decred
self.fork_id = fork_id
self.force_bip143 = force_bip143
self.dust_limit = dust_limit
self.address_prefix = address_prefix
self.min_address_length = min_address_length
self.max_address_length = max_address_length
self.icon = icon
p.MessageType.__init__(self, **kwargs)