/** * Get all addresses, used by this role. For public keys returns addresses too. * @deprecated the only usable check allowance method is isAllowedForKeys * @return list of strings with addresses */ @Deprecated public List<String> getAllAddresses() { List<String> res = new ArrayList<>(); getKeyAddresses().forEach(ka -> res.add(ka.toString())); getKeys().forEach(publicKey -> res.add(publicKey.getShortAddress().toString())); return res; }
private boolean hasAllKeys(SimpleRole role1, SimpleRole role2) { if(!role1.keyRecords.keySet().stream().allMatch(k-> role2.keyRecords.containsKey(k) || role2.keyAddresses.contains(k.getShortAddress()) || role2.keyAddresses.contains(k.getLongAddress()) || role2.anonymousIds.contains(new AnonymousId(k.createAnonymousId())))) return false; if(!role1.keyAddresses.stream().allMatch(ka-> role2.keyAddresses.contains(ka) || role2.keyRecords.keySet().stream().anyMatch(key->ka.equals(key.getShortAddress()) || ka.equals(key.getLongAddress())))) return false; if(!role1.anonymousIds.stream().allMatch(anonymousId -> role2.anonymousIds.contains(anonymousId) || role2.keyRecords.keySet().stream().anyMatch(key->new AnonymousId(key.createAnonymousId()).equals(anonymousId)))) return false; return true; }
@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; }
private static void doSign() throws IOException { String source = (String) options.valueOf("sign"); List<String> names = (List) options.valuesOf("output"); Contract c = Contract.fromPackedTransaction(Files.readAllBytes(Paths.get(source))); if(c != null) { keysMap().values().forEach( k -> c.addSignatureToSeal(k)); String name; if(names.size() > 0) { name = names.get(0); } else { String suffix = "_signedby_"+String.join("_",c.getSealedByKeys().stream().map(k->k.getShortAddress().toString()).collect(Collectors.toSet())); name = new FilenameTool(source).addSuffixToBase(suffix).toString(); } saveContract(c,name,true,false); } finish(); }
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; }
public void asd() throws Exception { PrivateKey key = new PrivateKey(Do.read("/Users/romanu/Downloads/ru/roman.uskov.privateKey.unikey")); Set<PrivateKey> issuers = new HashSet<>(); issuers.add(key); Set<PublicKey> owners = new HashSet<>(); owners.add(key.getPublicKey()); TestSpace testSpace = prepareTestSpace(); testSpace.nodes.forEach(n->n.config.setIsFreeRegistrationsAllowedFromYaml(true)); for(int i = 109; i < 110; i++) { Contract c = ContractsService.createTokenContract(issuers, owners, new BigDecimal("100000.9"), new BigDecimal("0.01")); c.setIssuerKeys(key.getPublicKey().getShortAddress()); c.setCreatorKeys(key.getPublicKey().getShortAddress()); c.setExpiresAt(ZonedDateTime.now().plusDays(10)); c.seal(); new FileOutputStream("/Users/romanu/Downloads/ru/token"+i+".unicon").write(c.getPackedTransaction()); assertEquals(testSpace.client.register(Contract.fromPackedTransaction(Do.read("/Users/romanu/Downloads/ru/token"+i+".unicon")).getPackedTransaction(),10000).state,ItemState.APPROVED); } }
@Test public void getAllAddresses() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); ListRole owner = new ListRole("owner"); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(0), TestKeys.publicKey(1)))); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(2).getLongAddress()))); owner.addRole(new SimpleRole("owner", Arrays.asList(TestKeys.publicKey(3).getShortAddress(), TestKeys.publicKey(4)))); contract.registerRole(owner); List<String> addresses = contract.getRole("owner").getAllAddresses(); System.out.println("owner: " + addresses); for (String addr : addresses) assertThat(addr, Matchers.anyOf( Matchers.equalTo(TestKeys.publicKey(0).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(1).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(2).getLongAddress().toString()), Matchers.equalTo(TestKeys.publicKey(3).getShortAddress().toString()), Matchers.equalTo(TestKeys.publicKey(4).getShortAddress().toString()) )); }
@Test public void testModifyDataPermission() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testModifyDataPermission');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"');"; js += "var modifyDataPermission = jsApi.getPermissionBuilder().createModifyDataPermission(simpleRole, " + "{some_field: [1, 2, 3]});"; js += "result = modifyDataPermission;"; contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); JSApiPermission res = (JSApiPermission) contract.execJS(js.getBytes()); ModifyDataPermission changeOwnerPermission = (ModifyDataPermission)res.extractPermission(new JSApiAccessor()); ModifyDataPermission sample = new ModifyDataPermission(new SimpleRole("test"), Binder.of("fields", Binder.of("some_field", Arrays.asList(1, 2, 3)))); Field field = Permission.class.getDeclaredField("name"); field.setAccessible(true); assertEquals(field.get(sample), field.get(changeOwnerPermission)); field = ModifyDataPermission.class.getDeclaredField("fields"); field.setAccessible(true); assertEquals(field.get(sample), field.get(changeOwnerPermission)); }
@Test public void testRevokePermission() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testRevokePermission');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"');"; js += "var revokePermission = jsApi.getPermissionBuilder().createRevokePermission(simpleRole);"; js += "result = revokePermission;"; contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); JSApiPermission res = (JSApiPermission) contract.execJS(js.getBytes()); RevokePermission revokePermission = (RevokePermission)res.extractPermission(new JSApiAccessor()); RevokePermission sample = new RevokePermission(new SimpleRole("test")); Field field = Permission.class.getDeclaredField("name"); field.setAccessible(true); assertEquals(field.get(sample), field.get(revokePermission)); }
@Test public void testChangeOwnerPermission() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testChangeOwnerPermission');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"');"; js += "var changeOwnerPermission = jsApi.getPermissionBuilder().createChangeOwnerPermission(simpleRole);"; js += "result = changeOwnerPermission;"; contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); JSApiPermission res = (JSApiPermission) contract.execJS(js.getBytes()); ChangeOwnerPermission changeOwnerPermission = (ChangeOwnerPermission)res.extractPermission(new JSApiAccessor()); ChangeOwnerPermission sample = new ChangeOwnerPermission(new SimpleRole("test")); Field field = Permission.class.getDeclaredField("name"); field.setAccessible(true); assertEquals(field.get(sample), field.get(changeOwnerPermission)); }
@Test public void testSplitJoinPermission() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); String p0 = TestKeys.publicKey(0).packToBase64String(); String p1 = TestKeys.publicKey(1).packToBase64String();
@Test public void testSimpleRole() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); KeyAddress k1 = TestKeys.publicKey(1).getShortAddress(); KeyAddress k2 = TestKeys.publicKey(2).getShortAddress(); KeyAddress k3 = TestKeys.publicKey(3).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testSimpleRole');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"', '"+k1.toString()+"', '"+k2.toString()+"');"; js += "result = simpleRole;"; JSApiScriptParameters scriptParameters = new JSApiScriptParameters(); contract.getDefinition().setJS(js.getBytes(), "client script.js", scriptParameters); contract.seal(); JSApiRole res = (JSApiRole)contract.execJS(js.getBytes()); assertTrue(res.isAllowedForKeys(TestKeys.publicKey(0), TestKeys.publicKey(1), TestKeys.publicKey(2))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(0))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(1))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(2))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(3))); }
@Test public void testSimpleRoleCheck() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); KeyAddress k1 = TestKeys.publicKey(1).getShortAddress(); KeyAddress k2 = TestKeys.publicKey(2).getShortAddress(); String p0 = TestKeys.publicKey(0).packToBase64String(); String p1 = TestKeys.publicKey(1).packToBase64String(); String p2 = TestKeys.publicKey(2).packToBase64String(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testSimpleRoleCheck');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"', '"+k1.toString()+"', '"+k2.toString()+"');"; js += "var check0 = simpleRole.isAllowedForKeys(jsApi.base64toPublicKey('"+p0+"'), jsApi.base64toPublicKey('"+p1+"'));"; js += "var check1 = simpleRole.isAllowedForKeys(jsApi.base64toPublicKey('"+p0+"'), jsApi.base64toPublicKey('"+p1+"'), jsApi.base64toPublicKey('"+p2+"'));"; js += "print('check0: ' + check0);"; js += "print('check1: ' + check1);"; js += "result = [check0, check1];"; JSApiScriptParameters scriptParameters = new JSApiScriptParameters(); contract.getDefinition().setJS(js.getBytes(), "client script.js", scriptParameters); contract.seal(); ScriptObjectMirror res = (ScriptObjectMirror)contract.execJS(js.getBytes()); assertFalse((boolean)res.get("0")); assertTrue((boolean)res.get("1")); }
@Test public void issuerTest() throws Exception { KeyAddress keyAddress1 = TestKeys.privateKey(0).getPublicKey().getShortAddress(); KeyAddress keyAddress2 = TestKeys.privateKey(1).getPublicKey().getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(2)); contract.setCreatorKeys(keyAddress1); contract.setIssuerKeys(keyAddress2); contract.addSignerKey(TestKeys.privateKey(0)); contract.seal(); assertFalse(contract.check()); contract.setIssuerKeys(keyAddress1); contract.getErrors().clear(); contract.seal(); assertTrue(contract.check()); }
@Test public void testListRole() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); KeyAddress k1 = TestKeys.publicKey(1).getShortAddress(); KeyAddress k2 = TestKeys.publicKey(2).getShortAddress(); KeyAddress k3 = TestKeys.publicKey(3).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('testListRole');"; js += "var simpleRole0 = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"');"; js += "var simpleRole1 = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k1.toString()+"');"; js += "var simpleRole2 = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k2.toString()+"');"; js += "var listRole = jsApi.getRoleBuilder().createListRole('listRole', 'all', simpleRole0, simpleRole1, simpleRole2);"; js += "result = listRole;"; JSApiScriptParameters scriptParameters = new JSApiScriptParameters(); contract.getDefinition().setJS(js.getBytes(), "client script.js", scriptParameters); contract.seal(); JSApiRole res = (JSApiRole)contract.execJS(js.getBytes()); assertTrue(res.isAllowedForKeys(TestKeys.publicKey(0), TestKeys.publicKey(1), TestKeys.publicKey(2))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(0))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(1))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(2))); assertFalse(res.isAllowedForKeys(TestKeys.publicKey(3))); }
@Test public void jsInContract() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); contract.setOwnerKeys(TestKeys.publicKey(1), TestKeys.publicKey(2), TestKeys.publicKey(3)); contract.setCreatorKeys(TestKeys.publicKey(4), TestKeys.publicKey(5).getLongAddress()); System.out.println("testKey[10].getShortAddress: " + TestKeys.publicKey(10).getShortAddress().toString()); System.out.println("testKey[11].getShortAddress: " + TestKeys.publicKey(11).getShortAddress().toString()); contract.getStateData().set("some_value", HashId.createRandom().toBase64String()); contract.getStateData().set("some_hash_id", HashId.createRandom()); String js = ""; js += "print('hello world');"; js += "var currentContract = jsApi.getCurrentContract();"; js += "print('currentContract.getId(): ' + currentContract.getId());"; js += "print('currentContract.getRevision(): ' + currentContract.getRevision());"; js += "print('currentContract.getCreatedAt(): ' + currentContract.getCreatedAt());"; js += "print('currentContract.getOrigin(): ' + currentContract.getOrigin());"; js += "print('currentContract.getParent(): ' + currentContract.getParent());"; js += "print('currentContract.getStateDataField(some_value): ' + currentContract.getStateDataField('some_value'));"; js += "print('currentContract.getStateDataField(some_hash_id): ' + currentContract.getStateDataField('some_hash_id'));"; js += "print('currentContract.getDefinitionDataField(scripts): ' + currentContract.getDefinitionDataField('scripts'));"; js += "print('currentContract.getIssuer(): ' + currentContract.getIssuer());"; js += "print('currentContract.getOwner(): ' + currentContract.getOwner());"; js += "print('currentContract.getCreator(): ' + currentContract.getCreator());"; js += "print('call currentContract.setOwner()...');"; js += "currentContract.setOwner(['ZastWpWNPMqvVJAMocsMUTJg45i8LoC5Msmr7Lt9EaJJRwV2xV', 'a1sxhjdtGhNeji8SWJNPkwV5m6dgWfrQBnhiAxbQwZT6Y5FsXD']);"; js += "print('currentContract.getOwner(): ' + currentContract.getOwner());"; contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); contract.execJS(js.getBytes()); }
@Test public void jsAddPermission() throws Exception { TestSpace testSpace = prepareTestSpace(TestKeys.privateKey(0)); testSpace.nodes.forEach(m -> m.config.setIsFreeRegistrationsAllowedFromYaml(true)); KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); Contract contract = new Contract(TestKeys.privateKey(0)); contract.getStateData().set("testval", 3); String js = ""; js += "print('addPermission');"; js += "var simpleRole = jsApi.getRoleBuilder().createSimpleRole('owner', '"+k0.toString()+"');"; js += "var changeNumberPermission = jsApi.getPermissionBuilder().createChangeNumberPermission(simpleRole, " + "{field_name: 'testval', min_value: 3, max_value: 80, min_step: 1, max_step: 3}" + ");"; js += "jsApi.getCurrentContract().addPermission(changeNumberPermission);"; contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.execJS(js.getBytes()); contract.seal(); ItemResult ir = testSpace.client.register(contract.getPackedTransaction(), 5000); assertEquals(ItemState.APPROVED, ir.state); Contract newRev = contract.createRevision(); newRev.addSignerKey(TestKeys.privateKey(0)); newRev.getStateData().set("testval", 5); newRev.seal(); ir = testSpace.client.register(newRev.getPackedTransaction(), 5000); assertEquals(ItemState.APPROVED, ir.state); testSpace.nodes.forEach(m -> m.shutdown()); }
@Test public void references() throws Exception { Contract contract = new Contract(TestKeys.privateKey(0)); String js = ""; js += "print('references');"; js += "var ref = jsApi.getReferenceBuilder().createReference('EXISTING_STATE');"; js += "ref.setConditions({'all_of':['ref.issuer=="+TestKeys.publicKey(1).getShortAddress().toString()+"']});"; js += "jsApi.getCurrentContract().addReference(ref);"; contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters()); contract.seal(); contract = Contract.fromPackedTransaction(contract.getPackedTransaction()); Contract batchContract = new Contract(TestKeys.privateKey(3)); batchContract.addNewItems(contract); batchContract.seal(); assertTrue(batchContract.check()); contract.execJS(new JSApiExecOptions(), js.getBytes()); contract.seal(); batchContract.seal(); assertFalse(batchContract.check()); }
@Test public void testListRoleCheckAll() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getShortAddress(); KeyAddress k1 = TestKeys.publicKey(1).getLongAddress(); KeyAddress k2 = TestKeys.publicKey(2).getShortAddress(); KeyAddress k3 = TestKeys.publicKey(3).getLongAddress(); String p0 = TestKeys.publicKey(0).packToBase64String();
@Test public void testListRoleCheckAny() throws Exception { KeyAddress k0 = TestKeys.publicKey(0).getLongAddress(); KeyAddress k1 = TestKeys.publicKey(1).getShortAddress(); KeyAddress k2 = TestKeys.publicKey(2).getLongAddress(); KeyAddress k3 = TestKeys.publicKey(3).getShortAddress(); String p0 = TestKeys.publicKey(0).packToBase64String(); String p1 = TestKeys.publicKey(1).packToBase64String();