use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class InnerContractsService method createFreshTU.
/**
* Creates fresh contract in the first revision with transaction units.
*<br><br>
* This contract should be registered and then should be used as payment for other contract's processing.
* TU contracts signs with special Universa keys and set as owner public keys from params.
*<br><br>
* @param amount is initial number of TU that will be have an owner
* @param ownerKeys is public keys that will became an owner of TU
* @param withTestTU if true TU will be created with test transaction units
* @return sealed TU contract; should be registered in the Universa by simplified procedure.
* @throws IOException with exceptions while contract preparing
*/
public static synchronized Contract createFreshTU(int amount, Set<PublicKey> ownerKeys, boolean withTestTU) throws IOException {
PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(Config.tuKeyPath));
Contract tu;
if (withTestTU) {
tu = Contract.fromDslFile(Config.testTUTemplatePath);
} else {
tu = Contract.fromDslFile(Config.tuTemplatePath);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
tu.registerRole(ownerRole);
tu.createRole("owner", ownerRole);
tu.getStateData().set("transaction_units", amount);
if (withTestTU) {
tu.getStateData().set("test_transaction_units", amount * 100);
}
tu.addSignerKey(manufacturePrivateKey);
tu.seal();
return tu;
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method doSelectKeyInFolder.
private static void doSelectKeyInFolder(String keysPath, String address) throws IOException {
File folder = new File(keysPath);
KeyAddress keyAddress;
if (!keysPath.endsWith("/"))
keysPath = keysPath.concat("/");
try {
keyAddress = new KeyAddress(address);
} catch (Exception e) {
report("Invalid address.");
finish();
return;
}
if (folder.exists()) {
for (File file : folder.listFiles()) {
if (!file.isDirectory()) {
boolean bResult = false;
try {
PrivateKey key = new PrivateKey(Do.read(keysPath + file.getName()));
bResult = keyAddress.isMatchingKey(key.getPublicKey());
} catch (Exception e) {
bResult = false;
}
if (bResult) {
report("Filekey: " + file.getName());
finish();
return;
}
}
}
report("File not found.");
} else
report("There is no such directory.");
finish();
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method doRegister.
private static void doRegister() throws IOException {
List<String> sources = new ArrayList<String>((List) options.valuesOf("register"));
String tuSource = (String) options.valueOf("tu");
int tuAmount = (int) options.valueOf("amount");
boolean tutest = options.has("tutest");
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);
Contract tu = null;
if (tuSource != null) {
tu = loadContract(tuSource, true);
report("load payment revision: " + tu.getState().getRevision() + " id: " + tu.getId());
}
Set<PrivateKey> tuKeys = new HashSet<>(keysMap().values());
if (contract != null) {
if (tu != null && tuKeys != null && tuKeys.size() > 0) {
report("registering the paid contract " + contract.getId() + " from " + source + " for " + tuAmount + " TU");
Parcel parcel = registerContract(contract, tu, tuAmount, tuKeys, tutest, (int) options.valueOf("wait"));
if (parcel != null) {
report("save payment revision: " + parcel.getPaymentContract().getState().getRevision() + " id: " + parcel.getPaymentContract().getId());
CopyOption[] copyOptions = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES };
Files.copy(Paths.get(tuSource), Paths.get(tuSource.replaceAll("(?i)\\.(unicon)$", "_rev" + tu.getRevision() + ".unicon")), copyOptions);
saveContract(parcel.getPaymentContract(), tuSource, true, false);
}
} else {
report("registering the contract " + contract.getId().toBase64String() + " from " + source);
registerContract(contract, (int) options.valueOf("wait"));
}
}
}
// print cost of processing if asked
if (options.has("cost")) {
doCost();
} else {
finish();
}
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method doAddressMatch.
private static void doAddressMatch(String address, String keyFilePath) throws IOException {
boolean bResult = false;
try {
PrivateKey key = new PrivateKey(Do.read(keyFilePath));
KeyAddress keyAddress = new KeyAddress(address);
bResult = keyAddress.isMatchingKey(key.getPublicKey());
} catch (Exception e) {
}
;
report("Matching address: " + address + " with key: " + keyFilePath);
report("Matching result: " + bResult);
finish();
}
use of com.icodici.crypto.PrivateKey in project universa by UniversaBlockchain.
the class CLIMain method keysMap.
public static synchronized Map<String, PrivateKey> keysMap() throws IOException {
if (keyFiles == null) {
keyFiles = new HashMap<>();
for (String fileName : keyFileNames) {
// keyFiles.put(fileName, pk);
try {
PrivateKey pk = PrivateKey.fromPath(Paths.get(fileName));
keyFiles.put(fileName, pk);
} catch (IOException e) {
addError(Errors.NOT_FOUND.name(), fileName.toString(), "failed to load key file: " + e.getMessage());
}
}
}
return keyFiles;
}
Aggregations