Guest User

Untitled

a guest
Dec 9th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. from __future__ import print_function
  2. from base58 import b58encode, b58decode
  3.  
  4. crypto_data = {
  5. 'btc': {'bip44_coin_type': 0x80000000},
  6. 'ltc': {'bip44_coin_type': 0x80000002},
  7. 'doge': {'bip44_coin_type': 0x80000003},
  8. 'bch': {'bip44_coin_type': 0x80000091},
  9. 'uno': {'bip44_coin_type': 0x8000005c},
  10. 'eth': {'bip44_coin_type': 0x8000003c},
  11. 'etc': {'bip44_coin_type': 0x8000003d},
  12. 'dash': {'bip44_coin_type': 0x80000005},
  13. 'zec': {'bip44_coin_type': 0x80000085},
  14. 'bcd': {'bip44_coin_type': 0x800003e7},
  15. }
  16.  
  17. primes = [
  18. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
  19. 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
  20. 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
  21. 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
  22. 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
  23. 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
  24. 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
  25. 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
  26. 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
  27. 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
  28. 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
  29. ]
  30.  
  31. def py2_from_bytes(data, big_endian = False):
  32. if isinstance(data, str):
  33. data = bytearray(data)
  34. if big_endian:
  35. data = reversed(data)
  36. num = 0
  37. for offset, byte in enumerate(data):
  38. num += byte << (offset * 8)
  39. return num
  40.  
  41. def to_bytes(number):
  42. try:
  43. # Python 3
  44. return number.to_bytes((number.bit_length() + 7) // 8, 'little')
  45. except AttributeError:
  46. # Python 2
  47. h = '%x' % number
  48. s = ('0'*(len(h) % 2) + h).zfill(2).decode('hex')
  49. return s[::-1]
  50.  
  51. def to_number(bytez):
  52. try:
  53. # python 3
  54. return int.from_bytes(bytez, 'little')
  55. except AttributeError:
  56. # python 2
  57. return py2_from_bytes(bytez)
  58.  
  59. def get_currency_by_order(order):
  60. bip44 = order + 0x80000000
  61. for currency, data in crypto_data.items():
  62. if data['bip44_coin_type'] == bip44:
  63. return currency
  64. raise Exception("Currency order %s not found" % order)
  65.  
  66. def get_currency_order(currency):
  67. try:
  68. curr = crypto_data[currency]
  69. except KeyError:
  70. raise Exception("%s Not Supported" % currency)
  71. return curr['bip44_coin_type'] - 0x80000000
  72.  
  73. def encode(currency_list):
  74. token = 1
  75. for currency in currency_list:
  76. order = get_currency_order(currency)
  77. #print "adding:", currency, "order:", order, "prime:", primes[order]
  78. token *= primes[order]
  79. #print "final product:", token
  80. return b58encode(to_bytes(token))
  81.  
  82. def decode(token):
  83. token = to_number(b58decode(token))
  84. currencies = []
  85. for i, p in enumerate(primes):
  86. if token % p == 0:
  87. currencies.append(get_currency_by_order(i))
  88. return sorted(currencies)
  89.  
  90. if __name__ == "__main__":
  91. cases = [
  92. ['ltc', 'btc', 'zec', 'doge', 'eth', 'bch', 'uno'],
  93. ['ltc', 'btc'],
  94. ['btc', 'bch'],
  95. ['dash', 'eth', 'bcd']
  96. ]
  97. for i, case in enumerate(cases):
  98. case = sorted(case)
  99. encoded = encode(case)
  100. result = sorted(decode(encoded))
  101. print("Case %s" % i, end=' ')
  102. if result == case:
  103. print("Passed", result, str(encoded))
  104. else:
  105. print("Failed. Decode returned %s expected %s" % (result, case))
  106.  
  107. print("--------")
Add Comment
Please, Sign In to add comment