Search in sources :

Example 1 with JSApiScriptParameters

use of com.icodici.universa.contract.jsapi.JSApiScriptParameters in project universa by UniversaBlockchain.

the class CLIMain method doCreateContractWithJs.

private static void doCreateContractWithJs() throws Exception {
    String jsFile = (String) options.valueOf("create-contract-with-js");
    byte[] jsFileBytes = Files.readAllBytes(Paths.get(jsFile));
    List<String> names = (List) options.valuesOf("o");
    List<String> keys = (List) options.valuesOf("k");
    if (names.size() == 0) {
        report("error: output file not specified, use -o option");
        finish();
    }
    if (keys.size() == 0) {
        report("error: key file(s) not specified, use -k option");
        finish();
    }
    String outputFile = names.get(0);
    Contract contract = new Contract();
    Set<PrivateKey> privateKeys = new HashSet<>();
    for (String keyPath : keys) {
        PrivateKey privateKey = PrivateKey.fromPath(Paths.get(keyPath));
        privateKeys.add(privateKey);
    }
    contract.setIssuerKeys(privateKeys);
    contract.setExpiresAt(ZonedDateTime.now().plusDays(90));
    contract.addRole(new RoleLink("owner", contract, "issuer"));
    contract.addRole(new RoleLink("creator", contract, "issuer"));
    RoleLink roleLink = new RoleLink("@change_owner_role", contract, "owner");
    contract.addPermission(new ChangeOwnerPermission(roleLink));
    contract.addSignerKeys(privateKeys);
    String filename = Paths.get(jsFile).getFileName().toString();
    JSApiScriptParameters scriptParameters = new JSApiScriptParameters();
    scriptParameters.domainMasks.add("localhost:*");
    scriptParameters.setPermission(JSApiScriptParameters.ScriptPermissions.PERM_HTTP_CLIENT, true);
    contract.getState().setJS(jsFileBytes, filename, scriptParameters, true);
    contract.seal();
    String createdFileName = FileTool.writeFileContentsWithRenaming(outputFile, contract.getPackedTransaction());
    report("file " + createdFileName + " saved");
    finish();
}
Also used : RoleLink(com.icodici.universa.contract.roles.RoleLink) ChangeOwnerPermission(com.icodici.universa.contract.permissions.ChangeOwnerPermission) Arrays.asList(java.util.Arrays.asList) JSApiScriptParameters(com.icodici.universa.contract.jsapi.JSApiScriptParameters)

Aggregations

JSApiScriptParameters (com.icodici.universa.contract.jsapi.JSApiScriptParameters)1 ChangeOwnerPermission (com.icodici.universa.contract.permissions.ChangeOwnerPermission)1 RoleLink (com.icodici.universa.contract.roles.RoleLink)1 Arrays.asList (java.util.Arrays.asList)1