/** * Create new revision to be changed, signed sealed and then ready to approve. Created "revision" contract is a copy * of this contract, with all fields and references correctly set. After this call one need to change mutable * fields, add signing keys, seal it and then apss to Universa network for approval. * * @param keys initially added and signer keys. Role "creator" is set to anonymous ids of these keys * @param transactional is {@link Transactional} section to create new revision with * @return new revision of this contract, identical to this one, to be modified. */ public synchronized Contract createRevisionAnonymously(Collection<?> keys, Transactional transactional) { Contract newRevision = createRevision(transactional); Set<AnonymousId> aids = new HashSet<>(); AtomicBoolean returnNull = new AtomicBoolean(false); keys.forEach(k -> { if (k instanceof AbstractKey) aids.add(AnonymousId.fromBytes(((AbstractKey) k).createAnonymousId())); else if (k instanceof AnonymousId) aids.add((AnonymousId)k); else returnNull.set(true); }); newRevision.setCreatorKeys(aids); if (returnNull.get()) return null; return newRevision; }
/** * Create new revision to be changed, signed sealed and then ready to approve. Created "revision" contract is a copy * of this contract, with all fields and references correctly set. After this call one need to change mutable * fields, add signing keys, seal it and then apss to Universa network for approval. * * @param keys initially added and signer keys. Role "creator" is set to addresses of these keys * @param transactional is {@link Transactional} section to create new revision with * @return new revision of this contract, identical to this one, to be modified. */ public synchronized Contract createRevisionWithAddress(Collection<?> keys, Transactional transactional) { Contract newRevision = createRevision(transactional); Set<KeyAddress> aids = new HashSet<>(); AtomicBoolean returnNull = new AtomicBoolean(false); keys.forEach(k -> { if (k instanceof AbstractKey) aids.add(((AbstractKey) k).getPublicKey().getShortAddress()); else if (k instanceof KeyAddress) aids.add((KeyAddress)k); else returnNull.set(true); }); newRevision.setCreatorKeys(aids); if (returnNull.get()) return null; return newRevision; }
SimpleRole role = new SimpleRole("owner", asList(new KeyAddress((String) owners.get(i)))); partContracts[i].registerRole(role); partContracts[i].setCreatorKeys(keysMap().values()); } catch (KeyAddress.IllegalAddressException e) { System.out.println("Not a valid address: " + owners.get(i)); contract.setCreatorKeys(keysMap().values());
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); } }
anonSignedContract.setCreatorKeys(stepaAddress); anonSignedContract.addSignerKey(stepaPrivateKeys.iterator().next()); anonSignedContract.seal();
anonSignedContract.setCreatorKeys(stepaAnonId); anonSignedContract.addSignerKey(stepaPrivateKeys.iterator().next()); anonSignedContract.seal();
@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 jsInStateAndDefinition() 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()); String jsDefinitionA = "print('hello world from definition A'); result = 'dA';"; String jsDefinitionB = "print('hello world from definition B'); result = 'dB';"; String jsStateA = "print('hello world from state A'); result = 'sA';"; String jsStateB = "print('hello world from state B'); result = 'sB';"; contract.getDefinition().setJS(jsDefinitionA.getBytes(), "script1.js", new JSApiScriptParameters(), true); contract.getDefinition().setJS(jsDefinitionB.getBytes(), "script2.js", new JSApiScriptParameters(), true); contract.getState().setJS(jsStateA.getBytes(), "script3.js", new JSApiScriptParameters(), true); contract.getState().setJS(jsStateB.getBytes(), "script4.js", new JSApiScriptParameters(), true); contract.seal(); String res1 = (String)contract.execJSByName("script1.js"); String res2 = (String)contract.execJSByName("script2.js"); String res3 = (String)contract.execJSByName("script3.js"); String res4 = (String)contract.execJSByName("script4.js"); assertEquals("dA", res1); assertEquals("dB", res2); assertEquals("sA", res3); assertEquals("sB", res4); }
@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()); }
badReferencedContract.setCreatorKeys(TestKeys.privateKey(1)); badReferencedContract.seal();