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
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@

Introducing the nostr-java library, a solution written in java for generating, signing, and publishing nostr events to relays.

For detailed instructions on how to utilise the library, please refer to the [nostr-client](https://github.com/tcheeric/nostr-client/) repository.
## Requirements
- Java 19. (Java 21 is currently not supported, due to a conflict with lombok. See issue [#101](https://github.com/tcheeric/nostr-java/issues/101)
- A Nostr relay to connect to. This is needed for the [unit tests](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-test). You can either run your own relay, or use a public one. To configure your test relay, update the [relays.properties](https://github.com/tcheeric/nostr-java/blob/main/nostr-java-test/src/test/resources/relays.properties) resource file in the test module.

## Building
We use Maven to build the project. To build the project, run the following command in the root directory of the project:

```bash
mvn clean install
```

## Supported NIPs
The following NIPs are supported by the API out-of-the-box:
- [NIP-1](https://github.com/nostr-protocol/nips/blob/master/01.md)
- [NIP-2](https://github.com/nostr-protocol/nips/blob/master/02.md)
- [NIP-3](https://github.com/nostr-protocol/nips/blob/master/03.md)
- [NIP-4](https://github.com/nostr-protocol/nips/blob/master/04.md)
- [NIP-5](https://github.com/nostr-protocol/nips/blob/master/05.md)
- [NIP-8](https://github.com/nostr-protocol/nips/blob/master/08.md)
- [NIP-9](https://github.com/nostr-protocol/nips/blob/master/09.md)
- [NIP-12](https://github.com/nostr-protocol/nips/blob/master/12.md)
- [NIP-14](https://github.com/nostr-protocol/nips/blob/master/14.md)
- [NIP-15](https://github.com/nostr-protocol/nips/blob/master/15.md)
- [NIP-20](https://github.com/nostr-protocol/nips/blob/master/20.md)
- [NIP-23](https://github.com/nostr-protocol/nips/blob/master/23.md)
- [NIP-25](https://github.com/nostr-protocol/nips/blob/master/25.md)
- [NIP-28](https://github.com/nostr-protocol/nips/blob/master/28.md)
- [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)

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)
7 changes: 7 additions & 0 deletions nostr-java-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nostr-java-api

This is a Java API for the Nostr protocol. It is a simple wrapper around the Nostr library, which allows you to interact with the Nostr for creating, signing, and publishing events to relays, using Java.

## Usage

For detailed instructions on how to utilise the library, please refer to the [nostr-client](https://github.com/tcheeric/nostr-client/) repository.
5 changes: 0 additions & 5 deletions nostr-java-crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,5 @@
<artifactId>nostr-java-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nostr-java-secp256k1</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
24 changes: 0 additions & 24 deletions nostr-java-event/README.md

This file was deleted.

46 changes: 46 additions & 0 deletions nostr-java-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# nostr-java-examples
This is a collection of examples for the Nostr Java API. It demonstrates how to use the API to create, sign, and publish events to relays.

## Creating an OOTB Nostr Event
Here is an example illustrating how to create a TextNoteEvent using the Nostr Java API, and submitting it to relays.
1. Create a sender [identity](https://github.com/tcheeric/nostr-java/tree/main/nostr-java-id). In the example below, we generate a random identity, but you may create an identity from a private key, or use the default identity, if setup.
2. You create a list of tags. This is an optional step. In the example below, we create a list of tags with a single PubKeyTag, which is the recipient's public key.
3. You create a NIP01 object, and call the createTextNoteEvent method, passing the list of tags and the message, then sign and send it.

```java
private static final Identity SENDER = Identity.generateRandomIdentity();
private final static Map<String, String> RELAYS = Map.of("lol", "nos.lol", "damus", "relay.damus.io", "ZBD",
"nostr.zebedee.cloud", "taxi", "relay.taxi", "mom", "nostr.mom");

private static TextNoteEvent sendTextNoteEvent() {
logHeader("sendTextNoteEvent");

List<BaseTag> tags = new ArrayList<>(List.of(new PubKeyTag(RECIPIENT.getPublicKey())));

var nip01 = new NIP01<TextNoteEvent>(SENDER);
nip01.createTextNoteEvent(tags, "Hello world, I'm here on nostr-java API!")
.sign()
.send(RELAYS);

return nip01.getEvent();
}

```

## Creating a custom Nostr Event and Tag
We use the ```nostr.api.EventNostr.GenericEventNostr.createGenericEvent(@NonNull Integer kind, @NonNull String content)``` method to create custom events, i.e. events not supported out-of-the-box by the nostr-java library. The example below re-creates the TextNoteEvent as a custom event, and a custom tag, then sends the event to relays.
The alt-tag is created using the TagFactory class, which is a helper class for creating custom tags. The TagFactory class has a method for creating each type of tag. In the example below, we create a custom tag with a tag name of ```alt```, a tag kind of ```31```, and a tag content of ```an alt text```. ```["alt", 31, "an alt text"]```.

```java
private static TextNoteEvent sendTextNoteEvent() {
logHeader("sendTextNoteEvent");

var event = new GenericEventNostr(SENDER);
nip01.createTextNoteEvent(1, "Hello world, I'm here on nostr-java API!")
.addTag(new TagFactory("alt", 31, "an alt text").create())
.sign()
.send(RELAYS);

return nip01.getEvent();
}
```
10 changes: 10 additions & 0 deletions nostr-java-id/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# nostr-java-id
Identities represent nsec, npub pairs. They are used to sign events and to verify the signature of events. The Nostr Java API provides a default identity, which is used if no identity is provided. You may also create an identity from a private key, generate a random identity, or create a custom identity.

## Creating an Identity from a private key (in hex format)
```java
Identity.getInstance(String privateKey);
```

## Creating a random identity
You use to create a random identity by calling the ```Identity.generateRandomIdentity()``` method.
Empty file added nostr-java-test/README.md
Empty file.