From d522f211fd95742d2d65d2bd2683734096d2057d Mon Sep 17 00:00:00 2001
From: Pavol Rusnak <stick@gk2.sk>
Date: Tue, 3 Jul 2018 12:23:52 +0200
Subject: [PATCH] defs: update backend urls, add backend check to
 build_coins.py

---
 defs/coins/bcash.json               | 10 +++++-----
 defs/coins/bgold.json               | 10 +++++-----
 defs/coins/bitcoin.json             | 10 +++++-----
 defs/coins/bitcore.json             |  2 +-
 defs/coins/bprivate.json            |  2 +-
 defs/coins/crown.json               |  4 ++--
 defs/coins/dash.json                | 10 +++++-----
 defs/coins/decred.json              |  2 +-
 defs/coins/decred_testnet.json      |  2 +-
 defs/coins/digibyte.json            |  2 +-
 defs/coins/dogecoin.json            | 10 +++++-----
 defs/coins/flashcoin.json           |  2 +-
 defs/coins/fujicoin.json            |  2 +-
 defs/coins/groestlcoin.json         |  2 +-
 defs/coins/groestlcoin_testnet.json |  2 +-
 defs/coins/koto.json                |  2 +-
 defs/coins/litecoin.json            | 10 +++++-----
 defs/coins/litecoin_testnet.json    |  2 +-
 defs/coins/monacoin.json            |  6 ++++--
 defs/coins/pesetacoin.json          |  2 +-
 defs/coins/terracoin.json           |  2 +-
 defs/coins/vertcoin.json            | 10 +++++-----
 defs/coins/viacoin.json             |  2 +-
 defs/coins/zcash.json               | 10 +++++-----
 defs/coins/zcash_testnet.json       |  2 +-
 defs/coins/zcoin.json               |  2 +-
 defs/coins/zencash.json             |  2 +-
 tools/build_coins.py                | 20 +++++++++++++++++---
 28 files changed, 80 insertions(+), 64 deletions(-)

diff --git a/defs/coins/bcash.json b/defs/coins/bcash.json
index 38c1fcb43d..5577863fb5 100644
--- a/defs/coins/bcash.json
+++ b/defs/coins/bcash.json
@@ -38,10 +38,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://bch1.trezor.io",
-    "https://bch2.trezor.io",
-    "https://bch3.trezor.io",
-    "https://bch4.trezor.io",
-    "https://bch5.trezor.io"
+    "https://bch1.trezor.io/api",
+    "https://bch2.trezor.io/api",
+    "https://bch3.trezor.io/api",
+    "https://bch4.trezor.io/api",
+    "https://bch5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/bgold.json b/defs/coins/bgold.json
index 83608398ba..c275002c5d 100644
--- a/defs/coins/bgold.json
+++ b/defs/coins/bgold.json
@@ -38,10 +38,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://btg1.trezor.io",
-    "https://btg2.trezor.io",
-    "https://btg3.trezor.io",
-    "https://btg4.trezor.io",
-    "https://btg5.trezor.io"
+    "https://btg1.trezor.io/api",
+    "https://btg2.trezor.io/api",
+    "https://btg3.trezor.io/api",
+    "https://btg4.trezor.io/api",
+    "https://btg5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/bitcoin.json b/defs/coins/bitcoin.json
index 8957df9b8e..a847f70e39 100644
--- a/defs/coins/bitcoin.json
+++ b/defs/coins/bitcoin.json
@@ -38,10 +38,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://btc1.trezor.io",
-    "https://btc2.trezor.io",
-    "https://btc3.trezor.io",
-    "https://btc4.trezor.io",
-    "https://btc5.trezor.io"
+    "https://btc1.trezor.io/api",
+    "https://btc2.trezor.io/api",
+    "https://btc3.trezor.io/api",
+    "https://btc4.trezor.io/api",
+    "https://btc5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/bitcore.json b/defs/coins/bitcore.json
index 10779b8733..55ad1bd5fa 100644
--- a/defs/coins/bitcore.json
+++ b/defs/coins/bitcore.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://insight.bitcore.cc"
+    "https://insight.bitcore.cc/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/bprivate.json b/defs/coins/bprivate.json
index 6f217aec58..ac577afa5e 100644
--- a/defs/coins/bprivate.json
+++ b/defs/coins/bprivate.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 95,
   "bitcore": [
-    "https://explorer.btcprivate.org"
+    "https://explorer.btcprivate.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/crown.json b/defs/coins/crown.json
index 4016fb701e..03ae5dfa74 100644
--- a/defs/coins/crown.json
+++ b/defs/coins/crown.json
@@ -34,8 +34,8 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://crw-bitcore.crown.tech",
-    "https://crw2-bitcore.crown.tech"
+    "https://crw-bitcore.crown.tech/api",
+    "https://crw2-bitcore.crown.tech/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/dash.json b/defs/coins/dash.json
index 0ade1f60ff..da9ecf0ea5 100644
--- a/defs/coins/dash.json
+++ b/defs/coins/dash.json
@@ -35,10 +35,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://dash1.trezor.io",
-    "https://dash2.trezor.io",
-    "https://dash3.trezor.io",
-    "https://dash4.trezor.io",
-    "https://dash5.trezor.io"
+    "https://dash1.trezor.io/api",
+    "https://dash2.trezor.io/api",
+    "https://dash3.trezor.io/api",
+    "https://dash4.trezor.io/api",
+    "https://dash5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/decred.json b/defs/coins/decred.json
index 3bb2837dc2..48690e10cd 100644
--- a/defs/coins/decred.json
+++ b/defs/coins/decred.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 35,
   "bitcore": [
-    "https://mainnet.decred.org"
+    "https://mainnet.decred.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/decred_testnet.json b/defs/coins/decred_testnet.json
index 1774fee334..cff5edf0ad 100644
--- a/defs/coins/decred_testnet.json
+++ b/defs/coins/decred_testnet.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 35,
   "bitcore": [
-    "https://testnet.decred.org"
+    "https://testnet.decred.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/digibyte.json b/defs/coins/digibyte.json
index 152bebc32b..e2f98a968c 100644
--- a/defs/coins/digibyte.json
+++ b/defs/coins/digibyte.json
@@ -37,7 +37,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://explorer.digibyteprojects.com"
+    "https://explorer.digibyteprojects.com/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/dogecoin.json b/defs/coins/dogecoin.json
index 640b0d2793..b7646dda37 100644
--- a/defs/coins/dogecoin.json
+++ b/defs/coins/dogecoin.json
@@ -35,10 +35,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://doge1.trezor.io",
-    "https://doge2.trezor.io",
-    "https://doge3.trezor.io",
-    "https://doge4.trezor.io",
-    "https://doge5.trezor.io"
+    "https://doge1.trezor.io/api",
+    "https://doge2.trezor.io/api",
+    "https://doge3.trezor.io/api",
+    "https://doge4.trezor.io/api",
+    "https://doge5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/flashcoin.json b/defs/coins/flashcoin.json
index 5d770505ef..76c65911c6 100644
--- a/defs/coins/flashcoin.json
+++ b/defs/coins/flashcoin.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://explorer.flashcoin.io"
+    "https://explorer.flashcoin.io/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/fujicoin.json b/defs/coins/fujicoin.json
index ab48668b56..b8725a2e5f 100644
--- a/defs/coins/fujicoin.json
+++ b/defs/coins/fujicoin.json
@@ -37,7 +37,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "http://explorer.fujicoin.org"
+    "http://explorer.fujicoin.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/groestlcoin.json b/defs/coins/groestlcoin.json
index ca5f2adf6b..4cc925414c 100644
--- a/defs/coins/groestlcoin.json
+++ b/defs/coins/groestlcoin.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://groestlsight.groestlcoin.org"
+    "https://groestlsight.groestlcoin.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/groestlcoin_testnet.json b/defs/coins/groestlcoin_testnet.json
index ee711d6b8d..da572ffa3b 100644
--- a/defs/coins/groestlcoin_testnet.json
+++ b/defs/coins/groestlcoin_testnet.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://groestlsight-test.groestlcoin.org"
+    "https://groestlsight-test.groestlcoin.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/koto.json b/defs/coins/koto.json
index 9297916587..913719f38c 100644
--- a/defs/coins/koto.json
+++ b/defs/coins/koto.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 95,
   "bitcore": [
-    "https://insight.kotocoin.info"
+    "https://insight.kotocoin.info/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/litecoin.json b/defs/coins/litecoin.json
index fc85ee1fb5..fa1683f70d 100644
--- a/defs/coins/litecoin.json
+++ b/defs/coins/litecoin.json
@@ -35,10 +35,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://ltc1.trezor.io",
-    "https://ltc2.trezor.io",
-    "https://ltc3.trezor.io",
-    "https://ltc4.trezor.io",
-    "https://ltc5.trezor.io"
+    "https://ltc1.trezor.io/api",
+    "https://ltc2.trezor.io/api",
+    "https://ltc3.trezor.io/api",
+    "https://ltc4.trezor.io/api",
+    "https://ltc5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/litecoin_testnet.json b/defs/coins/litecoin_testnet.json
index 5ac56f2b65..15d252c581 100644
--- a/defs/coins/litecoin_testnet.json
+++ b/defs/coins/litecoin_testnet.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://testnet.litecore.io"
+    "https://testnet.litecore.io/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/monacoin.json b/defs/coins/monacoin.json
index e1adde3e4a..a5ebdadcb7 100644
--- a/defs/coins/monacoin.json
+++ b/defs/coins/monacoin.json
@@ -26,7 +26,7 @@
   "bip115": false,
   "version_group_id": null,
   "default_fee_b": {
-    "Normal": 100000
+    "Normal": 1000
   },
   "dust_limit": 54600,
   "blocktime_seconds": 90,
@@ -34,7 +34,9 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://mona.chainsight.info"
+    "https://mona.chainsight.info/api",
+    "https://mona.insight.monaco-ex.org/insight-api-monacoin",
+    "https://insight.electrum-mona.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/pesetacoin.json b/defs/coins/pesetacoin.json
index d73016b851..42891fa6ce 100644
--- a/defs/coins/pesetacoin.json
+++ b/defs/coins/pesetacoin.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "http://live.pesetacoin.info"
+    "http://live.pesetacoin.info/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/terracoin.json b/defs/coins/terracoin.json
index c8e681b11e..824ef927e3 100644
--- a/defs/coins/terracoin.json
+++ b/defs/coins/terracoin.json
@@ -34,7 +34,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://insight.terracoin.io"
+    "https://insight.terracoin.io/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/vertcoin.json b/defs/coins/vertcoin.json
index f03835bed9..bc23403aa2 100644
--- a/defs/coins/vertcoin.json
+++ b/defs/coins/vertcoin.json
@@ -35,10 +35,10 @@
   "max_address_length": 34,
   "bitcore": [],
   "blockbook": [
-    "https://vtc1.trezor.io",
-    "https://vtc2.trezor.io",
-    "https://vtc3.trezor.io",
-    "https://vtc4.trezor.io",
-    "https://vtc5.trezor.io"
+    "https://vtc1.trezor.io/api",
+    "https://vtc2.trezor.io/api",
+    "https://vtc3.trezor.io/api",
+    "https://vtc4.trezor.io/api",
+    "https://vtc5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/viacoin.json b/defs/coins/viacoin.json
index 096f0c1da9..73744e9bf2 100644
--- a/defs/coins/viacoin.json
+++ b/defs/coins/viacoin.json
@@ -37,7 +37,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://explorer.viacoin.org"
+    "https://explorer.viacoin.org/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/zcash.json b/defs/coins/zcash.json
index ab8ead5671..ae7b08e3f2 100644
--- a/defs/coins/zcash.json
+++ b/defs/coins/zcash.json
@@ -35,10 +35,10 @@
   "max_address_length": 95,
   "bitcore": [],
   "blockbook": [
-    "https://zec1.trezor.io",
-    "https://zec2.trezor.io",
-    "https://zec3.trezor.io",
-    "https://zec4.trezor.io",
-    "https://zec5.trezor.io"
+    "https://zec1.trezor.io/api",
+    "https://zec2.trezor.io/api",
+    "https://zec3.trezor.io/api",
+    "https://zec4.trezor.io/api",
+    "https://zec5.trezor.io/api"
   ]
 }
diff --git a/defs/coins/zcash_testnet.json b/defs/coins/zcash_testnet.json
index b79d471305..ca07cfe66f 100644
--- a/defs/coins/zcash_testnet.json
+++ b/defs/coins/zcash_testnet.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 95,
   "bitcore": [
-    "https://explorer.testnet.z.cash"
+    "https://explorer.testnet.z.cash/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/zcoin.json b/defs/coins/zcoin.json
index f9e2f817ce..ac3347a155 100644
--- a/defs/coins/zcoin.json
+++ b/defs/coins/zcoin.json
@@ -37,7 +37,7 @@
   "min_address_length": 27,
   "max_address_length": 34,
   "bitcore": [
-    "https://insight.zcoin.io"
+    "https://insight.zcoin.io/api"
   ],
   "blockbook": []
 }
diff --git a/defs/coins/zencash.json b/defs/coins/zencash.json
index 5fe36d242b..c62eac47e4 100644
--- a/defs/coins/zencash.json
+++ b/defs/coins/zencash.json
@@ -34,7 +34,7 @@
   "min_address_length": 35,
   "max_address_length": 95,
   "bitcore": [
-    "https://explorer.zen-solutions.io"
+    "https://explorer.zen-solutions.io/api"
   ],
   "blockbook": []
 }
diff --git a/tools/build_coins.py b/tools/build_coins.py
index 78723551cf..858a058f05 100755
--- a/tools/build_coins.py
+++ b/tools/build_coins.py
@@ -16,7 +16,9 @@ import re
 import os
 import sys
 
-if '--defs' in sys.argv:
+
+BUILD_DEFS = '--defs' in sys.argv
+if BUILD_DEFS:
     from binascii import unhexlify
     from hashlib import sha256
     import ed25519
@@ -24,8 +26,10 @@ if '--defs' in sys.argv:
     from trezorlib.protobuf import dump_message
     from coindef import CoinDef
     BUILD_DEFS = True
-else:
-    BUILD_DEFS = False
+
+TEST_BACKEND = '--test-backend' in sys.argv
+if TEST_BACKEND:
+    import requests
 
 
 def check_type(val, types, nullable=False, empty=False, regex=None, choice=None):  # noqa:E501
@@ -56,6 +60,12 @@ def check_type(val, types, nullable=False, empty=False, regex=None, choice=None)
         return isinstance(val, types)
 
 
+def get_hash_genesis_block(api):
+    r = requests.get(api + '/block-index/0')
+    j = r.json()
+    return j['blockHash']
+
+
 def validate_coin(coin):
     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]+$')
@@ -101,9 +111,13 @@ def validate_coin(coin):
     assert check_type(coin['bitcore'], list, empty=True)
     for bc in coin['bitcore']:
         assert not bc.endswith('/')
+        if TEST_BACKEND:
+            assert get_hash_genesis_block(bc) == coin['hash_genesis_block']
     assert check_type(coin['blockbook'], list, empty=True)
     for bb in coin['blockbook']:
         assert not bb.endswith('/')
+        if TEST_BACKEND:
+            assert get_hash_genesis_block(bb) == coin['hash_genesis_block']
 
 
 def validate_icon(icon):