/** * Download contract from the specified url. * * @param url * * @return */ public static Contract downloadContract(String url) { // TODO: Download and check contract. report("downloading from " + url); return null; }
private static void anonymousKeyPrints() throws IOException { Map<String, PrivateKey> kk = keysMap(); if (kk.isEmpty()) report("please specify at least one key file with --fingerprints"); else { kk.forEach((name, key) -> { report("Anonymous key prints:"); report(name + "\t" + Base64.encodeCompactString(key.fingerprint())); }); } }
private static void printFingerprints() throws IOException { Map<String, PrivateKey> kk = keysMap(); if (kk.isEmpty()) report("please specify at least one key file with --fingerprints"); else { kk.forEach((name, key) -> { report("Fingerprints:"); report(name + "\t" + Base64.encodeCompactString(key.fingerprint())); }); } }
/** * Print processing cost (that was spent while checking) of a contract to console. * * @param contract */ private static void printProcessingCost(Contract contract) { report("Contract processing cost is " + contract.getProcessedCostU() + " U"); }
/** * Revoke specified contract and create a revocation transactional contract. * * @param contract * * @return Contract - revoking transaction contract. */ @Deprecated public static Contract revokeContract(Contract contract, PrivateKey... key) throws IOException { report("keys num: " + key.length); Contract tc = ContractsService.createRevocation(contract, key); registerContract(tc, 0, true); return tc; }
private static void doCreateAddress(String keyFilePath, boolean bShort) throws IOException { report("Generate " + (bShort ? "short" : "long") + " address from key: " + keyFilePath); PublicKey key = readKeyAndGetPublic(keyFilePath); KeyAddress address = new KeyAddress(key, 0, !bShort); report("Address: " + address.toString()); finish(); }
private static void doAddressMatch(String address, String keyFilePath) throws IOException { boolean bResult = false; try { PublicKey key = readKeyAndGetPublic(keyFilePath); KeyAddress keyAddress = new KeyAddress(address); bResult = keyAddress.isMatchingKey(key); } catch (Exception e) {}; report("Matching address: " + address + " with key: "+ keyFilePath); report("Matching result: " + bResult); finish(); }
private static void doResync() throws IOException { List<String> sources = new ArrayList<String>((List) options.valuesOf("resync")); List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments()); for (String opt : nonOptions) { sources.addAll(asList(opt.split(","))); } cleanNonOptionalArguments(sources); for (int s = 0; s < sources.size(); s++) { String source = sources.get(s); ItemResult ir = getClientNetwork().resync(source); report("Node has reported the state:"); report(ir.toString()); } finish(); }
private static void doFindContracts() throws IOException { List<String> sources = new ArrayList<String>((List) options.valuesOf("f")); List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments()); for (String opt : nonOptions) { sources.addAll(asList(opt.split(","))); } cleanNonOptionalArguments(sources); for (int s = 0; s < sources.size(); s++) { String source = sources.get(s); report("Looking for contracts at the " + source); HashMap<String, Contract> allFoundContracts = findContracts(source, options.has("r")); List<Wallet> wallets = Wallet.determineWallets(new ArrayList<>(allFoundContracts.values())); if (wallets.size() > 0) { printWallets(wallets); } else { report("No wallets found"); } if (allFoundContracts.size() > 0) { printContracts(allFoundContracts); } else { report("No contracts found"); } } finish(); }
/** * Register a specified contract. * * @param contract must be a sealed binary. */ public static Parcel prepareForRegisterContract(Contract contract, Contract u, int amount, Set<PrivateKey> uKeys, boolean withTestPayment) throws IOException { List<ErrorRecord> errors = contract.getErrors(); if (errors.size() > 0) { report("contract has errors and can't be submitted for registration"); report("contract id: " + contract.getId().toBase64String()); addErrors(errors); } else { Parcel parcel = ContractsService.createParcel(contract, u, amount, uKeys, withTestPayment); return parcel; } return null; }
private static void checkFile(File f) { try { TransactionPack tp = TransactionPack.unpack(Do.read(f), true); if (tp.isReconstructed()) { report("file " + f + " is a single contract"); } else { report("file " + f + " is a transaction pack"); } System.out.println(); checkContract(tp.getContract()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", f.getPath(), e.toString()); } catch (IOException e) { addError("READ_ERROR", f.getPath(), e.toString()); } catch (Exception e) { addError("UNKNOWN_ERROR", f.getPath(), e.toString()); } }
private static void doStartHttpServer(String routeFilePath) throws IOException { try { cliServices.startJsApiHttpServer( routeFilePath, hashId -> { try { return getClientNetwork().check(hashId).state == ItemState.APPROVED; } catch (IOException e) { report("error while checking contract for approve: " + e); return false; } }, (slotId, originId) -> { try { if (slotId == null) return null; return getClientNetwork().client.queryContract(slotId, originId, null); } catch (IOException e) { report("error while querying contract from slot1: " + e); return null; } }); } catch (Exception e) { report("http server error: " + e); } finish(); }
/** * Register a paying parcel. * * @param contract must be a sealed binary. */ public static Parcel prepareForRegisterPayingParcel(Contract contract, Contract u, int amount, int amountStorage, Set<PrivateKey> uKeys, boolean withTestPayment) throws IOException { List<ErrorRecord> errors = contract.getErrors(); if (errors.size() > 0) { report("contract has errors and can't be submitted for registration"); report("contract id: " + contract.getId().toBase64String()); addErrors(errors); } else { Set<PrivateKey> keys = new HashSet<>(keysMapContract().values()); if (keys != null && keys.size() > 0) contract.addSignerKeys(keys); Parcel parcel = ContractsService.createPayingParcel(contract.getTransactionPack(), u, amount, amountStorage, uKeys, withTestPayment); return parcel; } return null; }
/** * Revoke specified contract and create a revocation transactional contract. * * @param contract * * @return Parcel - revoking transaction contract. */ public static Parcel revokeContract(Contract contract, Contract u, int amount, Set<PrivateKey> uKeys, boolean withTestPayment, PrivateKey... key) throws IOException { report("keys num: " + key.length); Contract tc = ContractsService.createRevocation(contract, key); Parcel parcel = prepareForRegisterContract(tc, u, amount, uKeys, withTestPayment); if (parcel != null) registerParcel(parcel, 0); return parcel; }
private static void doExecJs() throws Exception { String contractFile = (String) options.valueOf("exec-js"); String scriptName = null; if (options.has("script-name")) scriptName = (String)options.valueOf("script-name"); Contract c = Contract.fromPackedTransaction(Files.readAllBytes(Paths.get(contractFile))); if(c != null) { ItemResult itemResult = getClientNetwork().client.getState(c.getId(), reporter); if (itemResult.state == ItemState.APPROVED) { if (scriptName != null) { c.execJSByName(scriptName); } else { List<String> scriptNames = c.extractJSNames(); if (scriptNames.size() == 1) c.execJSByName(scriptNames.get(0)); else if (scriptNames.size() > 1) report("error: contract has " + scriptNames.size() + " scripts attached, specify script filename please"); else report("error: contract has no scripts attached"); } } else { report("error: contract should be approved"); } } finish(); }
private static void doAnonymize() throws IOException { List<String> sources = new ArrayList<String>((List) options.valuesOf("anonymize")); List<String> roles = new ArrayList<String>((List) options.valuesOf("role")); List<String> names = (List) options.valuesOf("output"); List<String> nonOptions = new ArrayList<String>((List) options.nonOptionArguments()); for (String opt : nonOptions) { sources.addAll(asList(opt.split(","))); } cleanNonOptionalArguments(sources); for (int s = 0; s < sources.size(); s++) { String source = sources.get(s); Contract contract = loadContract(source); if (contract != null) { if(roles.size() <= 0) { roles = new ArrayList<>(contract.getRoles().keySet()); } for (String roleName : roles) { report("Anonymizing role " + roleName + " in " + source + "..."); contract.anonymizeRole(roleName); contract.seal(); } if (names.size() > s) { saveContract(contract, names.get(s), true, false); } else { saveContract(contract, new FilenameTool(source).addSuffixToBase("_anonymized").toString(), true, false); } } } finish(); }
private static void saveContractSubitems(String source, String suffix, Contract contract) throws IOException { try { report("unpack contract from " + source); int i = 1; if (contract.getNewItems() != null) { for (Approvable newItem : contract.getNewItems()) { String newItemFileName = new FilenameTool(source).addSuffixToBase(suffix+"_new_item_" + i).toString(); report("save newItem to " + newItemFileName); // ((Contract) newItem).seal(); saveContract((Contract) newItem, newItemFileName); i++; } } i = 1; if (contract.getRevokingItems() != null) { for (Approvable revokeItem : contract.getRevokingItems()) { String revokeItemFileName = new FilenameTool(source).addSuffixToBase(suffix+"_revoke_" + i).setExtension("unicon").toString(); report("save revokeItem to " + revokeItemFileName); saveContract((Contract) revokeItem, revokeItemFileName); i++; } } } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", contract.toString(), e.getMessage()); } }
/** * Register a specified parcel. * @param parcel must be ready for register * @param waitTime wait time for responce. * @return ItemResult for parcel's payload * @throws IOException */ public static ItemResult registerParcel(Parcel parcel, int waitTime) throws IOException { getClientNetwork().registerParcel(parcel.pack(), waitTime); ItemResult r = getClientNetwork().check(parcel.getPayloadContract().getId()); report("paid contract " + parcel.getPayloadContract().getId() + " submitted with result: " + r.toString()); report("payment became " + parcel.getPaymentContract().getId()); report("payment rev 1 " + parcel.getPaymentContract().getRevoking().get(0).getId()); return r; }
/** * Save specified parcel to file. * * @param parcel - parcel to save. * @param fileName - name of file to save to. * */ public static boolean saveParcel(Parcel parcel, String fileName) throws IOException { if (fileName == null) { fileName = "Universa_" + DateTimeFormatter.ofPattern("yyyy-MM-ddTHH:mm:ss").format(parcel.getPayloadContract().getCreatedAt()) + ".uniparcel"; } byte[] data = parcel.pack(); String newFileName = FileTool.writeFileContentsWithRenaming(fileName, data); report("Parcel is saved to: " + newFileName); report("Parcel size: " + data.length); try { if (parcel.getPaymentContract().check() && parcel.getPayloadContract().check()) { report("Parcel has no errors"); } else { addErrors(parcel.getPaymentContract().getErrors()); addErrors(parcel.getPayloadContract().getErrors()); } } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", parcel.toString(), e.getMessage()); } return (newFileName!=null); }