티스토리 뷰

BIP39 니모닉 단어 목록

https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md

 

 

파이썬 코드

from bitcoin import sha256
import requests

# fetch 2048 words
res = requests.get("https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/english.txt")
arr = res.text.strip().split('\n')

# 23 words selected
words_23 = 'aerobic bulk nice flip focus exit pause census globe garden vacant thumb guard live note whisper fringe snack absent certain prefer worth around'

# 23 words to 253 bits
list = words_23.split(' ')
bits253 = ''
for i in range(len(list)) :
    num = arr.index(list[i])
    bits253 += bin(num)[2:].zfill(11)


for i in range(8) :
    # 3bit for padding
    padding = bin(i)[2:].zfill(3)
    
    # 256 bit entropy
    hexStr = hex(int(bits253 + padding, 2))[2:].zfill(64)
    
    # sha256 of entropy
    entropy_hash = sha256(bytes.fromhex(hexStr))
    
    # first checksum 8bit of hash
    checksumHex = entropy_hash[:2]
    checksumDec = int(checksumHex, 16)
    checksumBin = bin(checksumDec)[2:].zfill(8)
    
    # index of 24 word
    index = int(padding + checksumBin, 2)
    
    # last word
    print(arr[index])

 

 

 니모닉 유효여부 체크 및 변환

from mnemonic import Mnemonic

m = Mnemonic('english')

words = m.generate(strength=256)
# 'divert parade result actor remain smoke float index hobby tongue expire glory author scheme admit pave burst group junior route kid federal cost pride'

# 유효한 니모닉인지 체크
m.check(words)

# 니모닉으로부터 엔트로피 구하기
ent = bytearray.hex(m.to_entropy(words))
# '3fd402df015b5999964b966c5c8d41b1c0f78140ed0c1eccdde4de47a4a8cc2d'

# 엔트로피로부터 니모닉 구하기
words = m.to_mnemonic(bytes.fromhex(ent))
# 'divert parade result actor remain smoke float index hobby tongue expire glory author scheme admit pave burst group junior route kid federal cost pride'

 

 

블로그내 관련 글

728x90
반응형
댓글
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함