use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Role method serialize.
@Override
public Binder serialize(BiSerializer s) {
Binder b = Binder.fromKeysValues("name", name);
if (!requiredAnyReferences.isEmpty() || !requiredAllReferences.isEmpty()) {
Binder required = new Binder();
if (!requiredAllReferences.isEmpty()) {
required.set(ALL_OF.name(), s.serialize(requiredAllReferences));
}
if (!requiredAllReferences.isEmpty()) {
required.set(ANY_OF.name(), s.serialize(requiredAnyReferences));
}
b.set("required", required);
}
return b;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class ExtendedSignature method sign.
/**
* Sign the data with a given key.
*
* @param key is {@link PrivateKey} to sign with.
* @param data to be sign with key.
* @param savePublicKey if true key will stored in the {@link ExtendedSignature}.
*
* @return binary signature
*/
public static byte[] sign(PrivateKey key, byte[] data, boolean savePublicKey) {
try {
Binder targetSignatureBinder = Binder.fromKeysValues("key", keyId(key), "sha512", new Sha512().digest(data), "sha3_384", new Sha3_384().digest(data), "created_at", ZonedDateTime.now());
if (savePublicKey)
targetSignatureBinder.put("pub_key", key.getPublicKey().pack());
byte[] targetSignature = Boss.pack(targetSignatureBinder);
Binder result = Binder.fromKeysValues("exts", targetSignature, "sign", key.sign(targetSignature, HashType.SHA512), "sign2", key.sign(targetSignature, HashType.SHA3_384));
return Boss.pack(result);
} catch (EncryptionError e) {
throw new RuntimeException("signature failed", e);
}
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Reference method equals.
public boolean equals(Reference a) {
Binder dataThis = serialize(new BiSerializer());
Binder dataA = a.serialize(new BiSerializer());
return dataThis.equals(dataA);
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class ChangeNumberPermission method checkChanges.
/**
* Check and remove changes that this permission allow. Note that it does not add errors itself,
* to allow using several such permission, from which some may allow the change, and some may not. If a check
* will add error, though, it will prevent subsequent permission objects to allow the change.
* @param contract source (valid) contract
* @param changed is contract for checking
* @param stateChanges map of changes, see {@link Delta} for details
*/
@Override
public void checkChanges(Contract contract, Contract changed, Map<String, Delta> stateChanges) {
MapDelta<String, Binder, Binder> dataChanges = (MapDelta<String, Binder, Binder>) stateChanges.get("data");
if (dataChanges == null)
return;
Delta delta = dataChanges.getChange(fieldName);
if (delta != null) {
if (!(delta instanceof ChangedItem))
return;
try {
int valueDelta = (int) delta.newValue() - (int) delta.oldValue();
if (valueDelta < minStep || valueDelta > maxStep)
return;
else {
newValue = (int) delta.newValue();
if (newValue > maxValue || newValue < minValue)
return;
else {
dataChanges.remove(fieldName);
}
}
} catch (Exception e) {
return;
}
}
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class UDPAdapter method sendAsDataBlock.
protected synchronized void sendAsDataBlock(Block rawDataBlock, Session session) throws InterruptedException {
report(getLabel(), () -> concatReportMessage("send data to ", session.remoteNodeId), VerboseLevel.BASE);
report(getLabel(), () -> concatReportMessage("sessionKey is ", session.sessionKey.hashCode(), " for ", session.remoteNodeId));
byte[] crc32Local = new Crc32().digest(rawDataBlock.payload);
report(getLabel(), () -> concatReportMessage("sendAsDataBlock: Crc32 id is ", Arrays.equals(rawDataBlock.crc32, crc32Local)));
try {
byte[] encrypted = session.sessionKey.etaEncrypt(rawDataBlock.payload.clone());
Binder binder = Binder.fromKeysValues("data", encrypted, "crc32", rawDataBlock.crc32);
byte[] packedData = Boss.pack(binder);
report(getLabel(), () -> concatReportMessage(" data size: ", rawDataBlock.payload.length, " for ", session.remoteNodeId));
Block block = new Block(myNodeInfo.getNumber(), session.remoteNodeId, rawDataBlock.blockId, PacketTypes.DATA, session.address, session.port, packedData);
sendBlock(block, session);
} catch (EncryptionError encryptionError) {
callErrorCallbacks("[sendAsDataBlock] EncryptionError in node " + myNodeInfo.getNumber() + ": " + encryptionError.getMessage());
if (sessionsById.containsKey(session.remoteNodeId)) {
sessionsById.remove(session.remoteNodeId);
}
Session newSession = getOrCreateSession(session.remoteNodeId, session.address, session.port);
newSession.publicKey = session.publicKey;
newSession.remoteNodeId = session.remoteNodeId;
newSession.addBlockToWaitingQueue(rawDataBlock);
sendHello(newSession);
}
}
Aggregations