/** * Clone the role with a different names, using the same (not copied) key records, in the new copy of the container. * So, it is safe to edit cloned keyRecords, while keys itself are not copied and are packed with Boss effeciently. * More or less ;) * * @param name is new name for the role * * @return cloned {@link SimpleRole} */ public SimpleRole cloneAs(String name) { SimpleRole r = new SimpleRole(name); keyRecords.values().forEach(kr -> r.addKeyRecord(kr)); anonymousIds.forEach(aid -> r.anonymousIds.add(aid)); keyAddresses.forEach(keyAddr -> r.keyAddresses.add(keyAddr)); return r; }
@Test public void serializeMany() throws Exception { SimpleRole sr = new SimpleRole("tr1"); keys.forEach(k-> sr.addKeyRecord(new KeyRecord(k.getPublicKey()))); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); Set<PublicKey> kk = ((SimpleRole)r1).getSimpleKeys(); keys.forEach(k->assertTrue(kk.contains(k.getPublicKey()))); }
@Test public void isAllowed() throws Exception { PublicKey key = keys.get(0).getPublicKey(); Set<PublicKey> keySet = new HashSet<>(); keySet.add(key); Contract contract = new Contract(); SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(key)); contract.registerRole(sr); assertTrue(!sr.isAllowedForKeys(new HashSet<>())); assertTrue(sr.isAllowedForKeys(keySet)); sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref2", Role.RequiredMode.ALL_OF); contract.getValidRoleReferences().add("ref1"); assertFalse(sr.isAllowedForKeys(keySet)); contract.getValidRoleReferences().add("ref2"); assertTrue(sr.isAllowedForKeys(keySet)); sr.addRequiredReference("ref3", Role.RequiredMode.ANY_OF); assertFalse(sr.isAllowedForKeys(keySet)); sr.addRequiredReference("ref4", Role.RequiredMode.ANY_OF); sr.addRequiredReference("ref5", Role.RequiredMode.ANY_OF); contract.getValidRoleReferences().add("ref4"); assertTrue(sr.isAllowedForKeys(keySet)); } }
@Override public boolean isU(Set<KeyAddress> issuerKeys, String issuerName) { Role issuer = getIssuer(); if(!(issuer instanceof SimpleRole)) return false; Set<KeyAddress> thisIssuerAddresses = new HashSet<>(((SimpleRole)issuer).getSimpleKeyAddresses()); for (PublicKey publicKey : ((SimpleRole)issuer).getSimpleKeys()) thisIssuerAddresses.add(publicKey.getShortAddress()); if (Collections.disjoint(issuerKeys, thisIssuerAddresses)) return false; if ( !issuerName.equals(getDefinition().getData().get("issuerName"))) return false; return true; }
@Test public void serializeAny() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref"); }
public JSApiSimpleRole(String name, String... addresses) throws KeyAddress.IllegalAddressException { List<KeyAddress> keyAddresses = new ArrayList<>(); for (int i = 0; i < addresses.length; ++i) { keyAddresses.add(new KeyAddress(addresses[i])); } simpleRole = new SimpleRole(name, keyAddresses); }
@Test public void serializeBoth() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref2", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertEquals(r1.getReferences(Role.RequiredMode.ALL_OF).size(),1); assertEquals(r1.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(r1.getReferences(Role.RequiredMode.ALL_OF).iterator().next(),"ref1"); assertEquals(r1.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref2"); }
@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()); }
@Test public void serializeOne() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.setComment("<New role comment>"); sr.addRequiredReference("ref1", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref2", Role.RequiredMode.ALL_OF); sr.addRequiredReference("ref3", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertEquals(r1.getComment(), "<New role comment>"); }
SimpleRole issuerRole = new SimpleRole("issuer"); for (PrivateKey k : fromKeys) { KeyRecord kr = new KeyRecord(k.getPublicKey()); issuerRole.addKeyRecord(kr); swapContract.registerRole((issuerRole).linkAs("owner")); swapContract.registerRole((issuerRole).linkAs("creator")); SimpleRole ownerFrom = new SimpleRole("owner"); SimpleRole creatorFrom = new SimpleRole("creator"); for (PrivateKey k : fromKeys) { KeyRecord kr = new KeyRecord(k.getPublicKey()); ownerFrom.addKeyRecord(kr); creatorFrom.addKeyRecord(kr); SimpleRole ownerTo = new SimpleRole("owner"); SimpleRole creatorTo = new SimpleRole("creator"); for (PublicKey k : toKeys) { KeyRecord kr = new KeyRecord(k); ownerTo.addKeyRecord(kr); creatorTo.addKeyRecord(kr);
@Override public void deserialize(Binder data, BiDeserializer deserializer) { super.deserialize(data, deserializer); // role can have keys - this should actually be refactored to let role // hold other roles and so on. List keyList = data.getList("keys", null); keyRecords.clear(); if (keyList != null) { keyList.forEach(kr -> { addKeyRecord(deserializer.deserialize(kr)); }); } List anonIdList = data.getList("anonIds", null); anonymousIds.clear(); if (anonIdList != null) { for (Object aid : anonIdList) { AnonymousId anonymousId = deserializer.deserialize(aid); anonymousIds.add(anonymousId); } } List keyAddrList = data.getList("addresses", null); keyAddresses.clear(); if (keyAddrList != null) { for (Object keyAddr : keyAddrList) { KeyAddress ka = deserializer.deserialize(keyAddr); keyAddresses.add(ka); } } }
@Test public void testAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); Set<PublicKey> pubKeys = new HashSet<>(); pubKeys.add(keys.get(0).getPublicKey()); assertTrue(sr.isAllowedForKeys(pubKeys)); assertTrue(r1.isAllowedForKeys(pubKeys)); assertEquals(sr, r1); }
@Test public void testCloneAsAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); SimpleRole r1 = sr.cloneAs("tr2"); SimpleRole r2 = r1.cloneAs("tr1"); assertEquals(sr, r2); } }
@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()); }
List<Binder> list = serializedRole.getListOrThrow("keys"); for(Object keyRecord : list) { addKeyRecord(new KeyRecord(Binder.of(keyRecord))); addKeyRecord(new KeyRecord(serializedRole)); } else { keysFound = false; initWithRecords(serializedRole.getListOrThrow("binders"));
/** * Check role is allowed to keys from other {@link SimpleRole} * * @param anotherRole is other {@link SimpleRole} with keys * @return true if role is allowed to keys from other {@link SimpleRole} */ public boolean isAllowedForKeys(SimpleRole anotherRole) { return isAllowedForKeys(anotherRole.keyRecords.keySet()); }
public static Set<PublicKey> extractKeys(Role role) { if(role instanceof SimpleRole) { return ((SimpleRole) role).getSimpleKeys(); } else if(role instanceof RoleLink) { return extractKeys(role.resolve()); } else if(role instanceof ListRole) { Set<PublicKey> result = new HashSet<>(); ((ListRole) role).getRoles().forEach(r -> result.addAll(extractKeys(r))); return result; } return null; }
/** * Get role as string. * * @return string with data of role */ @Override public String toString() { return "SimpleRole<" + System.identityHashCode(this) + ":" + getName() + ":anyOf:" + keyRecords.keySet() + "|" + anonymousIds + ":requiredAll:" + requiredAllReferences + ":requiredAny:" + requiredAnyReferences + ">"; }
/** * 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)); }
@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); }