Search in sources :

Example 66 with Binder

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;
}
Also used : Binder(net.sergeych.tools.Binder)

Example 67 with Binder

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);
    }
}
Also used : Binder(net.sergeych.tools.Binder) Sha512(com.icodici.crypto.digest.Sha512) Sha3_384(com.icodici.crypto.digest.Sha3_384)

Example 68 with Binder

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);
}
Also used : Binder(net.sergeych.tools.Binder) BiSerializer(net.sergeych.biserializer.BiSerializer)

Example 69 with Binder

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;
        }
    }
}
Also used : Binder(net.sergeych.tools.Binder) Delta(net.sergeych.diff.Delta) MapDelta(net.sergeych.diff.MapDelta) ChangedItem(net.sergeych.diff.ChangedItem) MapDelta(net.sergeych.diff.MapDelta)

Example 70 with Binder

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);
    }
}
Also used : Binder(net.sergeych.tools.Binder) Crc32(com.icodici.crypto.digest.Crc32)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3