| @@ -68,6 +68,8 @@ public interface DataCodes { | |||
| // public static final int METADATA_PARTICIPANT_INFO = 0x640; | |||
| public static final int METADATA_CRYPTO_SETTING = 0x642; | |||
| public static final int METADATA_CRYPTO_SETTING_PROVIDER = 0x643; | |||
| // public static final int ACCOUNT = 0x700; | |||
| @@ -4,6 +4,7 @@ import java.security.AccessControlContext; | |||
| import java.security.AccessController; | |||
| import java.security.PrivilegedAction; | |||
| import java.util.Collection; | |||
| import java.util.Collections; | |||
| import java.util.HashMap; | |||
| import java.util.LinkedHashMap; | |||
| import java.util.Map; | |||
| @@ -48,32 +49,45 @@ public final class ProviderManager { | |||
| * @return | |||
| */ | |||
| public <S> S getService(Class<S> serviceClazz, String providerName) { | |||
| NamedProviders<S> providers = getServiceProvider(serviceClazz); | |||
| NamedProviders<S> providers = getNamedProviders(serviceClazz); | |||
| return providers.getService(providerName); | |||
| } | |||
| public <S> Provider<S> getProvider(Class<S> serviceClazz, String providerName) { | |||
| @SuppressWarnings("unchecked") | |||
| NamedProviders<S> providers = (NamedProviders<S>) serviceProviders.get(serviceClazz); | |||
| if (providers == null) { | |||
| return null; | |||
| } | |||
| return providers.getProvider(providerName); | |||
| } | |||
| public <S> Collection<Provider<S>> getAllProviders(Class<S> serviceClazz) { | |||
| NamedProviders<S> providers = getServiceProvider(serviceClazz); | |||
| @SuppressWarnings("unchecked") | |||
| NamedProviders<S> providers = (NamedProviders<S>) serviceProviders.get(serviceClazz); | |||
| if (providers == null) { | |||
| return Collections.emptyList(); | |||
| } | |||
| return providers.getProviders(); | |||
| } | |||
| public <S> S installProvider(Class<S> serviceClazz, String providerFullName) { | |||
| NamedProviders<S> providers = getServiceProvider(serviceClazz); | |||
| NamedProviders<S> providers = getNamedProviders(serviceClazz); | |||
| return providers.install(providerFullName); | |||
| } | |||
| public <S> S installProvider(Class<S> service, String providerFullName, ClassLoader classLoader) { | |||
| NamedProviders<S> providers = getServiceProvider(service); | |||
| NamedProviders<S> providers = getNamedProviders(service); | |||
| return providers.install(providerFullName, classLoader); | |||
| } | |||
| public <S> void installAllProviders(Class<S> serviceClazz, ClassLoader classLoader) { | |||
| NamedProviders<S> providers = getServiceProvider(serviceClazz); | |||
| NamedProviders<S> providers = getNamedProviders(serviceClazz); | |||
| providers.installAll(classLoader); | |||
| } | |||
| @SuppressWarnings("unchecked") | |||
| private <S> NamedProviders<S> getServiceProvider(Class<S> serviceClazz) { | |||
| private <S> NamedProviders<S> getNamedProviders(Class<S> serviceClazz) { | |||
| NamedProviders<S> providers = (NamedProviders<S>) serviceProviders.get(serviceClazz); | |||
| if (providers == null) { | |||
| synchronized (mutex) { | |||
| @@ -189,6 +203,11 @@ public final class ProviderManager { | |||
| public Collection<Provider<S>> getProviders() { | |||
| return namedProviders.values(); | |||
| } | |||
| public Provider<S> getProvider(String providerFullName) { | |||
| return namedProviders.get(providerFullName); | |||
| } | |||
| public S getService(String name) { | |||
| String fullName = shortNames.get(name); | |||
| @@ -106,6 +106,38 @@ public final class Crypto { | |||
| private Crypto() { | |||
| } | |||
| public static CryptoProvider[] getProviders() { | |||
| Collection<Provider<CryptoService>> providers = pm.getAllProviders(CryptoService.class); | |||
| CryptoProvider[] infos = new CryptoProvider[providers.size()]; | |||
| int i = 0; | |||
| for (Provider<CryptoService> pd : providers) { | |||
| CryptoProviderInfo info = getProviderInfo(pd); | |||
| infos[i] = info; | |||
| } | |||
| return infos; | |||
| } | |||
| private static CryptoProviderInfo getProviderInfo(Provider<CryptoService> pd) { | |||
| Collection<CryptoFunction> functions = pd.getService().getFunctions(); | |||
| CryptoAlgorithm[] algorithms = new CryptoAlgorithm[functions.size()]; | |||
| int i = 0; | |||
| for (CryptoFunction function : functions) { | |||
| algorithms[i] = function.getAlgorithm(); | |||
| i++; | |||
| } | |||
| return new CryptoProviderInfo(pd.getFullName(), algorithms); | |||
| } | |||
| public static CryptoProvider getProvider(String providerFullName) { | |||
| Provider<CryptoService> pd = pm.getProvider(CryptoService.class, providerFullName); | |||
| if (pd == null) { | |||
| return null; | |||
| } | |||
| return getProviderInfo(pd); | |||
| } | |||
| public static Collection<CryptoAlgorithm> getAllAlgorithms() { | |||
| return algorithms.values(); | |||
| } | |||
| @@ -9,7 +9,7 @@ import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| import com.jd.blockchain.utils.io.BytesUtils; | |||
| //@DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||
| @DataContract(code = DataCodes.CRYPTO_ALGORITHM) | |||
| public interface CryptoAlgorithm { | |||
| /** | |||
| @@ -63,7 +63,7 @@ public interface CryptoAlgorithm { | |||
| * {@link #EXT_ALGORITHM}) 5 种); 接下来4位标识密钥类型(包括:{@link #SYMMETRIC_KEY}, | |||
| * {@link #ASYMMETRIC_KEY}); 最后8位是算法唯一ID; | |||
| */ | |||
| // @DataField(primitiveType = PrimitiveType.INT16, order = 0) | |||
| @DataField(order = 0, primitiveType = PrimitiveType.INT16) | |||
| short code(); | |||
| /** | |||
| @@ -75,7 +75,13 @@ public interface CryptoAlgorithm { | |||
| * | |||
| * @return | |||
| */ | |||
| @DataField(order = 1, primitiveType = PrimitiveType.TEXT) | |||
| String name(); | |||
| public static String getString(CryptoAlgorithm algorithm) { | |||
| return String.format("%s[%s]", algorithm.name(), (algorithm.code() & 0xFFFF)); | |||
| } | |||
| /** | |||
| * | |||
| @@ -23,16 +23,14 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| @Override | |||
| public String toString() { | |||
| return name + "[" + (code & 0xFFFF) + "]"; | |||
| return CryptoAlgorithm.getString(this); | |||
| } | |||
| /** | |||
| * 声明一项哈希算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm defineHash(String name, byte uid) { | |||
| @@ -43,10 +41,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| /** | |||
| * 声明一项非对称密码算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm defineSignature(String name, boolean encryptable, byte uid) { | |||
| @@ -62,10 +58,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| /** | |||
| * 声明一项非对称加密算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm defineAsymmetricEncryption(String name, byte uid) { | |||
| @@ -76,10 +70,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| /** | |||
| * 声明一项对称密码算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm defineSymmetricEncryption(String name, byte uid) { | |||
| @@ -90,10 +82,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| /** | |||
| * 声明一项随机数算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm defineRandom(String name, byte uid) { | |||
| @@ -104,10 +94,8 @@ public final class CryptoAlgorithmDefinition implements CryptoAlgorithm { | |||
| /** | |||
| * 声明一项扩展的密码算法; | |||
| * | |||
| * @param name | |||
| * 算法名称; | |||
| * @param uid | |||
| * 算法ID;需要在同类算法中保持唯一性; | |||
| * @param name 算法名称; | |||
| * @param uid 算法ID;需要在同类算法中保持唯一性; | |||
| * @return | |||
| */ | |||
| public static CryptoAlgorithm definExt(String name, byte uid) { | |||
| @@ -0,0 +1,17 @@ | |||
| package com.jd.blockchain.crypto; | |||
| import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code = DataCodes.METADATA_CRYPTO_SETTING_PROVIDER) | |||
| public interface CryptoProvider { | |||
| @DataField(order = 0, primitiveType = PrimitiveType.TEXT) | |||
| String getName(); | |||
| @DataField(order = 1, list = true, refContract = true) | |||
| CryptoAlgorithm[] getAlgorithms(); | |||
| } | |||
| @@ -0,0 +1,24 @@ | |||
| package com.jd.blockchain.crypto; | |||
| class CryptoProviderInfo implements CryptoProvider { | |||
| private String name; | |||
| private CryptoAlgorithm[] algorithms; | |||
| public CryptoProviderInfo(String name, CryptoAlgorithm[] algorithms) { | |||
| this.name = name; | |||
| this.algorithms = algorithms; | |||
| } | |||
| @Override | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| @Override | |||
| public CryptoAlgorithm[] getAlgorithms() { | |||
| return algorithms.clone(); | |||
| } | |||
| } | |||
| @@ -1,13 +1,23 @@ | |||
| package com.jd.blockchain.ledger.core; | |||
| import java.util.HashMap; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| public class CryptoConfig implements CryptoSetting { | |||
| private CryptoProvider[] cryptoProviders; | |||
| private short hashAlgorithm; | |||
| private boolean autoVerifyHash; | |||
| HashMap<String, CryptoProvider> providers; | |||
| HashMap<String, CryptoAlgorithm> nameAlgorithms; | |||
| HashMap<Short, CryptoAlgorithm> codeAlgorithms; | |||
| public CryptoConfig() { | |||
| } | |||
| @@ -17,6 +27,11 @@ public class CryptoConfig implements CryptoSetting { | |||
| this.autoVerifyHash = setting.getAutoVerifyHash(); | |||
| } | |||
| @Override | |||
| public CryptoProvider[] getSupportedProviders() { | |||
| return cryptoProviders == null ? null : cryptoProviders.clone(); | |||
| } | |||
| @Override | |||
| public short getHashAlgorithm() { | |||
| return hashAlgorithm; | |||
| @@ -27,11 +42,47 @@ public class CryptoConfig implements CryptoSetting { | |||
| return autoVerifyHash; | |||
| } | |||
| public void setSupportedProviders(CryptoProvider[] supportedProviders) { | |||
| HashMap<String, CryptoProvider> providers = new HashMap<String, CryptoProvider>(); | |||
| HashMap<String, CryptoAlgorithm> nameAlgorithms = new HashMap<String, CryptoAlgorithm>(); | |||
| HashMap<Short, CryptoAlgorithm> codeAlgorithms = new HashMap<Short, CryptoAlgorithm>(); | |||
| if (supportedProviders != null) { | |||
| // 检查是否存在重复的提供者以及算法; | |||
| for (CryptoProvider cryptoProvider : supportedProviders) { | |||
| if (providers.containsKey(cryptoProvider.getName())) { | |||
| throw new LedgerException("Duplicate crypto providers [" + cryptoProvider.getName() + "]!"); | |||
| } | |||
| CryptoAlgorithm[] algorithms = cryptoProvider.getAlgorithms(); | |||
| for (CryptoAlgorithm alg : algorithms) { | |||
| if (nameAlgorithms.containsKey(alg.name())) { | |||
| throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider " | |||
| + cryptoProvider.getName() + "!"); | |||
| } | |||
| if (codeAlgorithms.containsKey(alg.code())) { | |||
| throw new LedgerException("Duplicate crypto algorithms [" + alg.toString() + "] from provider" | |||
| + cryptoProvider.getName() + "!"); | |||
| } | |||
| nameAlgorithms.put(alg.name(), alg); | |||
| codeAlgorithms.put(alg.code(), alg); | |||
| } | |||
| providers.put(cryptoProvider.getName(), cryptoProvider); | |||
| } | |||
| } | |||
| this.providers = providers; | |||
| this.nameAlgorithms = nameAlgorithms; | |||
| this.codeAlgorithms = codeAlgorithms; | |||
| this.cryptoProviders = supportedProviders; | |||
| } | |||
| public void setHashAlgorithm(CryptoAlgorithm hashAlgorithm) { | |||
| this.hashAlgorithm = hashAlgorithm.code(); | |||
| setHashAlgorithm(hashAlgorithm.code()); | |||
| } | |||
| public void setHashAlgorithm(short hashAlgorithm) { | |||
| if (codeAlgorithms == null || !codeAlgorithms.containsKey(hashAlgorithm)) { | |||
| throw new LedgerException("The specified algorithm has no provider!"); | |||
| } | |||
| this.hashAlgorithm = hashAlgorithm; | |||
| } | |||
| @@ -6,8 +6,12 @@ import static org.junit.Assert.assertTrue; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.core.AccountSet; | |||
| @@ -17,6 +21,11 @@ import com.jd.blockchain.ledger.core.impl.OpeningAccessPolicy; | |||
| import com.jd.blockchain.storage.service.utils.MemoryKVStorage; | |||
| public class AccountSetTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| @Test | |||
| public void test() { | |||
| @@ -24,7 +33,12 @@ public class AccountSetTest { | |||
| MemoryKVStorage storage = new MemoryKVStorage(); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConf = new CryptoConfig(); | |||
| cryptoConf.setSupportedProviders(supportedProviders); | |||
| cryptoConf.setAutoVerifyHash(true); | |||
| cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -5,7 +5,11 @@ import static org.junit.Assert.assertFalse; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.core.BaseAccount; | |||
| @@ -22,12 +26,21 @@ import com.jd.blockchain.utils.io.BytesUtils; | |||
| */ | |||
| public class BaseAccountTest { | |||
| public static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| @Test | |||
| public void basicTest() { | |||
| String keyPrefix = ""; | |||
| MemoryKVStorage testStorage = new MemoryKVStorage(); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConf = new CryptoConfig(); | |||
| cryptoConf.setSupportedProviders(supportedProviders); | |||
| cryptoConf.setAutoVerifyHash(true); | |||
| cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -13,8 +13,12 @@ import com.jd.blockchain.ledger.LedgerMetadata; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| @@ -30,6 +34,9 @@ import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class LedgerAdminAccountTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| private Random rand = new Random(); | |||
| @Test | |||
| @@ -55,7 +62,13 @@ public class LedgerAdminAccountTest { | |||
| initSetting.setConsensusSettings(new Bytes(csSysSettingBytes)); | |||
| initSetting.setConsensusProvider("consensus-provider"); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setSupportedProviders(supportedProviders); | |||
| cryptoSetting.setAutoVerifyHash(true); | |||
| cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| initSetting.setCryptoSetting(cryptoSetting); | |||
| @@ -11,8 +11,11 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.SignatureFunction; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| @@ -34,6 +37,10 @@ import com.jd.blockchain.utils.io.BytesUtils; | |||
| import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class LedgerEditerTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| static { | |||
| DataContractRegistry.register(com.jd.blockchain.ledger.TransactionContent.class); | |||
| @@ -109,8 +116,14 @@ public class LedgerEditerTest { | |||
| private LedgerInitSetting createLedgerInitSetting() { | |||
| SignatureFunction signFunc = Crypto.getSignatureFunction("ED25519"); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setSupportedProviders(supportedProviders); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -12,7 +12,11 @@ import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| @@ -27,6 +31,9 @@ import com.jd.blockchain.utils.net.NetworkAddress; | |||
| public class LedgerInitOperationTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| byte[] seed = null; | |||
| byte[] csSysSettingBytes = null; | |||
| LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
| @@ -44,7 +51,12 @@ public class LedgerInitOperationTest { | |||
| csSysSettingBytes = new byte[64]; | |||
| rand.nextBytes(csSysSettingBytes); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -12,7 +12,11 @@ import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| import com.jd.blockchain.ledger.LedgerInitSetting; | |||
| @@ -30,6 +34,9 @@ public class LedgerInitSettingTest { | |||
| LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
| LedgerInitOpTemplate template = new LedgerInitOpTemplate(); | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| @Before | |||
| public void initCfg() { | |||
| @@ -41,7 +48,13 @@ public class LedgerInitSettingTest { | |||
| csSysSettingBytes = new byte[64]; | |||
| rand.nextBytes(csSysSettingBytes); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -13,9 +13,12 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.SignatureFunction; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockBody; | |||
| import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
| import com.jd.blockchain.ledger.BlockchainKeypair; | |||
| @@ -55,6 +58,10 @@ public class LedgerManagerTest { | |||
| DataContractRegistry.register(DataAccountRegisterOperation.class); | |||
| DataContractRegistry.register(BlockBody.class); | |||
| } | |||
| public static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| private SignatureFunction signatureFunction; | |||
| @@ -170,7 +177,16 @@ public class LedgerManagerTest { | |||
| } | |||
| private LedgerInitSetting createLedgerInitSetting() { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setSupportedProviders(supportedProviders); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -15,9 +15,13 @@ import org.junit.Test; | |||
| import com.jd.blockchain.binaryproto.BinaryProtocol; | |||
| import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.ParticipantNode; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| @@ -30,6 +34,10 @@ import com.jd.blockchain.utils.Bytes; | |||
| * Created by zhangshuang3 on 2018/8/31. | |||
| */ | |||
| public class LedgerMetaDataTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| byte[] seed = null; | |||
| String consensusProvider = "test-provider"; | |||
| byte[] consensusSettingBytes = null; | |||
| @@ -56,7 +64,13 @@ public class LedgerMetaDataTest { | |||
| // ConsensusConfig consensusConfig = new ConsensusConfig(); | |||
| // consensusConfig.setValue(settingValue);ClassicCryptoService.ED25519_ALGORITHM | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -93,7 +107,13 @@ public class LedgerMetaDataTest { | |||
| // ConsensusConfig consensusConfig = new ConsensusConfig(); | |||
| // consensusConfig.setValue(settingValue); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| @@ -133,7 +153,14 @@ public class LedgerMetaDataTest { | |||
| @Test | |||
| public void testSerialize_CryptoSetting() { | |||
| // LedgerCodes.METADATA_LEDGER_SETTING_CRYPTO | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| byte[] encodeBytes = BinaryProtocol.encode(cryptoConfig, CryptoSetting.class); | |||
| @@ -4,10 +4,13 @@ import java.util.Random; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.SignatureFunction; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| import com.jd.blockchain.ledger.PreparedTransaction; | |||
| @@ -22,6 +25,9 @@ public class LedgerTestUtils { | |||
| // private static ThreadLocalRandom rand = ThreadLocalRandom.current(); | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| private static Random rand = new Random(); | |||
| public static TransactionRequest createTxRequest(HashDigest ledgerHash) { | |||
| @@ -76,7 +82,14 @@ public class LedgerTestUtils { | |||
| } | |||
| public static CryptoSetting createDefaultCryptoSetting() { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setSupportedProviders(supportedProviders); | |||
| cryptoSetting.setAutoVerifyHash(true); | |||
| cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| return cryptoSetting; | |||
| @@ -14,8 +14,12 @@ import java.util.Set; | |||
| import org.junit.Test; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.core.CryptoConfig; | |||
| import com.jd.blockchain.ledger.core.MerkleDataSet; | |||
| import com.jd.blockchain.ledger.core.MerkleProof; | |||
| @@ -26,13 +30,23 @@ import com.jd.blockchain.utils.io.BytesUtils; | |||
| public class MerkleDataSetTest { | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| /** | |||
| * 测试存储的增长; | |||
| */ | |||
| @Test | |||
| public void testStorageIncreasement() { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| String keyPrefix = ""; | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| @@ -116,7 +130,13 @@ public class MerkleDataSetTest { | |||
| String keyPrefix = ""; | |||
| Random rand = new Random(); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| @@ -281,7 +301,13 @@ public class MerkleDataSetTest { | |||
| String keyPrefix = ""; | |||
| Random rand = new Random(); | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoConfig = new CryptoConfig(); | |||
| cryptoConfig.setSupportedProviders(supportedProviders); | |||
| cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
| cryptoConfig.setAutoVerifyHash(true); | |||
| @@ -1,12 +0,0 @@ | |||
| package com.jd.blockchain.ledger; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| public interface CryptoProviderInfo { | |||
| String getName(); | |||
| CryptoAlgorithm[] getAlgorithms(); | |||
| } | |||
| @@ -4,6 +4,7 @@ import com.jd.blockchain.binaryproto.DataContract; | |||
| import com.jd.blockchain.binaryproto.DataField; | |||
| import com.jd.blockchain.binaryproto.PrimitiveType; | |||
| import com.jd.blockchain.consts.DataCodes; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| /** | |||
| * 默克尔树算法相关的配置; | |||
| @@ -14,6 +15,15 @@ import com.jd.blockchain.consts.DataCodes; | |||
| @DataContract(code = DataCodes.METADATA_CRYPTO_SETTING) | |||
| public interface CryptoSetting { | |||
| /** | |||
| * 系统支持的密码服务提供者; | |||
| * | |||
| * @return | |||
| */ | |||
| public CryptoProvider[] getSupportedProviders(); | |||
| /** | |||
| * 系统中使用的 Hash 算法; <br> | |||
| * | |||
| @@ -27,7 +37,7 @@ public interface CryptoSetting { | |||
| public short getHashAlgorithm(); | |||
| /** | |||
| * 当有完整性证明的数据被从持久化介质中加载时,是否对其进行完整性校验(重新计算 hash 比对是否一致); <br> | |||
| * 当有加载附带哈希摘要的数据时,是否重新计算哈希摘要进行完整性校验; <br> | |||
| * | |||
| * 如果为 true ,则自动进行校验,如果校验失败,会引发异常; <br> | |||
| * | |||
| @@ -37,5 +47,6 @@ public interface CryptoSetting { | |||
| */ | |||
| @DataField(order = 2, primitiveType = PrimitiveType.BOOLEAN) | |||
| public boolean getAutoVerifyHash(); | |||
| } | |||
| @@ -49,7 +49,7 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { | |||
| public void set(String key, BytesValue value, long expVersion) { | |||
| if (kvset.containsKey(key)) { | |||
| throw new IllegalArgumentException("Cann't set the same key repeatly!"); | |||
| throw new IllegalArgumentException("Cann't set the same key repeatedly!"); | |||
| } | |||
| KVData kvdata = new KVData(key, value, expVersion); | |||
| kvset.put(key, kvdata); | |||
| @@ -57,7 +57,7 @@ public class DataAccountKVSetOpTemplate implements DataAccountKVSetOperation { | |||
| public void set(KVData kvData) { | |||
| if (kvset.containsKey(kvData.getKey())) { | |||
| throw new IllegalArgumentException("Cann't set the same key repeatly!"); | |||
| throw new IllegalArgumentException("Cann't set the same key repeatedly!"); | |||
| } | |||
| kvset.put(kvData.getKey(), kvData); | |||
| } | |||
| @@ -19,12 +19,15 @@ import com.jd.blockchain.consensus.ConsensusProviders; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.AddressEncoding; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.SignatureDigest; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.LedgerBlock; | |||
| import com.jd.blockchain.ledger.LedgerInitOperation; | |||
| import com.jd.blockchain.ledger.UserRegisterOperation; | |||
| @@ -60,6 +63,9 @@ public class LedgerInitializeTest { | |||
| DataContractRegistry.register(UserRegisterOperation.class); | |||
| } | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| public static final String PASSWORD = IntegrationBase.PASSWORD; | |||
| public static final String[] PUB_KEYS = IntegrationBase.PUB_KEYS; | |||
| @@ -254,7 +260,12 @@ public class LedgerInitializeTest { | |||
| ConsensusSettings csProps, ConsensusProvider csProvider, DBConnectionConfig dbConnConfig, | |||
| Prompter prompter, boolean autoVerifyHash) { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig cryptoSetting = new CryptoConfig(); | |||
| cryptoSetting.setSupportedProviders(supportedProviders); | |||
| cryptoSetting.setAutoVerifyHash(autoVerifyHash); | |||
| cryptoSetting.setHashAlgorithm(Crypto.getAlgorithm("SHA256")); | |||
| @@ -20,12 +20,15 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
| import com.jd.blockchain.consensus.ConsensusProvider; | |||
| import com.jd.blockchain.consensus.ConsensusSettings; | |||
| import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
| import com.jd.blockchain.crypto.CryptoProvider; | |||
| import com.jd.blockchain.crypto.Crypto; | |||
| import com.jd.blockchain.crypto.HashDigest; | |||
| import com.jd.blockchain.crypto.PrivKey; | |||
| import com.jd.blockchain.crypto.PubKey; | |||
| import com.jd.blockchain.crypto.SignatureDigest; | |||
| import com.jd.blockchain.crypto.SignatureFunction; | |||
| import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
| import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
| import com.jd.blockchain.ledger.BlockchainIdentity; | |||
| import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
| import com.jd.blockchain.ledger.CryptoSetting; | |||
| @@ -75,6 +78,10 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| static { | |||
| DataContractRegistry.register(TransactionRequest.class); | |||
| } | |||
| private static final String[] SUPPORTED_PROVIDERS = { ClassicCryptoService.class.getName(), | |||
| SMCryptoService.class.getName() }; | |||
| private static final String DEFAULT_SIGN_ALGORITHM = "ED25519"; | |||
| @@ -299,7 +306,12 @@ public class LedgerInitializeWebController implements LedgerInitProcess, LedgerI | |||
| } | |||
| public CryptoSetting createDefaultCryptoSetting() { | |||
| CryptoProvider[] supportedProviders = new CryptoProvider[SUPPORTED_PROVIDERS.length]; | |||
| for (int i = 0; i < SUPPORTED_PROVIDERS.length; i++) { | |||
| supportedProviders[i] = Crypto.getProvider(SUPPORTED_PROVIDERS[i]); | |||
| } | |||
| CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
| defCryptoSetting.setSupportedProviders(supportedProviders); | |||
| defCryptoSetting.setAutoVerifyHash(true); | |||
| defCryptoSetting.setHashAlgorithm(Crypto.getAlgorithm("SHA256")); | |||