Search in sources :

Example 1 with UnsContract

use of com.icodici.universa.contract.services.UnsContract in project universa by UniversaBlockchain.

the class Contract method fromSealedBinary.

public static Contract fromSealedBinary(byte[] sealedBinary, TransactionPack tp) throws IOException {
    Binder data = Boss.unpack(sealedBinary);
    if (!data.getStringOrThrow("type").equals("unicapsule"))
        throw new UnicapsuleExpectedException("wrong object type, unicapsule required");
    byte[] contractBytes = data.getBinaryOrThrow("data");
    // This must be explained. By default, Boss.load will apply contract transformation in place
    // as it is registered BiSerializable type, and we want to avoid it. Therefore, we decode boss
    // data without BiSerializer and then do it by hand calling deserialize:
    Binder payload = Boss.load(contractBytes, null);
    // contract can be extended type - we need know about it before
    String extendedType = payload.getBinder("contract").getBinder("definition").getString("extended_type", null);
    NSmartContract.SmartContractType scType = null;
    if (extendedType != null) {
        try {
            scType = NSmartContract.SmartContractType.valueOf(extendedType);
        } catch (IllegalArgumentException e) {
        }
    }
    Contract result;
    // and if extended type of contract is allowed - create extended contrac, otherwise create simple contract
    if (scType != null) {
        switch(scType) {
            case N_SMART_CONTRACT:
                result = new NSmartContract(data, tp);
                break;
            case SLOT1:
                result = new SlotContract(data, tp);
                break;
            case UNS1:
                result = new UnsContract(data, tp);
                break;
            case UNS2:
                result = new UnsContract(data, tp);
                break;
            case FOLLOWER1:
                result = new FollowerContract(data, tp);
                break;
            default:
                // unknwon extended type. create simple contract
                // TODO: should we throw?
                result = new Contract(data, tp);
        }
    } else {
        result = new Contract(data, tp);
    }
    result.sealedBinary = sealedBinary;
    return result;
}
Also used : Binder(net.sergeych.tools.Binder) NSmartContract(com.icodici.universa.contract.services.NSmartContract) FollowerContract(com.icodici.universa.contract.services.FollowerContract) UnsContract(com.icodici.universa.contract.services.UnsContract) NSmartContract(com.icodici.universa.contract.services.NSmartContract) UnsContract(com.icodici.universa.contract.services.UnsContract) FollowerContract(com.icodici.universa.contract.services.FollowerContract) SlotContract(com.icodici.universa.contract.services.SlotContract) SlotContract(com.icodici.universa.contract.services.SlotContract)

Aggregations

FollowerContract (com.icodici.universa.contract.services.FollowerContract)1 NSmartContract (com.icodici.universa.contract.services.NSmartContract)1 SlotContract (com.icodici.universa.contract.services.SlotContract)1 UnsContract (com.icodici.universa.contract.services.UnsContract)1 Binder (net.sergeych.tools.Binder)1