diff --git a/src/main/java/org/cardanofoundation/signify/app/Contacting.java b/src/main/java/org/cardanofoundation/signify/app/Contacting.java index 4c62767..07ef138 100644 --- a/src/main/java/org/cardanofoundation/signify/app/Contacting.java +++ b/src/main/java/org/cardanofoundation/signify/app/Contacting.java @@ -10,23 +10,19 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.DigestException; -import java.util.HashMap; import java.net.http.HttpResponse; 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; + +import org.cardanofoundation.signify.generated.keria.model.Challenge; +import org.cardanofoundation.signify.generated.keria.model.Contact; import org.cardanofoundation.signify.generated.keria.model.Identifier; public class Contacting { - @Getter - public static class Challenge { - public List words; - } - @Getter public static class Challenges { public final SignifyClient client; @@ -120,28 +116,6 @@ public Object responded(String source, String said) throws LibsodiumException, I } } - @Getter - public static class Contact { - private String alias; - private String oobi; - private String id; - private Map additionalProperties = new HashMap<>(); - - @JsonAnySetter - public void setAdditionalProperty(String key, Object value) { - additionalProperties.put(key, value); - } - - public T get(String key) { - return switch (key) { - case "alias" -> (T) alias; - case "oobi" -> (T) oobi; - case "id" -> (T) id; - default -> (T) additionalProperties.get(key); - }; - } - } - @Getter public static class Contacts { private final SignifyClient client; @@ -192,7 +166,7 @@ public Contact[] list() throws IOException, InterruptedException, LibsodiumExcep * @param pre Prefix of the contact * @return Optional containing the contact if found, or empty if not found */ - public Optional get(String pre) throws InterruptedException, IOException, LibsodiumException { + public Optional get(String pre) throws InterruptedException, IOException, LibsodiumException { String path = "/contacts/" + pre; String method = "GET"; HttpResponse response = this.client.fetch(path, method, null); @@ -201,7 +175,7 @@ public Optional get(String pre) throws InterruptedException, IOException return Optional.empty(); } - return Optional.of(Utils.fromJson(response.body(), Object.class)); + return Optional.of(Utils.fromJson(response.body(), Contact.class)); } /** @@ -210,11 +184,11 @@ public Optional get(String pre) throws InterruptedException, IOException * @param info Information about the contact * @return Result of the addition */ - public Object add(String pre, Map info) throws IOException, InterruptedException, LibsodiumException { + public Contact add(String pre, Map info) throws IOException, InterruptedException, LibsodiumException { String path = "/contacts/" + pre; String method = "POST"; HttpResponse response = this.client.fetch(path, method, info); - return Utils.fromJson(response.body(), Object.class); + return Utils.fromJson(response.body(), Contact.class); } /** @@ -233,11 +207,11 @@ public void delete(String pre) throws IOException, InterruptedException, Libsodi * @param info Updated information about the contact * @return Result of the update */ - public Object update(String pre, Object info) throws IOException, InterruptedException, LibsodiumException { + public Contact update(String pre, Object info) throws IOException, InterruptedException, LibsodiumException { String path = "/contacts/" + pre; String method = "PUT"; HttpResponse response = this.client.fetch(path, method, info); - return Utils.fromJson(response.body(), Object.class); + return Utils.fromJson(response.body(), Contact.class); } } } \ No newline at end of file diff --git a/src/test/java/org/cardanofoundation/signify/e2e/ChallengesTest.java b/src/test/java/org/cardanofoundation/signify/e2e/ChallengesTest.java index ce3b82f..9b08cc1 100644 --- a/src/test/java/org/cardanofoundation/signify/e2e/ChallengesTest.java +++ b/src/test/java/org/cardanofoundation/signify/e2e/ChallengesTest.java @@ -10,6 +10,8 @@ import org.cardanofoundation.signify.cesr.Serder; import org.cardanofoundation.signify.cesr.util.Utils; +import org.cardanofoundation.signify.generated.keria.model.Challenge; +import org.cardanofoundation.signify.generated.keria.model.Contact; import org.cardanofoundation.signify.generated.keria.model.OOBI; import org.cardanofoundation.signify.generated.keria.model.Tier; import org.junit.jupiter.api.Test; @@ -55,10 +57,10 @@ void ChallengeTest() throws Exception { client2.state(); // Generate challenge words - Contacting.Challenge challenge1_small = client1.challenges().generate(128); - assertEquals(12, challenge1_small.words.size()); - Contacting.Challenge challenge1_big = client1.challenges().generate(256); - assertEquals(24, challenge1_big.words.size()); + Challenge challenge1_small = client1.challenges().generate(128); + assertEquals(12, challenge1_small.getWords().size()); + Challenge challenge1_big = client1.challenges().generate(256); + assertEquals(24, challenge1_big.getWords().size()); // Create two identifiers, one for each client CreateIdentifierArgs kargs1 = new CreateIdentifierArgs(); @@ -109,18 +111,18 @@ void ChallengeTest() throws Exception { // List Client 1 contacts Contacting.Contacts contacts1 = client1.contacts(); - Contacting.Contact[] client1Contacts = contacts1.list(); - Contacting.Contact bobContact = findContact(client1Contacts, "bob"); + Contact[] client1Contacts = contacts1.list(); + Contact bobContact = findContact(client1Contacts, "bob"); assert bobContact != null; assertEquals("bob", bobContact.getAlias()); - assertEquals(((List) bobContact.get("challenges")).size(), 0); + assertEquals(0, bobContact.getChallenges().size()); // Bob responds to Alice's challenge - client2.challenges().respond("bob", (String) opResponse1.get("i"), challenge1_small.words); + client2.challenges().respond("bob", (String) opResponse1.get("i"), challenge1_small.getWords()); System.out.println("Bob responded to Alice's challenge with signed words"); // Alice verifies Bob's response - Object verifyResult = client1.challenges().verify((String) opResponse2.get("i"), challenge1_small.words); + Object verifyResult = client1.challenges().verify((String) opResponse2.get("i"), challenge1_small.getWords()); Operation op = Operation.fromObject(waitOperation(client1, verifyResult)); System.out.println("Alice verified challenge response"); opResponse = (HashMap) op.getResponse(); @@ -134,16 +136,15 @@ void ChallengeTest() throws Exception { bobContact = findContact(client1Contacts, "bob"); assertNotNull(bobContact); - Object challenges = bobContact.get("challenges"); - assertInstanceOf(List.class, challenges); - assertTrue((Boolean) Utils.toMap(((List) challenges).getFirst()).get("authenticated")); + List challenges = bobContact.getChallenges(); + assertTrue(challenges.getFirst().getAuthenticated()); List clientList = new ArrayList<>(Arrays.asList(client1, client2)); assertOperations(clientList); } - private static Contacting.Contact findContact(Contacting.Contact[] contacts, String alias) { - for (Contacting.Contact contact : contacts) { + private static Contact findContact(Contact[] contacts, String alias) { + for (Contact contact : contacts) { if (alias.equals(contact.getAlias())) { return contact; } diff --git a/src/test/java/org/cardanofoundation/signify/e2e/MultisigTest.java b/src/test/java/org/cardanofoundation/signify/e2e/MultisigTest.java index ee72358..46de586 100644 --- a/src/test/java/org/cardanofoundation/signify/e2e/MultisigTest.java +++ b/src/test/java/org/cardanofoundation/signify/e2e/MultisigTest.java @@ -140,7 +140,7 @@ public void multisig() throws Exception { // First member challenge the other members with a random list of words // List of words should be passed to the other members out of band // The other members should do the same challenge/response flow, not shown here for brevity - List words = client1.challenges().generate(128).words; + List words = client1.challenges().generate(128).getWords(); System.out.println("Member1 generated challenge words: " + words); client2.challenges().respond("member2", aid1.getPrefix(), words); diff --git a/src/test/java/org/cardanofoundation/signify/e2e/utils/TestUtils.java b/src/test/java/org/cardanofoundation/signify/e2e/utils/TestUtils.java index 88ba380..441cce8 100644 --- a/src/test/java/org/cardanofoundation/signify/e2e/utils/TestUtils.java +++ b/src/test/java/org/cardanofoundation/signify/e2e/utils/TestUtils.java @@ -21,6 +21,7 @@ import org.cardanofoundation.signify.cesr.Salter; import org.cardanofoundation.signify.cesr.util.Utils; import org.cardanofoundation.signify.cesr.exceptions.LibsodiumException; +import org.cardanofoundation.signify.generated.keria.model.Contact; import org.cardanofoundation.signify.generated.keria.model.Identifier; import java.io.IOException; @@ -286,9 +287,9 @@ public static String[] getOrCreateIdentifier(SignifyClient client, String name, } public static String getOrCreateContact(SignifyClient client, String name, String oobi) throws IOException, InterruptedException, LibsodiumException { - List list = Arrays.asList(client.contacts().list(null, "alias", "^" + name + "$")); + List list = Arrays.asList(client.contacts().list(null, "alias", "^" + name + "$")); if (!list.isEmpty()) { - Contacting.Contact contact = list.getFirst(); + Contact contact = list.getFirst(); if (contact.getOobi().equals(oobi)) { return contact.getId(); }