4

I tried to get a private view key but somehow I can't get it to work. However, I do manage to get the hash of the seed from the hexadecimal seed created from the mnemonic.

The hexadecimal seed was obtained from here for testing. I seem to manage to get the correct hash of th seed by keccak_256 hexadecimal seed.

I read many sites state that private view keys can be obtained by sc_reduce32 the hash of seed. But I don't get it working.

Here is my code; it runs in Python 2.7.

Can someone show me how to get the private view key from the hash of the seed?

If possible I prefer the method to work in Python 2.7 since most of my previous work was done in that Python version.

Glorfindel
  • 155
  • 1
  • 1
  • 9

1 Answers1

1

Your sc_reduce32 function was using the wrong value to reduce by, and you had not reversed the endianness of your bytes. Here is working code:

import sha3

l = 2 ** 252 + 27742317777372353535851937790883648493


def reverse_byte_order(hex):
    if(len(hex)%2==1): hex = '0' + hex
    return "".join(reversed([hex[i:i+2] for i in range(0, len(hex), 2)]))


def sc_reduce32(key):
    return reverse_byte_order("%x" % int((int(reverse_byte_order(key), 16) % l)))


def cn_fast_hash(hex):
    k = sha3.keccak_256()
    k.update(bytearray.fromhex(hex))
    return k.hexdigest()


hexadecimal_seed = "852249bca4446e65501cc7f8338027ec"
hash_of_seed = cn_fast_hash(hexadecimal_seed)
private_spend_key = sc_reduce32(hash_of_seed)
private_view_key = sc_reduce32(cn_fast_hash(hash_of_seed))

print "Non-standard Seed = ", hexadecimal_seed
print "Normalized Seed   = ", hash_of_seed
print "Private Spend Key = ", private_spend_key
print "Private View Key  = ", private_view_key
knaccc
  • 8,518
  • 17
  • 23