Title: Help in Programming bitcoin python module Post by: unknowncustomer on May 25, 2023, 09:23:22 PM 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. Title: Re: Help in Programming bitcoin python module Post by: PowerGlove on May 26, 2023, 05:12:21 AM Howdy! :)
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 That should work identically to the missing function (i.e. print(hex(int.from_bytes(hash256(b'my message'), 'big'))) should emit 0x231c6f3d980a6b0fb7152f85cee7eb52bf92433d9919b9c5218cb08e79cce78). Title: Re: Help in Programming bitcoin python module Post by: unknowncustomer on May 26, 2023, 08:28:06 AM 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 !! Title: Re: Help in Programming bitcoin python module Post by: witcher_sense on May 26, 2023, 08:32:22 AM print(h(h("my secret".encode()).hexdigest().encode()).hexdigest()) 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.It's almost working however the result does not match as it is not starting with 0x. 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()}') Title: Re: Help in Programming bitcoin python module Post by: PowerGlove on May 26, 2023, 10:29:45 AM OMG it is working thanks a lot !! I'm glad I could help! :)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: https://talkimg.com/images/2023/05/26/Before8e5d19e3919999bb.gif But after a while, you'll start to feel like this guy: https://talkimg.com/images/2023/05/26/Afterd7bcc36a958275f1.gif Just keep practicing. (Feel free to send me a PM if you get really stuck on something.) ;) Title: Re: Help in Programming bitcoin python module Post by: ABCbits on May 26, 2023, 10:39:52 AM 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 (https://github.com/bitcoinbook/bitcoinbook) [2] https://learnmeabitcoin.com/ (https://learnmeabitcoin.com/) Title: Re: Help in Programming bitcoin python module Post by: bettercrypto on May 26, 2023, 04:51:46 PM 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 |