use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class CryptoConfigInfo method copyCryptoProviders.
private void copyCryptoProviders(CryptoProvider[] cps) {
if (cps == null || cps.length == 0) {
throw new IllegalArgumentException("CryptoProviders is empty !!!");
}
supportedProviders = new CryptoProviderInfo[cps.length];
for (int i = 0; i < cps.length; i++) {
// 生成
CryptoProvider cp = cps[i];
CryptoAlgorithm[] cpAlgorithms = cp.getAlgorithms();
if (cpAlgorithms == null || cpAlgorithms.length == 0) {
throw new IllegalArgumentException("CryptoAlgorithm is empty !!!");
}
CryptoAlgorithmInfo[] cryptoAlgorithmInfos = new CryptoAlgorithmInfo[cpAlgorithms.length];
for (int j = 0; j < cpAlgorithms.length; j++) {
cryptoAlgorithmInfos[j] = new CryptoAlgorithmInfo(cpAlgorithms[j]);
}
CryptoProviderInfo cryptoProviderInfo = new CryptoProviderInfo(cp.getName(), cryptoAlgorithmInfos);
supportedProviders[i] = cryptoProviderInfo;
}
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerInitSettingSerializeTest method initCfg.
@Before
public void initCfg() {
DataContractRegistry.register(LedgerInitSetting.class);
Random rand = new Random();
seed = new byte[8];
rand.nextBytes(seed);
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);
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes));
ledgerInitSettingData.setConsensusProvider("cons-provider");
ledgerInitSettingData.setLedgerSeed(seed);
ledgerInitSettingData.setCryptoSetting(cryptoConfig);
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerManagerTest method createLedgerInitSetting.
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);
LedgerInitData initSetting = new LedgerInitData();
initSetting.setLedgerSeed(BytesUtils.toBytes("A Test Ledger seed!", "UTF-8"));
initSetting.setCryptoSetting(defCryptoSetting);
ConsensusParticipantData[] parties = new ConsensusParticipantData[4];
parties[0] = new ConsensusParticipantData();
parties[0].setId(0);
parties[0].setName("John");
AsymmetricKeypair kp0 = signatureFunction.generateKeypair();
parties[0].setPubKey(kp0.getPubKey());
parties[0].setAddress(AddressEncoding.generateAddress(kp0.getPubKey()));
parties[0].setHostAddress(new NetworkAddress("127.0.0.1", 9000));
parties[0].setParticipantState(ParticipantNodeState.CONSENSUS);
parties[1] = new ConsensusParticipantData();
parties[1].setId(1);
parties[1].setName("Mary");
AsymmetricKeypair kp1 = signatureFunction.generateKeypair();
parties[1].setPubKey(kp1.getPubKey());
parties[1].setAddress(AddressEncoding.generateAddress(kp1.getPubKey()));
parties[1].setHostAddress(new NetworkAddress("127.0.0.1", 9010));
parties[1].setParticipantState(ParticipantNodeState.CONSENSUS);
parties[2] = new ConsensusParticipantData();
parties[2].setId(2);
parties[2].setName("Jerry");
AsymmetricKeypair kp2 = signatureFunction.generateKeypair();
parties[2].setPubKey(kp2.getPubKey());
parties[2].setAddress(AddressEncoding.generateAddress(kp2.getPubKey()));
parties[2].setHostAddress(new NetworkAddress("127.0.0.1", 9020));
parties[2].setParticipantState(ParticipantNodeState.CONSENSUS);
parties[3] = new ConsensusParticipantData();
parties[3].setId(3);
parties[3].setName("Tom");
AsymmetricKeypair kp3 = signatureFunction.generateKeypair();
parties[3].setPubKey(kp3.getPubKey());
parties[3].setAddress(AddressEncoding.generateAddress(kp3.getPubKey()));
parties[3].setHostAddress(new NetworkAddress("127.0.0.1", 9030));
parties[3].setParticipantState(ParticipantNodeState.CONSENSUS);
initSetting.setConsensusParticipants(parties);
initSetting.setLedgerDataStructure(LedgerDataStructure.MERKLE_TREE);
initSetting.setIdentityMode(IdentityMode.KEYPAIR);
return initSetting;
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerMetaDataTest method testSerialize_LedgerSetting.
@Test
public void testSerialize_LedgerSetting() {
// LedgerCodes.METADATA_LEDGER_SETTING
Random rand = new Random();
byte[] csSettingsBytes = new byte[8];
rand.nextBytes(csSettingsBytes);
String consensusProvider = "testprovider";
// 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);
LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider, new Bytes(csSettingsBytes), cryptoConfig);
byte[] encodeBytes = BinaryProtocol.encode(ledgerConfiguration, LedgerSettings.class);
LedgerSettings deLedgerConfiguration = BinaryProtocol.decode(encodeBytes);
// verify start
assertTrue(ledgerConfiguration.getConsensusSetting().equals(deLedgerConfiguration.getConsensusSetting()));
assertEquals(ledgerConfiguration.getCryptoSetting().getAutoVerifyHash(), deLedgerConfiguration.getCryptoSetting().getAutoVerifyHash());
assertEquals(ledgerConfiguration.getCryptoSetting().getHashAlgorithm(), deLedgerConfiguration.getCryptoSetting().getHashAlgorithm());
return;
}
use of com.jd.blockchain.crypto.CryptoProvider in project jdchain-core by blockchain-jd-com.
the class LedgerMetaDataTest method testSerialize_LedgerMetadata.
@Test
public void testSerialize_LedgerMetadata() {
// LedgerCodes.METADATA
// prepare work
// 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);
// LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider,
// new Bytes(consensusSettingBytes), cryptoConfig);
HashDigest settingsHash = Crypto.getHashFunction("SHA256").hash(consensusSettingBytes);
LedgerAdminDataSetEditor.LedgerMetadataInfo ledgerMetadata = new LedgerAdminDataSetEditor.LedgerMetadataInfo();
ledgerMetadata.setSeed(seed);
ledgerMetadata.setSettingsHash(settingsHash);
HashDigest hashDigest = ClassicCryptoService.SHA256.hash(randomBytes);
ledgerMetadata.setParticipantsHash(hashDigest);
// encode and decode
byte[] encodeBytes = BinaryProtocol.encode(ledgerMetadata, LedgerMetadata.class);
LedgerMetadata deLedgerMetaData = BinaryProtocol.decode(encodeBytes);
// verify start
assertArrayEquals(ledgerMetadata.getSeed(), deLedgerMetaData.getSeed());
assertEquals(ledgerMetadata.getParticipantsHash(), deLedgerMetaData.getParticipantsHash());
assertEquals(ledgerMetadata.getSettingsHash(), deLedgerMetaData.getSettingsHash());
return;
}
Aggregations