Tabnine Logo
Contract.execJS
Code IndexAdd Tabnine to your IDE (free)

How to use
execJS
method
in
com.icodici.universa.contract.Contract

Best Java code snippets using com.icodici.universa.contract.Contract.execJS (Showing top 20 results out of 315)

origin: UniversaBlockchain/universa

public Object execJS(byte[] jsFileContent, String... params) throws Exception {
  return execJS(new JSApiExecOptions(), jsFileContent, params);
}
origin: UniversaBlockchain/universa

@Test
public void testSharedFolders_write() throws Exception {
  String f1content = "f1 content";
  String f2content = "f2 content";
  String f3content = "f3 content";
  String f4content = "f4 content";
  List<String> sharedFolders = prepareSharedFoldersForTest(f1content, f2content, f3content);
  Paths.get(sharedFolders.get(0) + "/folder2/f4.txt").toFile().delete();
  Paths.get(sharedFolders.get(0) + "/folder2/f4.txt").toFile().getParentFile().delete();
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('testSharedFolders_write');";
  js += "var file4Content = '"+f4content+"';";
  js += "jsApi.getSharedFolders().writeNewFile('folder2/f4.txt', jsApi.string2bin(file4Content));";
  JSApiExecOptions execOptions = new JSApiExecOptions();
  execOptions.sharedFolders.addAll(sharedFolders);
  contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  contract.execJS(execOptions, js.getBytes());
  String f4readed = new String(Files.readAllBytes(Paths.get(sharedFolders.get(0) + "/folder2/f4.txt")));
  System.out.println("f4: " + f4readed);
  assertEquals(f4content, f4readed);
}
origin: UniversaBlockchain/universa

@Test
public void twoJsInContract() throws Exception {
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js1d = "var result = 'return_from_script_1d';";
  String js2d = "var result = 'return_from_script_2d';";
  String js1s = "var result = 'return_from_script_1s';";
  String js2s = "var result = 'return_from_script_2s';";
  contract.getDefinition().setJS(js1d.getBytes(), "js1d.js", new JSApiScriptParameters());
  contract.getDefinition().setJS(js2d.getBytes(), "js2d.js", new JSApiScriptParameters());
  contract.getState().setJS(js1s.getBytes(), "js1s.js", new JSApiScriptParameters());
  contract.getState().setJS(js2s.getBytes(), "js2s.js", new JSApiScriptParameters());
  contract.seal();
  assertEquals("return_from_script_1d", contract.execJS(js1d.getBytes()));
  assertEquals("return_from_script_2d", contract.execJS(js2d.getBytes()));
  assertEquals("return_from_script_1s", contract.execJS(js1s.getBytes()));
  assertEquals("return_from_script_2s", contract.execJS(js2s.getBytes()));
  try {
    contract.execJS("print('another script');".getBytes());
    assert false;
  } catch (IllegalArgumentException e) {
    System.out.println(e);
    assert true;
  }
}
origin: UniversaBlockchain/universa

@Test
public void testSharedFolders_rewrite() throws Exception {
  String f1content = "f1 content";
  String f1contentUpdated = "f1 content updated";
  String f2content = "f2 content";
  String f2contentUpdated = "f2 content updated";
  String f3content = "f3 content";
  List<String> sharedFolders = prepareSharedFoldersForTest(f1content, f2content, f3content);
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('testSharedFolders_rewrite');";
  js += "jsApi.getSharedFolders().rewriteExistingFile('f1.txt', jsApi.string2bin('"+f1contentUpdated+"'));";
  js += "jsApi.getSharedFolders().rewriteExistingFile('folder/f2.txt', jsApi.string2bin('"+f2contentUpdated+"'));";
  JSApiExecOptions execOptions = new JSApiExecOptions();
  execOptions.sharedFolders.addAll(sharedFolders);
  contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  contract.execJS(execOptions, js.getBytes());
  String f1readed = new String(Files.readAllBytes(Paths.get(sharedFolders.get(0) + "/f1.txt")));
  String f2readed = new String(Files.readAllBytes(Paths.get(sharedFolders.get(1) + "/folder/f2.txt")));
  assertNotEquals(f1content, f1readed);
  assertNotEquals(f2content, f2readed);
  assertEquals(f1contentUpdated, f1readed);
  assertEquals(f2contentUpdated, f2readed);
}
origin: UniversaBlockchain/universa

@Test
public void jsInContract_execZeroParams() throws Exception {
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('jsApiParams.length: ' + jsApiParams.length);";
  js += "result = jsApiParams.length;";
  contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  assertEquals(0, contract.execJS(js.getBytes()));
}
origin: UniversaBlockchain/universa

@Test
public void testSharedFolders_restrictedPath() throws Exception {
  String f1content = "f1 content";
  String f2content = "f2 content";
  String f3content = "f3 content";
  List<String> sharedFolders = prepareSharedFoldersForTest(f1content, f2content, f3content);
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('testSharedFolders_restrictedPath');";
  js += "function bin2string(array){var result = '';for(var i = 0; i < array.length; ++i){result+=(String.fromCharCode(array[i]));}return result;}";
  js += "try {";
  js += "  var file3Content = jsApi.getSharedFolders().readAllBytes('../f3.txt');";
  js += "  print('file3Content: ' + bin2string(file3Content));";
  js += "} catch (err) {";
  js += "  result = err;";
  js += "}";
  JSApiExecOptions execOptions = new JSApiExecOptions();
  execOptions.sharedFolders.addAll(sharedFolders);
  contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  IOException res = (IOException) contract.execJS(execOptions, js.getBytes());
  System.out.println("IOException from js: " + res);
  assertTrue(res.toString().contains("file '../f3.txt' not found in shared folders"));
}
origin: UniversaBlockchain/universa

@Test
public void testHttpClientFromJS() throws Exception {
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('testHttpClientFromJS');";
  js += "var httpClient = jsApi.getHttpClient();";
  js += "var res0 = httpClient.sendGetRequest('https://httpbin.org/get?param=333', 'json');";
  js += "var res1 = httpClient.sendPostRequest('http://httpbin.org/post', 'json', {postparam:44}, 'form');";
  js += "var res2 = httpClient.sendPostRequest('http://httpbin.org/post', 'json', {jsonparam:55}, 'json');";
  js += "var result = [res0, res1, res2];";
  JSApiScriptParameters scriptParameters = new JSApiScriptParameters();
  scriptParameters.domainMasks.add("httpbin.org");
  scriptParameters.setPermission(JSApiScriptParameters.ScriptPermissions.PERM_HTTP_CLIENT, true);
  contract.getState().setJS(js.getBytes(), "client script.js", scriptParameters);
  contract.seal();
  contract = Contract.fromPackedTransaction(contract.getPackedTransaction());
  ScriptObjectMirror res = (ScriptObjectMirror)contract.execJS(new JSApiExecOptions(), js.getBytes());
  List res0 = (List)res.get("0");
  assertEquals(200, res0.get(0));
  assertEquals("333", ((Map)((Map)res0.get(1)).get("args")).get("param"));
  List res1 = (List)res.get("1");
  assertEquals(200, res1.get(0));
  assertEquals("44", ((Map)((Map)res1.get(1)).get("form")).get("postparam"));
  List res2 = (List)res.get("2");
  assertEquals(200, res2.get(0));
  assertEquals(55l, ((Map)((Map)res2.get(1)).get("json")).get("jsonparam"));
}
origin: UniversaBlockchain/universa

@Test
public void jsApiTimeLimit() throws Exception {
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "function hardWork(ms) {";
  js += "  var unixtime_ms = new Date().getTime();";
  js += "  while(new Date().getTime() < unixtime_ms + ms) {}";
  js += "}";
  js += "print('jsApiTimeLimit');";
  js += "print('start hardWork...');";
  js += "hardWork(10000);";
  js += "print('hardWork time is up');";
  JSApiScriptParameters scriptParameters = new JSApiScriptParameters();
  scriptParameters.timeLimitMillis= 500;
  contract.getDefinition().setJS(js.getBytes(), "client script.js", scriptParameters);
  contract.seal();
  try {
    contract.execJS(js.getBytes());
    assert false;
  } catch (InterruptedException e) {
    System.out.println("InterruptedException: " + e);
    assert true;
  }
}
origin: UniversaBlockchain/universa

@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));
}
origin: UniversaBlockchain/universa

@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));
}
origin: UniversaBlockchain/universa

@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));
}
origin: UniversaBlockchain/universa

@Test
public void jsInContract_execParams() throws Exception {
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('jsApiParams.length: ' + jsApiParams.length);";
  js += "result = [jsApiParams.length, jsApiParams[0], jsApiParams[1]];";
  contract.getDefinition().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  ScriptObjectMirror res = (ScriptObjectMirror) contract.execJS(js.getBytes(), "prm1", "prm2");
  assertEquals(2, res.get("0"));
  assertEquals("prm1", res.get("1"));
  assertEquals("prm2", res.get("2"));
}
origin: UniversaBlockchain/universa

@Test
public void testSharedFolders_read() throws Exception {
  String f1content = "f1 content";
  String f2content = "f2 content";
  String f3content = "f3 content";
  List<String> sharedFolders = prepareSharedFoldersForTest(f1content, f2content, f3content);
  Contract contract = new Contract(TestKeys.privateKey(0));
  String js = "";
  js += "print('testSharedFolders_read');";
  js += "function bin2string(array){var result = '';for(var i = 0; i < array.length; ++i){result+=(String.fromCharCode(array[i]));}return result;}";
  js += "var file1Content = jsApi.getSharedFolders().readAllBytes('f1.txt');";
  js += "var file2Content = jsApi.getSharedFolders().readAllBytes('folder/f2.txt');";
  js += "print('file1Content: ' + bin2string(file1Content));";
  js += "print('file2Content: ' + bin2string(file2Content));";
  js += "result = [bin2string(file1Content), bin2string(file2Content)];";
  JSApiExecOptions execOptions = new JSApiExecOptions();
  execOptions.sharedFolders.addAll(sharedFolders);
  contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  ScriptObjectMirror res = (ScriptObjectMirror)contract.execJS(execOptions, js.getBytes());
  assertEquals(f1content, res.get("0"));
  assertEquals(f2content, res.get("1"));
}
origin: UniversaBlockchain/universa

@Test
public void testTransactionalAccess() throws Exception {
  String t1value = "t1value";
  String t2value = "t2value";
  Contract contract = new Contract(TestKeys.privateKey(0));
  contract.getTransactionalData().set("t1", t1value);
  String js = "";
  js += "print('testTransactionalAccess');";
  js += "var t1 = jsApi.getCurrentContract().getTransactionalDataField('t1');";
  js += "print('t1: ' + t1);";
  js += "jsApi.getCurrentContract().setTransactionalDataField('t2', '"+t2value+"');";
  js += "result = t1;";
  contract.getState().setJS(js.getBytes(), "client script.js", new JSApiScriptParameters());
  contract.seal();
  String res = (String)contract.execJS(js.getBytes());
  assertEquals(t1value, res);
  assertEquals(t2value, contract.getTransactionalData().getStringOrThrow("t2"));
  System.out.println("t2: " + contract.getTransactionalData().getStringOrThrow("t2"));
}
origin: UniversaBlockchain/universa

@Test
public void rawJavaScript() throws Exception {
  String fileName = "somescript.js";
  String scriptDump = "cHJpbnQoJ2hlbGxvIHdvcmxkJyk7DQp2YXIgY3VycmVudENvbnRyYWN0ID0ganNBcGkuZ2V0Q3VycmVudENvbnRyYWN0KCk7DQpwcmludCgnY3VycmVudENvbnRyYWN0LmdldElkKCk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0SWQoKSk7DQpwcmludCgnY3VycmVudENvbnRyYWN0LmdldFJldmlzaW9uKCk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0UmV2aXNpb24oKSk7DQpwcmludCgnY3VycmVudENvbnRyYWN0LmdldENyZWF0ZWRBdCgpOiAnICsgY3VycmVudENvbnRyYWN0LmdldENyZWF0ZWRBdCgpKTsNCnByaW50KCdjdXJyZW50Q29udHJhY3QuZ2V0T3JpZ2luKCk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0T3JpZ2luKCkpOw0KcHJpbnQoJ2N1cnJlbnRDb250cmFjdC5nZXRQYXJlbnQoKTogJyArIGN1cnJlbnRDb250cmFjdC5nZXRQYXJlbnQoKSk7DQpwcmludCgnY3VycmVudENvbnRyYWN0LmdldFN0YXRlRGF0YUZpZWxkKHNvbWVfdmFsdWUpOiAnICsgY3VycmVudENvbnRyYWN0LmdldFN0YXRlRGF0YUZpZWxkKCdzb21lX3ZhbHVlJykpOw0KcHJpbnQoJ2N1cnJlbnRDb250cmFjdC5nZXRTdGF0ZURhdGFGaWVsZChzb21lX2hhc2hfaWQpOiAnICsgY3VycmVudENvbnRyYWN0LmdldFN0YXRlRGF0YUZpZWxkKCdzb21lX2hhc2hfaWQnKSk7DQpwcmludCgnY3VycmVudENvbnRyYWN0LmdldERlZmluaXRpb25EYXRhRmllbGQoc2NyaXB0cyk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0RGVmaW5pdGlvbkRhdGFGaWVsZCgnc2NyaXB0cycpKTsNCnByaW50KCdjdXJyZW50Q29udHJhY3QuZ2V0SXNzdWVyKCk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0SXNzdWVyKCkpOw0KcHJpbnQoJ2N1cnJlbnRDb250cmFjdC5nZXRPd25lcigpOiAnICsgY3VycmVudENvbnRyYWN0LmdldE93bmVyKCkpOw0KcHJpbnQoJ2N1cnJlbnRDb250cmFjdC5nZXRDcmVhdG9yKCk6ICcgKyBjdXJyZW50Q29udHJhY3QuZ2V0Q3JlYXRvcigpKTsNCnByaW50KCdjYWxsIGN1cnJlbnRDb250cmFjdC5zZXRPd25lcigpLi4uJyk7DQpjdXJyZW50Q29udHJhY3Quc2V0T3duZXIoWydaYXN0V3BXTlBNcXZWSkFNb2NzTVVUSmc0NWk4TG9DNU1zbXI3THQ5RWFKSlJ3VjJ4VicsICdhMXN4aGpkdEdoTmVqaThTV0pOUGt3VjVtNmRnV2ZyUUJuaGlBeGJRd1pUNlk1RnNYRCddKTsNCnByaW50KCdjdXJyZW50Q29udHJhY3QuZ2V0T3duZXIoKTogJyArIGN1cnJlbnRDb250cmFjdC5nZXRPd25lcigpKTsNCnJlc3VsdCA9IGpzQXBpUGFyYW1zWzBdICsganNBcGlQYXJhbXNbMV07DQo=";
  Contract contract = new Contract(TestKeys.privateKey(0));
  contract.getStateData().set("some_value", HashId.createRandom().toBase64String());
  contract.getStateData().set("some_hash_id", HashId.createRandom());
  contract.getDefinition().setJS(Base64.decodeLines(scriptDump), fileName, new JSApiScriptParameters());
  contract.seal();
  String res = (String)contract.execJS(Base64.decodeLines(scriptDump), "3", "6");
  System.out.println("res: " + res);
  assertEquals("36", res);
  String compression = contract.getDefinition().getData().getOrThrow("scripts", JSApiHelpers.fileName2fileKey(fileName), "compression");
  System.out.println("compression: " + compression);
  assertEquals(JSApiCompressionEnum.RAW, JSApiCompressionEnum.valueOf(compression));
}
origin: UniversaBlockchain/universa

@Test
public void compressedJavaScript() throws Exception {
  String fileName = "somescript.zip";
  String scriptDump = "UEsDBBQAAgAIAEGVA02XbF8YbAEAAPoEAAANAAAAc29tZXNjcmlwdC5qc62UXU+DMBSG7038D9yVRUOckTk1XkzmzMiY+xJ0y7JU6KATKLYF9vNlyj6cUtR42/O+z3vSntOI4pDLwEO+T6SUUN8BlavDgwRSyY4pRSHXSMgptLl0LS1YI8KKi7j2uSSvLNEHac+1UrcduXIpAelIKiiK7QOUYIZJKIBsJWKURhHkyGlwAWtHI4bdU+xiUVdrgRjTg6sTAWYtEGOGPOu6CTlsYeQ7MiMBmiXQj1ExeM8Cth7whzAPMm+GnV/G5a6ywCaa4xDz7Il3Um2KI86KA78zgdxVFthmLEZUNLe5oGRG0lBIyes/mFpCy2aW7IGg73/Rsk2koijvm16omIAxZNyKrG7PeE1MvWEQmxkPI909U3G9QzTVYAE97/CLW6jrg9Q8XZrgWAKwypbewuF3XhctcH1o6d3eS2qqQc1xrTnt34Qebiyf++l4VHtSW+yxCab/dokUsdjffFXZ5sCATU6mmW/3oDrNpG9QSwECFAAUAAIACABBlQNNl2xfGGwBAAD6BAAADQAAAAAAAAAAACAAAAAAAAAAc29tZXNjcmlwdC5qc1BLBQYAAAAAAQABADsAAACXAQAAAAA=";
  Contract contract = new Contract(TestKeys.privateKey(0));
  contract.getStateData().set("some_value", HashId.createRandom().toBase64String());
  contract.getStateData().set("some_hash_id", HashId.createRandom());
  JSApiScriptParameters scriptParameters = new JSApiScriptParameters();
  scriptParameters.isCompressed = true;
  contract.getDefinition().setJS(Base64.decodeLines(scriptDump), fileName, scriptParameters);
  contract.seal();
  String res = (String)contract.execJS(Base64.decodeLines(scriptDump), "3", "6");
  System.out.println("res: " + res);
  assertEquals("36", res);
  String compression = contract.getDefinition().getData().getOrThrow("scripts", JSApiHelpers.fileName2fileKey(fileName), "compression");
  System.out.println("compression: " + compression);
  assertEquals(JSApiCompressionEnum.ZIP, JSApiCompressionEnum.valueOf(compression));
}
origin: UniversaBlockchain/universa

@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());
}
origin: UniversaBlockchain/universa

@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"));
}
origin: UniversaBlockchain/universa

@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)));
}
origin: UniversaBlockchain/universa

@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)));
}
com.icodici.universa.contractContractexecJS

Javadoc

Executes javascript, that previously should be saved in contract's definition with Definition#setJS(byte[],String,JSApiScriptParameters) or State#setJS(byte[],String,JSApiScriptParameters). Provides instance of JSApi to this script, as 'jsApi' global var.

Popular methods of Contract

  • <init>
    Extract old, deprecated v2 self-contained binary partially unpacked by the TransactionPack, and fill
  • addNewItems
    Add one or more siblings to the contract. Note that those must be sealed before calling #seal() or #
  • addSignerKey
    Add private key to keys contract binary to be signed with when sealed next time. It is called before
  • getExpiresAt
    Get contract expiration time
  • getId
    Get the id sealing self if need
  • getPackedTransaction
    Pack the contract to the most modern .unicon format, same as TransactionPack#pack(). Uses bounded Tr
  • registerRole
    Register new role. Name must be unique otherwise existing role will be overwritten
  • seal
    Seal contract to binary. This call adds signatures from #getKeysToSignWith()
  • addSignatureToSeal
    Add signature to sealed (before) contract. Do not deserializing or changing contract bytes, but will
  • check
  • createRevision
    Create new revision to be changed, signed sealed and then ready to approve. Created "revision" contr
  • fromDslFile
    Create contract importing its parameters with passed .yaml file. No signatures are added automatical
  • createRevision,
  • fromDslFile,
  • fromPackedTransaction,
  • getCreatedAt,
  • getDefinition,
  • getErrors,
  • getKeysToSignWith,
  • getLastSealedBinary,
  • getNew,
  • getNewItems

Popular in Java

  • Start an intent from android
  • onRequestPermissionsResult (Fragment)
  • setScale (BigDecimal)
  • runOnUiThread (Activity)
  • InputStream (java.io)
    A readable source of bytes.Most clients will use input streams that read data from the file system (
  • URI (java.net)
    A Uniform Resource Identifier that identifies an abstract or physical resource, as specified by RFC
  • NumberFormat (java.text)
    The abstract base class for all number formats. This class provides the interface for formatting and
  • Hashtable (java.util)
    A plug-in replacement for JDK1.5 java.util.Hashtable. This version is based on org.cliffc.high_scale
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • Table (org.hibernate.mapping)
    A relational table
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now