additional document for BIP32

0

I am reading through BIP32's documentation and its implementation in Java on GitHub in the file supernode/api/src/main/java/com/bitsofproof/supernode/api/ExtendedKey.java.

Luckily, I was able to follow the code, but there are areas I am not sure about.

I have 2 questions:

  1. I know it is in BIP32.json file, but why are we doing if ((sequence & 0x8000000)==0)? It is a condition for master public key to child public key or master private key to child private key... but what sequence does for? if ( (sequence & 0x80000000) == 0 )

  2. My second one is why we add 4 bytes. Is there any detail document source I can find?

            extended = new byte[pub.length + 4];
            System.arraycopy (pub, 0, extended, 0, pub.length);
            extended[pub.length] = (byte) ((sequence >>> 24) & 0xff);
            extended[pub.length + 1] = (byte) ((sequence >>> 16) & 0xff);
            extended[pub.length + 2] = (byte) ((sequence >>> 8) & 0xff);
            extended[pub.length + 3] = (byte) (sequence & 0xff);
        }
        else
        {
            byte[] priv = master.getPrivate ();
            extended = new byte[priv.length + 5];
            System.arraycopy (priv, 0, extended, 1, priv.length);
            extended[priv.length + 1] = (byte) ((sequence >>> 24) & 0xff);
            extended[priv.length + 2] = (byte) ((sequence >>> 16) & 0xff);
            extended[priv.length + 3] = (byte) ((sequence >>> 8) & 0xff);
            extended[priv.length + 4] = (byte) (sequence & 0xff);
        }
    

Many thanks in advance.

Johnny C

Posted 2017-01-19T19:30:30.163

Reputation: 11

Welcome to Bitcoin.SE! You can help the site by marking answers as accepted if they are correct and address your question so that the question does not remain as "unanswered".Willtech 2018-03-04T07:48:56.450

Answers

3

Check https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format

((sequence & 0x80000000)==0) means it's not a hardened key. The key is hardened (public key derivation not possible) for child keys with index higher then 0x80000000 (== 2147483648 == 2^31).

From the BIP

4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private)
1 byte: depth: 0x00 for master nodes, 0x01 for level-1 derived keys, ....
4 bytes: the fingerprint of the parent's key (0x00000000 if master key)
4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key)
32 bytes: the chain code
33 bytes: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys)

Jonas Schnelli

Posted 2017-01-19T19:30:30.163

Reputation: 5 465

very cool... got it confused with serialize method below. I thought something different.Johnny C 2017-01-19T23:27:10.853