public void addReference(JSApiReference reference) { this.currentContract.addReference(reference.extractReference(new JSApiAccessor())); }
/** * 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; }
source.createTransactionalSection(); source.addReference(reference);
newConditions.add("ref.id=="+referencedContract.getId().toBase64String()); newReference.setConditions(Binder.of("all_of",newConditions)); newContract.addReference(newReference); newContract.getIssuer().addRequiredReference(newReference.getName(), Role.RequiredMode.ALL_OF); newContract.seal(); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal(); transactionRoot.getTransactionPack().addReferencedItem(referencedContract);
@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()); }
revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); revokeReference.setConditions(Binder.of("all_of",revokeCondtitions)); revokingContract.addReference(revokeReference); SimpleRole role = new SimpleRole("@revoke"); revokingContract.registerRole(role); rootConditions.add("ref.id=="+referencedContract.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal(); transactionRoot.getTransactionPack().addReferencedItem(referencedContract);
contract2.addReference(reference); contract2.getIssuer().addRequiredReference(reference, Role.RequiredMode.ALL_OF);
revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); newContractRef.setConditions(Binder.of("all_of",revokeCondtitions)); newContract.addReference(newContractRef); newContract.getIssuer().addRequiredReference("ref1", Role.RequiredMode.ALL_OF); newContract.seal(); rootConditions.add("ref.id=="+referencedContract2.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.getIssuer().addRequiredReference("ref1", Role.RequiredMode.ALL_OF); transactionRoot.seal();
ref.type = Reference.TYPE_EXISTING_DEFINITION; ref.setConditions(conditions); contract1.addReference(ref);
revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); revokeReference.setConditions(Binder.of("all_of",revokeCondtitions)); revokingContract.addReference(revokeReference); SimpleRole role = new SimpleRole("@revoke"); revokingContract.registerRole(role); rootConditions.add("ref.id=="+referencedContract2.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.seal(); transactionRoot.getTransactionPack().addReferencedItem(referencedContract2);
revokeCondtitions.add("ref.id=="+referencedContract.getId().toBase64String()); revokeReference.setConditions(Binder.of("all_of",revokeCondtitions)); revokingContract.addReference(revokeReference); SimpleRole role = new SimpleRole("@revoke"); revokingContract.registerRole(role); rootConditions.add("ref.id=="+referencedContract2.getId().toBase64String()); rootReference.setConditions(Binder.of("all_of",rootConditions)); transactionRoot.addReference(rootReference); transactionRoot.getIssuer().addRequiredReference("ref1", Role.RequiredMode.ALL_OF); transactionRoot.seal();
@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 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 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 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); }
goodReference.type = Reference.TYPE_EXISTING_STATE; goodReference.setConditions(goodConditions); contract.addReference(goodReference);
@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()); }