Search in sources :

Example 1 with AsymmetricKeypair

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

the class LedgerEditorTest method testGennesisBlockCreation.

/**
 * 测试创建账本;
 */
@Test
public void testGennesisBlockCreation() {
    LedgerEditor ldgEdt = createLedgerInitEditor();
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair userKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    UserAccount userAccount = ldgDS.getUserAccountSet().register(userKP.getAddress(), userKP.getPubKey());
    userAccount.setProperty("Name", "孙悟空", -1);
    userAccount.setProperty("Age", "10000", -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    TransactionRequest genesisTxReq = genisisTxCtx.getTransactionRequest();
    assertEquals(genesisTxReq.getTransactionHash(), tx.getTransactionHash());
    assertEquals(0, tx.getBlockHeight());
    LedgerBlock block = ldgEdt.prepare();
    assertEquals(0, block.getHeight());
    assertNotNull(block.getHash());
    assertNull(block.getLedgerHash());
    assertNull(block.getPreviousHash());
    // 提交数据,写入存储;
    ldgEdt.commit();
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) UserAccount(com.jd.blockchain.ledger.core.UserAccount) TransactionRequest(com.jd.blockchain.ledger.TransactionRequest) Test(org.junit.Test)

Example 2 with AsymmetricKeypair

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

the class LedgerEditorTest method testWriteDataAccoutKvOp.

@SuppressWarnings("unused")
@Test
public void testWriteDataAccoutKvOp() {
    MemoryKVStorage storage = new MemoryKVStorage();
    LedgerEditor ldgEdt = createLedgerInitEditor(storage);
    LedgerTransactionContext genisisTxCtx = createGenisisTx(ldgEdt, participants);
    LedgerDataSetEditor ldgDS = (LedgerDataSetEditor) genisisTxCtx.getDataset();
    AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair();
    BlockchainKeypair dataKP = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    DataAccount dataAccount = ldgDS.getDataAccountSet().register(dataKP.getAddress(), dataKP.getPubKey(), null);
    dataAccount.getDataset().setValue("A", TypedValue.fromText("abc"), -1);
    TransactionResult tx = genisisTxCtx.commit(TransactionState.SUCCESS);
    LedgerBlock block = ldgEdt.prepare();
    // 提交数据,写入存储;
    ldgEdt.commit();
    // 预期这是第1个区块;
    assertNotNull(block);
    assertNotNull(block.getHash());
    assertEquals(0, block.getHeight());
    // 验证数据读写的一致性;
    BytesValue bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    String textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    // 验证重新加载的正确性;
    LedgerManager manager = new LedgerManager();
    HashDigest ledgerHash = block.getHash();
    LedgerRepository repo = manager.register(ledgerHash, storage, LedgerDataStructure.MERKLE_TREE);
    dataAccount = repo.getDataAccountSet().getAccount(dataKP.getAddress());
    assertNotNull(dataAccount);
    bytes = dataAccount.getDataset().getValue("A");
    assertEquals(DataType.TEXT, bytes.getType());
    textValue = bytes.getBytes().toUTF8String();
    assertEquals("abc", textValue);
    LedgerTransaction tx_init = repo.getTransactionSet().getTransaction(tx.getTransactionHash());
    assertNotNull(tx_init);
}
Also used : LedgerBlock(com.jd.blockchain.ledger.LedgerBlock) LedgerManager(com.jd.blockchain.ledger.core.LedgerManager) TransactionResult(com.jd.blockchain.ledger.TransactionResult) LedgerDataSetEditor(com.jd.blockchain.ledger.core.LedgerDataSetEditor) LedgerEditor(com.jd.blockchain.ledger.core.LedgerEditor) BlockchainKeypair(com.jd.blockchain.ledger.BlockchainKeypair) LedgerTransactionContext(com.jd.blockchain.ledger.core.LedgerTransactionContext) BytesValue(com.jd.blockchain.ledger.BytesValue) LedgerRepository(com.jd.blockchain.ledger.core.LedgerRepository) DataAccount(com.jd.blockchain.ledger.core.DataAccount) AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) LedgerTransaction(com.jd.blockchain.ledger.LedgerTransaction) MemoryKVStorage(com.jd.blockchain.storage.service.utils.MemoryKVStorage) Test(org.junit.Test)

Example 3 with AsymmetricKeypair

use of com.jd.blockchain.crypto.AsymmetricKeypair 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 AsymmetricKeypair

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

the class LedgerTransactionDataTest method initTxRequestMessage.

private TxRequestMessage initTxRequestMessage(CryptoAlgorithm hashAlgorithm) throws Exception {
    TransactionContent txContent = initTransactionContent();
    HashDigest txHash = TxBuilder.computeTxContentHash(hashAlgorithm, txContent);
    TxRequestMessage txRequestMessage = new TxRequestMessage(txHash, txContent);
    AsymmetricKeypair keypair2 = ClassicCryptoService.ED25519.generateKeypair();
    SignatureDigest digest1 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "zhangsan".getBytes());
    SignatureDigest digest2 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "lisi".getBytes());
    DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(keypair.getPubKey(), digest1);
    DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(keypair2.getPubKey(), digest2);
    txRequestMessage.addEndpointSignatures(endPoint1);
    txRequestMessage.addEndpointSignatures(endPoint2);
    AsymmetricKeypair keypair4 = ClassicCryptoService.ED25519.generateKeypair();
    SignatureDigest digest3 = ClassicCryptoService.ED25519.sign(keypair.getPrivKey(), "wangwu".getBytes());
    SignatureDigest digest4 = ClassicCryptoService.ED25519.sign(keypair4.getPrivKey(), "zhaoliu".getBytes());
    DigitalSignatureBlob node1 = new DigitalSignatureBlob(keypair.getPubKey(), digest3);
    DigitalSignatureBlob node2 = new DigitalSignatureBlob(keypair4.getPubKey(), digest4);
    txRequestMessage.addNodeSignatures(node1);
    txRequestMessage.addNodeSignatures(node2);
    return txRequestMessage;
}
Also used : AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) HashDigest(com.jd.blockchain.crypto.HashDigest) SignatureDigest(com.jd.blockchain.crypto.SignatureDigest) DigitalSignatureBlob(com.jd.blockchain.transaction.DigitalSignatureBlob) TxRequestMessage(com.jd.blockchain.transaction.TxRequestMessage) TransactionContent(com.jd.blockchain.ledger.TransactionContent)

Example 5 with AsymmetricKeypair

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

the class RustContractCode method registerUser.

private RegisterUserResult registerUser(ContractEventContext eventContext, RegisterUserRequest request) {
    CryptoAlgorithm algorithm = Crypto.getAlgorithm(request.getAlgorithm());
    SignatureFunction signFunc = Crypto.getSignatureFunction(algorithm);
    AsymmetricKeypair cryptoKeyPair = signFunc.generateKeypair(request.getSeed().getBytes());
    BlockchainKeypair keypair = new BlockchainKeypair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
    eventContext.getLedger().users().register(keypair.getIdentity());
    return new RegisterUserResult(keypair.getAddress().toBase58());
}
Also used : AsymmetricKeypair(com.jd.blockchain.crypto.AsymmetricKeypair) SignatureFunction(com.jd.blockchain.crypto.SignatureFunction) CryptoAlgorithm(com.jd.blockchain.crypto.CryptoAlgorithm)

Aggregations

AsymmetricKeypair (com.jd.blockchain.crypto.AsymmetricKeypair)8 CryptoAlgorithm (com.jd.blockchain.crypto.CryptoAlgorithm)2 HashDigest (com.jd.blockchain.crypto.HashDigest)2 SignatureFunction (com.jd.blockchain.crypto.SignatureFunction)2 BlockchainKeypair (com.jd.blockchain.ledger.BlockchainKeypair)2 LedgerBlock (com.jd.blockchain.ledger.LedgerBlock)2 TransactionResult (com.jd.blockchain.ledger.TransactionResult)2 LedgerDataSetEditor (com.jd.blockchain.ledger.core.LedgerDataSetEditor)2 LedgerEditor (com.jd.blockchain.ledger.core.LedgerEditor)2 LedgerTransactionContext (com.jd.blockchain.ledger.core.LedgerTransactionContext)2 File (java.io.File)2 Test (org.junit.Test)2 CryptoProvider (com.jd.blockchain.crypto.CryptoProvider)1 SignatureDigest (com.jd.blockchain.crypto.SignatureDigest)1 BytesValue (com.jd.blockchain.ledger.BytesValue)1 LedgerTransaction (com.jd.blockchain.ledger.LedgerTransaction)1 TransactionContent (com.jd.blockchain.ledger.TransactionContent)1 TransactionRequest (com.jd.blockchain.ledger.TransactionRequest)1 CryptoConfig (com.jd.blockchain.ledger.core.CryptoConfig)1 DataAccount (com.jd.blockchain.ledger.core.DataAccount)1