public void setOrigin(HashId origin) { this.c.getState().origin = origin; }
private void checkCreateParcelFotTestNet(String contract_file_payload, String contract_file_payment) throws Exception { final String ROOT_PATH = "./src/test_contracts/contractService/"; PrivateKey privateKey = TestKeys.privateKey(3); Set<PrivateKey> privateKeys = new HashSet<>(); privateKeys.add(privateKey); Set<PublicKey> publicKeys = new HashSet<>(); publicKeys.add(privateKey.getPublicKey()); Contract payload = Contract.fromDslFile(ROOT_PATH + contract_file_payload); payload.addSignerKey(privateKey); payload.seal(); Contract payment = InnerContractsService.createFreshU(100, publicKeys, true); Parcel parcel = ContractsService.createParcel(payload, payment, 20, privateKeys, true); assertEquals(parcel.getPayloadContract().getState().getBranchId(), payload.getState().getBranchId()); assertEquals(parcel.getPaymentContract().getState().getBranchId(), payment.getState().getBranchId()); assertEquals(parcel.getPayloadContract().getStateData(), payload.getStateData()); assertEquals(parcel.getPaymentContract().getDefinition().getData(), payment.getDefinition().getData()); assertEquals(100, parcel.getPaymentContract().getStateData().getIntOrThrow("transaction_units")); assertEquals(10000 - 20, parcel.getPaymentContract().getStateData().getIntOrThrow("test_transaction_units")); System.out.println("OK"); }
public void setParent(HashId parent) { this.c.getState().parent = parent; }
private void checkCreateParcel(String contract_file_payload, String contract_file_payment) throws Exception { final String ROOT_PATH = "./src/test_contracts/contractService/"; PrivateKey privateKey = TestKeys.privateKey(3); Contract payload = Contract.fromDslFile(ROOT_PATH + contract_file_payload); payload.addSignerKey(privateKey); payload.seal(); Contract payment = Contract.fromDslFile(ROOT_PATH + contract_file_payment); payment.addSignerKey(privateKey); payment.seal(); Set<PrivateKey> PrivateKeys = new HashSet<>(); PrivateKeys.add(privateKey); Parcel parcel = ContractsService.createParcel(payload, payment, 20, PrivateKeys); assertEquals(parcel.getPayloadContract().getState().getBranchId(), payload.getState().getBranchId()); assertEquals(parcel.getPaymentContract().getState().getBranchId(), payment.getState().getBranchId()); assertEquals(parcel.getPayloadContract().getStateData(), payload.getStateData()); assertEquals(parcel.getPaymentContract().getDefinition().getData(), payment.getDefinition().getData()); System.out.println("OK"); }
public int getRevision() { return this.currentContract.getState().getRevision(); }
public List<String> extractJSNames() { List<String> res = new ArrayList<>(); Binder scriptBinder = getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder == null) scriptBinder = getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder != null) res.addAll(JSApiHelpers.getFileNamesFromScriptBinder(scriptBinder)); return res; }
@Test(timeout = 90000) public void checkPayment_originItself() throws Exception { Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys()); final Field field = payment.getState().getClass().getDeclaredField("origin"); field.setAccessible(true); field.set(payment.getState(), payment.getId()); final Field field2 = payment.getRevoking().get(0).getState().getClass().getDeclaredField("origin"); field2.setAccessible(true); field2.set(payment.getRevoking().get(0).getState(), payment.getId()); boolean res = payment.paymentCheck(config.getUIssuerKeys()); payment.traceErrors(); assertFalse(res); }
@Test(timeout = 20000) public void checkPayment_revision1() throws Exception { Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys()); final Field field = payment.getState().getClass().getDeclaredField("revision"); field.setAccessible(true); field.set(payment.getState(), 1); boolean res = payment.paymentCheck(config.getUIssuerKeys()); payment.traceErrors(); assertFalse(res); }
@Test(timeout = 90000) public void checkPayment_originMismatch() throws Exception { Contract payment = checkPayment_preparePaymentContract(checkPayment_preparePrivateKeys()); final Field field2 = payment.getRevoking().get(0).getState().getClass().getDeclaredField("origin"); field2.setAccessible(true); field2.set(payment.getRevoking().get(0).getState(), payment.getId()); boolean res = payment.paymentCheck(config.getUIssuerKeys()); payment.traceErrors(); assertFalse(res); }
@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); }
public void updateThisEnvironmentByName(Contract newContract, JSApiExecOptions execOptions) throws Exception { JSApiEnvironment env = execJSByName( newContract.getDefinition().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), newContract.getState().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, newContract, stringParams ); this.jsApi = env.jsApi; this.scriptEngine = env.scriptEngine; this.currentContract = env.currentContract; this.result = env.result; }
/** * Executes attached javascript if only one js is attached. Also, it should be attached with putContentIntoContract == true. */ public Object execJSByName(JSApiExecOptions execOptions, String jsFileName, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByName( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, this, params ); return env.callEvent("main", true); }
/** * Executes javascript, like {@link Contract#execJS(JSApiExecOptions, byte[], String...)}, * but searches script body in contract. It should be saved previously with {@link Definition#setJS(byte[], String, JSApiScriptParameters, boolean)} * or {@link State#setJS(byte[], String, JSApiScriptParameters, boolean)} with putContentIntoContract == true. * @param scriptHash is {@link HashId} from js-file content. */ public Object execJSByScriptHash(JSApiExecOptions execOptions, HashId scriptHash, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByScriptHash( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, scriptHash, this, params ); return env.callEvent("main", true); }
@Test public void shouldNotSplitWithAnotherRevision() throws Exception { Contract c = createCoin(); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); c.getState().setBranchNumber(1); try { Contract c2 = c.split(1)[0]; fail("Expected exception to be thrown."); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().equalsIgnoreCase("this revision is already split")); } }
@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")); }
/** * Executes javascript, that previously should be saved in contract's definition * with {@link Definition#setJS(byte[], String, JSApiScriptParameters)} or {@link State#setJS(byte[], String, JSApiScriptParameters)}. * Provides instance of {@link JSApi} to this script, as 'jsApi' global var. * @param params list of strings, will be passed to javascript * @return Object, got it from 'result' global var of javascript. * @throws ScriptException if javascript throws some errors * @throws IllegalArgumentException if javascript is not defined in contract's definition */ public Object execJS(JSApiExecOptions execOptions, byte[] jsFileContent, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJS( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, jsFileContent, this, params ); env.callEvent("main", true); return env.getResult(); }
@Test public void shouldSplitAndMergeOne() throws Exception { List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(100, 200)); List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount); //want to send 250 Wallet wallet = wallets.get(0); Contract output = wallet.buildContractWithValue("amount", new Decimal(250)); output.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(output, true); Contract contract = wallet.getContracts().get(0); sealCheckTrace(contract, true); assertEquals("50", contract.getState().getData().get("amount")); }
@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")); }
public void parcelAssertions(Parcel equal1, Parcel equal2 ) throws Exception { //few assertions assertEquals(equal1.getPayload().getContract().getId(), equal2.getPayload().getContract().getId()); assertEquals(equal1.getPayment().getContract().getId(), equal2.getPayment().getContract().getId()); assertEquals(equal1.getPayload().getContract().getState().getBranchId(), equal2.getPayload().getContract().getState().getBranchId()); assertEquals(equal1.getPayment().getContract().getState().getBranchId(), equal2.getPayment().getContract().getState().getBranchId()); assertEquals(equal1.getPayload().getContract().getState().getCreatedAt().getSecond(), equal2.getPayload().getContract().getState().getCreatedAt().getSecond()); assertEquals(equal1.getPayment().getContract().getState().getCreatedAt().getSecond(), equal2.getPayment().getContract().getState().getCreatedAt().getSecond()); assertEquals(equal1.getPayload().getContract().getExpiresAt().getDayOfYear(), equal2.getPayload().getContract().getExpiresAt().getDayOfYear()); assertEquals(equal1.getPayment().getContract().getExpiresAt().getDayOfYear(), equal2.getPayment().getContract().getExpiresAt().getDayOfYear()); assertEquals(equal1.getPayload().getSubItems().size(), equal2.getPayload().getSubItems().size()); assertEquals(equal1.getPayment().getSubItems(), equal2.getPayment().getSubItems()); }
@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()); }