additional document for BIP32 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:


I know it is in BIP32.json file, but why are we doing if ((sequence &amp; 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 &amp; 0x80000000) == 0 )
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 &gt;&gt;&gt; 24) &amp; 0xff);
        extended[pub.length + 1] = (byte) ((sequence &gt;&gt;&gt; 16) &amp; 0xff);
        extended[pub.length + 2] = (byte) ((sequence &gt;&gt;&gt; 8) &amp; 0xff);
        extended[pub.length + 3] = (byte) (sequence &amp; 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 &gt;&gt;&gt; 24) &amp; 0xff);
        extended[priv.length + 2] = (byte) ((sequence &gt;&gt;&gt; 16) &amp; 0xff);
        extended[priv.length + 3] = (byte) ((sequence &gt;&gt;&gt; 8) &amp; 0xff);
        extended[priv.length + 4] = (byte) (sequence &amp; 0xff);
    }



Many thanks in advance.
Check https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format
((sequence &amp; 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)