Skip to content

Instantly share code, notes, and snippets.

@cvasqxz
Last active December 12, 2018 04:20
Show Gist options
  • Save cvasqxz/1fa00f7d994f7f6a9c28a7606d36b6ca to your computer and use it in GitHub Desktop.
Save cvasqxz/1fa00f7d994f7f6a9c28a7606d36b6ca to your computer and use it in GitHub Desktop.
Script para generar una direcciones BTC en Python3
from hashlib import new
from binascii import a2b_hex
from collections import deque
# https://bitcoin.stackexchange.com/a/59806
def secp256k1(s):
G = (55066263022277343669578718895168534326250603453777594175500187360389116729240,
32670510020758816978083085130507043184471273380659243275938904335757337482424)
N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1
sk = int(s, 16)
def add(p, q):
px, py = p
qx, qy = q
if p == q:
lam = (3 * px * px) * pow(2 * py, P - 2, P)
else:
lam = (qy - py) * pow(qx - px, P - 2, P)
rx = lam**2 - px - qx
ry = lam * (px - rx) - py
return rx % P, ry % P
ret = None
for i in range(256):
if sk & (1 << i):
if ret is None:
ret = G
else:
ret = add(ret, G)
G = add(G, G)
return '04' + '{:064x}'.format(ret[0]) + '{:064x}'.format(ret[1])
# https://github.com/joeblackwaslike/base58check
def b58check(val):
charset = b'123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
output = b''
p, acc = 1, 0
pad_len = len(val)
val = val.lstrip(b'\0')
pad_len -= len(val)
for char in deque(reversed(val)):
acc += p * char
p = p << 8
while acc:
acc, idx = divmod(acc, len(charset))
output = charset[idx:idx+1] + output
prefix = bytes([charset[0]]) * pad_len
return prefix + output
def doubleSHA256(s):
return new('sha256', new('sha256', a2b_hex(s)).digest()).hexdigest()
def hash160(s):
return new('ripemd160', new('sha256', a2b_hex(s)).digest()).hexdigest()
def main():
privkey = new('sha256', b'probando el algoritmo').hexdigest()
# (CHAUCHA PUBKEY = 58 AND SECRET = d8)
b58_PUBKEY_ADDRESS = '00'
b58_SECRET_KEY = '80'
checksum = doubleSHA256(b58_SECRET_KEY + privkey)[:8]
wif = b58check(a2b_hex(b58_SECRET_KEY + privkey + checksum))
pubkey = secp256k1(privkey)
pubkeyhash = hash160(pubkey)
checksum = doubleSHA256(b58_PUBKEY_ADDRESS + pubkeyhash)[:8]
addr = b58check(a2b_hex(b58_PUBKEY_ADDRESS + pubkeyhash + checksum))
print('Private Key (WIF): %s' % wif.decode('utf-8'))
print('BTC Address: %s' % addr.decode('utf-8'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment