private static void clearData(List<byte[]> bytes) { for (byte[] b : bytes) { clearData(b); } }
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 clearDataAndThrow(byte[] bytes, String message) throws KeyFormatException { clearData(bytes); throw new KeyFormatException(message); }
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"); } }
private void clearDataAndThrow(List<byte[]> bytes, String message) throws KeyFormatException { clearData(bytes); throw new KeyFormatException(message); }
private byte[] parseBase58(String key) throws KeyFormatException { byte[] versionAndDataBytes; try { versionAndDataBytes = Base58.decodeChecked(key); } catch (AddressFormatException e) { throw new KeyFormatException(e); } version = versionAndDataBytes[0] & 0xFF; byte[] payload = new byte[versionAndDataBytes.length - 1]; System.arraycopy(versionAndDataBytes, 1, payload, 0, versionAndDataBytes.length - 1); clearData(versionAndDataBytes); return payload; }