Welcome to the Dash Forum!

Please sign up to discuss the most innovative cryptocurrency!

How convert WIF to address

Discussion in 'Development Tech Discussion' started by Jon789, Apr 3, 2018.

  1. Jon789

    Jon789 New Member

    Joined:
    Apr 3, 2018
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    please tell me how to get the address from the code with this code:

    import hashlib

    p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
    #r = 0xd47ce4c025c35ec440bc81d99834a624875161a26bf56ef7fdc0f5d52f843ad1
    #s1 = 0x44e1ff2dfd8102cf7a47c21d5c9fd5701610d04953c6836596b4fe9dd2f53e3e
    #s2 = 0x9a5f1c75e461d7ceb1cf3cab9013eb2dc85b6d0da8c3c6e27e3a5a5b3faa5bab
    z1 = 0xc0e2d0a89a348de88fda08211c70d1d7e52ccef2eb9459911bf977d587784c6e
    z2 = 0x17b0f41c8c337ac1e18c98759e83a8cccbc368dd9d89e5f03cb633c265fd0ddc

    # r1 and s1 are contained in this ECDSA signature encoded in DER (openssl default).
    der_sig1 = "30440220d47ce4c025c35ec440bc81d99834a624875161a26bf56ef7fdc0f5d52f843ad1022044e1ff2dfd8102cf7a47c21d5c9fd5701610d04953c6836596b4fe9dd2f53e3e01"

    # the same thing with the above line.
    der_sig2 = "30440220d47ce4c025c35ec440bc81d99834a624875161a26bf56ef7fdc0f5d52f843ad102209a5f1c75e461d7ceb1cf3cab9013eb2dc85b6d0da8c3c6e27e3a5a5b3faa5bab01"

    params = {'p':p,'sig1':der_sig1,'sig2':der_sig2,'z1':z1,'z2':z2}

    def hexify (s, flip=False):
    if flip:
    return s[::-1].encode ('hex')
    else:
    return s.encode ('hex')

    def unhexify (s, flip=False):
    if flip:
    return s.decode ('hex')[::-1]
    else:
    return s.decode ('hex')

    def inttohexstr(i):
    tmpstr = hex(i)
    hexstr = tmpstr.replace('0x','').replace('L','').zfill(64)
    return hexstr

    b58_digits = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

    def dhash(s):
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()

    def rhash(s):
    h1 = hashlib.new('ripemd160')
    h1.update(hashlib.sha256(s).digest())
    return h1.digest()

    def base58_encode(n):
    l = []
    while n > 0:
    n, r = divmod(n, 58)
    l.insert(0,(b58_digits[r]))
    return ''.join(l)

    def base58_encode_padded(s):
    res = base58_encode(int('0x' + s.encode('hex'), 16))
    pad = 0
    for c in s:
    if c == chr(0):
    pad += 1
    else:
    break
    return b58_digits[0] * pad + res

    def base58_check_encode(s, version=0):
    vs = chr(version) + s
    check = dhash(vs)[:4]
    return base58_encode_padded(vs + check)

    def get_der_field(i,binary):
    if (ord(binary) == 02):
    length = binary[i+1]
    end = i + ord(length) + 2
    string = binary[i+2:end]
    return string
    else:
    return None

    # Here we decode a DER encoded string separating r and s
    def der_decode(hexstring):
    binary = unhexify(hexstring)
    full_length = ord(binary[1])
    if ((full_length + 3) == len(binary)):
    r = get_der_field(2,binary)
    s = get_der_field(len(r)+4,binary)
    return r,s
    else:
    return None

    def show_results(privkeys):
    print "Posible Candidates..."
    for privkey in privkeys:
    hexprivkey = inttohexstr(privkey)
    print "intPrivkey = %d" % privkey
    print "hexPrivkey = %s" % hexprivkey
    print "bitcoin Privkey (WIF) = %s" % base58_check_encode(hexprivkey.decode('hex'),version=128)
    print "bitcoin Privkey (WIF compressed) = %s" % base58_check_encode((hexprivkey + "01").decode('hex'),version=128)
     
  2. UdjinM6

    UdjinM6 Official Dash Dev
    Dash Core Team Moderator

    Joined:
    May 20, 2014
    Messages:
    3,633
    Likes Received:
    3,533
    Trophy Points:
    1,183
  3. demo

    demo Well-known Member

    Joined:
    Apr 23, 2016
    Messages:
    3,114
    Likes Received:
    262
    Trophy Points:
    153
    Dash Address:
    XnpT2YQaYpyh7F9twM6EtDMn1TCDCEEgNX
  4. UdjinM6

    UdjinM6 Official Dash Dev
    Dash Core Team Moderator

    Joined:
    May 20, 2014
    Messages:
    3,633
    Likes Received:
    3,533
    Trophy Points:
    1,183
    That's kind of offtopic (hashing is not encryption) and not WIF related (WIF stands for Wallet Import Format and has nothing to do with encryption) but the scheme described in the article (ECDH) is completely legit and well known (https://en.wikipedia.org/wiki/Elliptic-curve_Diffie–Hellman). Wether you use private keys in WIF format or any other format doesn't matter as long as underlying crypto (math) works. Using private keys for addresses that no longer hold funds in some script to sign/decrypt messages should be safe. I wouldn't recommend this method if you reuse addresses however (i.e. that address still holds some funds because your wallet use the same address as a change address or anything like that).
     
    • Useful Useful x 1
  5. demo

    demo Well-known Member

    Joined:
    Apr 23, 2016
    Messages:
    3,114
    Likes Received:
    262
    Trophy Points:
    153
    Dash Address:
    XnpT2YQaYpyh7F9twM6EtDMn1TCDCEEgNX
    My goal is to sign and encrypt with the address I have money in it. This is how I put my words where my money is.

    As far as I understand from your answer, it is a better tactic to avoid exposing your private key to unknown scripts, and you have better use the address that contains the money in order to just sign a single message/statement that says that an empty address will be used for encryption. And then use this empty address in order to encrypt. So in case there is bug in the script you are using for encrypt messages, the compromised private key will be worthless and you may sign (and timestamp of course) a new message with a new empty address, that will be known for now on as your new legitimate one .
     
    #5 demo, Apr 15, 2018
    Last edited: Apr 15, 2018
    • Agree Agree x 1

Share This Page