@Override public List<Plaintext> findMessages(BitmessageAddress sender) { return find("sender='" + sender.getAddress() + "'"); }
@Override public void remove(BitmessageAddress address) { 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); } }
private boolean exists(BitmessageAddress address) { try ( Connection connection = config.getConnection(); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM Address " + "WHERE address='" + address.getAddress() + "'") ) { if (rs.next()) { return rs.getInt(1) > 0; } } catch (SQLException e) { LOG.error(e.getMessage(), e); } return false; }
@Override public List<Plaintext> findMessages(Plaintext.Status status, BitmessageAddress recipient) { return find("status='" + status.name() + "' AND recipient='" + recipient.getAddress() + "'"); }
private void insert(BitmessageAddress address) throws IOException, SQLException { try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement( "INSERT INTO Address (address, version, alias, public_key, private_key, subscribed, chan) " + "VALUES (?, ?, ?, ?, ?, ?, ?)") ) { ps.setString(1, address.getAddress()); ps.setLong(2, address.getVersion()); ps.setString(3, address.getAlias()); writePubkey(ps, 4, address.getPubkey()); writeBlob(ps, 5, address.getPrivateKey()); ps.setBoolean(6, address.isSubscribed()); ps.setBoolean(7, address.isChan()); ps.executeUpdate(); } }
private void update(Connection connection, Plaintext message) throws SQLException, IOException { try (PreparedStatement ps = connection.prepareStatement( "UPDATE Message SET iv=?, type=?, sender=?, recipient=?, data=?, ack_data=?, sent=?, received=?, " + "status=?, initial_hash=?, ttl=?, retries=?, next_try=? " + "WHERE id=?")) { ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash()); ps.setString(2, message.getType().name()); ps.setString(3, message.getFrom().getAddress()); ps.setString(4, message.getTo() == null ? null : message.getTo().getAddress()); writeBlob(ps, 5, message); ps.setBytes(6, message.getAckData()); ps.setLong(7, message.getSent()); ps.setLong(8, message.getReceived()); ps.setString(9, message.getStatus() == null ? null : message.getStatus().name()); ps.setBytes(10, message.getInitialHash()); ps.setLong(11, message.getTTL()); ps.setInt(12, message.getRetries()); ps.setObject(13, message.getNextTry()); ps.setLong(14, (Long) message.getId()); ps.executeUpdate(); } }
private void update(BitmessageAddress address) throws IOException, SQLException { StringBuilder statement = new StringBuilder("UPDATE Address SET alias=?"); if (address.getPubkey() != null) { statement.append(", public_key=?"); } if (address.getPrivateKey() != null) { statement.append(", private_key=?"); } statement.append(", subscribed=?, chan=? WHERE address=?"); try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement(statement.toString()) ) { int i = 0; ps.setString(++i, address.getAlias()); if (address.getPubkey() != null) { writePubkey(ps, ++i, address.getPubkey()); } if (address.getPrivateKey() != null) { writeBlob(ps, ++i, address.getPrivateKey()); } ps.setBoolean(++i, address.isSubscribed()); ps.setBoolean(++i, address.isChan()); ps.setString(++i, address.getAddress()); ps.executeUpdate(); } }
public static BitmessageAddress createIdentityFromPrivateKey(String address, byte[] privateSigningKey, byte[] privateEncryptionKey, long nonceTrialsPerByte, long extraBytes, int behaviourBitfield) { BitmessageAddress temp = new BitmessageAddress(address); PrivateKey privateKey = new PrivateKey(privateSigningKey, privateEncryptionKey, createPubkey(temp.getVersion(), temp.getStream(), Security.createPublicKey(privateSigningKey).getEncoded(false), Security.createPublicKey(privateEncryptionKey).getEncoded(false), nonceTrialsPerByte, extraBytes, behaviourBitfield)); BitmessageAddress result = new BitmessageAddress(privateKey); if (!result.getAddress().equals(address)) { throw new IllegalArgumentException("Address not matching private key. Address: " + address + "; Address derived from private key: " + result.getAddress()); } return result; }
private void insert(Connection connection, Plaintext message) throws SQLException, IOException { try (PreparedStatement ps = connection.prepareStatement( "INSERT INTO Message (iv, type, sender, recipient, data, ack_data, sent, received, " + "status, initial_hash, ttl, retries, next_try) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS) ) { ps.setBytes(1, message.getInventoryVector() == null ? null : message.getInventoryVector().getHash()); ps.setString(2, message.getType().name()); ps.setString(3, message.getFrom().getAddress()); ps.setString(4, message.getTo() == null ? null : message.getTo().getAddress()); writeBlob(ps, 5, message); ps.setBytes(6, message.getAckData()); ps.setLong(7, message.getSent()); ps.setLong(8, message.getReceived()); ps.setString(9, message.getStatus() == null ? null : message.getStatus().name()); ps.setBytes(10, message.getInitialHash()); ps.setLong(11, message.getTTL()); ps.setInt(12, message.getRetries()); ps.setObject(13, message.getNextTry()); ps.executeUpdate(); // get generated id try (ResultSet rs = ps.getGeneratedKeys()) { rs.next(); message.setId(rs.getLong(1)); } } }
public static BitmessageAddress createIdentityFromPrivateKey(String address, byte[] privateSigningKey, byte[] privateEncryptionKey, long nonceTrialsPerByte, long extraBytes, int behaviourBitfield) { BitmessageAddress temp = new BitmessageAddress(address); PrivateKey privateKey = new PrivateKey(privateSigningKey, privateEncryptionKey, createPubkey(temp.getVersion(), temp.getStream(), cryptography().createPublicKey(privateSigningKey), cryptography().createPublicKey(privateEncryptionKey), nonceTrialsPerByte, extraBytes, behaviourBitfield)); BitmessageAddress result = new BitmessageAddress(privateKey); if (!result.getAddress().equals(address)) { throw new IllegalArgumentException("Address not matching private key. Address: " + address + "; Address derived from private key: " + result.getAddress()); } return result; }
private void tryToFindMatchingPubkey(BitmessageAddress address) { BitmessageAddress stored = addressRepository.getAddress(address.getAddress()); if (stored != null) { address.setAlias(stored.getAlias());
protected void safeSenderIfNecessary(Plaintext message) { if (message.getId() == null) { BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress()); if (savedAddress == null) { ctx.getAddressRepository().save(message.getFrom()); } else if (savedAddress.getPubkey() == null && message.getFrom().getPubkey() != null) { savedAddress.setPubkey(message.getFrom().getPubkey()); ctx.getAddressRepository().save(savedAddress); } } }
/** * Be aware that if the pubkey already exists in the inventory, the metods will not request it and the callback * for freshly received pubkeys will not be called. Instead the pubkey is added to the contact and stored on DB. */ public void requestPubkey(final BitmessageAddress contact) { BitmessageAddress stored = addressRepository.getAddress(contact.getAddress()); tryToFindMatchingPubkey(contact); if (contact.getPubkey() != null) { if (stored != null) { stored.setPubkey(contact.getPubkey()); addressRepository.save(stored); } else { addressRepository.save(contact); } return; } if (stored == null) { addressRepository.save(contact); } long expires = UnixTime.now(TTL.getpubkey()); LOG.info("Expires at " + expires); final ObjectMessage request = new ObjectMessage.Builder() .stream(contact.getStream()) .expiresTime(expires) .payload(new GetPubkey(contact)) .build(); proofOfWorkService.doProofOfWork(request); }
public WifExporter addIdentity(BitmessageAddress identity) { Profile.Section section = ini.add(identity.getAddress()); section.add("label", identity.getAlias()); section.add("enabled", true); section.add("decoy", false); if (identity.isChan()) { section.add("chan", identity.isChan()); } section.add("noncetrialsperbyte", identity.getPubkey().getNonceTrialsPerByte()); section.add("payloadlengthextrabytes", identity.getPubkey().getExtraBytes()); section.add("privsigningkey", exportSecret(identity.getPrivateKey().getPrivateSigningKey())); section.add("privencryptionkey", exportSecret(identity.getPrivateKey().getPrivateEncryptionKey())); return this; }