public JSApiReference createReference(String type) { Reference reference = new Reference(); switch (type) { case "TRANSACTIONAL": reference.type = Reference.TYPE_TRANSACTIONAL; break; case "EXISTING_DEFINITION": reference.type = Reference.TYPE_EXISTING_DEFINITION; break; case "EXISTING_STATE": reference.type = Reference.TYPE_EXISTING_STATE; break; default: throw new IllegalArgumentException("JSApiReferenceBuilder.createReference error: wrong reference type '"+type+"'"); } return new JSApiReference(reference); }
private void addOriginReference(HashId origin) { Reference ref = new Reference(this); ref.type = Reference.TYPE_EXISTING_STATE; ref.setName(origin.toString()); List<Object> conditionsList = new ArrayList<>(); conditionsList.add(REFERENCE_CONDITION_PREFIX+origin.toBase64String()); Binder conditions = Binder.of(Reference.conditionsModeType.all_of.name(),conditionsList); ref.setConditions(conditions); if(originContracts.containsKey(origin)) ref.addMatchingItem(originContracts.get(origin)); addReference(ref); }
public void addReferencedItems(Approvable... items) { Stream.of(items).forEach(i -> { Reference refModel = new Reference(); refModel.name = i.getId().toBase64String(); refModel.type = Reference.TYPE_EXISTING_DEFINITION; refModel.addMatchingItem(i); references.put(refModel.name, refModel); }); }
/** * Add to base {@link Contract} reference and referenced contract. * When the returned {@link Contract} is unpacking referenced contract verifies * the compliance with the conditions of reference in base contract. * <br><br> * Also reference to base contract may be added by {@link Contract#addReference(Reference)}. * <br><br> * * @param baseContract is base contract fro adding reference. * @param refContract is referenced contract (which must satisfy the conditions of the reference). * @param refName is name of reference. * @param refType is type of reference (section, may be {@link Reference#TYPE_TRANSACTIONAL}, {@link Reference#TYPE_EXISTING_DEFINITION}, or {@link Reference#TYPE_EXISTING_STATE}). * @param conditions is conditions of the reference. * @return ready {@link Contract} */ public synchronized static Contract addReferenceToContract( Contract baseContract, Contract refContract, String refName, int refType, Binder conditions) { Reference reference = new Reference(baseContract); reference.name = refName; reference.type = refType; reference.setConditions(conditions); reference.addMatchingItem(refContract); baseContract.addReference(reference); baseContract.seal(); return baseContract; }
static public Reference fromDslBinder(Binder ref, Contract contract) { String name = ref.getString("name"); String comment = ref.getString("comment", null); Binder where = null; try { where = ref.getBinderOrThrow("where"); } catch (Exception e) { // Insert simple condition to binder with key all_of List<String> simpleConditions = ref.getList("where", null); if (simpleConditions != null) where = new Binder(all_of.name(), simpleConditions); } Reference reference = new Reference(contract); if (name == null) throw new IllegalArgumentException("Expected reference name"); reference.setName(name); reference.setComment(comment); if (where != null) reference.setConditions(where); return reference; }
consent.seal(); Reference reference = new Reference(); reference.setName("consent_"+consent.getId()); reference.type = Reference.TYPE_TRANSACTIONAL;
@Test public void revokeDifferentNameValidRefRoleNotRegistered() throws Exception { Contract referencedContract = new Contract(key1); referencedContract.seal(); Contract revokingContract = new Contract(key2); Reference revokeReference = new Reference(revokingContract); revokeReference.setName("ref1"); ArrayList<String> revokeCondtitions = new ArrayList<>(); revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); revokeReference.setConditions(Binder.of("all_of",revokeCondtitions)); revokingContract.addReference(revokeReference); SimpleRole role = new SimpleRole("@revoke"); role.addRequiredReference(revokeReference.getName(), Role.RequiredMode.ALL_OF); RevokePermission permission = new RevokePermission(role); revokingContract.addPermission(permission); revokingContract.seal(); Contract transactionRoot = new Contract(key3); transactionRoot.addRevokingItems(revokingContract); Reference rootReference = new Reference(transactionRoot); rootReference.setName("ref2"); ArrayList<String> rootConditions = new ArrayList<>(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal(); transactionRoot.getTransactionPack().addReferencedItem(referencedContract); transactionRoot = Contract.fromPackedTransaction(transactionRoot.getPackedTransaction()); transactionRoot.check(); assertTrue(transactionRoot.isOk()); }
contract3.addNewItems(contract4); Reference reference = new Reference(); reference.name = "consent_"+contract4.getId(); reference.type = Reference.TYPE_EXISTING_STATE;
@Test public void checkTransactional() throws Exception { PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey")); Contract delorean = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml"); delorean.addSignerKey(manufacturePrivateKey); delorean.seal(); delorean.traceErrors(); Contract.Transactional transactional = delorean.createTransactionalSection(); Reference reference = new Reference(); // reference.setName("transactional_example"); transactional.addReference(reference); Contract deloreanTransactional = delorean.createRevision(transactional); deloreanTransactional.addSignerKey(manufacturePrivateKey); deloreanTransactional.seal(); deloreanTransactional.traceErrors(); }
conditions.set("all_of", listConditions); Reference ref = new Reference(contract1); ref.name = "ref_can_play"; ref.type = Reference.TYPE_EXISTING_DEFINITION;
@Test public void refLessOrEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val<10") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldDateTimeForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.definition.created_at", "this.definition.created_at!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldRoleForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.issuer", "this.issuer!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingField() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val>-100") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldHashIdForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val!=ref.id", "this.id!=ref.state.data.val") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void refMissingFieldConstantForEquals() throws Exception { Contract contractA = new Contract(new PrivateKey(2048)); contractA.getStateData().put("another_val", 100); Contract contractB = new Contract(new PrivateKey(2048)); Reference ref = new Reference(); ref.type = Reference.TYPE_EXISTING_STATE; ref.setConditions(Binder.of( Reference.conditionsModeType.all_of.name(), asList("ref.state.data.val==false", "ref.state.data.ival==0", "false==ref.state.data.val", "0==ref.state.data.ival") )); contractB.addReference(ref); Contract batch = new Contract(new PrivateKey(2048)); batch.addNewItems(contractA); batch.addNewItems(contractB); batch.seal(); Boolean res = batch.check(); batch.traceErrors(); assertEquals(false, res); }
@Test public void modifyReferencesDeclined() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(contract.getRole("owner"),new Binder()); modifyDataPermission.addField("references", null); contract.addPermission(modifyDataPermission); contract.seal(); Contract referencedContract = new Contract(TestKeys.privateKey(1)); referencedContract.seal(); Contract changed = contract.createRevision(); changed.addSignerKey(TestKeys.privateKey(0)); Reference ref = new Reference(referencedContract); ref.addMatchingItem(referencedContract); ref.type = Reference.TYPE_EXISTING_STATE; changed.addReference(ref); changed.seal(); changed.check(); assertTrue(!changed.isOk()); }
@Test public void modifyReferencesAllowed() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(contract.getRole("owner"),new Binder()); modifyDataPermission.addField("/references", null); contract.addPermission(modifyDataPermission); contract.seal(); Contract referencedContract = new Contract(TestKeys.privateKey(1)); referencedContract.seal(); Contract changed = contract.createRevision(); changed.addSignerKey(TestKeys.privateKey(0)); Reference ref = new Reference(referencedContract); ref.type = Reference.TYPE_EXISTING_STATE; ref.addMatchingItem(referencedContract); changed.addReference(ref); changed.seal(); changed.check(); changed.traceErrors(); assertTrue(changed.isOk()); }