diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteService.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteService.java index 4f26739cfa8b9a5e8a6c29d7238098db96bd2cd5..57e0f7dec4fd02f8449f2af864e5c29095d647ed 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteService.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteService.java @@ -38,8 +38,26 @@ public interface WotRemoteService extends Service { WotLookup.Uid find(Peer peer, String uidOrPubKey); WotLookup.Uid find(String currencyId, String uidOrPubKey); - List<WotRequirements> getRequirements(Peer peer, String pubKey); - List<WotRequirements> getRequirements(String currencyId, String pubKey); + /** + * @deprecated Use getRequirementsByPubkey() intead + * @param peer + * @param uidOrPubkey + * @return + */ + @Deprecated + List<WotRequirements> getRequirements(Peer peer, String uidOrPubkey); + + /** + * @deprecated Use getRequirementsByPubkey() intead + * @param peer + * @param uidOrPubkey + * @return + */ + @Deprecated + List<WotRequirements> getRequirements(String currencyId, String uidOrPubkey); + + List<WotRequirements> getRequirementsByPubkey(Peer peer, String pubkey); + List<WotRequirements> getRequirementsByPubkey(String currencyId, String pubkey); WotLookup.Uid findByUid(Peer peer, String uid); WotLookup.Uid findByUid(String currencyId, String uid); @@ -57,7 +75,10 @@ public interface WotRemoteService extends Service { Collection<Certification> getCertifications(String currencyId, String uid, String pubkey, boolean isMember); WotCertification getCertifiedBy(Peer peer, String uid); - WotCertification getCertifiedBy(String currencyId, String uid); + WotCertification getCertifiedBy(String currencyId, String pubkeyOrUid); + + WotCertification getCertifiedByPubkey(Peer peer, String pubkey); + WotCertification getCertifiedByPubkey(String currencyId, String pubkey); long countValidCertifiers(Peer peer, String pubkey); long countValidCertifiers(String currencyId, String pubkey); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java index 2c967af07ceafedd3616e776ed4b454b996ce07f..824eca63614110c7b8c37173138a7f46041d7c10 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/WotRemoteServiceImpl.java @@ -67,12 +67,18 @@ public class WotRemoteServiceImpl extends BaseRemoteServiceImpl implements WotRe public static final String URL_LOOKUP = URL_BASE + "/lookup/%s"; - public static final String URL_REQUIREMENT = URL_BASE+"/requirements/%s"; + public static final String URL_REQUIREMENT = URL_BASE + "/requirements/%s"; + + public static final String URL_REQUIREMENTBY_PUBKEY = URL_REQUIREMENT+ "?pubkey=true"; public static final String URL_CERTIFIED_BY = URL_BASE + "/certified-by/%s"; + public static final String URL_CERTIFIED_BY_PUBKEY = URL_CERTIFIED_BY + "?pubkey=true"; + public static final String URL_CERTIFIERS_OF = URL_BASE + "/certifiers-of/%s"; + public static final String URL_CERTIFIERS_OF_BY_PUBKEY = URL_CERTIFIERS_OF + "?pubkey=true"; + /** * See https://github.com/ucoin-io/ucoin-cli/blob/master/bin/ucoin * > var hash = res.current ? res.current.hash : 'DA39A3EE5E6B4B0D3255BFEF95601890AFD80709'; @@ -190,19 +196,35 @@ public class WotRemoteServiceImpl extends BaseRemoteServiceImpl implements WotRe } @Override - public List<WotRequirements> getRequirements(Peer peer, String pubKey) { + public List<WotRequirements> getRequirements(Peer peer, String uidOrPubkey) { try { - WotRequirementsResponse response = httpService.executeRequest(peer, String.format(URL_REQUIREMENT, pubKey), WotRequirementsResponse.class); + WotRequirementsResponse response = httpService.executeRequest(peer, String.format(URL_REQUIREMENT, uidOrPubkey), WotRequirementsResponse.class); return ImmutableList.copyOf(response.getIdentities()); } catch (HttpBadRequestException | HttpNotFoundException e) { - log.debug(String.format("Unable to get memberships for {%.8s}", pubKey)); + log.debug(String.format("Unable to get memberships for {%.8s}", uidOrPubkey)); return null; } } @Override - public List<WotRequirements> getRequirements(String currencyId, String pubKey) { - return getRequirements(peerService.getActivePeerByCurrency(currencyId), pubKey); + public List<WotRequirements> getRequirements(String currencyId, String uidOrPubkey) { + return getRequirements(peerService.getActivePeerByCurrency(currencyId), uidOrPubkey); + } + + @Override + public List<WotRequirements> getRequirementsByPubkey(Peer peer, String pubkey) { + try { + WotRequirementsResponse response = httpService.executeRequest(peer, String.format(URL_REQUIREMENTBY_PUBKEY, pubkey), WotRequirementsResponse.class); + return ImmutableList.copyOf(response.getIdentities()); + } catch (HttpBadRequestException | HttpNotFoundException e) { + log.debug(String.format("Unable to get memberships for {%.8s}", pubkey)); + return null; + } + } + + @Override + public List<WotRequirements> getRequirementsByPubkey(String currencyId, String pubKey) { + return getRequirementsByPubkey(peerService.getActivePeerByCurrency(currencyId), pubKey); } public WotLookup.Uid findByUid(Peer peer, String uid) { @@ -305,21 +327,36 @@ public class WotRemoteServiceImpl extends BaseRemoteServiceImpl implements WotRe return getCertifications(peerService.getActivePeerByCurrency(currencyId), uid, pubkey, isMember); } - public WotCertification getCertifiedBy(Peer peer, String uid) { + public WotCertification getCertifiedBy(Peer peer, String uidOrPubkey) { if (log.isDebugEnabled()) { - log.debug(String.format("Try to get certifications done by uid: %s", uid)); + log.debug(String.format("Try to get certifications done by uid: %s", uidOrPubkey)); } // call certified-by - String path = String.format(URL_CERTIFIED_BY, uid); + String path = String.format(URL_CERTIFIED_BY, uidOrPubkey); WotCertification result = httpService.executeRequest(peer, path, WotCertification.class); return result; } - public WotCertification getCertifiedBy(String currencyId, String uid) { - return getCertifiedBy(peerService.getActivePeerByCurrency(currencyId), uid); + public WotCertification getCertifiedBy(String currencyId, String uidOrPubkey) { + return getCertifiedBy(peerService.getActivePeerByCurrency(currencyId), uidOrPubkey); + } + + public WotCertification getCertifiedByPubkey(Peer peer, String pubkey) { + if (log.isDebugEnabled()) { + log.debug(String.format("Try to get certifications done by pubkey: %s", pubkey)); + } + + // call certified-by + String path = String.format(URL_CERTIFIED_BY_PUBKEY, pubkey); + WotCertification result = httpService.executeRequest(peer, path, WotCertification.class); + return result; + } + @Override + public WotCertification getCertifiedByPubkey(String currencyId, String pubkey) { + return getCertifiedByPubkey(peerService.getActivePeerByCurrency(currencyId), pubkey); } public long countValidCertifiers(Peer peer, String pubkey) { @@ -354,6 +391,21 @@ public class WotRemoteServiceImpl extends BaseRemoteServiceImpl implements WotRe return getCertifiersOf(peerService.getActivePeerByCurrency(currencyId), uid); } + public WotCertification getCertifiersOfByPubkey(Peer peer, String pubkey) { + if (log.isDebugEnabled()) { + log.debug(String.format("Try to get certifications done to pubkey: %s", pubkey)); + } + + // call certifiers-of + String path = String.format(URL_CERTIFIERS_OF_BY_PUBKEY, pubkey); + WotCertification result = httpService.executeRequest(peer, path, WotCertification.class); + + return result; + } + + public WotCertification getCertifiersOfByPubkey(String currencyId, String pubkey) { + return getCertifiersOf(peerService.getActivePeerByCurrency(currencyId), pubkey); + } public void sendIdentity(Peer peer, String currency, byte[] pubKey, byte[] secKey, String uid, String blockUid) { // http post /wot/add HttpPost httpPost = new HttpPost(httpService.getPath(peer, URL_ADD)); diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/WotRemoteServiceTest.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/WotRemoteServiceTest.java index 41e0a197db725d74e9c2e87e35bc5fc148d32cbd..f2def1ae60f19c643d7f8a717ea2186f1a62ba57 100644 --- a/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/WotRemoteServiceTest.java +++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/service/bma/WotRemoteServiceTest.java @@ -209,6 +209,26 @@ public class WotRemoteServiceTest { }); } + @Test + public void getRequirementsByPubkey() { + List<WotPendingMembership> pendingMemberships = service.getPendingMemberships(peer); + Assume.assumeTrue(CollectionUtils.isNotEmpty(pendingMemberships)); + + MutableInt counter = new MutableInt(0); + pendingMemberships.stream() + // Get first 10 + .filter(ms -> { + counter.increment(); + return counter.getValue() < 10; + }) + .forEach(ms -> { + List<WotRequirements> result = service.getRequirementsByPubkey(peer, ms.getPubkey()); + Assert.assertNotNull(result); + Assert.assertNotNull(result.size() > 0); + }); + } + + /* -- internal methods */ protected Wallet createTestWallet() {