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
2 changes: 1 addition & 1 deletion nostr-java-base/src/main/java/nostr/base/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String toString() {

public static Signature fromString(String sig) {
Signature signature = new Signature();
signature.setRawData(NostrUtil.hexToBytes(sig));
signature.setRawData(NostrUtil.hex128ToBytes(sig));
return signature;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static SecretKeySpec getSharedSecretKeySpec(byte[] privateKey, byte[] pu
private static byte[] getSharedSecret(String privateKeyHex, String publicKeyHex) {

SecP256K1Curve curve = new SecP256K1Curve();
ECPoint pubKeyPt = curve.decodePoint(NostrUtil.hexToBytes("02" + publicKeyHex));
ECPoint pubKeyPt = curve.decodePoint(NostrUtil.nip04PubKeyHexToBytes("02" + publicKeyHex));
BigInteger tweakVal = new BigInteger(1, NostrUtil.hexToBytes(privateKeyHex));
return pubKeyPt.multiply(tweakVal).getEncoded(true);
}
Expand Down
5 changes: 5 additions & 0 deletions nostr-java-event/src/main/java/nostr/event/Kind.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.temporal.ValueRange;

/**
*
* @author squirrel
Expand Down Expand Up @@ -47,6 +49,9 @@ public enum Kind {

@JsonCreator
public static Kind valueOf(int value) {
if (!ValueRange.of(0, 65535).isValidIntValue(value)) {
throw new IllegalArgumentException(String.format("Kind must be between 0 and 65535 but was [%d]", value));
}
for (Kind k : values()) {
if (k.getValue() == value) {
return k;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@ public static class Stall extends AbstractEventContent<CreateOrUpdateStallEvent>

@JsonProperty
private final String id;

@JsonProperty
private String name;

@JsonProperty
private String description;

@JsonProperty
private String currency;

@JsonProperty
private Shipping shipping;
private Shipping shipping;

public Stall() {
this.id = UUID.randomUUID().toString();
this.id = UUID.randomUUID().toString().concat(UUID.randomUUID().toString()).substring(0, 64);
}

@Data
public static class Shipping {

@JsonProperty
private final String id;

@JsonProperty
private String name;

@JsonProperty
private Float cost;

@JsonProperty
private List<String> countries;

Expand Down
15 changes: 15 additions & 0 deletions nostr-java-event/src/main/java/nostr/event/impl/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import nostr.base.PublicKey;
import nostr.base.annotation.Key;
Expand Down Expand Up @@ -66,6 +67,20 @@ public class Filters {
@Setter(AccessLevel.NONE)
private Map<String, List<String>> genericTagQuery;

public void setUntil(@NonNull Long until) {
if (until < 0) {
throw new IllegalArgumentException("'until' filter cannot be negative.");
}
this.until = until;
}

public void setSince(@NonNull Long since) {
if (since < 0) {
throw new IllegalArgumentException("'since' filter cannot be negative.");
}
this.since = since;
}

@JsonAnyGetter
public Map<String, List<String>> getGenericTagQuery() {
return genericTagQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import nostr.event.json.deserializer.SignatureDeserializer;
import nostr.util.NostrException;
import nostr.util.NostrUtil;
import nostr.util.thread.HexStringValidator;

import java.beans.Transient;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -98,7 +99,7 @@ public GenericEvent() {

public GenericEvent(@NonNull String id) {
this();
this.id = id;
setId(id);
}

public GenericEvent(@NonNull PublicKey pubKey, @NonNull Kind kind) {
Expand Down Expand Up @@ -128,6 +129,11 @@ public GenericEvent(@NonNull PublicKey pubKey, @NonNull Integer kind, @NonNull L
updateTagsParents(tags);
}

public void setId(String id) {
HexStringValidator.validateHex(id, 64);
this.id = id;
}

@Override
public String toBech32() {
if (!isSigned()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import nostr.util.NostrUtil;

public class SignatureDeserializer extends JsonDeserializer<Signature> {

@Override
public Signature deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectMapper objectMapper = (ObjectMapper) jsonParser.getCodec();
JsonNode node = objectMapper.readTree(jsonParser);

String sigValue = node.asText();
byte[] rawData = NostrUtil.hexToBytes(sigValue);
byte[] rawData = NostrUtil.hex128ToBytes(sigValue);

Signature signature = new Signature();
signature.setRawData(rawData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import nostr.event.impl.Filters;
import nostr.event.json.codec.FiltersEncoder;

import java.time.temporal.ValueRange;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -32,12 +33,15 @@ public class ReqMessage extends BaseMessage {
@JsonProperty
private final List<Filters> filtersList;

public ReqMessage(String subscriptionId, Filters filters) {
public ReqMessage(@NonNull String subscriptionId, Filters filters) {
this(subscriptionId, List.of(filters));
}

public ReqMessage(String subscriptionId, List<Filters> incomingFiltersList) {
public ReqMessage(@NonNull String subscriptionId, List<Filters> incomingFiltersList) {
super(Command.REQ.name());
if (!ValueRange.of(1, 64).isValidIntValue(subscriptionId.length())) {
throw new IllegalArgumentException(String.format("subscriptionId length must be between 1 and 64 characters but was [%d]", subscriptionId.length()));
}
this.subscriptionId = subscriptionId;
this.filtersList = new ArrayList<>();
this.filtersList.addAll(incomingFiltersList);
Expand Down
80 changes: 80 additions & 0 deletions nostr-java-test/src/test/java/nostr/test/base/BaseKeyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package nostr.test.base;

import nostr.base.PublicKey;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

class BaseKeyTest {
public static final String VALID_HEXPUBKEY = "56adf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f6f";
public static final String INVALID_HEXPUBKEY_NON_HEX_DIGITS = "XYZdf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f6f";
public static final String INVALID_HEXPUBKEY_LENGTH_TOO_SHORT = "56adf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f6";
public static final String INVALID_HEXPUBKEY_LENGTH_TOO_LONG = "56adf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f666";
public static final String VALID_HEXPUBKEY_ALL_ZEROS = "0000000000000000000000000000000000000000000000000000000000000000";
public static final String VALID_HEXPUBKEY_ALL_FF = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
public static final String INVALID_HEXPUBKEY_HAS_MULTIPLE_UPPERCASE = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";
public static final String INVALID_HEXPUBKEY_HAS_SINGLE_UPPERCASE = "56adf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f6F";

@Test
public void testValidPublicKeyString() {
System.out.println("testValidPublicKeyString");
assertDoesNotThrow(() -> new PublicKey(VALID_HEXPUBKEY));
}

@Test
public void testValidPublicKeyByteArray() {
System.out.println("testValidPublicKeyByteArray");
assertDoesNotThrow(() -> new PublicKey(VALID_HEXPUBKEY.getBytes(StandardCharsets.UTF_8)));
}

@Test
public void testInValidNullPublicKeyString() {
System.out.println("testInValidNullPublicKeyString");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(""));
}

@Test
public void testInValidPublicKeyNonHexDigits() {
System.out.println("testInValidPublicKeyNonHexDigits");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(INVALID_HEXPUBKEY_NON_HEX_DIGITS));
}

@Test
public void testInValidPublicKeyLengthTooShort() {
System.out.println("testInValidPublicKeyLengthTooShort");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(INVALID_HEXPUBKEY_LENGTH_TOO_SHORT));
}

@Test
public void testInValidPublicKeyLengthTooLong() {
System.out.println("testInValidPublicKeyLengthTooShort");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(INVALID_HEXPUBKEY_LENGTH_TOO_LONG));
}

@Test
public void testValidPublicKeyAllZeros() {
System.out.println("testValidPublicKeyAllZeros");
assertDoesNotThrow(() -> new PublicKey(VALID_HEXPUBKEY_ALL_ZEROS));
}

@Test
public void testValidPublicKeyAllFF() {
System.out.println("testValidPublicKeyAllFF");
assertDoesNotThrow(() -> new PublicKey(VALID_HEXPUBKEY_ALL_FF));
}

@Test
public void testInvalidPublicKeyMultipleUppercase() {
System.out.println("testInvalidPublicKeyMultipleUppercase");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(INVALID_HEXPUBKEY_HAS_MULTIPLE_UPPERCASE));
}

@Test
public void testInvalidPublicKeySingleUppercase() {
System.out.println("testInvalidPublicKeySingleUppercase");
assertThrows(IllegalArgumentException.class, () -> new PublicKey(INVALID_HEXPUBKEY_HAS_SINGLE_UPPERCASE));
}
}
39 changes: 30 additions & 9 deletions nostr-java-test/src/test/java/nostr/test/base/NostrUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package nostr.test.base;

/**
*
* @author squirrel
*/
public class NostrUtilTest {

}
package nostr.test.base;

import lombok.extern.java.Log;
import nostr.util.NostrUtil;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author squirrel
*/
@Log
public class NostrUtilTest {
/**
* test intended to confirm conversion routines:
* (1) Hex string to byte[], then
* (2) byte[] back to Hex string
* are properly functioning inversions of each other
*/
@Test
public void testHexToBytesHex() {
log.info("testHexToBytesHex");
String pubKeyString = "56adf01ca1aa9d6f1c35953833bbe6d99a0c85b73af222e6bd305b51f2749f6f";
assertEquals(
pubKeyString,
NostrUtil.bytesToHex( // (2)
NostrUtil.hexToBytes( // (1)
pubKeyString)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class ApiEventTest {
public void testNIP01CreateTextNoteEvent() throws NostrException {
System.out.println("testNIP01CreateTextNoteEvent");

PublicKey publicKey = new PublicKey("");
PublicKey publicKey = new PublicKey(NOSTR_JAVA_PUBKEY);
var recipient = NIP01.createPubKeyTag(publicKey);
List<BaseTag> tags = new ArrayList<>();
tags.add(recipient);
Expand Down
Loading