<p>Check <a href="https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format" rel="nofollow noreferrer">https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format</a></p>
<p><code>((sequence &amp; 0x80000000)==0)</code> 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).</p>
<h3>From the BIP</h3>
<pre><code>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)
</code></pre>

CC BY-SA 3.0
2017-01-19T23:27:10.853Z
very cool... got it confused with serialize method below.  I thought something different.
Johnny C
CC BY-SA 3.0
2017-01-19T20:00:57.957Z
2018-03-03T22:32:32.400Z
2020-06-16T11:10:03.350Z
Jonas Schnelli
<p>I am reading through BIP32's documentation and its implementation in Java on GitHub in the file <code>supernode/api/src/main/java/com/bitsofproof/supernode/api/ExtendedKey.java</code>.</p>

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

<p>I have 2 questions:</p>

<ol>
<li><p>I know it is in BIP32.json file, but why are we doing <code>if ((sequence &amp; 0x8000000)==0)</code>?  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? <code>if ( (sequence &amp; 0x80000000) == 0 )</code></p></li>
<li><p>My second one is why we add 4 bytes.   Is there any detail document source I can find?</p>

<pre><code>        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);
    }
</code></pre></li>
</ol>

<p>Many thanks in advance.</p>

CC BY-SA 3.0
2018-03-04T07:48:56.450Z
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
<bip32-hd-wallets>
additional document for BIP32
Johnny C
# additional document for BIP32

(Asked by: username_0 on 2017-01-19)

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.

**Tags:** bip32-hd-wallets

> **Comments:**
> **username_1**: 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".  

---

## 1 answers

### Answer by username_2 on 2017-01-19. Score: 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)

```

> **Comments:**
> **username_0**: very cool... got it confused with serialize method below.  I thought something different.  

---

