Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions nostr-java-api/src/main/java/nostr/api/NIP04.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package nostr.api;

import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.api.factory.impl.GenericEventFactory;
import nostr.base.PublicKey;
import nostr.config.Constants;
Expand All @@ -24,7 +24,7 @@
/**
* @author eric
*/
@Log
@Slf4j
public class NIP04 extends EventNostr {

public NIP04(@NonNull Identity sender, @NonNull PublicKey recipient) {
Expand All @@ -38,6 +38,7 @@ public NIP04(@NonNull Identity sender, @NonNull PublicKey recipient) {
* @param content the DM content in clear-text
*/
public NIP04 createDirectMessageEvent(@NonNull String content) {
log.debug("Creating direct message event");
var encryptedContent = encrypt(getSender(), content, getRecipient());
List<BaseTag> tags = List.of(new PubKeyTag(getRecipient()));

Expand All @@ -64,6 +65,7 @@ public NIP04 encrypt() {
* @return the encrypted message
*/
public static String encrypt(@NonNull Identity senderId, @NonNull String message, @NonNull PublicKey recipient) {
log.debug("Encrypting message from {} to {}", senderId.getPublicKey(), recipient);
MessageCipher cipher = new MessageCipher04(senderId.getPrivateKey().getRawData(), recipient.getRawData());
return cipher.encrypt(message);
}
Expand All @@ -77,6 +79,7 @@ public static String encrypt(@NonNull Identity senderId, @NonNull String message
* @return the DM content in clear-text
*/
public static String decrypt(@NonNull Identity identity, @NonNull String encryptedMessage, @NonNull PublicKey recipient) {
log.debug("Decrypting message for {}", identity.getPublicKey());
MessageCipher cipher = new MessageCipher04(identity.getPrivateKey().getRawData(), recipient.getRawData());
return cipher.decrypt(encryptedMessage);
}
Expand Down Expand Up @@ -124,12 +127,14 @@ public static String decrypt(@NonNull Identity rcptId, @NonNull GenericEvent eve
boolean rcptFlag = amITheRecipient(rcptId, event);

if (!rcptFlag) { // I am the message sender
log.debug("Decrypting own sent message");
MessageCipher cipher = new MessageCipher04(rcptId.getPrivateKey().getRawData(), pTag.getPublicKey().getRawData());
return cipher.decrypt(event.getContent());
}

// I am the message recipient
var sender = event.getPubKey();
log.debug("Decrypting message from {}", sender);
MessageCipher cipher = new MessageCipher04(rcptId.getPrivateKey().getRawData(), sender.getRawData());
return cipher.decrypt(event.getContent());
}
Expand Down
7 changes: 3 additions & 4 deletions nostr-java-api/src/main/java/nostr/api/NIP44.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nostr.api;

import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.base.PublicKey;
import nostr.encryption.MessageCipher;
import nostr.encryption.MessageCipher44;
Expand All @@ -13,9 +13,8 @@

import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.logging.Level;

@Log
@Slf4j
public class NIP44 extends EventNostr {

public static String encrypt(@NonNull Identity sender, @NonNull String message, @NonNull PublicKey recipient) {
Expand Down Expand Up @@ -47,7 +46,7 @@ public static String decrypt(@NonNull Identity recipient, @NonNull GenericEvent

// I am the message recipient
var sender = event.getPubKey();
log.log(Level.FINE, "The message is being decrypted for {0}", sender);
log.debug("Decrypting message for {}", sender);
MessageCipher cipher = new MessageCipher44(recipient.getPrivateKey().getRawData(), sender.getRawData());
return cipher.decrypt(event.getContent());
}
Expand Down
14 changes: 6 additions & 8 deletions nostr-java-api/src/main/java/nostr/api/NIP46.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.api.factory.impl.GenericEventFactory;
import nostr.base.PublicKey;
import nostr.config.Constants;
Expand All @@ -15,10 +15,10 @@
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.logging.Level;

import static nostr.base.IEvent.MAPPER_AFTERBURNER;

@Slf4j
public final class NIP46 extends EventNostr {

public NIP46(@NonNull Identity sender) {
Expand Down Expand Up @@ -55,7 +55,7 @@ public NIP46 createResponseEvent(@NonNull NIP46.Response response, @NonNull Publ
@Data
@AllArgsConstructor
@NoArgsConstructor
@Log
@Slf4j
public static final class Request implements Serializable {
private String id;
private String method;
Expand All @@ -70,8 +70,7 @@ public String toString() {
try {
return MAPPER_AFTERBURNER.writeValueAsString(this);
} catch (JsonProcessingException ex) {
// Handle the exception if needed
log.log(Level.WARNING, "Error converting to JSON: {0}", ex.getMessage());
log.warn("Error converting request to JSON: {}", ex.getMessage());
return "{}"; // Return an empty JSON object as a fallback
}
}
Expand All @@ -88,7 +87,7 @@ public static Request fromString(@NonNull String jsonString) {
@Data
@AllArgsConstructor
@NoArgsConstructor
@Log
@Slf4j
public static final class Response implements Serializable {
private String id;
private String error;
Expand All @@ -98,8 +97,7 @@ public String toString() {
try {
return MAPPER_AFTERBURNER.writeValueAsString(this);
} catch (JsonProcessingException ex) {
// Handle the exception if needed
log.log(Level.WARNING, "Error converting to JSON: {0}", ex.getMessage());
log.warn("Error converting response to JSON: {}", ex.getMessage());
return "{}"; // Return an empty JSON object as a fallback
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nostr.api.integration;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.api.EventNostr;
import nostr.api.NIP01;
import nostr.api.NIP04;
Expand Down Expand Up @@ -54,7 +54,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.logging.Level;

import static nostr.base.IEvent.MAPPER_AFTERBURNER;
import static org.awaitility.Awaitility.await;
Expand All @@ -66,8 +65,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringJUnitConfig(RelayConfig.class)
@ActiveProfiles("test")
@Log
@Slf4j
@Disabled("Requires running relay at ws://localhost:5555")
public class ApiEventIT {
@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nostr.api.unit;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.api.NIP01;
import nostr.api.util.JsonComparator;
import nostr.base.Command;
Expand Down Expand Up @@ -61,7 +61,7 @@
/**
* @author eric
*/
@Log
@Slf4j
public class JsonParseTest {
@Test
public void testBaseMessageDecoderEventFilter() throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nostr.api.unit;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.api.NIP57;
import nostr.base.ElementAttribute;
import nostr.base.PublicKey;
Expand All @@ -18,7 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Log
@Slf4j
public class NIP57ImplTest {

@Test
Expand Down
5 changes: 3 additions & 2 deletions nostr-java-base/src/main/java/nostr/base/Relay.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.ToString;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,7 +22,7 @@
@ToString(onlyExplicitlyIncluded = true)
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@AllArgsConstructor
@Log
@Slf4j
public class Relay {

@EqualsAndHashCode.Include
Expand All @@ -47,6 +47,7 @@ public Relay(@NonNull String uri, RelayInformationDocument doc) {
}
this.scheme = s[0];
this.host = s[1];
log.debug("Created relay with scheme {} and host {}", this.scheme, this.host);
}

// Helper method
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nostr.crypto.nip44;

import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.generators.HKDFBytesGenerator;
import org.bouncycastle.crypto.params.ECDomainParameters;
Expand All @@ -28,9 +28,8 @@
import java.security.spec.KeySpec;
import java.util.Arrays;
import java.util.Base64;
import java.util.logging.Level;

@Log
@Slf4j
public class EncryptedPayloads {

public static String encrypt(String plaintext, byte[] conversationKey, byte[] nonce) throws Exception {
Expand Down Expand Up @@ -78,7 +77,7 @@ public static String decrypt(String payload, byte[] conversationKey) throws Exce
byte[] calculatedMac = hmac.doFinal();

if (!MessageDigest.isEqual(calculatedMac, mac)) {
log.log(Level.FINE, "Calculated MAC = {0} --- Mac = {1}", new Object[]{Arrays.toString(calculatedMac), Arrays.toString(mac)});
log.debug("Calculated MAC {} does not match expected {}", Arrays.toString(calculatedMac), Arrays.toString(mac));
throw new Exception("Invalid MAC");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package nostr.event.entities;

import java.net.URL;
import java.util.logging.Level;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -10,7 +9,7 @@
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.base.IBech32Encodable;
import nostr.base.PublicKey;
import nostr.crypto.bech32.Bech32;
Expand All @@ -26,7 +25,7 @@
@EqualsAndHashCode
@SuperBuilder
@NoArgsConstructor
@Log
@Slf4j
public final class UserProfile extends Profile implements IBech32Encodable {

@JsonIgnore
Expand All @@ -45,7 +44,7 @@ public String toBech32() {
try {
return Bech32.encode(Bech32.Encoding.BECH32, Bech32Prefix.NPROFILE.getCode(), this.publicKey.getRawData());
} catch (Exception ex) {
log.log(Level.SEVERE, null, ex);
log.error("", ex);
throw new RuntimeException(ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.base.ElementAttribute;
import nostr.base.IGenericElement;
import nostr.base.ISignable;
Expand Down Expand Up @@ -39,14 +39,13 @@
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;

import static nostr.base.Encoder.ENCODER_MAPPED_AFTERBURNER;

/**
* @author squirrel
*/
@Log
@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
public class GenericEvent extends BaseEvent implements ISignable, IGenericElement, Deleteable {
Expand Down Expand Up @@ -204,7 +203,7 @@ public void update() {
} catch (NostrException | NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
} catch (AssertionError ex) {
log.log(Level.WARNING, ex.getMessage());
log.warn(ex.getMessage());
throw new RuntimeException(ex);
}
}
Expand Down Expand Up @@ -295,7 +294,7 @@ public Consumer<Signature> getSignatureConsumer() {
@Override
public Supplier<ByteBuffer> getByeArraySupplier() {
this.update();
log.log(Level.FINER, "Serialized event: {0}", new String(this.get_serializedEvent()));
log.debug("Serialized event: {}", new String(this.get_serializedEvent()));
return () -> ByteBuffer.wrap(this.get_serializedEvent());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.base.ElementAttribute;
import nostr.base.IDecoder;
import nostr.event.tag.GenericTag;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.stream.IntStream;

@Data
@Log
@Slf4j
public class GenericTagDecoder<T extends GenericTag> implements IDecoder<T> {

private final Class<T> clazz;
Expand Down Expand Up @@ -56,7 +55,7 @@ public T decode(@NonNull String json) {
.toList());
*/

log.log(Level.INFO, ">>> Decoded GenericTag: {0}", genericTag);
log.info("Decoded GenericTag: {}", genericTag);

return (T) genericTag;
} catch (JsonProcessingException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import nostr.base.ElementAttribute;
import nostr.event.BaseTag;
import nostr.event.tag.GenericTag;
Expand All @@ -14,14 +14,13 @@
import java.io.Serial;
import java.lang.reflect.Field;
import java.util.List;
import java.util.logging.Level;

import static nostr.event.json.codec.BaseTagEncoder.BASETAG_ENCODER_MAPPED_AFTERBURNER;

/**
* @author guilhermegps
*/
@Log
@Slf4j
public class TagSerializer extends StdSerializer<BaseTag> {

@Serial
Expand Down
Loading