Search in sources :

Example 1 with CryptoProvider

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;
    }
}
Also used : CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoAlgorithm(com.jd.blockchain.crypto.CryptoAlgorithm)

Example 2 with CryptoProvider

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);
}
Also used : Bytes(utils.Bytes) Random(java.util.Random) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) Before(org.junit.Before)

Example 3 with CryptoProvider

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;
}
Also used : ConsensusParticipantData(com.jd.blockchain.transaction.ConsensusParticipantData) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) NetworkAddress(utils.net.NetworkAddress) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) LedgerInitData(com.jd.blockchain.transaction.LedgerInitData) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig)

Example 4 with CryptoProvider

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;
}
Also used : Bytes(utils.Bytes) Random(java.util.Random) LedgerConfiguration(com.jd.blockchain.ledger.core.LedgerConfiguration) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) LedgerSettings(com.jd.blockchain.ledger.LedgerSettings) Test(org.junit.Test)

Example 5 with CryptoProvider

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;
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerMetadata(com.jd.blockchain.ledger.LedgerMetadata) CryptoProvider(com.jd.blockchain.crypto.CryptoProvider) CryptoConfig(com.jd.blockchain.ledger.core.CryptoConfig) LedgerAdminDataSetEditor(com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor) Test(org.junit.Test)

Aggregations

CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)18 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)15 Test (org.junit.Test)8 MemoryKVStorage (com.jd.blockchain.storage.service.utils.MemoryKVStorage)6 Bytes (utils.Bytes)6 HashDigest (com.jd.blockchain.crypto.HashDigest)5 Random (java.util.Random)4 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)3 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)3 MerkleHashDataset (com.jd.blockchain.ledger.core.MerkleHashDataset)3 LedgerException (com.jd.blockchain.ledger.LedgerException)2 LedgerSettings (com.jd.blockchain.ledger.LedgerSettings)2 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)2 LedgerConfiguration (com.jd.blockchain.ledger.core.LedgerConfiguration)2 ConsensusParticipantData (com.jd.blockchain.transaction.ConsensusParticipantData)2 LedgerInitData (com.jd.blockchain.transaction.LedgerInitData)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 NetworkAddress (utils.net.NetworkAddress)2 AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)1