Bitcoin Address Generator (+priv key)

Discussion in 'Python' started by LoveBug, 22 Jun 2022.

  1. LoveBug

    LoveBug Member

    19 Jun 2022
    Likes Received:
    Подойдет для интерграции в шоп/etc. Полностью оффлайн.

    # pip install pycryptodome base58 ecdsa
    import hashlib
    from Crypto.Hash import RIPEMD160
    from binascii import hexlify
    from hashlib import sha256
    from os import urandom
    from random import randrange
    from time import time
    import base58
    from ecdsa import SECP256k1, SigningKey
    class BitcoinWallet:
        def __init__(self, ver: str = '3') -> None:
            self.prefix = b'\x80'
            self.sufix = b'\x01'
            self.addr_ver = ver
        def checksum(self, what: str) -> bytes:
            return sha256(sha256(what).digest()).digest()[:4]
        def _seed_gen(self) -> None:
            self.digest = hashlib.sha256(
                    str(urandom(32).hex()) +
                    str(randrange(2 ** 256)) +
                    str(int(time() * 1000000))
            self.hex = hexlify(self.digest).decode()
            wif_dig = self.prefix + self.digest
            if self.addr_ver == '1':
                self.wif = base58.b58encode(
                    wif_dig + self.checksum(wif_dig)
                self.wif = base58.b58encode(
        def hash160(self, v: str) -> bytes:
            r =
            return r
        def _generate_compressed_pubkey(self) -> str:
            ecdsa_digest = SigningKey.from_string(
                self.digest, curve=SECP256k1
            x_coord = ecdsa_digest[:int(len(ecdsa_digest) / 2)]
            y_coord = ecdsa_digest[int(len(ecdsa_digest) / 2):]
            if int(hexlify(y_coord), 16) % 2 == 0:
                p = b'\x02' + x_coord
                p = b'\x03' + x_coord
            self.pubkeyc = str(hexlify(p).decode('utf-8'))
            return p
        def _generate_address(self) -> str:
            p = self._generate_compressed_pubkey()
            redeem_script = self.hash160(
                b'\x00\x14' + self.hash160(p).digest()
            m = b'\x05' + redeem_script
            c = self.checksum(m)
            return base58.b58encode(m + c).decode('utf-8')
        def new(self) -> tuple:
            return (
                self._generate_address(), f'p2wpkh-p2sh:{self.wif}'
  2. lifescore

    lifescore Elder - Старейшина

    27 Aug 2011
    Likes Received:
    зачем велосипедить
    есть же

     ku --help
    usage: ku [-h] [-w] [-W] [-a] [-u] [-P] [-j] [-b [BRIEF [BRIEF ...]]]
              [-s SUBKEY]
              [item [item ...]]
    Crypto coin utility ku ("key utility") to show information about Bitcoin or
    other cryptocoin data structures.
    positional arguments:
      item                  a BIP0032 wallet key string; a WIF; a bitcoin address;
                            an SEC (ie. a 66 hex chars starting with 02, 03 or a
                            130 hex chars starting with 04); the literal string
                            "create" to create a new wallet key using strong
                            entropy sources; P:wallet passphrase (NOT
                            RECOMMENDED); H:wallet passphrase in hex (NOT
                            RECOMMENDED); E:electrum value (either a master
                            public, master private, or initial data);
                            secret_exponent (in decimal or hex); x,y where x,y
                            form a public pair (y is a number or one of the
                            strings "even" or "odd"); hash160 (as 40 hex
                            characters). If this argument is missing, input data
                            will be read from stdin.
    optional arguments:
      -h, --help            show this help message and exit
      -w, --wallet          show just Bitcoin wallet key
      -W, --wif             show just Bitcoin WIF
      -a, --address         show just Bitcoin address
      -u, --uncompressed    show output in uncompressed form
      -P, --public          only show public version of wallet keys
      -j, --json            output as JSON
      -b [BRIEF [BRIEF ...]], --brief [BRIEF [BRIEF ...]]
                            brief output; display a single field
      -s SUBKEY, --subkey SUBKEY
                            subkey path (example: 0H/2/15-20)
                            specify network
                            override detected network type
    Known networks codes: ARG (Argentum mainnet), AXE (Axe mainnet), BC (Blackcoin
    mainnet), BCH (Bcash mainnet), BSD (BitSend mainnet), BTC (Bitcoin mainnet),
    BTCD (BitcoinDark mainnet), BTDX (Bitcloud mainnet), BTG (Bgold mainnet), BTX
    (BitCore mainnet), CHA (Chaucha mainnet), DASH (Dash mainnet), DCR (Decred
    mainnet), DCRT (Decred testnet), DFC (DEFCOIN mainnet), DGB (Digibyte
    mainnet), DOGE (Dogecoin mainnet), FAI (Faircoin mainnet), FTC (Feathercoin
    mainnet), FTX (Feathercoin testnet), GRS (Groestlcoin mainnet), JBS (Jumbucks
    mainnet), LTC (Litecoin mainnet), MEC (Megacoin mainnet), MONA (Monacoin
    mainnet), MZC (Mazacoin mainnet), PIVX (PIVX mainnet), POLIS (Polis mainnet),
    RIC (Riecoin mainnet), STRAT (Strat mainnet), TBTX (BitCore testnet3), TDASH
    (Dash testnet), TMONA (Monacoin testnet4), TPIVX (PIVX testnet), TVI (Viacoin
    testnet), VIA (Viacoin mainnet), XCH (Bcash testnet3), XDT (Dogecoin testnet),
    XLT (Litecoin testnet), XMY (Myriadcoin mainnet), XRT (Bitcoin regtest), XTG
    (Bgold testnet), XTN (Bitcoin testnet3), ZEC (Zcash mainnet)
    k36 and crlf like this.