Code cleanup
This commit is contained in:
parent
9ca28ead66
commit
57057298a1
@ -21,7 +21,6 @@ import ch.dissem.bitmessage.entity.payload.Pubkey;
|
||||
import ch.dissem.bitmessage.entity.payload.V3Pubkey;
|
||||
import ch.dissem.bitmessage.entity.payload.V4Pubkey;
|
||||
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
|
||||
import ch.dissem.bitmessage.exception.ApplicationException;
|
||||
import ch.dissem.bitmessage.factory.Factory;
|
||||
import ch.dissem.bitmessage.ports.AddressRepository;
|
||||
import org.slf4j.Logger;
|
||||
@ -92,9 +91,12 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
|
||||
private List<BitmessageAddress> find(String where) {
|
||||
List<BitmessageAddress> result = new LinkedList<>();
|
||||
try (Connection connection = config.getConnection()) {
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed FROM Address WHERE " + where);
|
||||
ResultSet rs = stmt.executeQuery("SELECT address, alias, public_key, private_key, subscribed " +
|
||||
"FROM Address WHERE " + where)
|
||||
) {
|
||||
while (rs.next()) {
|
||||
BitmessageAddress address;
|
||||
|
||||
@ -126,9 +128,12 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
}
|
||||
|
||||
private boolean exists(BitmessageAddress address) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Address WHERE address='" + address.getAddress() + "'");
|
||||
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Address " +
|
||||
"WHERE address='" + address.getAddress() + "'")
|
||||
) {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1) > 0;
|
||||
}
|
||||
@ -152,7 +157,6 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
}
|
||||
|
||||
private void update(BitmessageAddress address) throws IOException, SQLException {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
StringBuilder statement = new StringBuilder("UPDATE Address SET alias=?");
|
||||
if (address.getPubkey() != null) {
|
||||
statement.append(", public_key=?");
|
||||
@ -161,7 +165,10 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
statement.append(", private_key=?");
|
||||
}
|
||||
statement.append(", subscribed=? WHERE address=?");
|
||||
PreparedStatement ps = connection.prepareStatement(statement.toString());
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
PreparedStatement ps = connection.prepareStatement(statement.toString())
|
||||
) {
|
||||
int i = 0;
|
||||
ps.setString(++i, address.getAlias());
|
||||
if (address.getPubkey() != null) {
|
||||
@ -177,9 +184,12 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
}
|
||||
|
||||
private void insert(BitmessageAddress address) throws IOException, SQLException {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
PreparedStatement ps = connection.prepareStatement(
|
||||
"INSERT INTO Address (address, version, alias, public_key, private_key, subscribed) VALUES (?, ?, ?, ?, ?, ?)");
|
||||
"INSERT INTO Address (address, version, alias, public_key, private_key, subscribed) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?)")
|
||||
) {
|
||||
ps.setString(1, address.getAddress());
|
||||
ps.setLong(2, address.getVersion());
|
||||
ps.setString(3, address.getAlias());
|
||||
@ -202,8 +212,10 @@ public class JdbcAddressRepository extends JdbcHelper implements AddressReposito
|
||||
|
||||
@Override
|
||||
public void remove(BitmessageAddress address) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
Statement stmt = connection.createStatement();
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
Statement stmt = connection.createStatement()
|
||||
) {
|
||||
stmt.executeUpdate("DELETE FROM Address WHERE address = '" + address.getAddress() + "'");
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
|
@ -61,11 +61,12 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
||||
if (cache.get(stream) == null) {
|
||||
result = new ConcurrentHashMap<>();
|
||||
cache.put(stream, result);
|
||||
|
||||
try (Connection connection = config.getConnection()) {
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT hash, expires FROM Inventory WHERE expires > "
|
||||
+ now(-5 * MINUTE) + " AND stream = " + stream);
|
||||
ResultSet rs = stmt.executeQuery("SELECT hash, expires FROM Inventory " +
|
||||
"WHERE expires > " + now(-5 * MINUTE) + " AND stream = " + stream)
|
||||
) {
|
||||
while (rs.next()) {
|
||||
result.put(new InventoryVector(rs.getBytes("hash")), rs.getLong("expires"));
|
||||
}
|
||||
@ -116,7 +117,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory {
|
||||
query.append(" AND version = ").append(version);
|
||||
}
|
||||
if (types.length > 0) {
|
||||
query.append(" AND type IN (").append(join(types)).append(")");
|
||||
query.append(" AND type IN (").append(join(types)).append(')');
|
||||
}
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
|
@ -27,10 +27,13 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
|
||||
|
||||
@Override
|
||||
public Item getItem(byte[] initialHash) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT data, version, nonce_trials_per_byte, extra_bytes FROM POW WHERE initial_hash=?");
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT data, version, nonce_trials_per_byte, " +
|
||||
"extra_bytes FROM POW WHERE initial_hash=?")
|
||||
) {
|
||||
ps.setBytes(1, initialHash);
|
||||
ResultSet rs = ps.executeQuery();
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
Blob data = rs.getBlob("data");
|
||||
return new Item(
|
||||
@ -41,6 +44,7 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
|
||||
} else {
|
||||
throw new IllegalArgumentException("Object requested that we don't have. Initial hash: " + Strings.hex(initialHash));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
throw new ApplicationException(e);
|
||||
@ -49,10 +53,12 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
|
||||
|
||||
@Override
|
||||
public List<byte[]> getItems() {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
List<byte[]> result = new LinkedList<>();
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery("SELECT initial_hash FROM POW");
|
||||
ResultSet rs = stmt.executeQuery("SELECT initial_hash FROM POW")
|
||||
) {
|
||||
List<byte[]> result = new LinkedList<>();
|
||||
while (rs.next()) {
|
||||
result.add(rs.getBytes("initial_hash"));
|
||||
}
|
||||
@ -65,8 +71,11 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
|
||||
|
||||
@Override
|
||||
public void putObject(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, nonce_trials_per_byte, extra_bytes) VALUES (?, ?, ?, ?, ?)");
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, " +
|
||||
"nonce_trials_per_byte, extra_bytes) VALUES (?, ?, ?, ?, ?)")
|
||||
) {
|
||||
ps.setBytes(1, security().getInitialHash(object));
|
||||
writeBlob(ps, 2, object);
|
||||
ps.setLong(3, object.getVersion());
|
||||
@ -81,8 +90,10 @@ public class JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWork
|
||||
|
||||
@Override
|
||||
public void removeObject(byte[] initialHash) {
|
||||
try (Connection connection = config.getConnection()) {
|
||||
PreparedStatement ps = connection.prepareStatement("DELETE FROM POW WHERE initial_hash=?");
|
||||
try (
|
||||
Connection connection = config.getConnection();
|
||||
PreparedStatement ps = connection.prepareStatement("DELETE FROM POW WHERE initial_hash=?")
|
||||
) {
|
||||
ps.setBytes(1, initialHash);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
|
@ -23,8 +23,6 @@ import ch.dissem.bitmessage.factory.Factory;
|
||||
import ch.dissem.bitmessage.utils.Base58;
|
||||
import org.ini4j.Ini;
|
||||
import org.ini4j.Profile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
@ -39,6 +37,9 @@ import static ch.dissem.bitmessage.utils.Singleton.security;
|
||||
* @author Christian Basler
|
||||
*/
|
||||
public class WifImporter {
|
||||
private static final byte WIF_FIRST_BYTE = (byte) 0x80;
|
||||
private static final int WIF_SECRET_LENGTH = 37;
|
||||
|
||||
private final BitmessageContext ctx;
|
||||
private final List<BitmessageAddress> identities = new LinkedList<>();
|
||||
|
||||
@ -76,10 +77,12 @@ public class WifImporter {
|
||||
|
||||
private byte[] getSecret(String walletImportFormat) throws IOException {
|
||||
byte[] bytes = Base58.decode(walletImportFormat);
|
||||
if (bytes[0] != (byte) 0x80)
|
||||
throw new IOException("Unknown format: 0x80 expected as first byte, but secret " + walletImportFormat + " was " + bytes[0]);
|
||||
if (bytes.length != 37)
|
||||
throw new IOException("Unknown format: 37 bytes expected, but secret " + walletImportFormat + " was " + bytes.length + " long");
|
||||
if (bytes[0] != WIF_FIRST_BYTE)
|
||||
throw new IOException("Unknown format: 0x80 expected as first byte, but secret " + walletImportFormat +
|
||||
" was " + bytes[0]);
|
||||
if (bytes.length != WIF_SECRET_LENGTH)
|
||||
throw new IOException("Unknown format: " + WIF_SECRET_LENGTH +
|
||||
" bytes expected, but secret " + walletImportFormat + " was " + bytes.length + " long");
|
||||
|
||||
byte[] hash = security().doubleSha256(bytes, 33);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
|
Loading…
Reference in New Issue
Block a user