Bitcoin Forum
May 21, 2024, 04:35:58 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Help in Programming bitcoin python module  (Read 112 times)
unknowncustomer (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 27


View Profile
May 25, 2023, 09:23:22 PM
Merited by PowerGlove (4), ABCbits (1)
 #1

Hello,

I would need some help on Python:

I am trying to import helper package on Python with module hash256 like the colored lined of the below code however it’s not working.

from ecc import S256Point, G, N
from helper import hash256
e = int.from_bytes(hash256(b'my secret'), 'big')
z = int.from_bytes(hash256(b'my message'), 'big')
k = 1234567890
r = (k*G).x.num
k_inv = pow(k, N-2, N)
s=(z+r*e)*k_inv%N
point = e*G

print(point) # S256Point(028d003eab2e428d11983f3e97c3fa0addf3b42740df0d211795ffb3be2f6c52, \ 0ae987b9ec6ea159c78cb2a937ed89096fb218d9e7594f02b547526d8cd309e2)
print(hex(z)) # 0x231c6f3d980a6b0fb7152f85cee7eb52bf92433d9919b9c5218cb08e79cce78
print(hex(r)) # 0x2b698a0f0a4041b77e63488ad48c23e8e8838dd1fb7520408b121697b782ef22
print(hex(s)) # 0xbb14e602ef9e3f872e25fad328466b34e6734b7a0fcd58b1eb635447ffae8cb9
Or do you have an idea to do a double sha256 (it is hash256)?

I tried this:

from hashlib import sha256 as h

print(h(h("my secret".encode()).hexdigest().encode()).hexdigest())
It's almost working however the result does not match as it is not starting with 0x.
PowerGlove
Hero Member
*****
hacker
Offline Offline

Activity: 514
Merit: 4093



View Profile
May 26, 2023, 05:12:21 AM
Merited by ABCbits (1)
 #2

Howdy! Smiley

Yup, it looks to me like that hash256 function would've done a double SHA-256. You can define your own function to replace that import, like this:

Code:
from hashlib import sha256

def hash256(data):

    return sha256(sha256(data).digest()).digest()

That should work identically to the missing function (i.e. print(hex(int.from_bytes(hash256(b'my message'), 'big'))) should emit 0x231c6f3d980a6b0fb7152f85cee7eb52bf92433d9919b9c5218cb08e79cce78).
unknowncustomer (OP)
Jr. Member
*
Offline Offline

Activity: 48
Merit: 27


View Profile
May 26, 2023, 08:28:06 AM
 #3

OMG it is working thanks a lot !!
I am quite new to Python and Programming Bitcoin, any idea where I should start ? which books ?
Thanks !!
witcher_sense
Legendary
*
Offline Offline

Activity: 2352
Merit: 4369


🔐BitcoinMessage.Tools🔑


View Profile WWW
May 26, 2023, 08:32:22 AM
 #4

print(h(h("my secret".encode()).hexdigest().encode()).hexdigest())
It's almost working however the result does not match as it is not starting with 0x.
Your solution produces a completely different hash because before making a second hash, you represent bytes as hex values instead of raw bytes. Only use hexdigest() method after you have calculated a final hash.

Instead of

Code:
print(h(h("my secret".encode()).hexdigest().encode()).hexdigest())

Do

Code:
print(h(h("my secret".encode()).digest()).hexdigest())

You can also use f-strings to add 0x to final representation:

Code:
print(f'0x{h(h("my secret".encode()).digest()).hexdigest()}')

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
PowerGlove
Hero Member
*****
hacker
Offline Offline

Activity: 514
Merit: 4093



View Profile
May 26, 2023, 10:29:45 AM
 #5

OMG it is working thanks a lot !!
I'm glad I could help! Smiley

I am quite new to Python and Programming Bitcoin, any idea where I should start ? which books ?
I'd say you're already on the right path; just keep trying to do stuff and ask questions when you get really stuck. The best programmers I know basically taught themselves by trial-and-error. Books, and courses, etc. can help, but (in my experience) nothing will teach you better or faster than mindful practice.

In the beginning, you'll feel like this guy:



But after a while, you'll start to feel like this guy:



Just keep practicing. (Feel free to send me a PM if you get really stuck on something.) Wink
ABCbits
Legendary
*
Offline Offline

Activity: 2884
Merit: 7509


Crypto Swap Exchange


View Profile
May 26, 2023, 10:39:52 AM
Merited by PowerGlove (2)
 #6

OMG it is working thanks a lot !!
I am quite new to Python and Programming Bitcoin, any idea where I should start ? which books ?
Thanks !!

If you really insist to use book, people (including me) usually would recommend Mastering Bitcoin 2nd Edition[1]. Although if you prefer something shorter, check learn me a bitcoin website[2] instead. Aside from @PowerGlove said, i find reading documentation and example of certain library/function is helpful to prevent problem like this.

[1] https://github.com/bitcoinbook/bitcoinbook
[2] https://learnmeabitcoin.com/

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
bettercrypto
Sr. Member
****
Offline Offline

Activity: 1344
Merit: 268


★Bitvest.io★ Play Plinko or Invest!


View Profile WWW
May 26, 2023, 04:51:46 PM
 #7

OMG it is working thanks a lot !!
I am quite new to Python and Programming Bitcoin, any idea where I should start ? which books ?
Thanks !!

Well, perhaps this might help you, friend, if you really want to, just check if this is what you are looking for
Good luck...

source:

- https://bitcoin.stackexchange.com/questions/5671/how-do-you-perform-double-sha-256-encoding
- https://gist.github.com/vincenzopalazzo/78c3545090ce609a145f4f6261a7fa0f
- https://stackoverflow.com/questions/72411835/how-to-correctly-do-a-double-sha256-hashing



BIG WINNER!
[15.00000000 BTC]


▄████████████████████▄
██████████████████████
██████████▀▀██████████
█████████░░░░█████████
██████████▄▄██████████
███████▀▀████▀▀███████
██████░░░░██░░░░██████
███████▄▄████▄▄███████
████▀▀████▀▀████▀▀████
███░░░░██░░░░██░░░░███
████▄▄████▄▄████▄▄████
██████████████████████
▀████████████████████▀
▄████████████████████▄
██████████████████████
█████▀▀█▀▀▀▀▀▀██▀▀████
█████░░░░░░░░░░░░░▄███
█████░░░░░░░░░░░░▄████
█████░░▄███▄░░░░██████
█████▄▄███▀░░░░▄██████
█████████░░░░░░███████
████████░░░░░░░███████
███████░░░░░░░░███████
███████▄▄▄▄▄▄▄▄███████
██████████████████████
▀████████████████████▀
▄████████████████████▄
███████████████▀▀▀▀▀▀▀
███████████▀▀▄▄█░░░░░█
█████████▀░░█████░░░░█
███████▀░░░░░████▀░░░▀
██████░░░░░░░░▀▄▄█████
█████░▄░░░░░▄██████▀▀█
████░████▄░███████░░░░
███░█████░█████████░░█
███░░░▀█░██████████░░█
███░░░░░░████▀▀██▀░░░░
███░░░░░░███░░░░░░░░░░
▀██░▄▄▄▄░████▄▄██▄░░░░
▄████████████▀▀▀▀▀▀▀██▄
█████████████░█▀▀▀█░███
██████████▀▀░█▀░░░▀█░▀▀
███████▀░▄▄█░█░░░░░█░█▄
████▀░▄▄████░▀█░░░█▀░██
███░▄████▀▀░▄░▀█░█▀░▄░▀
█▀░███▀▀▀░░███░▀█▀░███░
▀░███▀░░░░░████▄░▄████░
░███▀░░░░░░░█████████░░
░███░░░░░░░░░███████░░░
███▀░██░░░░░░▀░▄▄▄░▀░░░
███░██████▄▄░▄█████▄░▄▄
▀██░████████░███████░█▀
▄████████████████████▄
████████▀▀░░░▀▀███████
███▀▀░░░░░▄▄▄░░░░▀▀▀██
██░▀▀▄▄░░░▀▀▀░░░▄▄▀▀██
██░▄▄░░▀▀▄▄░▄▄▀▀░░░░██
██░▀▀░░░░░░█░░░░░██░██
██░░░▄▄░░░░█░██░░░░░██
██░░░▀▀░░░░█░░░░░░░░██
██░░░░░▄▄░░█░░░░░██░██
██▄░░░░▀▀░░█░██░░░░░██
█████▄▄░░░░█░░░░▄▄████
█████████▄▄█▄▄████████
▀████████████████████▀




Rainbot
Daily Quests
Faucet
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!