3

I need to create a Tezos public key hash based on a public ed25519 key only.

I am aware of an utility that generates entire keystore based on mnemonics - this is not what I need.

I'd welcome either a step by step description how to get from an edpk to a tz1 form or a code snippet.

Thanks.

4 Answers 4

8

Heres how to do it in python, where the variable pubkey is the bytes array of the public key

P2HASH_MAGIC = bytes.fromhex('06a1a4')
blake2bhash = blake2b(pubkey, digest_size=20).digest()
shabytes = sha256(sha256(P2HASH_MAGIC + blake2bhash).digest()).digest()[:4]
pkhash = b58encode(P2HASH_MAGIC + blake2bhash + shabytes).decode()
2
  • Thanks, that's great. Would you mind to improve that answer by adding comments explaining what happens there? For example - what is P2HASH_MAGIC and why do we cut out some bytes in line 3.
    – K SS
    Jan 16, 2020 at 19:47
  • Input for b58encode is going to be 51 bytes long (P2HASH_MAGIC is 3 bytes, blake2bhash is 20 bytes shabytes is 32 - 4 = 28 bytes) . This is going to produce a 49-char long base58check string. However, a tz1 address is 36-char long. Surely, this method is not correct, is it?
    – K SS
    Feb 11, 2020 at 15:05
5

Do you have the tezos repo built? If yes, you can run the ocaml CLI with tezos-crypto:

 $ dune utop src/lib_crypto

and then:

ocaml# open Tezos_crypto;;
ocaml# Ed25519.Public_key.of_b58check_exn
           "edpkuoK2J2UVbDcSTdJgP85JmDN3gxBCswcgApbtY5d7zHVunwCKNR"
       |> Ed25519.Public_key.hash
       |> Ed25519.Public_key_hash.to_b58check;;                                                                                                                                                      
- : string = "tz1L1bypLzuxGHmx3d6bHFJ2WCi4ZDbocSCA"
1
  • While this is probably a good answer, it is not helpful in my case. I need to do it programatically, in a nodejs app.
    – K SS
    Jan 16, 2020 at 19:41
4

In python, using the git version of pytezos:

from pytezos.crypto import Key

public_key = 'edpkuoK2J2UVbDcSTdJgP85JmDN3gxBCswcgApbtY5d7zHVunwCKNR'
hash = Key(public_key).public_key_hash()
print(hash)
1

In NodeJS it is also possible to do it using sotez:

import {Key} from 'sotez';

const sotezKey = new Key({ key: 'edpktx799pgw7M4z8551URER52VcENNCSZwE9f9cst4v6h5vCrQmJE' });
await sotezKey.ready;
console.log(sotezKey.publicKeyHash());

prints: tz1Xv78KMT7cHyVDLi9RmQP1KuWULHDafZdK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.