Search in sources :

Example 11 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class CryptoConfig method setSupportedProviders.

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

Example 12 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class EventGroupPublisher method publish.

/**
 * 发布事件
 *
 * @param event
 * @return
 */
@Override
public long publish(Event event) {
    Bytes key = encodeKey(event.getName());
    long newSequence = events.setValue(key, BinaryProtocol.encode(event, Event.class), event.getSequence() - 1);
    if (newSequence < 0) {
        throw new LedgerException("Event sequence conflict! --[" + key + "]");
    }
    // 属于新发布的事件名
    if (ledgerDataStructure.equals(LedgerDataStructure.KV)) {
        if (newSequence == 0) {
            long nv = events.setValue(SYSTEMEVENT_SEQUENCE_KEY_PREFIX.concat(Bytes.fromString(String.valueOf(events.getDataCount() + event_index_in_block))), key.toBytes(), -1);
            if (nv < 0) {
                throw new LedgerException("Event seq already exist! --[id=" + key + "]");
            }
            event_index_in_block++;
        }
    }
    return newSequence;
}
Also used : Bytes(utils.Bytes) Event(com.jd.blockchain.ledger.Event) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 13 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class UserAccount method setDataPubKey.

public void setDataPubKey(PubKey pubKey, long version) {
    TypedValue value = TypedValue.fromPubKey(dataPubKey);
    long newVersion = getHeaders().setValue(DATA_PUB_KEY, value, version);
    if (newVersion > -1) {
        dataPubKey = pubKey;
    } else {
        throw new LedgerException("Data public key was updated failed!");
    }
}
Also used : LedgerException(com.jd.blockchain.ledger.LedgerException) TypedValue(com.jd.blockchain.ledger.TypedValue)

Example 14 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class RootCAUpdateOperationHandle method doProcess.

@Override
protected void doProcess(RootCAUpdateOperation op, LedgerTransactionContext transactionContext, TransactionRequestExtension requestContext, LedgerQuery ledger, OperationHandleContext handleContext, EventManager manager) {
    // 权限校验;
    SecurityPolicy securityPolicy = SecurityContext.getContextUsersPolicy();
    securityPolicy.checkEndpointPermission(LedgerPermission.UPDATE_ROOT_CA, MultiIDsPolicy.AT_LEAST_ONE);
    LedgerAdminDataSet adminDataset = transactionContext.getDataset().getAdminDataset();
    if (adminDataset.getAdminSettings().getMetadata().getIdentityMode() == IdentityMode.CA) {
        String[] ledgerCAs = adminDataset.getAdminSettings().getMetadata().getLedgerCertificates();
        Map<PublicKey, String> ledgerCAMap = new HashMap<>();
        for (int i = 0; i < ledgerCAs.length; i++) {
            X509Certificate cert = CertificateUtils.parseCertificate(ledgerCAs[i]);
            PublicKey publicKey = cert.getPublicKey();
            ledgerCAMap.put(publicKey, ledgerCAs[i]);
        }
        String[] certificatesAdd = op.getCertificatesAdd();
        for (String cert : certificatesAdd) {
            X509Certificate certificate = CertificateUtils.parseCertificate(cert);
            CertificateUtils.checkCACertificate(certificate);
            CertificateUtils.checkValidity(certificate);
            if (!ledgerCAMap.containsKey(certificate.getPublicKey())) {
                ledgerCAMap.put(certificate.getPublicKey(), cert);
            } else {
                throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] already exists in the ledger!");
            }
        }
        String[] certificatesUpdate = op.getCertificatesUpdate();
        for (String cert : certificatesUpdate) {
            X509Certificate certificate = CertificateUtils.parseCertificate(cert);
            CertificateUtils.checkCACertificate(certificate);
            CertificateUtils.checkValidity(certificate);
            if (ledgerCAMap.containsKey(certificate.getPublicKey())) {
                ledgerCAMap.put(certificate.getPublicKey(), cert);
            } else {
                throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] not exists in the ledger!");
            }
        }
        String[] certificatesRemove = op.getCertificatesRemove();
        for (String cert : certificatesRemove) {
            X509Certificate certificate = CertificateUtils.parseCertificate(cert);
            CertificateUtils.checkCACertificate(certificate);
            if (ledgerCAMap.containsKey(certificate.getPublicKey())) {
                ledgerCAMap.remove(certificate.getPublicKey(), cert);
            } else {
                throw new LedgerException("Certificate [" + CertificateUtils.toPEMString(certificate) + "] not exists in the ledger!");
            }
        }
        if (ledgerCAMap.size() == 0) {
            throw new LedgerException("At least one root certificate is required!");
        }
        ((LedgerAdminDataSetEditor) adminDataset).updateLedgerCA(ledgerCAMap.values().toArray(new String[0]));
    } else {
        throw new LedgerException("Not in CA identity mode!");
    }
}
Also used : LedgerAdminDataSet(com.jd.blockchain.ledger.core.LedgerAdminDataSet) HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) SecurityPolicy(com.jd.blockchain.ledger.SecurityPolicy) LedgerException(com.jd.blockchain.ledger.LedgerException) X509Certificate(java.security.cert.X509Certificate) LedgerAdminDataSetEditor(com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)

Example 15 with LedgerException

use of com.jd.blockchain.ledger.LedgerException in project jdchain-core by blockchain-jd-com.

the class TransactionSetEditor method commit.

@Override
public synchronized void commit() {
    if (ledgerDataStructure.equals(LedgerDataStructure.MERKLE_TREE)) {
        txSequence.commit();
        HashDigest txReqRootHash = txSequence.getRootHash();
        long v = txStateSet.setValue(TX_REQUEST_ROOT_HASH, txReqRootHash.toBytes(), txRequestBlockID);
        if (v < 0) {
            throw new LedgerException("Fail to save the root hash of transaction request!");
        }
        txStateSet.commit();
        txRequestBlockID = v;
    } else {
        origin_txIndex = txIndex;
        saveTotalByHeight();
        txStateSet.commit();
    }
}
Also used : HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerException(com.jd.blockchain.ledger.LedgerException)

Aggregations

LedgerException (com.jd.blockchain.ledger.LedgerException)17 Bytes (utils.Bytes)10 HashMap (java.util.HashMap)3 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)2 PubKey (com.jd.blockchain.crypto.PubKey)2 HashDigest (com.jd.blockchain.crypto.HashDigest)1 ContractExecuteException (com.jd.blockchain.ledger.ContractExecuteException)1 Event (com.jd.blockchain.ledger.Event)1 RolePrivileges (com.jd.blockchain.ledger.RolePrivileges)1 SecurityPolicy (com.jd.blockchain.ledger.SecurityPolicy)1 TypedValue (com.jd.blockchain.ledger.TypedValue)1 LedgerAdminDataSet (com.jd.blockchain.ledger.core.LedgerAdminDataSet)1 LedgerAdminDataSetEditor (com.jd.blockchain.ledger.core.LedgerAdminDataSetEditor)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1