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();
}
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);
}
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;
}
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;
}
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());
}
Aggregations