Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from base58 import b58encode, b58decode
- crypto_data = {
- 'btc': {'bip44_coin_type': 0x80000000},
- 'ltc': {'bip44_coin_type': 0x80000002},
- 'doge': {'bip44_coin_type': 0x80000003},
- 'bch': {'bip44_coin_type': 0x80000091},
- 'uno': {'bip44_coin_type': 0x8000005c},
- 'eth': {'bip44_coin_type': 0x8000003c},
- 'etc': {'bip44_coin_type': 0x8000003d},
- 'dash': {'bip44_coin_type': 0x80000005},
- 'zec': {'bip44_coin_type': 0x80000085},
- 'bcd': {'bip44_coin_type': 0x800003e7},
- }
- primes = [
- 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
- 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
- 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233,
- 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
- 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
- 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
- 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
- 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
- 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811,
- 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
- 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
- ]
- def py2_from_bytes(data, big_endian = False):
- if isinstance(data, str):
- data = bytearray(data)
- if big_endian:
- data = reversed(data)
- num = 0
- for offset, byte in enumerate(data):
- num += byte << (offset * 8)
- return num
- def to_bytes(number):
- try:
- # Python 3
- return number.to_bytes((number.bit_length() + 7) // 8, 'little')
- except AttributeError:
- # Python 2
- h = '%x' % number
- s = ('0'*(len(h) % 2) + h).zfill(2).decode('hex')
- return s[::-1]
- def to_number(bytez):
- try:
- # python 3
- return int.from_bytes(bytez, 'little')
- except AttributeError:
- # python 2
- return py2_from_bytes(bytez)
- def get_currency_by_order(order):
- bip44 = order + 0x80000000
- for currency, data in crypto_data.items():
- if data['bip44_coin_type'] == bip44:
- return currency
- raise Exception("Currency order %s not found" % order)
- def get_currency_order(currency):
- try:
- curr = crypto_data[currency]
- except KeyError:
- raise Exception("%s Not Supported" % currency)
- return curr['bip44_coin_type'] - 0x80000000
- def encode(currency_list):
- token = 1
- for currency in currency_list:
- order = get_currency_order(currency)
- #print "adding:", currency, "order:", order, "prime:", primes[order]
- token *= primes[order]
- #print "final product:", token
- return b58encode(to_bytes(token))
- def decode(token):
- token = to_number(b58decode(token))
- currencies = []
- for i, p in enumerate(primes):
- if token % p == 0:
- currencies.append(get_currency_by_order(i))
- return sorted(currencies)
- if __name__ == "__main__":
- cases = [
- ['ltc', 'btc', 'zec', 'doge', 'eth', 'bch', 'uno'],
- ['ltc', 'btc'],
- ['btc', 'bch'],
- ['dash', 'eth', 'bcd']
- ]
- for i, case in enumerate(cases):
- case = sorted(case)
- encoded = encode(case)
- result = sorted(decode(encoded))
- print("Case %s" % i, end=' ')
- if result == case:
- print("Passed", result, str(encoded))
- else:
- print("Failed. Decode returned %s expected %s" % (result, case))
- print("--------")
Add Comment
Please, Sign In to add comment