Python Forum
Hi all any help with this python code please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi all any help with this python code please
#1
Hi all hope every one is very well, its been a very long time since i done any programming over 10
years ago atleast, im looking to make a application its a bitcoin wallet, but i want to learn a few steps
before i dive deep into the whole btc thing, i want to try and familiarize myself with python again.

I was wondering if any one could possibly point out the obvious im looking at this code below and was
wondering if any one would be able to help me implement a main.loop() of some type and also how can
i make decisions based on the value returned if the btc address in the below code is not an actual
address it will print false to the interpreter window, and true if the string is a valid bitcoin address

what i would like to do for starters is have the code loop without using too much memory when i tried
this last night it was just eating my cpu 70-80% my implementation of looping the bitcoin checking
function over and over again seemed to bog my system down allot.

What im hoping to learn and implement from this code a main loop when either and exception is hit
or if the value returned false i would like it too print "1" and when false is returned i would like
it too go back to the beginning of the start of the btc check function and keep looping until a valid
address is found once it is found print "1".

I did manage to get a working version last night but really did eat allot of cpu i was wondering if any
one would be able to implement the above into this code just as an example, Mine was very poor and just
ate resources.

Or even something simple like if True is returned do this if false is returned do that ?Any help would be very much appreciated.It could be the way its structured but after 2hrs of messing around i had a working example
was embarrassing lol but it worked but just sat and ate cpu.

from hashlib import sha256
 
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
 
def decode_base58(bc, length):
    n = 0
    for char in bc:
        n = n * 58 + digits58.index(char)
    return n.to_bytes(length, 'big')
def check_bc(bc):
    try:
        bcbytes = decode_base58(bc, 25)
        return bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]
    except Exception:
        return False
 
print(check_bc('1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i'))
print(check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j"))
Or if some one could run through the code with my too better understand what is going on
thank you in advance.obviously this takes two hardcoded strings returns true for the correct
btc address and false is address is not a btc address, and the exception or even none gets returned
if there is an issue.Any help would be Vmuch appreciated.

It could be the way its formatted in the above example as to why im struggling to get it two do A if true
is returned and B if false is returned and if true loop back to the beginning again.

Credit to rosettacode.org for the above code.
Reply
#2
I'd like to help. But I don't understand what you want. So do you want the program to return false if an incorrect address is entered and true if a correct address is entered?

In that case, this would probably work.

from hashlib import sha256
  
digits58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
  
def decode_base58(bc, length):
    n = 0
    for char in bc:
        n = n * 58 + digits58.index(char)
    return n.to_bytes(length, 'big')
def check_bc(bc):
    try:
        bcbytes = decode_base58(bc, 25)
        if bcbytes[-4:] == sha256(sha256(bcbytes[:-4]).digest()).digest()[:4]:
            return True
        else:
            return False
    except Exception as e:
        return e #this line is just returning the exception
  
print(check_bc('1AGNa15ZQXAZUgFiqJ3i7Z2DPU2J6hW62i'))
print(check_bc("17NdbrSGoUotzeGCcMMCqnFkEvLymoou9j"))
That fixes the true-false issue. (Hopefully)
Now for the loop are these keys stored in a file? reply so i can help you.
Don't doubt always question. There's a difference.
Reply
#3
Hi thank you for your help it is Vmuch appreciated no they are fetched from a server which is yet to be coded but should not be too hard this basically checks that the user has in fact entered a bitcoin address and if not will pop up an error, i will do this with Tk maybe later but the address is basically passed like a variable.

btc_string = fetch.data()
print(check_bc(btc_string))

Basically keep checking that btc address passed by the variable and function
if returns either a boolean 0 or false or even 1 or true go to loop beginging
of code and start again and if false go to loop and execute function.



like passing the string as a variable to another function fetch.data()
do what ever i want to string.This works above but i could also hard code
the adresses like below its for my own personal use so wont be going to git
or any thing, just trying to freshen up and start learning again, i would like
it to keep checking the btc address if false then go back to the beginning and
start all the code again.



Im not 100% sure but it does what i asked too at the moment it prints false and
true which will be changed for different parts of this script its only simple
to start with..Its hard for me to explain fully, but i was going to make it loop
when it hits false or even the exception is reached, then if true call function
do my bit of code then return back to a loop the program i will be adding these
by hand i think for starters, just till i get a better grip on python..


i might have to find a different method i will read more tonight.thanks for your
help mate.
Reply
#4
(Mar-22-2018, 12:13 AM)BooohWhooo2018 Wrote: Hi thank you for your help it is Vmuch appreciated no they are fetched from a server which is yet to be coded but should not be too hard this basically checks that the user has in fact entered a bitcoin address and if not will pop up an error, i will do this with Tk maybe later but the address is basically passed like a variable.

btc_string = fetch.data()
print(check_bc(btc_string))

Basically keep checking that btc address passed by the variable and function
if returns either a boolean 0 or false or even 1 or true go to loop beginging
of code and start again and if false go to loop and execute function.



like passing the string as a variable to another function fetch.data()
do what ever i want to string.This works above but i could also hard code
the adresses like below its for my own personal use so wont be going to git
or any thing, just trying to freshen up and start learning again, i would like
it to keep checking the btc address if false then go back to the beginning and
start all the code again.



Im not 100% sure but it does what i asked too at the moment it prints false and
true which will be changed for different parts of this script its only simple
to start with..Its hard for me to explain fully, but i was going to make it loop
when it hits false or even the exception is reached, then if true call function
do my bit of code then return back to a loop the program i will be adding these
by hand i think for starters, just till i get a better grip on python..


i might have to find a different method i will read more tonight.thanks for your
help mate.

No problem. Glad to help. Good luck with the research. Smile
Don't doubt always question. There's a difference.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020