@Override public void putObject(Item item) { try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement("INSERT INTO POW (initial_hash, data, version, " + "nonce_trials_per_byte, extra_bytes, expiration_time, message_id) " + "VALUES (?, ?, ?, ?, ?, ?, ?)") ) { ps.setBytes(1, cryptography().getInitialHash(item.object)); writeBlob(ps, 2, item.object); ps.setLong(3, item.object.getVersion()); ps.setLong(4, item.nonceTrialsPerByte); ps.setLong(5, item.extraBytes); if (item.message == null) { ps.setObject(6, null); ps.setObject(7, null); } else { ps.setLong(6, item.expirationTime); ps.setLong(7, (Long) item.message.getId()); } ps.executeUpdate(); } catch (IOException | SQLException e) { LOG.debug("Error storing object of type " + item.object.getPayload().getClass().getSimpleName(), e); throw new ApplicationException(e); } }
@Override public void storeObject(ObjectMessage object) { if (getCache(object.getStream()).containsKey(object.getInventoryVector())) return; try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement("INSERT INTO Inventory " + "(hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)") ) { InventoryVector iv = object.getInventoryVector(); LOG.trace("Storing object " + iv); ps.setBytes(1, iv.getHash()); ps.setLong(2, object.getStream()); ps.setLong(3, object.getExpiresTime()); writeBlob(ps, 4, object); ps.setLong(5, object.getType()); ps.setLong(6, object.getVersion()); ps.executeUpdate(); getCache(object.getStream()).put(iv, object.getExpiresTime()); } catch (SQLException e) { LOG.debug("Error storing object of type " + object.getPayload().getClass().getSimpleName(), e); } catch (Exception e) { LOG.error(e.getMessage(), e); } }
private void tryToFindBroadcastsForAddress(BitmessageAddress address) { for (ObjectMessage object : ctx.getInventory().getObjects(address.getStream(), Broadcast.getVersion(address), ObjectType.BROADCAST)) { try { Broadcast broadcast = (Broadcast) object.getPayload(); broadcast.decrypt(address); listener.receive(broadcast.getPlaintext()); } catch (DecryptionFailedException ignore) { } catch (Exception e) { LOG.debug(e.getMessage(), e); } } }
public void doProofOfWork(BitmessageAddress recipient, ObjectMessage object) { Pubkey pubkey = recipient == null ? null : recipient.getPubkey(); long nonceTrialsPerByte = pubkey == null ? NETWORK_NONCE_TRIALS_PER_BYTE : pubkey.getNonceTrialsPerByte(); long extraBytes = pubkey == null ? NETWORK_EXTRA_BYTES : pubkey.getExtraBytes(); powRepo.putObject(object, nonceTrialsPerByte, extraBytes); if (object.getPayload() instanceof PlaintextHolder) { Plaintext plaintext = ((PlaintextHolder) object.getPayload()).getPlaintext(); plaintext.setInitialHash(cryptography.getInitialHash(object)); messageRepo.save(plaintext); } cryptography.doProofOfWork(object, nonceTrialsPerByte, extraBytes, this); }
private void tryToFindBroadcastsForAddress(BitmessageAddress address) { for (ObjectMessage object : ctx.getInventory().getObjects(address.getStream(), Broadcast.getVersion(address), ObjectType.BROADCAST)) { try { Broadcast broadcast = (Broadcast) object.getPayload(); broadcast.decrypt(address); // This decrypts it twice, but on the other hand it doesn't try to decrypt the objects with // other subscriptions and the interface stays as simple as possible. ctx.getNetworkListener().receive(object); } catch (DecryptionFailedException ignore) { } catch (Exception e) { LOG.debug(e.getMessage(), e); } } }
@Override public void receive(ObjectMessage object) throws IOException { ObjectPayload payload = object.getPayload(); if (payload.getType() == null) return; switch (payload.getType()) { case GET_PUBKEY: { receive(object, (GetPubkey) payload); break; } case PUBKEY: { receive(object, (Pubkey) payload); break; } case MSG: { receive(object, (Msg) payload); break; } case BROADCAST: { receive(object, (Broadcast) payload); break; } } }
private void tryToFindMatchingPubkey(BitmessageAddress address) { for (ObjectMessage object : ctx.getInventory().getObjects(address.getStream(), address.getVersion(), ObjectType.PUBKEY)) { try { Pubkey pubkey = (Pubkey) object.getPayload(); if (address.getVersion() == 4) { V4Pubkey v4Pubkey = (V4Pubkey) pubkey; if (Arrays.equals(address.getTag(), v4Pubkey.getTag())) { v4Pubkey.decrypt(address.getPublicDecryptionKey()); if (object.isSignatureValid(v4Pubkey)) { address.setPubkey(v4Pubkey); ctx.getAddressRepo().save(address); break; } else { LOG.debug("Found pubkey for " + address + " but signature is invalid"); } } } else { if (Arrays.equals(pubkey.getRipe(), address.getRipe())) { address.setPubkey(pubkey); ctx.getAddressRepo().save(address); break; } } } catch (Exception e) { LOG.debug(e.getMessage(), e); } } }
@Override @SuppressWarnings("ConstantConditions") public void receive(ObjectMessage object) throws IOException { ObjectPayload payload = object.getPayload(); if (payload.getType() == null) { if (payload instanceof GenericPayload) {
Pubkey pubkey = (Pubkey) object.getPayload(); if (address.getVersion() == 4) { V4Pubkey v4Pubkey = (V4Pubkey) pubkey;
object.sign(item.message.getFrom().getPrivateKey()); if (object.getPayload() instanceof Encrypted) { object.encrypt(item.message.getTo().getPubkey());