Search in sources :

Example 11 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class KvAccountSetEditor method register.

/**
 * 注册一个新账户; <br>
 *
 * 如果账户已经存在,则会引发 {@link LedgerException} 异常; <br>
 *
 * 如果指定的地址和公钥不匹配,则会引发 {@link LedgerException} 异常;
 *
 * @param address 区块链地址;
 * @param pubKey  公钥;
 * @return 注册成功的账户对象;
 */
public CompositeAccount register(BlockchainIdentity accountId) {
    if (isReadonly()) {
        throw new IllegalArgumentException("This AccountSet is readonly!");
    }
    Bytes address = accountId.getAddress();
    PubKey pubKey = accountId.getPubKey();
    verifyAddressEncoding(address, pubKey);
    InnerSimpleAccount cachedAcc = latestAccountsCache.get(address);
    if (cachedAcc != null) {
        if (cachedAcc.getVersion() < 0) {
            // 同一个新账户已经注册,但尚未提交,所以重复注册不会引起任何变化;
            return cachedAcc;
        }
        // 相同的账户已经存在;
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    long version = kvDataset.getVersion(address);
    if (version >= 0) {
        throw new LedgerException("The registering account already exist!", TransactionState.ACCOUNT_REGISTER_CONFLICT);
    }
    if (!accessPolicy.checkRegistering(address, pubKey)) {
        throw new LedgerException("Account Registering was rejected for the access policy!");
    }
    long accountIndex = kvDataset.getDataCount() + account_index_in_block;
    Bytes indexBytes = Bytes.fromString(String.valueOf(accountIndex));
    Bytes prefix = keyPrefix.concat(indexBytes);
    InnerSimpleAccount acc = createInstance(accountId, cryptoSetting, prefix);
    latestAccountsCache.put(address, acc);
    // 该设置用来维护注册用户的顺序
    // 写入数据库的前缀为L:/*S/SQ/accountindex
    byte[] pubKeyBytes = BinaryProtocol.encode(TypedValue.fromPubKey(accountId.getPubKey()), BytesValue.class);
    long nv = kvDataset.setValue(ACCOUNTSET_SEQUENCE_KEY_PREFIX.concat(indexBytes), pubKeyBytes, -1);
    // 写入数据库的前缀为L:/accountset/I/accountaddr, index, -1
    nv = kvDataset.setValue(Bytes.fromString("I/").concat(address), BytesUtils.toBytes(accountIndex), -1);
    if (nv < 0) {
        throw new LedgerException("Account Registering sequence already exist!");
    }
    updated = true;
    account_index_in_block++;
    return acc;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) Bytes(utils.Bytes) LedgerException(com.jd.blockchain.ledger.LedgerException)

Example 12 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class LedgerMetaDataTest method testSerialize_ParticipantCert.

@Test
public void testSerialize_ParticipantCert() {
    // LedgerCodes.METADATA_PARTICIPANT_CERT
    // prepare work
    int id = 1;
    // String address = "xxxxxxxxxxxxxx";
    PubKey pubKey = Crypto.getSignatureFunction(ClassicAlgorithm.ED25519).generateKeypair().getPubKey();
    // ParticipantInfo info = new ParticipantCertData.ParticipantInfoData(1, "yyy");
    // SignatureDigest signature = new SignatureDigest(CryptoAlgorithm.SM2,
    // rawDigestBytes);
    String name = "John";
    // NetworkAddress consensusAddress = new NetworkAddress("192.168.1.1", 9001,
    // false);
    Bytes address = AddressEncoding.generateAddress(pubKey);
    ParticipantCertData participantCertData = new ParticipantCertData(address, name, pubKey, ParticipantNodeState.CONSENSUS);
    // encode and decode
    byte[] encodeBytes = BinaryProtocol.encode(participantCertData, ParticipantNode.class);
    ParticipantNode deParticipantInfoData = BinaryProtocol.decode(encodeBytes);
    // verify start
    assertEquals(participantCertData.getAddress(), deParticipantInfoData.getAddress());
    assertEquals(participantCertData.getPubKey(), deParticipantInfoData.getPubKey());
    assertEquals(participantCertData.getName(), deParticipantInfoData.getName());
    return;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) Bytes(utils.Bytes) ParticipantNode(com.jd.blockchain.ledger.ParticipantNode) ParticipantCertData(com.jd.blockchain.ledger.core.ParticipantCertData) Test(org.junit.Test)

Example 13 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method getNodesSignatures.

private DigitalSignature[] getNodesSignatures() {
    ParticipantNode[] parties = this.ledgerInitConfig.getParticipants();
    List<DigitalSignature> signatures = new ArrayList<>();
    for (int i = 0; i < parties.length; i++) {
        if (parties[i].getParticipantNodeState() != ParticipantNodeState.CONSENSUS) {
            continue;
        }
        PubKey pubKey = parties[i].getPubKey();
        SignatureDigest signDigest = this.permissions[i].getTransactionSignature();
        signatures.add(new DigitalSignatureBlob(pubKey, signDigest));
    }
    return signatures.toArray(new DigitalSignature[signatures.size()]);
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) ParticipantNode(com.jd.blockchain.ledger.ParticipantNode) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) DigitalSignatureBlob(com.jd.blockchain.transaction.DigitalSignatureBlob) ArrayList(java.util.ArrayList) DigitalSignature(com.jd.blockchain.ledger.DigitalSignature)

Example 14 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method requestPermission.

@RequestMapping(path = "/legerinit/permission/{requesterId}", method = RequestMethod.POST, produces = LedgerInitMessageConverter.CONTENT_TYPE_VALUE, consumes = LedgerInitMessageConverter.CONTENT_TYPE_VALUE)
@Override
public LedgerInitProposal requestPermission(@PathVariable(name = "requesterId") int requesterId, @RequestBody SignatureDigest signature) {
    if (requesterId == currentId) {
        throw new LedgerInitException("There is a id conflict!");
    }
    int retry = 0;
    while (currentId == -1 || ledgerInitConfig == null || localPermission == null) {
        // 本地尚未完成初始化;
        if (retry < 30) {
            try {
                Thread.sleep(800);
            } catch (InterruptedException e) {
            // ignore interrupted exception;
            }
        } else {
            return null;
        }
        retry++;
    }
    ParticipantNode[] participants = ledgerInitConfig.getParticipants();
    if (requesterId < 0 || requesterId >= participants.length) {
        throw new LedgerInitException("The id of requester is out of the bound of participant list!");
    }
    byte[] requestCodeBytes = BytesUtils.concat(BytesUtils.toBytes(requesterId), ledgerInitConfig.getLedgerSettings().getLedgerSeed());
    PubKey requesterPubKey = participants[requesterId].getPubKey();
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(requesterPubKey.getAlgorithm());
    if (!signatureFunction.verify(signature, requesterPubKey, requestCodeBytes)) {
        throw new LedgerInitException("The requester signature is invalid!");
    }
    return localPermission;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) ParticipantNode(com.jd.blockchain.ledger.ParticipantNode) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with PubKey

use of com.jd.blockchain.crypto.PubKey in project jdchain-core by blockchain-jd-com.

the class LedgerInitializeWebController method prepareLocalPermission.

public LedgerInitProposal prepareLocalPermission(int currentId, PrivKey privKey, LedgerInitConfiguration ledgerInitConfig) {
    // 创建初始化配置;
    ParticipantProperties[] participants = ledgerInitConfig.getParticipants();
    if (currentId < 0 || currentId >= participants.length) {
        throw new LedgerInitException("Your id is out of bound of participant list!");
    }
    this.currentId = currentId;
    // 校验当前的公钥、私钥是否匹配;
    byte[] testBytes = BytesUtils.toBytes(currentId);
    SignatureFunction signatureFunction = Crypto.getSignatureFunction(privKey.getAlgorithm());
    SignatureDigest testSign = signatureFunction.sign(privKey, testBytes);
    PubKey myPubKey = participants[currentId].getPubKey();
    if (!signatureFunction.verify(testSign, myPubKey, testBytes)) {
        throw new LedgerInitException("Your pub-key specified in the init-settings isn't match your priv-key!");
    }
    this.initializerAddresses = new NetworkAddress[participants.length];
    // 记录每个参与方的账本初始化服务地址;
    for (int i = 0; i < participants.length; i++) {
        initializerAddresses[i] = participants[i].getInitializerAddress();
    }
    // 初始化账本;
    this.initializer = LedgerInitializer.create(ledgerInitConfig.getLedgerSettings(), ledgerInitConfig.getSecuritySettings());
    // 对初始交易签名,生成当前参与者的账本初始化许可;
    SignatureDigest permissionSign = initializer.signTransaction(privKey);
    LedgerInitProposalData permission = new LedgerInitProposalData(currentId, permissionSign);
    this.currentId = currentId;
    this.permissions = new LedgerInitProposal[participants.length];
    this.permissions[currentId] = permission;
    this.localPermission = permission;
    return permission;
}
Also used : PubKey(com.jd.blockchain.crypto.PubKey) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) LedgerInitProposalData(com.jd.blockchain.ledger.core.LedgerInitProposalData) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) ParticipantProperties(com.jd.blockchain.ledger.LedgerInitProperties.ParticipantProperties) LedgerInitException(com.jd.blockchain.ledger.LedgerInitException)

Aggregations

PubKey (com.jd.blockchain.crypto.PubKey)21 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)6 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)5 Bytes (utils.Bytes)5 PrivKey (com.jd.blockchain.crypto.PrivKey)4 ParticipantNode (com.jd.blockchain.ledger.ParticipantNode)4 NetworkAddress (utils.net.NetworkAddress)4 NodeSettings (com.jd.blockchain.consensus.NodeSettings)3 LedgerInitException (com.jd.blockchain.ledger.LedgerInitException)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 MsgQueueConsensusConfig (com.jd.blockchain.consensus.mq.config.MsgQueueConsensusConfig)2 MsgQueueNodeConfig (com.jd.blockchain.consensus.mq.config.MsgQueueNodeConfig)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerException (com.jd.blockchain.ledger.LedgerException)2 NetworkReplica (com.jd.blockchain.consensus.NetworkReplica)1 BftsmartClientAuthCredit (com.jd.blockchain.consensus.bftsmart.BftsmartClientAuthCredit)1 BftsmartConsensusConfig (com.jd.blockchain.consensus.bftsmart.BftsmartConsensusConfig)1 BftsmartNodeConfig (com.jd.blockchain.consensus.bftsmart.BftsmartNodeConfig)1 BftsmartNodeSettings (com.jd.blockchain.consensus.bftsmart.BftsmartNodeSettings)1