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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ The following NIPs are supported by the API out-of-the-box:
- [NIP-30](https://github.com/nostr-protocol/nips/blob/master/30.md)
- [NIP-32](https://github.com/nostr-protocol/nips/blob/master/32.md)
- [NIP-40](https://github.com/nostr-protocol/nips/blob/master/40.md)
- [NIP-42](https://github.com/nostr-protocol/nips/blob/master/42.md)
- [NIP-44](https://github.com/nostr-protocol/nips/blob/master/44.md)
- [NIP-46](https://github.com/nostr-protocol/nips/blob/master/46.md)
- [NIP-57](https://github.com/nostr-protocol/nips/blob/master/57.md)

We also provide the classes [GenericEvent](https://github.com/tcheeric/nostr-java/blob/main/nostr-java-event/src/main/java/nostr/event/impl/GenericEvent.java) and [GenericTag](https://github.com/tcheeric/nostr-java/blob/main/nostr-java-event/src/main/java/nostr/event/impl/GenericTag.java) for creating events and tags that are currently not supported out-of-the-box.
See working example [here](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-examples)
See a working example [here](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-examples)

Additional reading:
- [nostr-java-api](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-api)
- [nostr-java-id](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-id)
- [nostr-java-examples](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-examples)

## Dev Discussion Group:
- Nostr Public Channel: nostr:nevent1qqszqdmxg26sehmnyrcu2ler8azz6wyj6fh0qg3ad5fnnm6xfqqvhzcppamhxue69uhkummnw3ezumt0d5pzpl7nwh45p66gvet2q28dhjpcyh6clux4cjsm5gh7waza9pzjnmgglv06ew
3 changes: 1 addition & 2 deletions nostr-java-api/src/main/java/nostr/api/EventNostr.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
/**
* @author guilhermegps
*/
@Getter
@NoArgsConstructor
public abstract class EventNostr<T extends GenericEvent> extends Nostr {

@Getter
@Setter
private T event;

@Getter
private PublicKey recipient;

public EventNostr(@NonNull Identity sender) {
Expand Down
8 changes: 5 additions & 3 deletions nostr-java-api/src/main/java/nostr/api/NIP01.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import nostr.event.NIP01Event;
import nostr.event.impl.Filters;
import nostr.event.list.EventList;
import nostr.event.list.FiltersList;
import nostr.event.list.GenericTagQueryList;
import nostr.event.list.KindList;
import nostr.event.list.PublicKeyList;
Expand Down Expand Up @@ -222,6 +223,7 @@ public static Filters createFilters(EventList events, PublicKeyList authors, Kin
.build();
}


/**
* Create an event message to send events requested by clients
*
Expand All @@ -239,11 +241,11 @@ public static EventMessage createEventMessage(@NonNull IEvent event, @NonNull St
* Create a REQ message to request events and subscribe to new updates
*
* @param subscriptionId the subscription id
* @param filters the filters object
* @param filtersList the filters list
* @return a REQ message
*/
public static ReqMessage createReqMessage(@NonNull String subscriptionId, @NonNull Filters filters) {
return new ReqMessageFactory(subscriptionId, filters).create();
public static ReqMessage createReqMessage(@NonNull String subscriptionId, @NonNull FiltersList filtersList) {
return new ReqMessageFactory(subscriptionId, filtersList).create();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions nostr-java-api/src/main/java/nostr/api/NIP04.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ public static String encrypt(@NonNull IIdentity senderId, @NonNull String messag
* @param rcptId
* @param dm the encrypted direct message
* @return the DM content in clear-text
* @throws NostrException
*/
public static String decrypt(@NonNull IIdentity rcptId, @NonNull DirectMessageEvent dm) throws NostrException {
public static String decrypt(@NonNull IIdentity rcptId, @NonNull DirectMessageEvent dm) {
return NIP04.decrypt(rcptId, (GenericEvent) dm);
}

Expand Down
10 changes: 5 additions & 5 deletions nostr-java-api/src/main/java/nostr/api/NIP44.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public NIP44<T> encrypt() {
}

public static String encrypt(@NonNull IIdentity senderId, @NonNull String message, @NonNull PublicKey recipient) {
MessageCipher cipher = new MessageCipher44(senderId.getPrivateKey().toString(), recipient.toString());
MessageCipher cipher = new MessageCipher44(senderId.getPrivateKey().getRawData(), recipient.getRawData());
return cipher.encrypt(message);
}

Expand All @@ -97,7 +97,7 @@ public static String decrypt(@NonNull IIdentity rcptId, @NonNull EncryptedPayloa
* @return the ep content in clear-text
*/
public static String decrypt(@NonNull IIdentity identity, @NonNull String encrypteEPessage, @NonNull PublicKey recipient) {
MessageCipher cipher = new MessageCipher44(identity.getPrivateKey().toString(), recipient.toString());
MessageCipher cipher = new MessageCipher44(identity.getPrivateKey().getRawData(), recipient.getRawData());
return cipher.decrypt(encrypteEPessage);
}

Expand All @@ -112,14 +112,14 @@ public static String decrypt(@NonNull IIdentity rcptId, @NonNull GenericEvent ev
boolean rcptFlag = amITheRecipient(rcptId, event);

if (!rcptFlag) { // I am the message sender
MessageCipher cipher = new MessageCipher44(rcptId.getPrivateKey().toString(), pTag.getPublicKey().toString());
MessageCipher cipher = new MessageCipher44(rcptId.getPrivateKey().getRawData(), pTag.getPublicKey().getRawData());
return cipher.decrypt(event.getContent());
}

// I am the message recipient
var sender = event.getPubKey();
log.log(Level.FINE, "The message is being decrypted for {0}", sender);
MessageCipher cipher = new MessageCipher44(rcptId.getPrivateKey().toString(), sender.toString());
MessageCipher cipher = new MessageCipher44(rcptId.getPrivateKey().getRawData(), sender.getRawData());
return cipher.decrypt(event.getContent());
}

Expand All @@ -128,7 +128,7 @@ private static void encryptDirectMessage(@NonNull IIdentity senderId, @NonNull E
ITag pkTag = ep.getTags().get(0);
if (pkTag instanceof PubKeyTag pubKeyTag) {
var rcptPublicKey = pubKeyTag.getPublicKey();
MessageCipher cipher = new MessageCipher44(senderId.getPrivateKey().toString(), rcptPublicKey.toString());
MessageCipher cipher = new MessageCipher44(senderId.getPrivateKey().getRawData(), rcptPublicKey.getRawData());
var encryptedContent = cipher.encrypt(ep.getContent());
ep.setContent(encryptedContent);
}
Expand Down
31 changes: 22 additions & 9 deletions nostr-java-api/src/main/java/nostr/api/Nostr.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import nostr.event.json.codec.BaseTagDecoder;
import nostr.event.json.codec.BaseTagEncoder;
import nostr.event.json.codec.FiltersDecoder;
import nostr.event.json.codec.FiltersEncoder;
import nostr.event.json.codec.FiltersListEncoder;
import nostr.event.json.codec.GenericEventDecoder;
import nostr.event.json.codec.GenericTagQueryEncoder;
import nostr.event.list.FiltersList;
import nostr.id.IIdentity;

import java.util.List;
Expand Down Expand Up @@ -83,12 +84,24 @@ public void send(@NonNull IEvent event) {
}

public void send(@NonNull Filters filters, @NonNull String subscriptionId) {
getClient().send(filters, subscriptionId);
FiltersList filtersList = new FiltersList();
filtersList.add(filters);
getClient().send(filtersList, subscriptionId);
}

public void send(@NonNull FiltersList filtersList, @NonNull String subscriptionId) {
getClient().send(filtersList, subscriptionId);
}

public void send(@NonNull Filters filters, @NonNull String subscriptionId, Map<String, String> relays) {
FiltersList filtersList = new FiltersList();
filtersList.add(filters);
send(filtersList, subscriptionId, relays);
}

public void send(@NonNull FiltersList filtersList, @NonNull String subscriptionId, Map<String, String> relays) {
setRelays(relays);
getClient().send(filters, subscriptionId);
getClient().send(filtersList, subscriptionId);
}

/**
Expand Down Expand Up @@ -184,19 +197,19 @@ public static BaseTag decodeTag(@NonNull String json) {
// Filters

/**
* @param filters
* @param filtersList
* @param relay
*/
public static String encode(@NonNull Filters filters, Relay relay) {
final var enc = new FiltersEncoder(filters, relay);
public static String encode(@NonNull FiltersList filtersList, Relay relay) {
final var enc = new FiltersListEncoder(filtersList, relay);
return enc.encode();
}

/**
* @param filters
* @param filtersList
*/
public static String encode(@NonNull Filters filters) {
return Nostr.Json.encode(filters, null);
public static String encode(@NonNull FiltersList filtersList) {
return Nostr.Json.encode(filtersList, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import nostr.event.BaseTag;
import nostr.event.Marker;
import nostr.event.impl.EphemeralEvent;
import nostr.event.impl.Filters;
import nostr.event.impl.MetadataEvent;
import nostr.event.impl.ParameterizedReplaceableEvent;
import nostr.event.impl.ReplaceableEvent;
import nostr.event.impl.TextNoteEvent;
import nostr.event.list.FiltersList;
import nostr.event.message.CloseMessage;
import nostr.event.message.EoseMessage;
import nostr.event.message.EventMessage;
Expand Down Expand Up @@ -153,11 +153,11 @@ public EventMessage create() {
public static class ReqMessageFactory extends MessageFactory<ReqMessage> {

private final String subscriptionId;
private final Filters filters;
private final FiltersList filtersList;

@Override
public ReqMessage create() {
return new ReqMessage(subscriptionId, filters);
return new ReqMessage(subscriptionId, filtersList);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ public VerifyPaymentOrShippedEventFactory(@NonNull List<BaseTag> tags, VerifyPay
this.customer = customer;
}

@Deprecated
public VerifyPaymentOrShippedEventFactory(@NonNull IIdentity sender, @NonNull VerifyPaymentOrShippedEvent.PaymentShipmentStatus status, @NonNull CustomerOrderEvent.Customer customer) {
super(sender, status.toString());
this.status = status;
this.customer = customer;
}

@Override
public VerifyPaymentOrShippedEvent create() {
return new VerifyPaymentOrShippedEvent(getSender(), customer, status);
Expand Down
8 changes: 6 additions & 2 deletions nostr-java-base/src/main/java/nostr/base/BaseKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@ public abstract class BaseKey implements IKey {
protected final Bech32Prefix prefix;

@Override
public String getBech32() {
public String toBech32String() {
try {
return Bech32.toBech32(prefix, rawData);
} catch (NostrException ex) {
throw new RuntimeException(ex);
}
}

@JsonValue
@Override
@JsonValue
public String toString() {
return toHexString();
}

public String toHexString() {
return NostrUtil.bytesToHex(rawData);
}

Expand Down
2 changes: 1 addition & 1 deletion nostr-java-base/src/main/java/nostr/base/IKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public interface IKey extends Serializable {

byte[] getRawData();

String getBech32();
String toBech32String();
}
44 changes: 22 additions & 22 deletions nostr-java-client/src/main/java/nostr/client/Client.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
package nostr.client;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.NonNull;
import lombok.extern.java.Log;
import nostr.base.IEvent;
import nostr.base.Relay;
import nostr.event.BaseMessage;
import nostr.event.impl.ClientAuthenticationEvent;
import nostr.event.impl.Filters;
import nostr.event.list.FiltersList;
import nostr.event.message.ClientAuthenticationMessage;
import nostr.event.message.CloseMessage;
import nostr.event.message.EventMessage;
Expand All @@ -37,6 +19,24 @@
import nostr.ws.handler.spi.IRequestHandler;
import nostr.ws.request.handler.provider.DefaultRequestHandler;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import java.util.stream.Collectors;

@Log
public class Client {

Expand Down Expand Up @@ -122,8 +122,8 @@ public void send(@NonNull IEvent event, String subsciptionId) {
send(message);
}

public void send(@NonNull Filters filters, String subscriptionId) {
ReqMessage message = new ReqMessage(subscriptionId, filters);
public void send(@NonNull FiltersList filtersList, String subscriptionId) {
ReqMessage message = new ReqMessage(subscriptionId, filtersList);
send(message);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public void auth(Identity identity, String challenge) {
this.send(authMsg);
}

public void auth(String challenge, Relay relay) throws NostrException {
public void auth(String challenge, Relay relay) {
auth(Identity.getInstance(), challenge, relay);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class MessageCipher44 implements MessageCipher {

private static final int NONCE_LENGTH = 32;

private final String senderPrivateKey;
private final String recipientPublicKey;
private final byte[] senderPrivateKey;
private final byte[] recipientPublicKey;

@Override
public String encrypt(@NonNull String message) {
Expand All @@ -42,7 +42,7 @@ public String decrypt(@NonNull String payload) {

private byte[] getConversationKey() {
try {
return EncryptedPayloads.getConversationKey(senderPrivateKey, "02" + recipientPublicKey);
return EncryptedPayloads.getConversationKey(NostrUtil.bytesToHex(senderPrivateKey), "02" + NostrUtil.bytesToHex(recipientPublicKey));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException(e);
}
Expand Down
2 changes: 0 additions & 2 deletions nostr-java-event/src/main/java/nostr/event/BaseEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package nostr.event;

import nostr.base.IEvent;
import nostr.base.PublicKey;
import nostr.event.impl.GenericEvent;

/**
Expand All @@ -14,7 +13,6 @@ public abstract class BaseEvent implements IEvent {
public static class ProxyEvent extends GenericEvent {

public ProxyEvent(String id) {
super(new PublicKey(new byte[]{}), Kind.UNDEFINED);
setId(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Event(name = "Ephemeral Events", nip = 1)
@Event(name = "Ephemeral Events")
public class EphemeralEvent extends NIP01Event {

public EphemeralEvent(PublicKey pubKey, Integer kind, List<BaseTag> tags, String content) {
Expand Down
Loading