diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/GsonUtils.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/GsonUtils.java index 497ff6600ac68487121e75e9b962e8a392da95cd..677c1c2b899a47edd0bf3e898692a01a4cb61a5b 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/GsonUtils.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/GsonUtils.java @@ -27,6 +27,8 @@ import com.google.common.collect.Multimap; import com.google.gson.GsonBuilder; import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.NetworkPeering; +import org.duniter.core.util.json.JsonArrayParser; +import org.duniter.core.util.json.JsonAttributeParser; import java.util.List; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/CertificationDeserializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/CertificationDeserializer.java index 542130abf1f05b46b3be48cdb7272d27b395672a..0d869e6232c132942325317beef28a39ee468e8d 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/CertificationDeserializer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/CertificationDeserializer.java @@ -3,9 +3,9 @@ package org.duniter.core.client.model.bma.jackson; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import com.google.gson.JsonParseException; import org.apache.commons.lang3.StringUtils; import org.duniter.core.client.model.bma.BlockchainBlock; +import org.duniter.core.util.json.JsonSyntaxException; import java.io.IOException; @@ -22,7 +22,7 @@ public class CertificationDeserializer extends JsonDeserializer<BlockchainBlock. String[] parts = certificationStr.split(":"); if (parts.length != 4) { - throw new JsonParseException(String.format("Bad format for BlockchainBlock.Certification. Should have 4 parts, but found %s.", parts.length)); + throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Certification. Should have 4 parts, but found %s.", parts.length)); } BlockchainBlock.Certification result = new BlockchainBlock.Certification(); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointDeserializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointDeserializer.java index e67c1d2b23f179b6401afe0268a4a0bf45ce802a..2681d5df51fa4855c21a34d6026e537e38927d26 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointDeserializer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/EndpointDeserializer.java @@ -1,13 +1,9 @@ package org.duniter.core.client.model.bma.jackson; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import com.google.gson.JsonParseException; -import org.apache.commons.lang3.StringUtils; import org.apache.http.conn.util.InetAddressUtils; -import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.EndpointProtocol; import org.duniter.core.client.model.bma.NetworkPeering; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/IdentityDeserializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/IdentityDeserializer.java index 49dd54b7d77504626b3ab1601c1a277da4789318..42f6612fa516ae32e7299640f2e414400d0a1944 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/IdentityDeserializer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/IdentityDeserializer.java @@ -1,12 +1,11 @@ package org.duniter.core.client.model.bma.jackson; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import com.google.gson.JsonParseException; import org.apache.commons.lang3.StringUtils; import org.duniter.core.client.model.bma.BlockchainBlock; +import org.duniter.core.util.json.JsonSyntaxException; import java.io.IOException; @@ -24,7 +23,7 @@ public class IdentityDeserializer extends JsonDeserializer<BlockchainBlock.Ident String[] identityParts = identityStr.split(":"); if (identityParts.length != 4) { - throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length)); + throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length)); } BlockchainBlock.Identity result = new BlockchainBlock.Identity(); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java index b7118cac3fc9358c06d1a9ee868f310cdecdd9cf..e51c2a1b25f5176390110ca4b607df4b4fc0c7b3 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JacksonUtils.java @@ -4,8 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.NetworkPeering; -import org.duniter.core.client.model.bma.gson.JsonArrayParser; -import org.duniter.core.client.model.bma.gson.JsonAttributeParser; +import org.duniter.core.util.json.JsonArrayParser; +import org.duniter.core.util.json.JsonAttributeParser; import java.util.List; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JoinerDeserializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JoinerDeserializer.java index 2cfa2a1e01c1e5e9fd65e316d0c99fc37e604157..5e18c24e163d98847ba5c40bf0893ad6f567b4c4 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JoinerDeserializer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/JoinerDeserializer.java @@ -1,12 +1,11 @@ package org.duniter.core.client.model.bma.jackson; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import com.google.gson.JsonParseException; import org.apache.commons.lang3.StringUtils; import org.duniter.core.client.model.bma.BlockchainBlock; +import org.duniter.core.util.json.JsonSyntaxException; import java.io.IOException; @@ -23,7 +22,7 @@ public class JoinerDeserializer extends JsonDeserializer<BlockchainBlock.Joiner> String[] identityParts = identityStr.split(":"); if (identityParts.length != 5) { - throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", identityParts.length)); + throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", identityParts.length)); } BlockchainBlock.Joiner result = new BlockchainBlock.Joiner(); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/RevokedDeserializer.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/RevokedDeserializer.java index 5a9f7787e6df874023d16887d36dc5ccad38a04e..b0ea88426e4189dce843e4e9fb7475fad0e09b60 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/RevokedDeserializer.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/jackson/RevokedDeserializer.java @@ -3,9 +3,9 @@ package org.duniter.core.client.model.bma.jackson; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; -import com.google.gson.JsonParseException; import org.apache.commons.lang3.StringUtils; import org.duniter.core.client.model.bma.BlockchainBlock; +import org.duniter.core.util.json.JsonSyntaxException; import java.io.IOException; @@ -22,7 +22,7 @@ public class RevokedDeserializer extends JsonDeserializer<BlockchainBlock.Revoke String[] parts = str.split(":"); if (parts.length != 2) { - throw new JsonParseException(String.format("Bad format for BlockchainBlock.Revoked. Should have 2 parts, but found %s.", parts.length)); + throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Revoked. Should have 2 parts, but found %s.", parts.length)); } BlockchainBlock.Revoked result = new BlockchainBlock.Revoked(); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java index b0735d8c79edb8eefcf2d82d855e4c9cbbba7ba1..7e86ec65d66784ba0a6b3e2cb4a0da51f2b30f96 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java +++ b/duniter4j-core-client/src/main/java/org/duniter/core/client/service/bma/BlockchainRemoteServiceImpl.java @@ -29,7 +29,7 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.duniter.core.client.config.Configuration; import org.duniter.core.client.model.bma.*; -import org.duniter.core.client.model.bma.gson.JsonArrayParser; +import org.duniter.core.util.json.JsonArrayParser; import org.duniter.core.client.model.local.Identity; import org.duniter.core.client.model.local.Peer; import org.duniter.core.client.model.local.Wallet; diff --git a/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/gson/JsonArrayParserTest.java b/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/gson/JsonArrayParserTest.java index 2b1e6d596be3dc982b82d58fcbb7ba20346a7e07..930f6c4d8a205915f726e3f526d933462ef39c49 100644 --- a/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/gson/JsonArrayParserTest.java +++ b/duniter4j-core-client/src/test/java/org/duniter/core/client/model/bma/gson/JsonArrayParserTest.java @@ -22,6 +22,7 @@ package org.duniter.core.client.model.bma.gson; * #L% */ +import org.duniter.core.util.json.JsonArrayParser; import org.junit.Assert; import org.junit.Test; diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/service/CryptoService.java b/duniter4j-core-shared/src/main/java/org/duniter/core/service/CryptoService.java index 2da1b39d93016041f32ab44c8fb406c6566f95f0..0fcf80a0da111b25d3e6cca31963280008ed6158 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/service/CryptoService.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/service/CryptoService.java @@ -53,6 +53,8 @@ public interface CryptoService extends Bean { */ KeyPair getKeyPairFromSeed(byte[] seed); + KeyPair getRandomKeypair(); + String sign(String message, byte[] secretKey); String sign(String message, String secretKey); diff --git a/duniter4j-core-shared/src/main/java/org/duniter/core/service/Ed25519CryptoServiceImpl.java b/duniter4j-core-shared/src/main/java/org/duniter/core/service/Ed25519CryptoServiceImpl.java index 57575d810e83fb1daefc00455de952ee69b83509..f822e0046a0bc2c54618e07949e59803569b8481 100644 --- a/duniter4j-core-shared/src/main/java/org/duniter/core/service/Ed25519CryptoServiceImpl.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/service/Ed25519CryptoServiceImpl.java @@ -97,6 +97,11 @@ public class Ed25519CryptoServiceImpl implements CryptoService { return new KeyPair(publicKey, secretKey); } + @Override + public KeyPair getRandomKeypair() { + return getKeyPairFromSeed(String.valueOf(System.currentTimeMillis()).getBytes()); + } + @Override public String sign(String message, byte[] secretKey) { byte[] messageBinary = CryptoUtils.decodeUTF8(message); diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonArrayParser.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonArrayParser.java similarity index 92% rename from duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonArrayParser.java rename to duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonArrayParser.java index 9c52222b354018b8905be63cace244dd8a1384a8..f336054b3ce479372ac10488673fea22024d28b6 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonArrayParser.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonArrayParser.java @@ -1,4 +1,4 @@ -package org.duniter.core.client.model.bma.gson; +package org.duniter.core.util.json; /* * #%L @@ -22,7 +22,6 @@ package org.duniter.core.client.model.bma.gson; * #L% */ -import com.google.gson.JsonParseException; import org.duniter.core.util.CollectionUtils; import java.util.ArrayList; @@ -40,7 +39,7 @@ public class JsonArrayParser { READING_ARRAY } - public String[] getValuesAsArray(String jsonArray) throws JsonParseException { + public String[] getValuesAsArray(String jsonArray) throws JsonSyntaxException { List<String> result = getValuesAsList(jsonArray); if (CollectionUtils.isEmpty(result)) { return null; @@ -48,7 +47,7 @@ public class JsonArrayParser { return result.toArray(new String[result.size()]); } - public List<String> getValuesAsList(String jsonArray) throws JsonParseException { + public List<String> getValuesAsList(String jsonArray) throws JsonSyntaxException { ParserState state = ParserState.READING_ARRAY; List<String> result = new ArrayList<String>(); StringBuilder currentObject = null; @@ -67,7 +66,7 @@ public class JsonArrayParser { } case '}': { if (state == ParserState.READING_ARRAY) { - throw new JsonParseException("unexpected '}' at " + i); + throw new JsonSyntaxException("unexpected '}' at " + i); } else { currentObject.append(c); parenthesisBalance--; diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonAttributeParser.java similarity index 98% rename from duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java rename to duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonAttributeParser.java index aac0d7ed4801e2ddbd4692d24dd95977e7c5d71c..62b04ec87546f97383c00440d05991e663fc4e71 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/model/bma/gson/JsonAttributeParser.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonAttributeParser.java @@ -1,4 +1,4 @@ -package org.duniter.core.client.model.bma.gson; +package org.duniter.core.util.json; /* * #%L diff --git a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/exception/JsonSyntaxException.java b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonSyntaxException.java similarity index 96% rename from duniter4j-core-client/src/main/java/org/duniter/core/client/service/exception/JsonSyntaxException.java rename to duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonSyntaxException.java index 1e3bc0896634125e49d15d96765c6df37da63d6a..bc22463df738db299b967f62ea7613ab178f91dd 100644 --- a/duniter4j-core-client/src/main/java/org/duniter/core/client/service/exception/JsonSyntaxException.java +++ b/duniter4j-core-shared/src/main/java/org/duniter/core/util/json/JsonSyntaxException.java @@ -1,4 +1,4 @@ -package org.duniter.core.client.service.exception; +package org.duniter.core.util.json; /* * #%L diff --git a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java index 7c765a24f2c9f6064bfa00b5769e60b290bc5868..b5f826d1bc97d83a909dbe0c318f2442eeb7eeea 100644 --- a/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java +++ b/duniter4j-es-core/src/main/java/org/duniter/elasticsearch/service/BlockchainService.java @@ -31,13 +31,13 @@ import com.google.common.collect.Lists; import org.duniter.core.client.model.bma.BlockchainBlock; import org.duniter.core.client.model.bma.BlockchainParameters; import org.duniter.core.client.model.bma.EndpointProtocol; -import org.duniter.core.client.model.bma.gson.JsonAttributeParser; +import org.duniter.core.util.json.JsonAttributeParser; import org.duniter.core.client.model.bma.jackson.JacksonUtils; import org.duniter.core.client.model.local.Peer; import org.duniter.core.client.service.bma.BlockchainRemoteService; import org.duniter.core.client.service.bma.NetworkRemoteService; import org.duniter.core.client.service.exception.BlockNotFoundException; -import org.duniter.core.client.service.exception.JsonSyntaxException; +import org.duniter.core.util.json.JsonSyntaxException; import org.duniter.core.exception.TechnicalException; import org.duniter.core.model.NullProgressionModel; import org.duniter.core.model.ProgressionModel; diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java index a3447cb813e89b1300970358196f1ed245be9ca6..8420fe9dbc5900e44a5419a56809ccd662896acf 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/BlockchainUserEventService.java @@ -33,12 +33,11 @@ import org.duniter.core.exception.TechnicalException; import org.duniter.core.service.CryptoService; import org.duniter.core.util.CollectionUtils; import org.duniter.core.util.websocket.WebsocketClientEndpoint; -import org.duniter.elasticsearch.user.PluginSettings; -import org.duniter.elasticsearch.user.service.AbstractService; import org.duniter.elasticsearch.service.BlockchainService; import org.duniter.elasticsearch.service.changes.ChangeEvent; import org.duniter.elasticsearch.service.changes.ChangeService; import org.duniter.elasticsearch.service.changes.ChangeSource; +import org.duniter.elasticsearch.user.PluginSettings; import org.duniter.elasticsearch.user.model.UserEvent; import org.duniter.elasticsearch.user.model.UserEventCodes; import org.elasticsearch.client.Client; @@ -46,7 +45,10 @@ import org.elasticsearch.common.inject.Inject; import org.nuiton.i18n.I18n; import java.io.IOException; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; /** * Created by Benoit on 30/03/2015. diff --git a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java index 79277567c11be0b83a2637e699632f535ca4467e..8b48a96ae92ef2b1d84cd253e37e7234c20fd33a 100644 --- a/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java +++ b/duniter4j-es-user/src/main/java/org/duniter/elasticsearch/user/service/UserEventService.java @@ -191,8 +191,8 @@ public class UserEventService extends AbstractService implements ChangeService.C signedEvent.setSignature(signature); eventJson = toJson(signedEvent); } else { + logger.debug("Could not generate hash for new user event (no keyring)"); // Node has not keyring: do NOT sign it - // TODO : autogen a key pair ? eventJson = event.toJson(locale); } @@ -377,14 +377,23 @@ public class UserEventService extends AbstractService implements ChangeService.C } private KeyPair getNodeKeyPairOrNull(PluginSettings pluginSettings) { - + KeyPair result; if (StringUtils.isNotBlank(pluginSettings.getKeyringSalt()) && StringUtils.isNotBlank(pluginSettings.getKeyringPassword())) { - return cryptoService.getKeyPair(pluginSettings.getKeyringSalt(), + result = cryptoService.getKeyPair(pluginSettings.getKeyringSalt(), pluginSettings.getKeyringPassword()); } + else { + // Use a ramdom keypair + result = cryptoService.getRandomKeypair(); + logger.warn(String.format("No keyring in config. salt/password (or keyring) is need to signed user event documents. Will use a generated key [%s]", getNodePubKey(result))); + if (logger.isDebugEnabled()) { + logger.debug(String.format(" salt: " + pluginSettings.getKeyringSalt().replaceAll(".", "*"))); + logger.debug(String.format("password: " + pluginSettings.getKeyringPassword().replaceAll(".", "*"))); + } + } - return null; + return result; } private String getNodePubKey(KeyPair nodeKeyPair) {