๋น„๋ฐ€ํ‚ค๋ฅผ ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

๋น„๋ฐ€ํ‚ค ์ง๋ ฌํ™”

  • ๋น„๋ฐ€ํ‚ค๋ฅผ ์ง๋ ฌํ™”ํ•  ์ด์œ ๋Š” ๊ทธ๋‹ฅ ์—†๋‹ค.
  • ๋„คํŠธ์›Œํฌ๋กœ ์ „ํŒŒํ•  ์ผ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋น„๋ฐ€ํ‚ค ์ „ํŒŒ๋Š” ์ง€๊ฐ‘์ด ํ„ธ๋ฆด ์ˆ˜ ์žˆ๋Š” ์œ„ํ—˜์ด ๋งค์šฐ ํฌ๋‹ค.
  • ๊ทธ๋Ÿฐ๋ฐ๋„ ํ•ด๋‹น ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ์ด์œ ๋Š”, ์ง€๊ฐ‘์„ ์˜ฎ๊ธฐ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
    • ์ข…์ด์ง€๊ฐ‘ > ์†Œํ”„ํŠธ์›จ์–ด ์ง€๊ฐ‘
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด WIF ํฌ๋งท์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 
def encode_base58_checksum(b):
    return encode_base58(b + hash256(b)[:4])
 
def encode_base58(s):
    count = 0
    for c in s:  # <1>
        if c == 0:
            count += 1
        else:
            break
    num = int.from_bytes(s, 'big')
    prefix = '1' * count
    result = ''
    while num > 0:  # <2>
        num, mod = divmod(num, 58)
        result = BASE58_ALPHABET[mod] + result
    return prefix + result  # <3>
 
def hash256(s):
    '''two rounds of sha256'''
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()
 
class PrivateKey:
 
    def __init__(self, secret):
        self.secret = secret
        self.point = secret * G
 
    def wif(self, compressed=True, testnet=False):
        secret_bytes = self.secret.to_bytes(32, 'big')
        if testnet:
            prefix = b'\xef'
        else:
            prefix = b'\x80'
 
        if compressed:
            suffix = b'\x01'
        else:
            suffix = b''
 
        return encode_base58_checksum(prefix + secret_bytes + suffix)
 

Reference