use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.
the class ContractsService method createNotaryContract.
/**
* Creates a simple notary contract for given keys.
*<br><br>
* The service creates a notary contract with issuer, creator and owner roles;
* with change_owner permission for owner and revoke permissions for owner and issuer.
* By default expires at time is set to 60 months from now.
*<br><br>
* @param issuerKeys is issuer private keys.
* @param ownerKeys is owner public keys.
* @return signed and sealed contract, ready for register.
*/
public static synchronized Contract createNotaryContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys) {
Contract notaryContract = new Contract();
notaryContract.setApiLevel(3);
Contract.Definition cd = notaryContract.getDefinition();
cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
Binder data = new Binder();
data.set("name", "Default notary");
data.set("description", "Default notary description.");
cd.setData(data);
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : issuerKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
notaryContract.registerRole(issuerRole);
notaryContract.createRole("issuer", issuerRole);
notaryContract.createRole("creator", issuerRole);
notaryContract.registerRole(ownerRole);
notaryContract.createRole("owner", ownerRole);
ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
notaryContract.addPermission(changeOwnerPerm);
RevokePermission revokePerm1 = new RevokePermission(ownerRole);
notaryContract.addPermission(revokePerm1);
RevokePermission revokePerm2 = new RevokePermission(issuerRole);
notaryContract.addPermission(revokePerm2);
notaryContract.seal();
notaryContract.addSignatureToSeal(issuerKeys);
return notaryContract;
}
use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.
the class ContractsService method createTwoSignedContract.
/**
* Creates a contract with two signatures.
*<br><br>
* The service creates a contract which asks two signatures.
* It can not be registered without both parts of deal, so it is make sure both parts that they agreed with contract.
* Service creates a contract that should be send to partner,
* then partner should sign it and return back for final sign from calling part.
*<br><br>
* @param BaseContract is base contract
* @param fromKeys is own private keys
* @param toKeys is foreign public keys
* @param createNewRevision create new revision if true
* @return contract with two signatures that should be send from first part to partner.
*/
public static synchronized Contract createTwoSignedContract(Contract BaseContract, Set<PrivateKey> fromKeys, Set<PublicKey> toKeys, boolean createNewRevision) {
Contract twoSignContract = BaseContract;
if (createNewRevision) {
twoSignContract = BaseContract.createRevision(fromKeys);
twoSignContract.getKeysToSignWith().clear();
}
SimpleRole creatorFrom = new SimpleRole("creator");
for (PrivateKey k : fromKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
creatorFrom.addKeyRecord(kr);
}
SimpleRole ownerTo = new SimpleRole("owner");
for (PublicKey k : toKeys) {
KeyRecord kr = new KeyRecord(k);
ownerTo.addKeyRecord(kr);
}
twoSignContract.createTransactionalSection();
twoSignContract.getTransactional().setId(HashId.createRandom().toBase64String());
Reference reference = new Reference(twoSignContract);
reference.transactional_id = twoSignContract.getTransactional().getId();
reference.type = Reference.TYPE_TRANSACTIONAL;
reference.required = true;
reference.signed_by = new ArrayList<>();
reference.signed_by.add(creatorFrom);
reference.signed_by.add(ownerTo);
twoSignContract.getTransactional().addReference(reference);
twoSignContract.setOwnerKeys(toKeys);
twoSignContract.seal();
return twoSignContract;
}
use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.
the class ContractsService method createShareContract.
/**
* Creates a share contract for given keys.
*<br><br>
* The service creates a simple share contract with issuer, creator and owner roles;
* with change_owner permission for owner, revoke permissions for owner and issuer and split_join permission for owner.
* Split_join permission has by default following params: 1 for min_value, 1 for min_unit, "amount" for field_name,
* "state.origin" for join_match_fields.
* By default expires at time is set to 60 months from now.
*<br><br>
* @param issuerKeys is issuer private keys.
* @param ownerKeys is owner public keys.
* @param amount is maximum shares number.
* @return signed and sealed contract, ready for register.
*/
public static synchronized Contract createShareContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys, String amount) {
Contract shareContract = new Contract();
shareContract.setApiLevel(3);
Contract.Definition cd = shareContract.getDefinition();
cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
Binder data = new Binder();
data.set("name", "Default share name");
data.set("currency_code", "DSH");
data.set("currency_name", "Default share name");
data.set("description", "Default share description.");
cd.setData(data);
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : issuerKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
shareContract.registerRole(issuerRole);
shareContract.createRole("issuer", issuerRole);
shareContract.createRole("creator", issuerRole);
shareContract.registerRole(ownerRole);
shareContract.createRole("owner", ownerRole);
shareContract.getStateData().set("amount", amount);
ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
shareContract.addPermission(changeOwnerPerm);
Binder params = new Binder();
params.set("min_value", 1);
params.set("min_unit", 1);
params.set("field_name", "amount");
List<String> listFields = new ArrayList<>();
listFields.add("state.origin");
params.set("join_match_fields", listFields);
SplitJoinPermission splitJoinPerm = new SplitJoinPermission(ownerRole, params);
shareContract.addPermission(splitJoinPerm);
RevokePermission revokePerm1 = new RevokePermission(ownerRole);
shareContract.addPermission(revokePerm1);
RevokePermission revokePerm2 = new RevokePermission(issuerRole);
shareContract.addPermission(revokePerm2);
shareContract.seal();
shareContract.addSignatureToSeal(issuerKeys);
return shareContract;
}
use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.
the class TransactionPack method deserialize.
@Override
public void deserialize(Binder data, BiDeserializer deserializer) throws IOException {
synchronized (this) {
// It is local quantiser that should throw exception
// if limit is got while deserializing TransactionPack.
Quantiser quantiser = new Quantiser();
quantiser.reset(Contract.getTestQuantaLimit());
List<Object> keysList = deserializer.deserializeCollection(data.getList("keys", new ArrayList<>()));
keysForPack = new HashSet<>();
if (keysList != null) {
for (Object x : keysList) {
if (x instanceof Bytes)
x = ((Bytes) x).toArray();
if (x instanceof byte[]) {
keysForPack.add(new PublicKey((byte[]) x));
} else {
throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
}
}
}
List<Bytes> foreignReferenceBytesList = deserializer.deserializeCollection(data.getList("referencedItems", new ArrayList<>()));
if (foreignReferenceBytesList != null) {
for (Bytes b : foreignReferenceBytesList) {
Contract frc = new Contract(b.toArray(), this);
quantiser.addWorkCostFrom(frc.getQuantiser());
referencedItems.put(frc.getId(), frc);
}
}
List<Bytes> subItemsBytesList = deserializer.deserializeCollection(data.getListOrThrow("subItems"));
HashMap<ContractDependencies, Bytes> allContractsTrees = new HashMap<>();
List<HashId> allContractsHids = new ArrayList<>();
ArrayList<Bytes> sortedSubItemsBytesList = new ArrayList<>();
if (subItemsBytesList != null) {
// First of all extract contracts dependencies from subItems
for (Bytes b : subItemsBytesList) {
ContractDependencies ct = new ContractDependencies(b.toArray());
allContractsTrees.put(ct, b);
allContractsHids.add(ct.id);
}
// and add items to subItems on the each level of tree's hierarchy
do {
// first add contract from ends of trees, means without own subitems
sortedSubItemsBytesList = new ArrayList<>();
List<ContractDependencies> removingContractDependencies = new ArrayList<>();
for (ContractDependencies ct : allContractsTrees.keySet()) {
if (ct.dependencies.size() == 0) {
sortedSubItemsBytesList.add(allContractsTrees.get(ct));
removingContractDependencies.add(ct);
}
}
// remove found items from tree's list
for (ContractDependencies ct : removingContractDependencies) {
allContractsTrees.remove(ct);
}
// then add contract with already exist subitems in the subItems or will never find in the tree
removingContractDependencies = new ArrayList<>();
for (ContractDependencies ct : allContractsTrees.keySet()) {
boolean allDependenciesSafe = true;
for (HashId hid : ct.dependencies) {
if (!subItems.containsKey(hid) && allContractsHids.contains(hid)) {
allDependenciesSafe = false;
}
}
if (allDependenciesSafe) {
sortedSubItemsBytesList.add(allContractsTrees.get(ct));
removingContractDependencies.add(ct);
}
}
// remove found items from tree's list
for (ContractDependencies ct : removingContractDependencies) {
allContractsTrees.remove(ct);
}
// add found binaries on the hierarchy level to subItems
for (int i = 0; i < sortedSubItemsBytesList.size(); i++) {
Contract c = new Contract(sortedSubItemsBytesList.get(i).toArray(), this);
quantiser.addWorkCostFrom(c.getQuantiser());
subItems.put(c.getId(), c);
}
// then repeat until we can find hierarchy
} while (sortedSubItemsBytesList.size() != 0);
// finally add not found binaries on the hierarchy levels to subItems
for (Bytes b : allContractsTrees.values()) {
Contract c = new Contract(b.toArray(), this);
quantiser.addWorkCostFrom(c.getQuantiser());
subItems.put(c.getId(), c);
}
}
byte[] bb = data.getBinaryOrThrow("contract");
contract = new Contract(bb, this);
quantiser.addWorkCostFrom(contract.getQuantiser());
}
}
use of com.icodici.crypto.PublicKey in project universa by UniversaBlockchain.
the class SimpleRole method initWithRecords.
private void initWithRecords(@NonNull Collection records) {
records.forEach(x -> {
KeyRecord kr = null;
AnonymousId anonId = null;
if (x instanceof KeyRecord)
kr = (KeyRecord) x;
else if (x instanceof PublicKey)
kr = new KeyRecord((PublicKey) x);
else if (x instanceof AnonymousId)
anonId = (AnonymousId) x;
else if (x instanceof PrivateKey)
kr = new KeyRecord(((PrivateKey) x).getPublicKey());
else if (x instanceof KeyAddress)
keyAddresses.add((KeyAddress) x);
else
throw new IllegalArgumentException("Cant create KeyRecord from " + x);
if (anonId != null)
anonymousIds.add(anonId);
else if (kr != null)
keyRecords.put(kr.getPublicKey(), kr);
});
}
Aggregations