public Role setCreator(Role role) { return registerRole(role); }
/** * 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; }
/** * Set role with given name to given keys * @param name role name * @param keys keys to set role to * @return registened role */ @NonNull private Role setRole(String name, Collection keys) { return registerRole(new SimpleRole(name, keys)); }
public void registerRole(JSApiRole role) { this.currentContract.registerRole(role.extractRole(new JSApiAccessor())); }
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; }
/** * 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; }
public void deserealizeWith(Binder data, BiDeserializer d) { createdAt = data.getZonedDateTimeOrThrow("created_at"); expiresAt = data.getZonedDateTime("expires_at", null); revision = data.getIntOrThrow("revision"); this.references = d.deserialize(data.getList("references", null)); if (revision <= 0) throw new IllegalArgumentException("illegal revision number: " + revision); Role r = registerRole(d.deserialize(data.getBinderOrThrow("owner"))); if (!r.getName().equals("owner")) throw new IllegalArgumentException("bad owner role name"); r = registerRole(d.deserialize(data.getBinderOrThrow("created_by"))); if (!r.getName().equals("creator")) throw new IllegalArgumentException("bad creator role name"); this.data = data.getBinder("data", Binder.EMPTY); branchId = data.getString("branch_id", null); parent = d.deserialize(data.get("parent")); origin = d.deserialize(data.get("origin")); }
@Test public void detectsCirculars() throws Exception { Contract c = new Contract(); RoleLink r1 = new RoleLink("bar", "foo"); RoleLink r2 = new RoleLink("foo", "bar"); c.registerRole(r1); c.registerRole(r2); assertNull(r2.resolve()); }
public void deserializeWith(Binder data, BiDeserializer d) { registerRole(d.deserialize(data.getBinderOrThrow("issuer"))); createdAt = data.getZonedDateTimeOrThrow("created_at"); expiresAt = data.getZonedDateTime("expires_at", null); extendedType = data.getString("extended_type", null); this.data = d.deserialize(data.getBinder("data", Binder.EMPTY)); references = d.deserialize(data.getList("references", null)); Map<String, Permission> perms = d.deserialize(data.getOrThrow("permissions")); perms.forEach((id, perm) -> { perm.setId(id); addPermission(perm); }); }
@Test public void shouldPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertTrue(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@Test public void shouldNotPerformRoleWithAllMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAllMode", ListRole.Mode.ALL, Do.listOf(s1, s2)); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(1).getPublicKey())); assertFalse(roleList.isAllowedForKeys(keys)); }
@Test public void shouldNotPerformRoleWithQuorumMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listQuorumMode"); roleList.setQuorum(2); roleList.addAll(Do.listOf(s1, s2)); c.registerRole(roleList); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(1).getPublicKey())); assertFalse(roleList.isAllowedForKeys(keys)); }
@Test public void shouldPerformRoleWithQuorumMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); SimpleRole s3 = new SimpleRole("owner3"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); s3.addKeyRecord(new KeyRecord(keys.get(3).getPublicKey())); c.registerRole(s1); c.registerRole(s2); c.registerRole(s3); ListRole roleList = new ListRole("listQuorumMode", 2, Do.listOf(s1, s3, s2)); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(0).getPublicKey(), ListRoleTest.keys.get(2).getPublicKey())); assertTrue(roleList.isAllowedForKeys(keys)); }
@Test public void serializeAll() throws Exception { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); s1.addRequiredReference("refName", Role.RequiredMode.ALL_OF); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); ListRole roleList = new ListRole("listAllMode", ListRole.Mode.ALL, Do.listOf(s1)); c.registerRole(s1); c.registerRole(roleList); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
@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()); }
@Test public void shouldNotPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode"); roleList.setMode(ListRole.Mode.ANY); roleList.addRole(s1) .addRole(s2); assertFalse(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@Test public void shouldPerformRoleWithAllMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAllMode"); roleList.setMode(ListRole.Mode.ALL); roleList.addRole(s1) .addRole(s2); HashSet<AbstractKey> keys = new HashSet<>(Do.listOf( ListRoleTest.keys.get(0).getPublicKey(), ListRoleTest.keys.get(1).getPublicKey())); assertTrue(roleList.isAllowedForKeys(keys)); }
private Contract createNetConfigContract(Contract contract, List<NodeInfo> netConfig, Collection<PrivateKey> currentConfigKeys) throws IOException { contract = contract.createRevision(); 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); contract.getStateData().set("net_config",netConfig); List<KeyRecord> creatorKeys = new ArrayList<>(); for(PrivateKey key : currentConfigKeys) { creatorKeys.add(new KeyRecord(key.getPublicKey())); contract.addSignerKey(key); } contract.setCreator(creatorKeys); contract.seal(); return contract; }
@Test public void serializeContractWithListRole() throws Exception { Contract c = Contract.fromDslFile("./src/test_contracts/simple_root_contract.yml"); SimpleRole s1 = new SimpleRole("role"); ListRole listRole = new ListRole("owner", 1, Do.listOf(s1)); c.registerRole(listRole); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); assertEquals(c.getRole("owner"), c1.getRole("owner")); }
@Test(timeout = 90000) public void checkPayment_wrongIssuer() throws Exception { Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys()); PrivateKey manufactureFakePrivateKey = new PrivateKey(Do.read(ROOT_PATH + "keys/marty_mcfly.private.unikey")); SimpleRole issuerRole = new SimpleRole("issuer"); KeyRecord kr = new KeyRecord(manufactureFakePrivateKey.getPublicKey()); issuerRole.addKeyRecord(kr); payment.registerRole(issuerRole); boolean res = payment.paymentCheck(config.getUIssuerKeys()); payment.traceErrors(); assertFalse(res); }