티스토리 뷰
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
반응형
'비트코인 공부' 카테고리의 다른 글
니모닉 <=> 비밀키(엔트로피) 변환 파이썬 코드 (4) | 2023.02.02 |
---|---|
Native Segwit 주소 생성 (2) | 2023.01.14 |
파이썬) SegWit 주소 생성하고 출금하기 (28) | 2022.12.16 |
SegWit 주소 사용이 권장되는 이유 (0) | 2022.12.14 |
비트코인 코어 풀노드 설치 (0) | 2022.12.05 |
댓글