private void parseBip38(String key) throws KeyFormatException { byte[] bytes = parseBase58(key); if (version != 0x01) clearDataAndThrow(bytes, "Mismatched version number: " + version); if (bytes.length != 38) clearDataAndThrow(bytes, "Wrong number of bytes, excluding version byte: " + bytes.length); hasLotAndSequence = (bytes[1] & 0x04) != 0; // bit 2 compressed = (bytes[1] & 0x20) != 0; // bit 5 if ((bytes[1] & 0x01) != 0) // bit 0 clearDataAndThrow(bytes, "Bit 0x01 reserved for future use."); if ((bytes[1] & 0x02) != 0) // bit 1 clearDataAndThrow(bytes, "Bit 0x02 reserved for future use."); if ((bytes[1] & 0x08) != 0) // bit 3 clearDataAndThrow(bytes, "Bit 0x08 reserved for future use."); if ((bytes[1] & 0x10) != 0) // bit 4 clearDataAndThrow(bytes, "Bit 0x10 reserved for future use."); final int byte0 = bytes[0] & 0xff; if (byte0 == 0x42) { clearDataAndThrow(bytes, "Bits 0x40 and 0x80 must be set for non-EC-multiplied keys."); ecMultiply = false; if (hasLotAndSequence) clearDataAndThrow(bytes, "Non-EC-multiplied keys cannot have lot/sequence."); } else if (byte0 == 0x43) { clearDataAndThrow(bytes, "Bits 0x40 and 0x80 must be cleared for EC-multiplied keys."); ecMultiply = true; } else { clearDataAndThrow(bytes, "Second byte must by 0x42 or 0x43.");
private void parseMini(String key) throws KeyFormatException { byte[] bytes = key.getBytes(); byte[] checkBytes = new byte[31]; // 30 chars + '?' List<byte[]> allBytes = ImmutableList.of(bytes, checkBytes); if (!key.startsWith("S")) { clearDataAndThrow(allBytes, "Mini private keys must start with 'S'"); } if (bytes.length != 30) { clearDataAndThrow(allBytes, "Mini private keys must be 30 characters long"); } System.arraycopy(bytes, 0, checkBytes, 0, 30); checkBytes[30] = '?'; // Check if the sha256 hash of key + "?" starts with 0x00 if (Sha256Hash.create(checkBytes).getBytes()[0] != 0x00) { clearDataAndThrow(allBytes, "Not well formed mini private key"); } compressed = false; // Mini keys are not compressed content = Sha256Hash.create(bytes).getBytes(); clearData(allBytes); }
private void parseWif(String key) throws KeyFormatException { byte[] keyBytes = parseBase58(key); // Check if compatible boolean isCompatible = false; for (CoinType type : CoinID.getSupportedCoins()) { if (version == type.getDumpedPrivateKeyHeader()) { isCompatible = true; } } if (!isCompatible) { clearDataAndThrow(keyBytes, "No coin with private key version: " + version); } if (keyBytes.length == 33 && keyBytes[32] == 1) { compressed = true; content = Arrays.copyOf(keyBytes, 32); // Chop off the additional marker byte. clearData(keyBytes); } else if (keyBytes.length == 32) { compressed = false; content = keyBytes; } else { clearDataAndThrow(keyBytes, "Wrong number of bytes for a private key, not 32 or 33"); } }