public SerializedKey(String key) throws KeyFormatException { if (PATTERN_WIF_PRIVATE_KEY.matcher(key).matches()) { parseWif(key); keyType = Type.WIF; } else if (PATTERN_BIP38_PRIVATE_KEY.matcher(key).matches()) { parseBip38(key); keyType = Type.BIP38; } else if (PATTERN_MINI_PRIVATE_KEY.matcher(key).matches()) { parseMini(key); keyType = Type.MINI; } else { throw new KeyFormatException("Unknown key format."); } }
private boolean validatePrivateKey(boolean isTyping) { if (privateKeyText == null) return false; String privateKey = privateKeyText.getText().toString().trim(); if (privateKey.isEmpty()) return false; try { serializedKey = new SerializedKey(privateKey); error = Error.NONE; } catch (SerializedKey.KeyFormatException e) { serializedKey = null; if (isTyping) { error = Error.NONE; } else { log.info("Invalid private key: {}", e.getMessage()); error = Error.BAD_FORMAT; } } updateView(); return serializedKey != null; }
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; }
private void clearDataAndThrow(byte[] bytes, String message) throws KeyFormatException { clearData(bytes); throw new KeyFormatException(message); }
private void clearDataAndThrow(List<byte[]> bytes, String message) throws KeyFormatException { clearData(bytes); throw new KeyFormatException(message); }