Git Product home page Git Product logo

Comments (4)

Isaacdelly avatar Isaacdelly commented on June 11, 2024

The database we’re using includes bitcoin addresses in P2PKH (address starts with 1), P2SH (address start with 3), and P2WPKH (address start with bc1) formats.

The algorithm we’re using only generates P2PKH addresses and has been really optimized to do it as fast as possible. Since only addresses that start with 1 get generated, all the other bitcoin address types can be ignored when we load the database.

We could make an extension to include the other types. But P2PKH is considered the legacy address format that older forgotten whale bitcoin wallets are in. And doing the other formats would take a bunch of time

from plutus.

davidfritsche17 avatar davidfritsche17 commented on June 11, 2024

Got it. Thanks for the clarification and the program. Running great. I've added a few things to show progress that don't seem to slow anything down. Checked for various ways to speed it up, but it seems you've done a good job already. :-)

I figure I am guaranteed to find a code within the next 1000 million years.

from plutus.

davidfritsche17 avatar davidfritsche17 commented on June 11, 2024

Testing this: (sorry the formatting was lost in this post)
The public_key_to_p2pkh_address function generates a P2PKH address by hashing the public key using RIPEMD-160, prefixing the resulting hash with '00', and then base58 encoding the result.

The public_key_to_p2sh_address function generates a P2SH address by creating a redeem script that pushes the hash of the public key onto the stack, and then base58 encoding the redeem script with a prefix of '05'.

The public_key_to_p2wpkh_address function generates a P2WPKH address by hashing the public key using RIPEMD-160, prefixing the resulting hash with '0014', and then base58 encoding the result with a prefix of 'bc'.

The P2WPKH address format is compatible with the Bech32 address format (starting with 'bc1'), which is the preferred address format for SegWit transactions. However, this implementation uses the base58 encoding format for consistency with the other address formats.

import binascii
import hashlib
import os
import base58
from fastecdsa import keys, curve
from bitcoinlib.encoding import b58encode_check
from bitcoinlib.keys import HDKey, Key

def generate_private_key():
return binascii.hexlify(os.urandom(32)).decode('utf-8').upper()

def private_key_to_public_key(private_key, fastecdsa):
if fastecdsa:
key = keys.get_public_key(int('0x' + private_key, 0), curve.secp256k1)
return '04' + (hex(key.x)[2:] + hex(key.y)[2:]).zfill(128)
else:
pk = Key(privkey=bytes.fromhex(private_key))
return pk.pubkey.serialize(compressed=False).hex()

def public_key_to_p2pkh_address(public_key):
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
var = hashlib.new('ripemd160')
encoding = binascii.unhexlify(public_key.encode())
var.update(hashlib.sha256(encoding).digest())
var_encoded = ('00' + var.hexdigest()).encode()
digest = hashlib.sha256(binascii.unhexlify(var_encoded)).digest()
var_hex = '00' + var.hexdigest() + hashlib.sha256(digest).hexdigest()[0:8]
count = [char != '0' for char in var_hex].index(True) // 2
n = int(var_hex, 16)
output = []
while n > 0:
n, remainder = divmod(n, 58)
output.append(alphabet[remainder])
for i in range(count): output.append(alphabet[0])
return ''.join(output[::-1])

def public_key_to_p2sh_address(public_key):
redeem_script = '76a914' + hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(public_key)).digest()).hexdigest() + '88ac'
return b58encode_check(bytes.fromhex('05' + redeem_script)).decode('utf-8')

def public_key_to_p2wpkh_address(public_key):
witness_program = hashlib.new('ripemd160', hashlib.sha256(binascii.unhexlify(public_key)).digest()).hexdigest()
return b58encode_check(bytes.fromhex('0014' + witness_program)).decode('utf-8')

from plutus.

TheDevilWalks avatar TheDevilWalks commented on June 11, 2024

Y

from plutus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.