Post
Topic
Board Development & Technical Discussion
Re: sagemath script
by
witcher_sense
on 11/01/2023, 14:40:36 UTC

Code:
import hashlib
import base58

def hash160(hex_str):
sha = hashlib.sha256()
rip = hashlib.new('ripemd160')
sha.update(hex_str)
rip.update(sha.digest())
return rip.hexdigest()  # .hexdigest() is hex ASCII

pub_keys = open('pubkey.json', 'r', encoding='utf-8')
new_file = open('addresses.json', 'a', encoding='utf-8')
compress_pubkey = False

for pub_key in pub_keys:
pub_key = pub_key.replace('\n', '')
if compress_pubkey:
if (ord(bytearray.fromhex(pub_key[-2:])) % 2 == 0):
pubkey_compressed = '02'
else:
pubkey_compressed = '03'
pubkey_compressed += pub_key[2:66]
hex_str = bytearray.fromhex(pubkey_compressed)
else:
hex_str = bytearray.fromhex(pub_key)

key_hash = '00' + hash160(hex_str)


sha = hashlib.sha256()
sha.update(bytearray.fromhex(key_hash))
checksum = sha.digest()
sha = hashlib.sha256()
sha.update(checksum)
checksum = sha.hexdigest()[0:8]


new_file.write("" + (base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8'))

new_file.write((base58.b58encode(bytes(bytearray.fromhex(key_hash + checksum)))).decode('utf-8') + "\n")


pub_keys.close()
new_file.close()

this code python public 2 addresses
any easy sage method please... public 2 addresses
As you can see, in the python code that you posted, the base58 module is imported to encode public key hashes using base58_check (which is basically a bitcoin address). Unless you find people who voluntarily agree to implement such an algorithm in sagemath (I think it is not that hard to implement it, but still...), you're not going to convert these public keys to address. But you haven't answered my questions: why don't you just use a python code that works?