IoTone / TheShellProject

The Shell Project aims to build a viable general purpose blockchain written in Dlang, with attributes of security, ease of deployment, clear specification, ease of use for DAPPs, and multiple implementation interop
1 stars 1 forks source link

Need a way to convert a public key to an address #9

Open truedat101 opened 6 years ago

truedat101 commented 6 years ago

Review NEM's approach: https://docs.nem.io/en/address-components/address-components-2 Review section 2.3 https://www.nem.io/wp-content/themes/nem/files/NEM_techRef.pdf

Steps:

  1. Perform 256-bit Sha3 on the public key
  2. Perform 160-bit Ripemd of hash resulting from step 1.
  3. Prepend version byte to Ripemd hash (either 0x68 or 0x98)
  4. Perform 256-bit Sha3 on the result, take the first four bytes as a checksum
  5. Concatenate output of step 3 and the checksum from step 4
  6. Encode result using base32

This is the JS code directly out of their example in the documentation:

private static String generateEncoded(final byte version, final byte[] publicKey) {
                // step 1: sha3 hash of the public key
                final byte[] sha3PublicKeyHash = Hashes.sha3_256(publicKey);

                // step 2: ripemd160 hash of (1)
                final byte[] ripemd160StepOneHash = Hashes.ripemd160(sha3PublicKeyHash);

                // step 3: add version byte in front of (2)
                final byte[] versionPrefixedRipemd160Hash = ArrayUtils.concat(new byte[] { version }, ripemd160StepOneHash);

                // step 4: get the checksum of (3)
                final byte[] stepThreeChecksum = generateChecksum(versionPrefixedRipemd160Hash);

                // step 5: concatenate (3) and (4)
                final byte[] concatStepThreeAndStepSix = ArrayUtils.concat(versionPrefixedRipemd160Hash, stepThreeChecksum);

                // step 6: base32 encode (5)
                return Base32Encoder.getString(concatStepThreeAndStepSix);
        }
truedat101 commented 6 years ago

NOTE, it looks like the NEM project is using Bouncy Castle and also a java wrapper around NACL.

pedroalvesbatista commented 6 years ago

Are all that features covered in libsodium, I mean, the SHA-3 algorithm and the checksum proccesses ?

truedat101 commented 6 years ago

Look at the api. My understanding is we have everything we need in libsodium, however, we need to perform steps 1-6 using those apis.

truedat101 commented 6 years ago

Moving this into next milestone.

pedroalvesbatista commented 6 years ago

I'm starting to generate some skeleton here and scratch the algorithm, based on the updates you @truedat101 made with genericHash.

pedroalvesbatista commented 5 years ago

I'm dealing with this now ! Following the implementation from the docs.

pedroalvesbatista commented 5 years ago

What about the step 1 on this ?

pedroalvesbatista commented 5 years ago

http://translate.google.com.tr/translate?sl=tr&tl=en&js=n&prev=_t&hl=tr&ie=UTF-8&eotf=1&u=http%3A%2F%2Fddili.org%2Fforum%2Fthread%2F1094&act=url

This is how Base32 encoding is treated.

@acehreli Take a look on what we found here :D A post of yours from some years ago, very awesome !

pedroalvesbatista commented 5 years ago

https://code.dlang.org/packages/base32

The link for Base32 from dub registry.

pedroalvesbatista commented 5 years ago

I got this error :

source/addresscore.d(28,8): Error: module base32 is in file 'base32.d' which cannot be read import path[0] = source/ import path[1] = ../../../../../.dub/packages/dddb-0.0.6/dddb/source/ import path[2] = ../../../../../.dub/packages/sodium-0.1.7/sodium/deimos import path[3] = /usr/include/dmd/phobos import path[4] = /usr/include/dmd/druntime/import

I runned dub run base32 and was included into the packages of the project.

truedat101 commented 5 years ago

You forgot to add to the dub.json dependency

truedat101 commented 5 years ago

@pedroalvesbatista I added the exact code used in the JS to do the public key generation from an address . See description.