From 34224748115fc221a0915e5ec3a4977496f0bf57 Mon Sep 17 00:00:00 2001 From: Tomas Susanka Date: Wed, 2 May 2018 16:47:24 +0200 Subject: [PATCH] nem: mosaics definitions are generated from trezor-common --- src/apps/nem/mosaic/helpers.py | 15 ++++++ .../mosaic/{definitions.py => nem_mosaics.py} | 31 ++++--------- src/apps/nem/transfer/layout.py | 2 +- tests/test_apps.nem.mosaic.py | 2 +- tools/codegen/gen_nem_mosaics.py | 46 +++++++++++++++++++ 5 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 src/apps/nem/mosaic/helpers.py rename src/apps/nem/mosaic/{definitions.py => nem_mosaics.py} (63%) create mode 100755 tools/codegen/gen_nem_mosaics.py diff --git a/src/apps/nem/mosaic/helpers.py b/src/apps/nem/mosaic/helpers.py new file mode 100644 index 0000000000..9f112b39e2 --- /dev/null +++ b/src/apps/nem/mosaic/helpers.py @@ -0,0 +1,15 @@ +from .nem_mosaics import mosaics + + +def get_mosaic_definition(namespace_name: str, mosaic_name: str, network: int): + for m in mosaics: + if namespace_name == m["namespace"] and mosaic_name == m["mosaic"]: + if ("networks" not in m) or (network in m["networks"]): + return m + return None + + +def is_nem_xem_mosaic(namespace_name: str, mosaic_name: str): + if namespace_name == "nem" and mosaic_name == "xem": + return True + return False diff --git a/src/apps/nem/mosaic/definitions.py b/src/apps/nem/mosaic/nem_mosaics.py similarity index 63% rename from src/apps/nem/mosaic/definitions.py rename to src/apps/nem/mosaic/nem_mosaics.py index c1daf9e3c5..94d70caaa2 100644 --- a/src/apps/nem/mosaic/definitions.py +++ b/src/apps/nem/mosaic/nem_mosaics.py @@ -1,19 +1,4 @@ -# todo move to common and generate via script - - -def get_mosaic_definition(namespace_name: str, mosaic_name: str, network: int): - for m in mosaics: - if namespace_name == m["namespace"] and mosaic_name == m["mosaic"]: - if ("networks" not in m) or (network in m["networks"]): - return m - return None - - -def is_nem_xem_mosaic(namespace_name: str, mosaic_name: str): - if namespace_name == "nem" and mosaic_name == "xem": - return True - return False - +# generated using gen_nem_mosaics.py from trezor-common nem_mosaics.json - do not edit directly! mosaics = [ { @@ -21,7 +6,7 @@ mosaics = [ "ticker": " XEM", "namespace": "nem", "mosaic": "xem", - "divisibility": 6 + "divisibility": 6, }, { "name": "DIMCOIN", @@ -33,7 +18,7 @@ mosaics = [ "fee": 10, "levy_namespace": "dim", "levy_mosaic": "coin", - "networks": [ 104 ] + "networks": [104], }, { "name": "DIM TOKEN", @@ -41,7 +26,7 @@ mosaics = [ "namespace": "dim", "mosaic": "token", "divisibility": 6, - "networks": [ 104 ] + "networks": [104], }, { "name": "Breeze Token", @@ -49,7 +34,7 @@ mosaics = [ "namespace": "breeze", "mosaic": "breeze-token", "divisibility": 0, - "networks": [ 104 ] + "networks": [104], }, { "name": "PacNEM Game Credits", @@ -57,7 +42,7 @@ mosaics = [ "namespace": "pacnem", "mosaic": "heart", "divisibility": 0, - "networks": [ 104 ] + "networks": [104], }, { "name": "PacNEM Score Tokens", @@ -69,6 +54,6 @@ mosaics = [ "fee": 100, "levy_namespace": "nem", "levy_mosaic": "xem", - "networks": [ 104 ] - } + "networks": [104], + }, ] diff --git a/src/apps/nem/transfer/layout.py b/src/apps/nem/transfer/layout.py index 7bf4f948dd..6b339989d1 100644 --- a/src/apps/nem/transfer/layout.py +++ b/src/apps/nem/transfer/layout.py @@ -1,5 +1,5 @@ from apps.nem.layout import * -from apps.nem.mosaic.definitions import * +from apps.nem.mosaic.helpers import * from trezor.messages import NEMImportanceTransferMode from trezor.messages import NEMTransfer from trezor.messages import NEMImportanceTransfer diff --git a/tests/test_apps.nem.mosaic.py b/tests/test_apps.nem.mosaic.py index dde6c6ec71..f17d9a046d 100644 --- a/tests/test_apps.nem.mosaic.py +++ b/tests/test_apps.nem.mosaic.py @@ -1,6 +1,6 @@ from common import * from apps.nem.transfer import * -from apps.nem.mosaic.definitions import get_mosaic_definition +from apps.nem.mosaic.helpers import get_mosaic_definition class TestNemMosaic(unittest.TestCase): diff --git a/tools/codegen/gen_nem_mosaics.py b/tools/codegen/gen_nem_mosaics.py new file mode 100755 index 0000000000..b24dfcd77f --- /dev/null +++ b/tools/codegen/gen_nem_mosaics.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import json + + +def format_str(value): + return '"' + value + '"' + + +def format_primitive(value): + if isinstance(value, int): + return value + elif isinstance(value, str): + return format_str(value) + elif isinstance(value, list): + return value + else: + raise TypeError + + +fields = [ + 'name', + 'ticker', + 'namespace', + 'mosaic', + 'divisibility', + 'levy', + 'fee', + 'levy_namespace', + 'levy_mosaic', + 'networks', +] + +mosaics = json.load(open('../../vendor/trezor-common/defs/nem/nem_mosaics.json', 'r')) + +print('# generated using gen_nem_mosaics.py from trezor-common nem_mosaics.json - do not edit directly!') +print('') +print('mosaics = [') +for m in mosaics: + print(' {') + for name in fields: + if name in m: + print(' %s: %s,' % (format_str(name), format_primitive(m[name]))) + # else: + # print(' %s: None,' % format_str(name)) + print(' },') +print(']')