Bitcoin Address Generator (+priv key)

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

  1. LoveBug

    LoveBug Member

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

    Code:
    # 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
            self._seed_gen()
    
        def checksum(self, what: str) -> bytes:
            return sha256(sha256(what).digest()).digest()[:4]
    
        def _seed_gen(self) -> None:
            self.digest = hashlib.sha256(
                str(
                    str(urandom(32).hex()) +
                    str(randrange(2 ** 256)) +
                    str(int(time() * 1000000))
                ).encode()
            ).digest()
            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)
                ).decode('utf-8')
            else:
                self.wif = base58.b58encode(
                    wif_dig+self.sufix+self.checksum(wif_dig+self.sufix)
                ).decode('utf-8')
    
        def hash160(self, v: str) -> bytes:
            r = RIPEMD160.new()
            r.update(hashlib.sha256(v).digest())
            return r
    
        def _generate_compressed_pubkey(self) -> str:
            ecdsa_digest = SigningKey.from_string(
                self.digest, curve=SECP256k1
            ).get_verifying_key().to_string()
            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
            else:
                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()
            ).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 - Старейшина

    Joined:
    27 Aug 2011
    Messages:
    641
    Likes Received:
    502
    Reputations:
    72
    зачем велосипедить
    есть же

    https://pybtc.readthedocs.io/en/master/examples.html

    Code:
     ku --help
    usage: ku [-h] [-w] [-W] [-a] [-u] [-P] [-j] [-b [BRIEF [BRIEF ...]]]
              [-s SUBKEY]
              [-n {ARG,AXE,BC,BCH,BSD,BTC,BTCD,BTDX,BTG,BTX,CHA,DASH,DCR,DCRT,DFC,DGB,DOGE,FAI,FTC,FTX,GRS,JBS,LTC,MEC,MONA,MZC,PIVX,POLIS,RIC,STRAT,TBTX,TDASH,TMONA,TPIVX,TVI,VIA,XCH,XDT,XLT,XMY,XRT,XTG,XTN,ZEC}]
              [--override-network {ARG,AXE,BC,BCH,BSD,BTC,BTCD,BTDX,BTG,BTX,CHA,DASH,DCR,DCRT,DFC,DGB,DOGE,FAI,FTC,FTX,GRS,JBS,LTC,MEC,MONA,MZC,PIVX,POLIS,RIC,STRAT,TBTX,TDASH,TMONA,TPIVX,TVI,VIA,XCH,XDT,XLT,XMY,XRT,XTG,XTN,ZEC}]
              [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)
      -n {ARG,AXE,BC,BCH,BSD,BTC,BTCD,BTDX,BTG,BTX,CHA,DASH,DCR,DCRT,DFC,DGB,DOGE,FAI,FTC,FTX,GRS,JBS,LTC,MEC,MONA,MZC,PIVX,POLIS,RIC,STRAT,TBTX,TDASH,TMONA,TPIVX,TVI,VIA,XCH,XDT,XLT,XMY,XRT,XTG,XTN,ZEC}, --network {ARG,AXE,BC,BCH,BSD,BTC,BTCD,BTDX,BTG,BTX,CHA,DASH,DCR,DCRT,DFC,DGB,DOGE,FAI,FTC,FTX,GRS,JBS,LTC,MEC,MONA,MZC,PIVX,POLIS,RIC,STRAT,TBTX,TDASH,TMONA,TPIVX,TVI,VIA,XCH,XDT,XLT,XMY,XRT,XTG,XTN,ZEC}
                            specify network
      --override-network {ARG,AXE,BC,BCH,BSD,BTC,BTCD,BTDX,BTG,BTX,CHA,DASH,DCR,DCRT,DFC,DGB,DOGE,FAI,FTC,FTX,GRS,JBS,LTC,MEC,MONA,MZC,PIVX,POLIS,RIC,STRAT,TBTX,TDASH,TMONA,TPIVX,TVI,VIA,XCH,XDT,XLT,XMY,XRT,XTG,XTN,ZEC}
                            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.