diff options
Diffstat (limited to 'frozen_deps/mnemonic')
-rw-r--r-- | frozen_deps/mnemonic/__init__.py | 1 | ||||
-rw-r--r-- | frozen_deps/mnemonic/mnemonic.py | 291 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/chinese_simplified.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/chinese_traditional.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/english.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/french.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/italian.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/japanese.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/korean.txt | 2048 | ||||
-rw-r--r-- | frozen_deps/mnemonic/wordlist/spanish.txt | 2048 |
10 files changed, 16676 insertions, 0 deletions
diff --git a/frozen_deps/mnemonic/__init__.py b/frozen_deps/mnemonic/__init__.py new file mode 100644 index 0000000..47e293d --- /dev/null +++ b/frozen_deps/mnemonic/__init__.py @@ -0,0 +1 @@ +from .mnemonic import Mnemonic # noqa: F401 diff --git a/frozen_deps/mnemonic/mnemonic.py b/frozen_deps/mnemonic/mnemonic.py new file mode 100644 index 0000000..935620a --- /dev/null +++ b/frozen_deps/mnemonic/mnemonic.py @@ -0,0 +1,291 @@ +# +# Copyright (c) 2013 Pavol Rusnak +# Copyright (c) 2017 mruddy +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +import binascii +import bisect +import hashlib +import hmac +import itertools +import os +import sys +import unicodedata + +PBKDF2_ROUNDS = 2048 + + +class ConfigurationError(Exception): + pass + + +# From <https://stackoverflow.com/questions/212358/binary-search-bisection-in-python/2233940#2233940> +def binary_search(a, x, lo=0, hi=None): # can't use a to specify default for hi + hi = hi if hi is not None else len(a) # hi defaults to len(a) + pos = bisect.bisect_left(a, x, lo, hi) # find insertion position + return pos if pos != hi and a[pos] == x else -1 # don't walk off the end + + +# Refactored code segments from <https://github.com/keis/base58> +def b58encode(v): + alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + + p, acc = 1, 0 + for c in reversed(v): + if sys.version < "3": + c = ord(c) + acc += p * c + p = p << 8 + + string = "" + while acc: + acc, idx = divmod(acc, 58) + string = alphabet[idx : idx + 1] + string + return string + + +class Mnemonic(object): + def __init__(self, language): + self.radix = 2048 + if sys.version < "3": + with open("%s/%s.txt" % (self._get_directory(), language), "r") as f: + self.wordlist = [w.strip().decode("utf8") for w in f.readlines()] + else: + with open( + "%s/%s.txt" % (self._get_directory(), language), "r", encoding="utf-8" + ) as f: + self.wordlist = [w.strip() for w in f.readlines()] + if len(self.wordlist) != self.radix: + raise ConfigurationError( + "Wordlist should contain %d words, but it contains %d words." + % (self.radix, len(self.wordlist)) + ) + + @classmethod + def _get_directory(cls): + return os.path.join(os.path.dirname(__file__), "wordlist") + + @classmethod + def list_languages(cls): + return [ + f.split(".")[0] + for f in os.listdir(cls._get_directory()) + if f.endswith(".txt") + ] + + @classmethod + def normalize_string(cls, txt): + if isinstance(txt, str if sys.version < "3" else bytes): + utxt = txt.decode("utf8") + elif isinstance(txt, unicode if sys.version < "3" else str): # noqa: F821 + utxt = txt + else: + raise TypeError("String value expected") + + return unicodedata.normalize("NFKD", utxt) + + @classmethod + def detect_language(cls, code): + code = cls.normalize_string(code) + first = code.split(" ")[0] + languages = cls.list_languages() + + for lang in languages: + mnemo = cls(lang) + if first in mnemo.wordlist: + return lang + + raise ConfigurationError("Language not detected") + + def generate(self, strength=128): + if strength not in [128, 160, 192, 224, 256]: + raise ValueError( + "Strength should be one of the following [128, 160, 192, 224, 256], but it is not (%d)." + % strength + ) + return self.to_mnemonic(os.urandom(strength // 8)) + + # Adapted from <http://tinyurl.com/oxmn476> + def to_entropy(self, words): + if not isinstance(words, list): + words = words.split(" ") + if len(words) not in [12, 15, 18, 21, 24]: + raise ValueError( + "Number of words must be one of the following: [12, 15, 18, 21, 24], but it is not (%d)." + % len(words) + ) + # Look up all the words in the list and construct the + # concatenation of the original entropy and the checksum. + concatLenBits = len(words) * 11 + concatBits = [False] * concatLenBits + wordindex = 0 + if self.detect_language(" ".join(words)) == "english": + use_binary_search = True + else: + use_binary_search = False + for word in words: + # Find the words index in the wordlist + ndx = ( + binary_search(self.wordlist, word) + if use_binary_search + else self.wordlist.index(word) + ) + if ndx < 0: + raise LookupError('Unable to find "%s" in word list.' % word) + # Set the next 11 bits to the value of the index. + for ii in range(11): + concatBits[(wordindex * 11) + ii] = (ndx & (1 << (10 - ii))) != 0 + wordindex += 1 + checksumLengthBits = concatLenBits // 33 + entropyLengthBits = concatLenBits - checksumLengthBits + # Extract original entropy as bytes. + entropy = bytearray(entropyLengthBits // 8) + for ii in range(len(entropy)): + for jj in range(8): + if concatBits[(ii * 8) + jj]: + entropy[ii] |= 1 << (7 - jj) + # Take the digest of the entropy. + hashBytes = hashlib.sha256(entropy).digest() + if sys.version < "3": + hashBits = list( + itertools.chain.from_iterable( + ( + [ord(c) & (1 << (7 - i)) != 0 for i in range(8)] + for c in hashBytes + ) + ) + ) + else: + hashBits = list( + itertools.chain.from_iterable( + ([c & (1 << (7 - i)) != 0 for i in range(8)] for c in hashBytes) + ) + ) + # Check all the checksum bits. + for i in range(checksumLengthBits): + if concatBits[entropyLengthBits + i] != hashBits[i]: + raise ValueError("Failed checksum.") + return entropy + + def to_mnemonic(self, data): + if len(data) not in [16, 20, 24, 28, 32]: + raise ValueError( + "Data length should be one of the following: [16, 20, 24, 28, 32], but it is not (%d)." + % len(data) + ) + h = hashlib.sha256(data).hexdigest() + b = ( + bin(int(binascii.hexlify(data), 16))[2:].zfill(len(data) * 8) + + bin(int(h, 16))[2:].zfill(256)[: len(data) * 8 // 32] + ) + result = [] + for i in range(len(b) // 11): + idx = int(b[i * 11 : (i + 1) * 11], 2) + result.append(self.wordlist[idx]) + if ( + self.detect_language(" ".join(result)) == "japanese" + ): # Japanese must be joined by ideographic space. + result_phrase = u"\u3000".join(result) + else: + result_phrase = " ".join(result) + return result_phrase + + def check(self, mnemonic): + mnemonic = self.normalize_string(mnemonic).split(" ") + # list of valid mnemonic lengths + if len(mnemonic) not in [12, 15, 18, 21, 24]: + return False + try: + idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic) + b = "".join(idx) + except ValueError: + return False + l = len(b) # noqa: E741 + d = b[: l // 33 * 32] + h = b[-l // 33 :] + nd = binascii.unhexlify(hex(int(d, 2))[2:].rstrip("L").zfill(l // 33 * 8)) + nh = bin(int(hashlib.sha256(nd).hexdigest(), 16))[2:].zfill(256)[: l // 33] + return h == nh + + def expand_word(self, prefix): + if prefix in self.wordlist: + return prefix + else: + matches = [word for word in self.wordlist if word.startswith(prefix)] + if len(matches) == 1: # matched exactly one word in the wordlist + return matches[0] + else: + # exact match not found. + # this is not a validation routine, just return the input + return prefix + + def expand(self, mnemonic): + return " ".join(map(self.expand_word, mnemonic.split(" "))) + + @classmethod + def to_seed(cls, mnemonic, passphrase=""): + mnemonic = cls.normalize_string(mnemonic) + passphrase = cls.normalize_string(passphrase) + passphrase = "mnemonic" + passphrase + mnemonic = mnemonic.encode("utf-8") + passphrase = passphrase.encode("utf-8") + stretched = hashlib.pbkdf2_hmac("sha512", mnemonic, passphrase, PBKDF2_ROUNDS) + return stretched[:64] + + @classmethod + def to_hd_master_key(cls, seed): + if len(seed) != 64: + raise ValueError("Provided seed should have length of 64") + + # Compute HMAC-SHA512 of seed + seed = hmac.new(b"Bitcoin seed", seed, digestmod=hashlib.sha512).digest() + + # Serialization format can be found at: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Serialization_format + xprv = b"\x04\x88\xad\xe4" # Version for private mainnet + xprv += b"\x00" * 9 # Depth, parent fingerprint, and child number + xprv += seed[32:] # Chain code + xprv += b"\x00" + seed[:32] # Master key + + # Double hash using SHA256 + hashed_xprv = hashlib.sha256(xprv).digest() + hashed_xprv = hashlib.sha256(hashed_xprv).digest() + + # Append 4 bytes of checksum + xprv += hashed_xprv[:4] + + # Return base58 + return b58encode(xprv) + + +def main(): + import binascii + import sys + + if len(sys.argv) > 1: + data = sys.argv[1] + else: + data = sys.stdin.readline().strip() + data = binascii.unhexlify(data) + m = Mnemonic("english") + print(m.to_mnemonic(data)) + + +if __name__ == "__main__": + main() diff --git a/frozen_deps/mnemonic/wordlist/chinese_simplified.txt b/frozen_deps/mnemonic/wordlist/chinese_simplified.txt new file mode 100644 index 0000000..b90f1ed --- /dev/null +++ b/frozen_deps/mnemonic/wordlist/chinese_simplified.txt @@ -0,0 +1,2048 @@ +的 +一 +是 +在 +不 +了 +有 +和 +人 +这 +中 +大 +为 +上 +个 +国 +我 +以 +要 +他 +时 +来 +用 +们 +生 +到 +作 +地 +于 +出 +就 +分 +对 +成 +会 +可 +主 +发 +年 +动 +同 +工 +也 +能 +下 +过 +子 +说 +产 +种 +面 +而 +方 +后 +多 +定 +行 +学 +法 +所 +民 +得 +经 +十 +三 +之 +进 +着 +等 +部 +度 +家 +电 +力 +里 +如 +水 +化 +高 +自 +二 +理 +起 +小 +物 +现 +实 +加 +量 +都 +两 +体 +制 +机 +当 +使 +点 +从 +业 +本 +去 +把 +性 +好 +应 +开 +它 +合 +还 +因 +由 +其 +些 +然 +前 +外 +天 +政 +四 +日 +那 +社 +义 +事 +平 +形 +相 +全 +表 +间 +样 +与 +关 +各 +重 +新 +线 +内 +数 +正 +心 +反 +你 +明 +看 +原 +又 +么 +利 +比 +或 +但 +质 +气 +第 +向 +道 +命 +此 +变 +条 +只 +没 +结 +解 +问 +意 +建 +月 +公 +无 +系 +军 +很 +情 +者 +最 +立 +代 +想 +已 +通 +并 +提 +直 +题 +党 +程 +展 +五 +果 +料 +象 +员 +革 +位 +入 +常 +文 +总 +次 +品 +式 +活 +设 +及 +管 +特 +件 +长 +求 +老 +头 +基 +资 +边 +流 +路 +级 +少 +图 +山 +统 +接 +知 +较 +将 +组 +见 +计 +别 +她 +手 +角 +期 +根 +论 +运 +农 +指 +几 +九 +区 +强 +放 +决 +西 +被 +干 +做 +必 +战 +先 +回 +则 +任 +取 +据 +处 +队 +南 +给 +色 +光 +门 +即 +保 +治 +北 +造 +百 +规 +热 +领 +七 +海 +口 +东 +导 +器 +压 +志 +世 +金 +增 +争 +济 +阶 +油 +思 +术 +极 +交 +受 +联 +什 +认 +六 +共 +权 +收 +证 +改 +清 +美 +再 +采 +转 +更 +单 +风 +切 +打 +白 +教 +速 +花 +带 +安 +场 +身 +车 +例 +真 +务 +具 +万 +每 +目 +至 +达 +走 +积 +示 +议 +声 +报 +斗 +完 +类 +八 +离 +华 +名 +确 +才 +科 +张 +信 +马 +节 +话 +米 +整 +空 +元 +况 +今 +集 +温 +传 +土 +许 +步 +群 +广 +石 +记 +需 +段 +研 +界 +拉 +林 +律 +叫 +且 +究 +观 +越 +织 +装 +影 +算 +低 +持 +音 +众 +书 +布 +复 +容 +儿 +须 +际 +商 +非 +验 +连 +断 +深 +难 +近 +矿 +千 +周 +委 +素 +技 +备 +半 +办 +青 +省 +列 +习 +响 +约 +支 +般 +史 +感 +劳 +便 +团 +往 +酸 +历 +市 +克 +何 +除 +消 +构 +府 +称 +太 +准 +精 +值 +号 +率 +族 +维 +划 +选 +标 +写 +存 +候 +毛 +亲 +快 +效 +斯 +院 +查 +江 +型 +眼 +王 +按 +格 +养 +易 +置 +派 +层 +片 +始 +却 +专 +状 +育 +厂 +京 +识 +适 +属 +圆 +包 +火 +住 +调 +满 +县 +局 +照 +参 +红 +细 +引 +听 +该 +铁 +价 +严 +首 +底 +液 +官 +德 +随 +病 +苏 +失 +尔 +死 +讲 +配 +女 +黄 +推 +显 +谈 +罪 +神 +艺 +呢 +席 +含 +企 +望 +密 +批 +营 +项 +防 +举 +球 +英 +氧 +势 +告 +李 +台 +落 +木 +帮 +轮 +破 +亚 +师 +围 +注 +远 +字 +材 +排 +供 +河 +态 +封 +另 +施 +减 +树 +溶 +怎 +止 +案 +言 +士 +均 +武 +固 +叶 +鱼 +波 +视 +仅 +费 +紧 +爱 +左 +章 +早 +朝 +害 +续 +轻 +服 +试 +食 +充 +兵 +源 +判 +护 +司 +足 +某 +练 +差 +致 +板 +田 +降 +黑 +犯 +负 +击 +范 +继 +兴 +似 +余 +坚 +曲 +输 +修 +故 +城 +夫 +够 +送 +笔 +船 +占 +右 +财 +吃 +富 +春 +职 +觉 +汉 +画 +功 +巴 +跟 +虽 +杂 +飞 +检 +吸 +助 +升 +阳 +互 +初 +创 +抗 +考 +投 +坏 +策 +古 +径 +换 +未 +跑 +留 +钢 +曾 +端 +责 +站 +简 +述 +钱 +副 +尽 +帝 +射 +草 +冲 +承 +独 +令 +限 +阿 +宣 +环 +双 +请 +超 +微 +让 +控 +州 +良 +轴 +找 +否 +纪 +益 +依 +优 +顶 +础 +载 +倒 +房 +突 +坐 +粉 +敌 +略 +客 +袁 +冷 +胜 +绝 +析 +块 +剂 +测 +丝 +协 +诉 +念 +陈 +仍 +罗 +盐 +友 +洋 +错 +苦 +夜 +刑 +移 +频 +逐 +靠 +混 +母 +短 +皮 +终 +聚 +汽 +村 +云 +哪 +既 +距 +卫 +停 +烈 +央 +察 +烧 +迅 +境 +若 +印 +洲 +刻 +括 +激 +孔 +搞 +甚 +室 +待 +核 +校 +散 +侵 +吧 +甲 +游 +久 +菜 +味 +旧 +模 +湖 +货 +损 +预 +阻 +毫 +普 +稳 +乙 +妈 +植 +息 +扩 +银 +语 +挥 +酒 +守 +拿 +序 +纸 +医 +缺 +雨 +吗 +针 +刘 +啊 +急 +唱 +误 +训 +愿 +审 +附 +获 +茶 +鲜 +粮 +斤 +孩 +脱 +硫 +肥 +善 +龙 +演 +父 +渐 +血 +欢 +械 +掌 +歌 +沙 +刚 +攻 +谓 +盾 +讨 +晚 +粒 +乱 +燃 +矛 +乎 +杀 +药 +宁 +鲁 +贵 +钟 +煤 +读 +班 +伯 +香 +介 +迫 +句 +丰 +培 +握 +兰 +担 +弦 +蛋 +沉 +假 +穿 +执 +答 +乐 +谁 +顺 +烟 +缩 +征 +脸 +喜 +松 +脚 +困 +异 +免 +背 +星 +福 +买 +染 +井 +概 +慢 +怕 +磁 +倍 +祖 +皇 +促 +静 +补 +评 +翻 +肉 +践 +尼 +衣 +宽 +扬 +棉 +希 +伤 +操 +垂 +秋 +宜 +氢 +套 +督 +振 +架 +亮 +末 +宪 +庆 +编 +牛 +触 +映 +雷 +销 +诗 +座 +居 +抓 +裂 +胞 +呼 +娘 +景 +威 +绿 +晶 +厚 +盟 +衡 +鸡 +孙 +延 +危 +胶 +屋 +乡 +临 +陆 +顾 +掉 +呀 +灯 +岁 +措 +束 +耐 +剧 +玉 +赵 +跳 +哥 +季 +课 +凯 +胡 +额 +款 +绍 +卷 +齐 +伟 +蒸 +殖 +永 +宗 +苗 +川 +炉 +岩 +弱 +零 +杨 +奏 +沿 +露 +杆 +探 +滑 +镇 +饭 +浓 +航 +怀 +赶 +库 +夺 +伊 +灵 +税 +途 +灭 +赛 +归 +召 +鼓 +播 +盘 +裁 +险 +康 +唯 +录 +菌 +纯 +借 +糖 +盖 +横 +符 +私 +努 +堂 +域 +枪 +润 +幅 +哈 +竟 +熟 +虫 +泽 +脑 +壤 +碳 +欧 +遍 +侧 +寨 +敢 +彻 +虑 +斜 +薄 +庭 +纳 +弹 +饲 +伸 +折 +麦 +湿 +暗 +荷 +瓦 +塞 +床 +筑 +恶 +户 +访 +塔 +奇 +透 +梁 +刀 +旋 +迹 +卡 +氯 +遇 +份 +毒 +泥 +退 +洗 +摆 +灰 +彩 +卖 +耗 +夏 +择 +忙 +铜 +献 +硬 +予 +繁 +圈 +雪 +函 +亦 +抽 +篇 +阵 +阴 +丁 +尺 +追 +堆 +雄 +迎 +泛 +爸 +楼 +避 +谋 +吨 +野 +猪 +旗 +累 +偏 +典 +馆 +索 +秦 +脂 +潮 +爷 +豆 +忽 +托 +惊 +塑 +遗 +愈 +朱 +替 +纤 +粗 +倾 +尚 +痛 +楚 +谢 +奋 +购 +磨 +君 +池 +旁 +碎 +骨 +监 +捕 +弟 +暴 +割 +贯 +殊 +释 +词 +亡 +壁 +顿 +宝 +午 +尘 +闻 +揭 +炮 +残 +冬 +桥 +妇 +警 +综 +招 +吴 +付 +浮 +遭 +徐 +您 +摇 +谷 +赞 +箱 +隔 +订 +男 +吹 +园 +纷 +唐 +败 +宋 +玻 +巨 +耕 +坦 +荣 +闭 +湾 +键 +凡 +驻 +锅 +救 +恩 +剥 +凝 +碱 +齿 +截 +炼 +麻 +纺 +禁 +废 +盛 +版 +缓 +净 +睛 +昌 +婚 +涉 +筒 +嘴 +插 +岸 +朗 +庄 +街 +藏 +姑 +贸 +腐 +奴 +啦 +惯 +乘 +伙 +恢 +匀 +纱 +扎 +辩 +耳 +彪 +臣 +亿 +璃 +抵 +脉 +秀 +萨 +俄 +网 +舞 +店 +喷 +纵 +寸 +汗 +挂 +洪 +贺 +闪 +柬 +爆 +烯 +津 +稻 +墙 +软 +勇 +像 +滚 +厘 +蒙 +芳 +肯 +坡 +柱 +荡 +腿 +仪 +旅 +尾 +轧 +冰 +贡 +登 +黎 +削 +钻 +勒 +逃 +障 +氨 +郭 +峰 +币 +港 +伏 +轨 +亩 +毕 +擦 +莫 +刺 +浪 +秘 +援 +株 +健 +售 +股 +岛 +甘 +泡 +睡 +童 +铸 +汤 +阀 +休 +汇 +舍 +牧 +绕 +炸 +哲 +磷 +绩 +朋 +淡 +尖 +启 +陷 +柴 +呈 +徒 +颜 |