@Override public ExtendedPrivateKey cKDpriv(final int index) { final byte[] data = new byte[37]; final ByteArrayWriter writer = new ByteArrayWriter(data); writer.concat((byte) 0); writer.concat(hdKey.getKey(), 32); } else { writer.concat(hdKey.getPoint()); writer.concatSer32(index); final byte[] Il = head32(I); final byte[] Ir = tail32(I);
byte[] serialize(final byte[] key, final byte[] chainCode) { if (key == null) throw new IllegalArgumentException("Key is null"); if (chainCode == null) throw new IllegalArgumentException("Chain code is null"); if (chainCode.length != 32) throw new IllegalArgumentException("Chain code must be 32 bytes"); if (neutered) { if (key.length != 33) throw new IllegalArgumentException("Key must be 33 bytes for neutered serialization"); } else { if (key.length != 32) throw new IllegalArgumentException("Key must be 32 bytes for non neutered serialization"); } final byte[] privateKey = new byte[82]; final ByteArrayWriter writer = new ByteArrayWriter(privateKey); writer.concatSer32(getVersion()); writer.concat((byte) depth); writer.concatSer32(fingerprint); writer.concatSer32(childNumber); writer.concat(chainCode); if (!neutered) { writer.concat((byte) 0); writer.concat(key); } else { writer.concat(key); } writer.concat(checksum(privateKey), 4); return privateKey; }
@Override public ExtendedPublicKey cKDpub(final int index) { if (isHardened(index)) throw new IllegalCKDCall("Cannot derive a hardened key from a public key"); final HdKey parent = this.hdKey; final byte[] kPar = parent.getKey(); final byte[] data = new byte[37]; final ByteArrayWriter writer = new ByteArrayWriter(data); writer.concat(kPar, 33); writer.concatSer32(index); final byte[] I = hmacSha512(parent.getChainCode(), data); final byte[] Il = head32(I); final byte[] Ir = tail32(I); final BigInteger parse256_Il = parse256(Il); final ECPoint ki = gMultiplyAndAddPoint(parse256_Il, kPar); if (parse256_Il.compareTo(n()) >= 0 || ki.isInfinity()) { return cKDpub(index + 1); } final byte[] key = pointSerP(ki); return new ExtendedPublicKey(new HdKey.Builder() .network(parent.getNetwork()) .neutered(true) .depth(parent.depth() + 1) .parentFingerprint(parent.calculateFingerPrint()) .key(key) .chainCode(Ir) .childNumber(index) .build()); }
void concat(final byte[] bytesSource) { concat(bytesSource, bytesSource.length); }