release (OP)
Member
Offline
Activity: 184
Merit: 14
|
|
January 21, 2021, 07:56:57 AM |
|
I am hoping someone can provide mean script preferably python that converts private key in binary and/or hex and converts to public key. Preferably just the X coordinate. Y coordinate is fine also or compressed/uncompressed. Don't need the address generated I can do that myself just wanted a stand alone script. Thanks
|
|
|
|
odolvlobo
Legendary
Online
Activity: 4438
Merit: 3343
|
|
January 21, 2021, 08:27:49 AM |
|
If you are looking for a challenge, this Javascript code will do it (requires Node.js): Crypto = require('crypto'); ecdh = Crypto.createECDH('secp256k1'); ecdh.setPrivateKey(privKey); publicKey = ecdh.getPublicKey(null, 'compressed');
|
Join an anti-signature campaign: Click ignore on the members of signature campaigns. PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
|
|
|
release (OP)
Member
Offline
Activity: 184
Merit: 14
|
|
January 21, 2021, 08:34:27 AM |
|
If you are looking for a challenge, this Javascript code will do it (requires Node.js): Crypto = require('crypto'); ecdh = Crypto.createECDH('secp256k1'); ecdh.setPrivateKey(privKey); publicKey = ecdh.getPublicKey(null, 'compressed');
Lol thank you. Do you know python?
|
|
|
|
release (OP)
Member
Offline
Activity: 184
Merit: 14
|
|
January 21, 2021, 08:35:54 AM |
|
Looking for a simple script with like: print(publickey)
|
|
|
|
BlackHatCoiner
Legendary
Offline
Activity: 1638
Merit: 8122
Bitcoin is a royal fork
|
|
January 21, 2021, 10:31:37 AM |
|
On secp256k1-py: from secp256k1 import PrivateKey
key = '7ccca75d019dbae79ac4266501578684ee64eeb3c9212105f7a3bdc0ddb0f27e'
privkey = PrivateKey(bytes(bytearray.fromhex(key))) pubkey_ser = privkey.pubkey.serialize() pubkey_ser_uncompressed = privkey.pubkey.serialize(compressed=False) I guess that does the job.
|
|
|
|
release (OP)
Member
Offline
Activity: 184
Merit: 14
|
|
January 21, 2021, 12:43:05 PM |
|
On secp256k1-py: from secp256k1 import PrivateKey
key = '7ccca75d019dbae79ac4266501578684ee64eeb3c9212105f7a3bdc0ddb0f27e'
privkey = PrivateKey(bytes(bytearray.fromhex(key))) pubkey_ser = privkey.pubkey.serialize() pubkey_ser_uncompressed = privkey.pubkey.serialize(compressed=False) I guess that does the job. Perfect thank you
|
|
|
|
bkelly13
Member
Offline
Activity: 69
Merit: 34
|
|
December 05, 2022, 06:14:58 AM |
|
Does anyone know where to find the specific steps to convert from private to public key? By that I exclude library functions from Java, or Perl or anything where the transformation takes place in a pre-written function that the programmer calls. In other words, maybe something than can be written into C code and make the transformation.
I have found code for SHA-256 and RIPEMD-160 and can do those individual steps. But I have not figured out how to put them together to go from private to public then to address.
I am not a math major.
|
|
|
|
witcher_sense
Legendary
Offline
Activity: 2436
Merit: 4408
🔐BitcoinMessage.Tools🔑
|
|
December 05, 2022, 08:34:02 AM |
|
Does anyone know where to find the specific steps to convert from private to public key?
It is not that hard: you take a generator point of secp256k1 elliptic curve and multiply it your private key times using specific elliptic curve multiplication. Elliptic curve point multiplication is unlike standard multiplication because it consists of two operations instead of one: point addition and point doubling, which in itself involve calculating modulo inverse using an extended Euclidean algorithm. Essentially, you take a private key (a 256-bit number) and perform point addition each time you see '1', and you perform point doubling each time you see '0'. 256 operations to calculate a point on an elliptic curve that will correspond to your secret number. So, the whole algorithm for calculating a public key from a private one consists of special math functions performing all those calculations. If you are interested, you can check my "poorly written" Python re-implementation of this algorithm: Converting HEX private key to bitcoin address
|
|
|
|
bkelly13
Member
Offline
Activity: 69
Merit: 34
|
|
December 06, 2022, 02:03:42 AM |
|
Seems to me its quite a bit harder than at first appears. Re: “you take a generator point of secp256k1 elliptic curve…” There is much more to that than immediately meets the eye. I tried reading about elliptic curve and have no idea of how to implement it into code. I looked at your Python and see many steps in there I don’t know how to translate into C code. I don’t know Python. The best I have found so far is here: https://academy.bit2me.com/en/como-se-crea-una-direccion-bitcoin/But when the private key they used is put in an on-line translate page, the numbers do not match the public key of that page. Well, that is to say I have not been able to produce identical results. I have code to do SHA-256 and RIPEMD-160, and wrote some test apps that does those translations, and they get the same values as some on-line tools. But there are intermediate steps that I have not figured out or found. Is there anyone that knows the step-by-step process to translate the private key into the public key?
|
|
|
|
Edwardard
|
|
December 06, 2022, 02:57:05 AM Last edit: December 06, 2022, 03:12:57 AM by Edwardard |
|
Is there anyone that knows the step-by-step process to translate the private key into the public key?
Check this out: Bitcoin python tutorial for beginners. PS: It's a long one, but quite informative
|
|
|
|
bkelly13
Member
Offline
Activity: 69
Merit: 34
|
|
December 06, 2022, 04:04:25 AM |
|
Is there anyone that knows the step-by-step process to translate the private key into the public key?
Check this out: Bitcoin python tutorial for beginners. PS: It's a long one, but quite informative At about 7:50 the narrator states they will call the function and not use the code to implement the function. That does not help me. Thanks for the reply.
|
|
|
|
odolvlobo
Legendary
Online
Activity: 4438
Merit: 3343
|
|
December 07, 2022, 10:08:51 AM |
|
If you want C code, this will give you plenty: https://duckduckgo.com/?q=elliptic+curve+library+CKeep in mind that most people don't write their own because there are already libraries in most languages that have been tested.
|
Join an anti-signature campaign: Click ignore on the members of signature campaigns. PGP Fingerprint: 6B6BC26599EC24EF7E29A405EAF050539D0B2925 Signing address: 13GAVJo8YaAuenj6keiEykwxWUZ7jMoSLt
|
|
|
DannyHamilton
Legendary
Offline
Activity: 3472
Merit: 4798
|
|
December 09, 2022, 12:04:49 AM |
|
Keep in mind that most people don't write their own because there are already libraries in most languages that have been tested.
Sure, but I have to say that I found it quite fun to write my own C program that would take a file of private keys and generate the associated public keys and Legacy bitcoin addresses.
|
|
|
|
PawGo
Legendary
Offline
Activity: 952
Merit: 1373
|
|
December 09, 2022, 05:05:28 PM |
|
Keep in mind that most people don't write their own because there are already libraries in most languages that have been tested.
Sure, but I have to say that I found it quite fun to write my own C program that would take a file of private keys and generate the associated public keys and Legacy bitcoin addresses. Do you have it available anywhere? Please share it. It would be really interesting to see, as it contains a lot of intermediary steps: point multiplication, hash calculation etc.
|
|
|
|
bkelly13
Member
Offline
Activity: 69
Merit: 34
|
|
December 11, 2022, 04:24:09 AM |
|
Keep in mind that most people don't write their own because there are already libraries in most languages that have been tested.
Sure, but I have to say that I found it quite fun to write my own C program that would take a file of private keys and generate the associated public keys and Legacy bitcoin addresses. I would really like to hear about that and see your code. A large part is the question of what transformations are applied. I cannot find the post from here right now but someone provided this link: https://gobittest.appspot.com/AddressIts the best I have seen. But in this one, it shows item 0 the private ECDSA Key, then item 1 the Public ECDSA Key. I don't see anything about how to translate from private to public.
|
|
|
|
witcher_sense
Legendary
Offline
Activity: 2436
Merit: 4408
🔐BitcoinMessage.Tools🔑
|
I would really like to hear about that and see your code. A large part is the question of what transformations are applied. I cannot find the post from here right now but someone provided this link: https://gobittest.appspot.com/AddressIts the best I have seen. But in this one, it shows item 0 the private ECDSA Key, then item 1 the Public ECDSA Key. I don't see anything about how to translate from private to public. I have rewritten my original script to make the conversion process simpler to understand. Specifically, I added comprehensive comments describing each step of calculating bitcoin addresses and private keys in wallet import format (WIF). I also added links where you can find corresponding formulas to calculate public keys from private keys. I think, if you have a basic understanding of some programming language like C/C++, you can read Python code and adapt it if needed. import base58 # pip install base58
# these modules are taken from here https://github.com/karpathy/cryptos/tree/main/cryptos from ripemd160 import ripemd160 from sha256 import sha256
class PublicKey:
"""Calculating public key from private key""" """Parameters of an elliptic curve""" # you can find these parameters here https://en.bitcoin.it/wiki/Secp256k1 or # in the official specification https://www.secg.org/sec2-v2.pdf p_curve = (2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1) # The proven prime # Number of points in the field n_curve = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 # These two defines the elliptic curve. y^2 = x^3 + Acurve * x + Bcurve a_curve, b_curve = 0, 7 gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240 gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424 gpoint = (gx, gy) # This is generator point.
def __init__(self, private_key):
self.private_key = private_key
@property def private_key(self): return self.__private_key
@private_key.setter def private_key(self, key): # catch invalid private keys as early as possible try: key = int(str(key), 16) except ValueError: raise NotImplementedError( 'Private key must be a hexadecimal number') if key <= 0 or key >= self.n_curve: raise Exception("Invalid Scalar/Private Key")
self.__private_key = key
def __modinv(self, a, n): """Extended Euclidean Algorithm""" # https://www.geeksforgeeks.org/euclidean-algorithms-basic-and-extended/ lm, hm = 1, 0 low, high = a % n, n while low > 1: ratio = int(high/low) nm, new = int(hm-lm*ratio), int(high-low*ratio) lm, low, hm, high = nm, new, lm, low return lm % n
def __ec_add(self, a, b): """Point addition""" # https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_addition lam_add = ((b[1]-a[1]) * self.__modinv(b[0] - a[0], self.p_curve)) % self.p_curve x = (pow(lam_add, 2)-a[0]-b[0]) % self.p_curve y = (lam_add*(a[0]-x)-a[1]) % self.p_curve return x, y
def __ec_double(self, a): """EC point doubling""" # https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_doubling lam = ((3*a[0]*a[0]+self.a_curve) * self.__modinv((2*a[1]), self.p_curve)) % self.p_curve x = int((lam*lam-2*a[0]) % self.p_curve) y = int((lam*(a[0]-x)-a[1]) % self.p_curve) return x, y
def __ec_multiply(self, genpoint, scalarhex): """EC point multiplication""" # https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add scalarbin = str(bin(scalarhex))[2:] q = genpoint for i in range(1, len(scalarbin)): # Double and add to multiply point q = self.__ec_double(q) if scalarbin[i] == "1": q = self.__ec_add(q, genpoint) return q
def __compute_public(self): """Calculating a public key""" return self.__ec_multiply(self.gpoint, self.private_key)
def __public_address(self, key): """Function calculating address""" # steps are described here https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses # bytes representation is needed for sha256 and ripemd160 key = bytes.fromhex(key) # Take the corresponding public key # Perform SHA-256 hashing on the public key # Perform RIPEMD-160 hashing on the result of SHA-256 # Add version byte in front of RIPEMD-160 hash (0x00 for Main Network) address = bytes.fromhex(f"00{ripemd160(sha256(key)).hex()}") # Perform SHA-256 hash on the extended RIPEMD-160 result # Perform SHA-256 hash on the result of the previous SHA-256 hash checksum = sha256(sha256(address)) # Take the first 4 bytes of the second SHA-256 hash. This is the address checksum # Add the checksum bytes at the end of extended RIPEMD-160 hash. # This is the 25-byte binary Bitcoin Address. address = f"{address.hex()}{checksum.hex()[:8]}" # Convert the result from a byte string into a base58 string using Base58Check encoding. # This is the most commonly used Bitcoin Address format address = base58.b58encode(bytes.fromhex(address)).decode("UTF-8") return address
def __convert_private_to_wif(self): """Converting a private key to WIF"""
# convert private key to bytes for sha256 # steps are described here https://en.bitcoin.it/wiki/Wallet_import_format # take a private key in hex and add a 0x80 byte in front of it for mainnet addresses private_wif = bytes.fromhex(f"80{hex(self.private_key)[2:]:0>64}") private_wif_comp = bytes.fromhex( f"80{hex(self.private_key)[2:]:0>64}01") # Perform SHA-256 hash on the extended key. # Perform SHA-256 hash on result of SHA-256 hash. checksum = sha256(sha256(private_wif)) checksum_comp = sha256(sha256(private_wif_comp)) # Take the first 4 bytes of the second SHA-256 hash; this is the checksum. # Add the checksum bytes at the end of the extended key. private_wif = f"{private_wif.hex()}{checksum.hex()[:8]}" private_wif_comp = f"{private_wif_comp.hex()}{checksum_comp.hex()[:8]}" # Convert the result from a byte string into a base58 string using Base58Check encoding. # This is the wallet import format (WIF). private_wif = base58.b58encode( bytes.fromhex(private_wif)).decode("UTF-8") private_wif_comp = base58.b58encode( bytes.fromhex(private_wif_comp)).decode("UTF-8")
return private_wif, private_wif_comp
def print_private_keys(self): """Printing private keys in WIF""" priv_key, priv_key_comp = self.__convert_private_to_wif() priv_hex = f'0x{hex(self.private_key)[2:]:0>64}' print(f"\nHEX - private key\n{priv_hex}") print(f"Length: {len(priv_hex)}\n") print(f"\nWIF - private key\n{priv_key}") print(f"Length: {len(priv_key)}\n") print(f"WIF compressed - private key\n{priv_key_comp}") print(f"Length: {len(priv_key_comp)}\n")
def print_public_keys(self): """Printing public key in different formats""" public_key = self.__compute_public() print("\nThe uncompressed public key (not address):") print(public_key) print("\nThe uncompressed public key (HEX):") uncomp_pub = f"04{(hex(public_key[0])[2:]):0>64}{(hex(public_key[1])[2:]):0>64}" print(uncomp_pub) print(f"Length: {len(uncomp_pub)}\n") uncomp_addr = self.__public_address(uncomp_pub) print(f"Address from uncompressed key\n{uncomp_addr}") print("\nThe official Public Key - compressed:") if public_key[1] % 2 == 1: # If the Y value for the Public Key is odd. comp_pub = f"03{hex(public_key[0])[2:]:0>64}" print(comp_pub) print(f"Length: {len(comp_pub)}\n") comp_addr = self.__public_address(comp_pub) print(f"Address from compressed key\n{comp_addr}") else: # Or else, if the Y value is even. comp_pub = f"02{hex(public_key[0])[2:]:0>64}" print(comp_pub) print(f"Length: {len(comp_pub)}\n") comp_addr = self.__public_address(comp_pub) print(f"Address from compressed key\n{comp_addr}")
prompt = input(f"Please insert your private key in HEX format (0x): ")
my_key = PublicKey(prompt) my_key.print_public_keys() my_key.print_private_keys() for i in range(11, 16): my_key.private_key = hex(i) my_key.print_public_keys() my_key.print_private_keys()
Source: https://github.com/witcher-sense/learning_python/blob/main/PrivateToPublic_v2.py
|
|
|
|
ramakrishna0905
Newbie
Offline
Activity: 3
Merit: 0
|
|
December 15, 2022, 01:25:57 PM Last edit: December 15, 2022, 01:50:47 PM by ramakrishna0905 |
|
Hi Any one have python code for Public key to address converter Public key =Input.txt file Address = output.txt file
Input file to output file
|
|
|
|
witcher_sense
Legendary
Offline
Activity: 2436
Merit: 4408
🔐BitcoinMessage.Tools🔑
|
|
December 15, 2022, 01:56:37 PM |
|
Hi Any one have python code for Public key to address converter Public key =Input.txt file Address = output.txt file
Input file to output file
I spent ten minutes writing this program, I hope you like it. Ask questions if there is something you don't understand. Please do not use this script for any serious stuff! Pub keys need to be in this format: 02F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9 02E493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13 03421F5FC9A21065445C96FDB91C0C1E2F2431741C72713B4B99DDCB316F31E9FC ...
Result: 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb 1JtK9CQw1syfWj1WtFMWomrYdV3W2tWBF9 1GWqhXL1DGAEc6MVntAnmQe4GFnR9k2Ykd ... import base58 import sys
# these modules are taken from here https://github.com/karpathy/cryptos/tree/main/cryptos from ripemd160 import ripemd160 from sha256 import sha256
def public_address(key): key = bytes.fromhex(key) address = bytes.fromhex(f"00{ripemd160(sha256(key)).hex()}") checksum = sha256(sha256(address)) address = f"{address.hex()}{checksum.hex()[:8]}" address = base58.b58encode(bytes.fromhex(address)).decode("UTF-8") return address
if len(sys.argv) < 3: print('Usage: app.py <path/to/src> <path/to/dest>') sys.exit(1)
path_src = sys.argv[1] path_dest = sys.argv[2] addresses = []
with open(path_src, 'r', encoding='UTF-8') as file: pub_keys = file.readlines()
pub_keys = (pub_key.strip() for pub_key in pub_keys)
for pub_key in pub_keys: addr = public_address(pub_key) addresses.append(addr)
with open(path_dest, 'w', encoding='UTF-8') as file: for address in addresses: file.write(address + '\n')
|
|
|
|
ramakrishna0905
Newbie
Offline
Activity: 3
Merit: 0
|
|
December 15, 2022, 02:00:28 PM |
|
Thankyou
I will check and get back to you
|
|
|
|
ramakrishna0905
Newbie
Offline
Activity: 3
Merit: 0
|
|
December 16, 2022, 06:37:17 AM |
|
Hi Any one have python code for Public key to address converter Public key =Input.txt file Address = output.txt file
Input file to output file
I spent ten minutes writing this program, I hope you like it. Ask questions if there is something you don't understand. Please do not use this script for any serious stuff! Pub keys need to be in this format: 02F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9 02E493DBF1C10D80F3581E4904930B1404CC6C13900EE0758474FA94ABE8C4CD13 03421F5FC9A21065445C96FDB91C0C1E2F2431741C72713B4B99DDCB316F31E9FC ...
Result: 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb 1JtK9CQw1syfWj1WtFMWomrYdV3W2tWBF9 1GWqhXL1DGAEc6MVntAnmQe4GFnR9k2Ykd ... import base58 import sys
# these modules are taken from here https://github.com/karpathy/cryptos/tree/main/cryptos from ripemd160 import ripemd160 from sha256 import sha256
def public_address(key): key = bytes.fromhex(key) address = bytes.fromhex(f"00{ripemd160(sha256(key)).hex()}") checksum = sha256(sha256(address)) address = f"{address.hex()}{checksum.hex()[:8]}" address = base58.b58encode(bytes.fromhex(address)).decode("UTF-8") return address
if len(sys.argv) < 3: print('Usage: app.py <path/to/src> <path/to/dest>') sys.exit(1)
path_src = sys.argv[1] path_dest = sys.argv[2] addresses = []
with open(path_src, 'r', encoding='UTF-8') as file: pub_keys = file.readlines()
pub_keys = (pub_key.strip() for pub_key in pub_keys)
for pub_key in pub_keys: addr = public_address(pub_key) addresses.append(addr)
with open(path_dest, 'w', encoding='UTF-8') as file: for address in addresses: file.write(address + '\n')
Thankyou very much It's working perfectly ...
|
|
|
|
|