public JSApiRoleLink(String newRoleName, String existingRoleName) { roleLink = new RoleLink(newRoleName, existingRoleName); }
/** * Create alias role to this one using {@link RoleLink}. If this role has attached to some contract, this method * also registers new role in the same contract. * * @param roleName new role name * * @return linked {@link RoleLink} */ public RoleLink linkAs(String roleName) { RoleLink newRole = new RoleLink(roleName, name); if (contract != null) contract.registerRole(newRole); return newRole; }
static Contract createSimpleContract(PrivateKey key) { Contract result = new Contract(); // default expiration date result.setExpiresAt(ZonedDateTime.now().plusDays(90)); // issuer role is a key for a new contract result.setIssuerKeys(key.getPublicKey().getShortAddress()); // issuer is owner, link roles result.registerRole(new RoleLink("owner", "issuer")); result.registerRole(new RoleLink("creator", "issuer")); return result; }
/** * Initializes combining role from dsl. * * @param serializedRole is {@link Binder} from dsl with data of combining role */ @Override public void initWithDsl(Binder serializedRole) { List<Object> roleBinders = serializedRole.getListOrThrow("roles"); mode = Mode.valueOf(serializedRole.getStringOrThrow("mode").toUpperCase()); if(mode == Mode.QUORUM) quorumSize = serializedRole.getIntOrThrow("quorumSize"); roleBinders.stream().forEach(x -> { if(x instanceof String) { roles.add(new RoleLink(x+"link"+ Instant.now().toEpochMilli(), (String) x)); } else { Binder binder = Binder.of(x); if (binder.size() == 1) { String name = binder.keySet().iterator().next(); roles.add(Role.fromDslBinder(name, binder.getBinderOrThrow(name))); } else { roles.add(Role.fromDslBinder(null, binder)); } } }); }
/** * Create a default empty new contract using a provided key as issuer and owner and sealer. Default expiration is * set to 90 days. * <p> * This constructor adds key as sealing signature so it is ready to {@link #seal()} just after construction, thought * it is necessary to put real data to it first. It is allowed to change owner, expiration and data fields after * creation (but before sealing). * <p> * Change owner permission is added by default * @param key is {@link PrivateKey} for creating roles "issuer", "owner", "creator" and sign contract */ public Contract(PrivateKey key) { this(); // default expiration date setExpiresAt(ZonedDateTime.now().plusDays(90)); // issuer role is a key for a new contract setIssuerKeys(key.getPublicKey()); // issuer is owner, link roles registerRole(new RoleLink("owner", "issuer")); registerRole(new RoleLink("creator", "issuer")); RoleLink roleLink = new RoleLink("@change_ower_role","owner"); roleLink.setContract(this); // owner can change permission addPermission(new ChangeOwnerPermission(roleLink)); // issuer should sign addSignerKey(key); }
/** * Create a batch contract, which registers all the included contracts, possibily referencing each other, * in the single transaction, saving time and reducing U cost. Note that if any of the batched contracts * fails, the whole batch is rejected. * * @param contracts to register all in one batch. Shuld be prepared and sealed. * @param keys to sign batch with. * @return batch contract that includes all contracts as new items. */ public static Contract createBatch(Collection<PrivateKey> keys, Contract... contracts) { Contract batch = new Contract(); batch.setIssuerKeys(keys); batch.registerRole(new RoleLink("creator","issuer")); batch.registerRole(new RoleLink("owner","issuer")); batch.setExpiresAt(ZonedDateTime.now().plusDays(3)); for(Contract c : contracts) { batch.addNewItems(c); } batch.addSignerKeys(keys); batch.seal(); return batch; }
/** * Creates a token contract with possible additional emission. * <br><br> * The service creates a simple token contract with issuer, creator and owner roles; * with change_owner permission for owner, revoke permissions for owner and issuer and split_join permission for owner. * Split_join permission has by default following params: "minValue" for min_value and min_unit, "amount" for field_name, * "state.origin" for join_match_fields. * Modify_data permission has by default following params: fields: "amount". * By default expires at time is set to 60 months from now. * * @param issuerKeys is issuer private keys. * @param ownerKeys is owner public keys. * @param amount is start token number. * @param minValue is minimum token value. * @return signed and sealed contract, ready for register. */ @Deprecated public synchronized static Contract createTokenContractWithEmission(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys, BigDecimal amount, BigDecimal minValue) { Contract tokenContract = createTokenContract(issuerKeys, ownerKeys, amount, minValue); RoleLink issuerLink = new RoleLink("issuer_link", "issuer"); tokenContract.registerRole(issuerLink); HashMap<String, Object> fieldsMap = new HashMap<>(); fieldsMap.put("amount", null); Binder modifyDataParams = Binder.of("fields", fieldsMap); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(issuerLink, modifyDataParams); tokenContract.addPermission(modifyDataPermission); tokenContract.seal(); tokenContract.addSignatureToSeal(issuerKeys); return tokenContract; }
result = new SimpleRole(name); } else if(type.equalsIgnoreCase("link")) { result = new RoleLink(name); } else if(type.equalsIgnoreCase("list")) { result = new ListRole(name);
/** * Resolve object describing role and create either: - new role object - symlink to named role instance, ensure it * is register and return it, if it is a Map, tries to construct and register {@link Role} then return it. * * @param roleName is name of the role * @param roleObject is object for role creating * * @return created {@link Role} */ @NonNull public Role createRole(String roleName, Object roleObject) { if (roleObject instanceof CharSequence) { return registerRole(new RoleLink(roleName, roleObject.toString())); } if (roleObject instanceof Role) if(((Role)roleObject).getName() != null && ((Role)roleObject).getName().equals(roleName)) return registerRole(((Role) roleObject)); else return registerRole(((Role) roleObject).linkAs(roleName)); if (roleObject instanceof Map) { Role r = Role.fromDslBinder(roleName, Binder.from(roleObject)); return registerRole(r); } throw new IllegalArgumentException("cant make role from " + roleObject); }
@Test public void equals() throws Exception { RoleLink r1 = new RoleLink("name", "target"); RoleLink r2 = new RoleLink("name", "target1"); RoleLink r3 = new RoleLink("name1", "target"); assertTrue(r1.equalsIgnoreName(r3)); assertFalse(r1.equalsIgnoreName(r2)); assertFalse(r2.equalsIgnoreName(r3)); assertNotEquals(r1, r3); assertNotEquals(r1, r2); assertNotEquals(r2, r3); }
consent.setExpiresAt(ZonedDateTime.now().plusDays(10)); consent.setIssuerKeys(Arrays.asList(consentKeyAddresses)); consent.registerRole(new RoleLink("creator","issuer")); consent.registerRole(new RoleLink("owner","issuer")); RoleLink ownerLink = new RoleLink("@owner_link","owner"); consent.registerRole(ownerLink); consent.addPermission(new RevokePermission(ownerLink));
@Test public void serialize() throws Exception { RoleLink r1 = new RoleLink("name", "target"); r1.addRequiredReference("ref", Role.RequiredMode.ALL_OF); Binder s = DefaultBiMapper.serialize(r1); RoleLink r2 = DefaultBiMapper.deserialize(s); assertEquals(r1, r2); assertEquals(r1.getName(), r2.getName()); }
private Contract createNetConfigContract(List<NodeInfo> netConfig,PrivateKey issuerKey) throws IOException { Contract contract = new Contract(); contract.setIssuerKeys(issuerKey.getPublicKey()); contract.registerRole(new RoleLink("creator", "issuer")); ListRole listRole = new ListRole("owner"); for(NodeInfo ni: netConfig) { SimpleRole role = new SimpleRole(ni.getName()); contract.registerRole(role); role.addKeyRecord(new KeyRecord(ni.getPublicKey())); listRole.addRole(role); } listRole.setQuorum(netConfig.size()-1); contract.registerRole(listRole); RoleLink ownerLink = new RoleLink("ownerlink","owner"); ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink); HashMap<String,Object> fieldsMap = new HashMap<>(); fieldsMap.put("net_config",null); Binder modifyDataParams = Binder.of("fields",fieldsMap); ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink,modifyDataParams); contract.addPermission(changeOwnerPermission); contract.addPermission(modifyDataPermission); contract.setExpiresAt(ZonedDateTime.now().plusYears(40)); contract.getStateData().set("net_config",netConfig); contract.addSignerKey(issuerKey); contract.seal(); return contract; }
@Test public void registerContractWithAnonymousId() throws Exception { TestSpace ts = prepareTestSpace(); PrivateKey newPrivateKey = new PrivateKey(Do.read("./src/test_contracts/keys/u_key.private.unikey")); byte[] myAnonId = newPrivateKey.createAnonymousId(); Contract contract = new Contract(); contract.setExpiresAt(ZonedDateTime.now().plusDays(90)); Role r = contract.setIssuerKeys(AnonymousId.fromBytes(myAnonId)); contract.registerRole(new RoleLink("owner", "issuer")); contract.registerRole(new RoleLink("creator", "issuer")); contract.addPermission(new ChangeOwnerPermission(r)); contract.addSignerKey(newPrivateKey); contract.seal(); assertTrue(contract.isOk()); System.out.println("contract.check(): " + contract.check()); contract.traceErrors(); ts.client.getSession().setPrivateKey(newPrivateKey); ts.client.restart(); ItemResult itemResult = ts.client.register(contract.getPackedTransaction(), 5000); assertEquals(ItemState.APPROVED, itemResult.state); ts.nodes.forEach(x -> x.shutdown()); }
@Test public void resolve() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); c.registerRole(s1); RoleLink r1 = new RoleLink("lover", "owner"); c.registerRole(r1); RoleLink r2 = r1.linkAs("mucker"); assertSame(s1, s1.resolve()); assertSame(s1, r1.resolve()); assertSame(s1, r2.resolve()); }
Role ownerLink = new RoleLink("@onwer_link","owner"); contract.registerRole(ownerLink); SplitJoinPermission splitJoinPermission = new SplitJoinPermission(ownerLink,params); contractToJoin.setOwnerKey(ownerKey2.getPublicKey()); contractToJoin.getDefinition().getData().set("currency","UTN"); ownerLink = new RoleLink("@onwer_link","owner"); contractToJoin.getStateData().set("amount","100.0"); contractToJoin.registerRole(ownerLink);
@Test public void goodRevokeAnother() throws Exception { Contract c1 = new Contract(ownerKey1); RoleLink rl = new RoleLink("@revoke", "owner"); rl.setContract(c1); c1.addPermission(new RevokePermission(rl)); c1.seal(); Contract c2 = new Contract(ownerKey2); c2.seal(); Assert.assertTrue(c2.check()); Contract c3 = c2.createRevision(ownerKey2); //to prevent "state is identical" c3.setOwnerKeys(ownerKey3); c3.addSignerKey(ownerKey1); c3.addRevokingItems(c1); c3.seal(); assertTrue(c3.check()); }
@Test public void badRevokeAnother() throws Exception { Contract c1 = new Contract(ownerKey1); RoleLink rl = new RoleLink("@revoke", "owner"); rl.setContract(c1); c1.addPermission(new RevokePermission(rl)); c1.seal(); Contract c2 = new Contract(ownerKey2); c2.seal(); Assert.assertTrue(c2.check()); Contract c3 = c2.createRevision(ownerKey2); //to prevent "state is identical" c3.setOwnerKeys(ownerKey3); c3.addRevokingItems(c1); c3.seal(); assertFalse(c3.check()); }
@Test public void revokeAnotherAfterSplit() throws Exception { Contract c1 = new Contract(ownerKey1); RoleLink rl = new RoleLink("@revoke", "owner"); rl.setContract(c1); c1.addPermission(new RevokePermission(rl));