Skip to content
Draft
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
16 changes: 12 additions & 4 deletions src/main/java/org/cardanofoundation/signify/app/Contacting.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.cardanofoundation.signify.core.States;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.DigestException;
Expand All @@ -15,6 +16,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -64,7 +66,8 @@ public Challenge generate() throws LibsodiumException, IOException, InterruptedE
* @throws Exception if the fetch operation fails
*/
public Object respond(String name, String recipient, List<String> words) throws IOException, InterruptedException, DigestException, ExecutionException, LibsodiumException {
States.HabState hab = this.client.identifiers().get(name);
States.HabState hab = this.client.identifiers().get(name)
.orElseThrow(() -> new IllegalArgumentException("Identifier not found: " + name));
Exchanging.Exchanges exchanges = this.client.exchanges();

Map<String, Object> payload = new LinkedHashMap<>();
Expand Down Expand Up @@ -187,13 +190,18 @@ public Contact[] list() throws IOException, InterruptedException, LibsodiumExcep
/**
* Get a contact
* @param pre Prefix of the contact
* @return The contact
* @return Optional containing the contact if found, or empty if not found
*/
public Object get(String pre) throws InterruptedException, IOException, LibsodiumException {
public Optional<Object> get(String pre) throws InterruptedException, IOException, LibsodiumException {
String path = "/contacts/" + pre;
String method = "GET";
HttpResponse<String> response = this.client.fetch(path, method, null);
return Utils.fromJson(response.body(), Object.class);

if (response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Optional.empty();
}

return Optional.of(Utils.fromJson(response.body(), Object.class));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/org/cardanofoundation/signify/app/Exchanging.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.cardanofoundation.signify.core.States.HabState;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.http.HttpResponse;
import java.security.DigestException;
import java.util.*;
Expand Down Expand Up @@ -154,13 +155,18 @@ public Object sendFromEvents(
* Get exn message by said
*
* @param said The said of the exn message
* @return The exn message
* @return Optional containing the exn message if found, or empty if not found
*/
public Object get(String said) throws Exception {
public Optional<Object> get(String said) throws Exception {
String path = String.format("/exchanges/%s", said);
String method = "GET";
HttpResponse<String> res = this. client.fetch(path, method, null);
return Utils.fromJson(res.body(), Object.class);
HttpResponse<String> res = this.client.fetch(path, method, null);

if (res.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Optional.empty();
}

return Optional.of(Utils.fromJson(res.body(), Object.class));
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/cardanofoundation/signify/app/Grouping.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.cardanofoundation.signify.cesr.util.Utils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.http.HttpResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class Grouping {
@Getter
Expand All @@ -27,14 +29,21 @@ public Groups(SignifyClient client) {
/**
* Get group request messages
* @param said SAID of exn message to load
* @return The list of replay messages
* @throws Exception if the fetch operation fails
* @return Optional containing the replay messages if found, or empty if not found
* @throws LibsodiumException if a Sodium error occurs
* @throws IOException if an I/O error occurs
* @throws InterruptedException if the operation is interrupted
*/
public Object getRequest(String said) throws LibsodiumException, IOException, InterruptedException {
public Optional<Object> getRequest(String said) throws LibsodiumException, IOException, InterruptedException {
String path = "/multisig/request/" + said;
String method = "GET";
HttpResponse<String> response = this.client.fetch(path, method, null);
return Utils.fromJson(response.body(), Object.class);

if (response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Optional.empty();
}

return Optional.of(Utils.fromJson(response.body(), Object.class));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.cardanofoundation.signify.cesr.Salter.Tier;
import org.cardanofoundation.signify.core.Manager.Algos;
import org.cardanofoundation.signify.core.States.HabState;
import org.cardanofoundation.signify.cesr.Sith;

@Getter
@Setter
Expand All @@ -14,8 +15,8 @@
@AllArgsConstructor
public class CreateIdentifierArgs {
private Boolean transferable;
private Object isith;
private Object nsith;
private Sith isith;
private Sith nsith;
private List<String> wits;
private Integer toad;
private String proxy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import org.cardanofoundation.signify.cesr.Keeping;
import org.cardanofoundation.signify.cesr.Serder;
import org.cardanofoundation.signify.cesr.Sith;
import org.cardanofoundation.signify.cesr.Codex.MatterCodex;
import org.cardanofoundation.signify.cesr.Keeping.Keeper;
import org.cardanofoundation.signify.cesr.Keeping.KeeperResult;
Expand All @@ -21,6 +22,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.http.HttpResponse;
import java.security.DigestException;
Expand Down Expand Up @@ -83,14 +85,19 @@ public IdentifierListResponse list(Integer start) throws IOException, Interrupte
* Get information for a managed identifier
*
* @param name Prefix or alias of the identifier
* @return A HabState to the identifier information
* @return An Optional containing the HabState if found, or empty if not found
*/
public States.HabState get(String name) throws InterruptedException, IOException, LibsodiumException {
public Optional<States.HabState> get(String name) throws InterruptedException, IOException, LibsodiumException {
final String path = "/identifiers/" + URI.create(name).toASCIIString();
final String method = "GET";

HttpResponse<String> response = this.client.fetch(path, method, null);
return Utils.fromJson(response.body(), States.HabState.class);

if (response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return Optional.empty();
}

return Optional.of(Utils.fromJson(response.body(), States.HabState.class));
}

/**
Expand Down Expand Up @@ -120,25 +127,15 @@ public EventResult create(String name, CreateIdentifierArgs kargs) throws Interr
Algos algo = kargs.getAlgo() == null ? Algos.salty : kargs.getAlgo();

boolean transferable = kargs.getTransferable() != null ? kargs.getTransferable() : true;
Object isith;
Object nsith;
Sith isith;
Sith nsith;

if (kargs.getIsith() == null) {
isith = "1";
nsith = "1";
isith = Sith.fromString("1");
nsith = Sith.fromString("1");
} else {
// String or number
if(!(kargs.getIsith() instanceof List<?>)) {
isith = kargs.getIsith().toString();
} else {
isith = kargs.getIsith();
}

if(!(kargs.getNsith() instanceof List<?>)) {
nsith = kargs.getNsith().toString();
} else {
nsith = kargs.getNsith();
}
isith = kargs.getIsith();
nsith = kargs.getNsith() != null ? kargs.getNsith() : isith;
}
List<String> wits = kargs.getWits() != null ? kargs.getWits() : new ArrayList<>();
int toad = kargs.getToad() != null ? kargs.getToad() : 0;
Expand All @@ -163,7 +160,7 @@ public EventResult create(String name, CreateIdentifierArgs kargs) throws Interr

if (!transferable) {
ncount = 0;
nsith = "0";
nsith = Sith.fromString("0");
dcode = MatterCodex.Ed25519N.getValue();
}

Expand Down Expand Up @@ -276,7 +273,8 @@ public EventResult create(String name, CreateIdentifierArgs kargs) throws Interr
* @throws LibsodiumException if there is an error in the cryptographic operations
*/
public EventResult addEndRole(String name, String role, String eid, String stamp) throws InterruptedException, DigestException, IOException, LibsodiumException {
States.HabState hab = this.get(name);
States.HabState hab = this.get(name)
.orElseThrow(() -> new IllegalArgumentException("Identifier not found: " + name));
String pre = hab.getPrefix();

// Assuming makeEndRole is a method that returns an object with getRaw() and getKed() methods
Expand Down Expand Up @@ -330,7 +328,8 @@ public EventResult interact(String name, Object data) throws InterruptedExceptio
}

public InteractionResponse createInteract(String name, Object data) throws InterruptedException, DigestException, IOException, LibsodiumException {
States.HabState hab = this.get(name);
States.HabState hab = this.get(name)
.orElseThrow(() -> new IllegalArgumentException("Identifier not found: " + name));
String pre = hab.getPrefix();

States.State state = hab.getState();
Expand Down Expand Up @@ -368,7 +367,8 @@ public EventResult rotate(String name, RotateIdentifierArgs kargs) throws Interr
String ncode = kargs.getNcode() != null ? kargs.getNcode() : MatterCodex.Ed25519_Seed.getValue();
int ncount = kargs.getNcount() != null ? kargs.getNcount() : 1;

States.HabState hab = this.get(name);
States.HabState hab = this.get(name)
.orElseThrow(() -> new IllegalArgumentException("Identifier not found: " + name));
String pre = hab.getPrefix();
boolean delegated = !hab.getState().getDi().isEmpty();

Expand All @@ -377,21 +377,25 @@ public EventResult rotate(String name, RotateIdentifierArgs kargs) throws Interr
String dig = state.getD();
int ridx = Integer.parseInt(state.getS(), 16) + 1;
List<String> wits = state.getB();
Object isith = state.getNt();

Object nsith = kargs.getNsith() != null ? kargs.getNsith() : isith;

// if isith is None: # compute default from newly rotated verfers above
if (isith == null) {
isith = Integer.toHexString(Math.max(1, (int) Math.ceil(count / 2.0)));

// Convert state.getNt() to Sith
Sith isith;
if (state.getNt() != null) {
isith = Sith.fromObject(state.getNt());
} else {
isith = Sith.fromString(Integer.toHexString(Math.max(1, (int) Math.ceil(count / 2.0))));
}
// if nsith is None: # compute default from newly rotated digers above
if (nsith == null) {
nsith = Integer.toHexString(Math.max(1, (int) Math.ceil(count / 2.0)));

// Use nsith from args or default to isith
Sith nsith;
if (kargs.getNsith() != null) {
nsith = kargs.getNsith();
} else {
nsith = isith;
}

Object cst = new Tholder(null, null, isith).getSith(); // current signing threshold
Object nst = new Tholder(null, null, nsith).getSith(); // next signing threshold
Sith cst = new Tholder(isith).getSith(); // current signing threshold
Sith nst = new Tholder(nsith).getSith(); // next signing threshold

// Regenerate next keys to sign rotation event
Keeper<?> keeper = this.client.getManager().get(hab);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.cardanofoundation.signify.cesr.Sith;
import org.cardanofoundation.signify.core.States;

import java.util.List;
Expand All @@ -12,7 +13,7 @@
@Setter
public class RotateIdentifierArgs {
private Boolean transferable;
private Object nsith;
private Sith nsith;
private Integer toad;
private List<String> cuts;
private List<String> adds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ public HttpResponse<String> fetch(
response = client.send(requestBuilder.build(),
HttpResponse.BodyHandlers.ofString());

if ("GET".equals(method) && response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
return response;
}

if (200 < response.statusCode() && response.statusCode() > 300) {
throw new UnexpectedResponseStatusException(String.format("HTTP %s %s - %d - %s",
method, path, response.statusCode(), response.body()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.cardanofoundation.signify.cesr.Tholder;
import org.cardanofoundation.signify.cesr.Verfer;
import org.cardanofoundation.signify.cesr.CesrNumber;
import org.cardanofoundation.signify.cesr.Sith;
import org.cardanofoundation.signify.cesr.args.RawArgs;
import org.cardanofoundation.signify.cesr.exceptions.extraction.IlkException;
import org.cardanofoundation.signify.cesr.exceptions.material.InvalidValueException;
Expand Down Expand Up @@ -44,12 +45,12 @@ public Agent(Object agent) {
}


Tholder tholder = new Tholder(null, null, event.get("kt"));
Tholder tholder = new Tholder(Sith.fromObject(event.get("kt")));
if (tholder.getNum() != 1) {
throw new InvalidValueException("invalid threshold " + tholder.getNum() + ", must be 1");
}

Tholder ntholder = new Tholder(null, null, event.get("nt"));
Tholder ntholder = new Tholder(Sith.fromObject(event.get("nt")));
if (ntholder.getNum() != 1) {
throw new InvalidValueException(
"invalid next threshold " + ntholder.getNum() + ", must be 1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public Controller(String bran, Tier tier, Integer ridx, Object state) throws Dig
if (stateMap.isEmpty() || ee.get("s").equals("0")) {
InceptArgs args = InceptArgs.builder()
.keys(this.keys)
.isith("1")
.nsith("1")
.isith(Sith.fromString("1"))
.nsith(Sith.fromString("1"))
.ndigs(this.ndigs)
.code(MatterCodex.Blake3_256.getValue())
.toad(0)
Expand Down Expand Up @@ -161,8 +161,8 @@ public Serder derive(Object state) throws DigestException {
if (state != null && ((States.State) state).getEe().getS().equals("0")) {
return Eventing.incept(InceptArgs.builder()
.keys(this.keys)
.isith("1")
.nsith("1")
.isith(Sith.fromString("1"))
.nsith(Sith.fromString("1"))
.ndigs(this.ndigs)
.code(MatterCodex.Blake3_256.getValue())
.toad(0)
Expand Down Expand Up @@ -227,8 +227,8 @@ public Map<String, Object> rotate(String bran, List<Map<String, Object>> aids) t
.pre(this.getPre())
.keys(this.keys)
.dig((String) this.serder.getKed().get("d"))
.isith(Arrays.asList("1", "0"))
.nsith("1")
.isith(Sith.fromStringWeighted(Arrays.asList("1", "0")))
.nsith(Sith.fromString("1"))
.ndigs(this.ndigs)
.build());

Expand Down
Loading
Loading