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