use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class MainTest method createNetConfigContract.
private Contract createNetConfigContract(List<NodeInfo> netConfig, PrivateKey issuerKey) throws IOException {
Contract contract = new Contract();
contract.setIssuerKeys(issuerKey.getPublicKey());
contract.registerRole(new RoleLink("creator", "issuer"));
ListRole listRole = new ListRole("owner");
for (NodeInfo ni : netConfig) {
SimpleRole role = new SimpleRole(ni.getName());
contract.registerRole(role);
role.addKeyRecord(new KeyRecord(ni.getPublicKey()));
listRole.addRole(role);
}
listRole.setQuorum(netConfig.size() - 1);
contract.registerRole(listRole);
RoleLink ownerLink = new RoleLink("ownerlink", "owner");
ChangeOwnerPermission changeOwnerPermission = new ChangeOwnerPermission(ownerLink);
HashMap<String, Object> fieldsMap = new HashMap<>();
fieldsMap.put("net_config", null);
Binder modifyDataParams = Binder.of("fields", fieldsMap);
ModifyDataPermission modifyDataPermission = new ModifyDataPermission(ownerLink, modifyDataParams);
contract.addPermission(changeOwnerPermission);
contract.addPermission(modifyDataPermission);
contract.setExpiresAt(ZonedDateTime.now().plusYears(40));
contract.getStateData().set("net_config", netConfig);
contract.addSignerKey(issuerKey);
contract.seal();
return contract;
}
use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class MainTest method registerContractWithAnonymousId.
@Test
public void registerContractWithAnonymousId() throws Exception {
TestSpace ts = prepareTestSpace();
PrivateKey myPrivKey = TestKeys.privateKey(1);
PublicKey myPubKey = myPrivKey.getPublicKey();
byte[] myAnonId = myPrivKey.createAnonymousId();
Contract contract = new Contract();
contract.setExpiresAt(ZonedDateTime.now().plusDays(90));
Role r = contract.setIssuerKeys(AnonymousId.fromBytes(myAnonId));
contract.registerRole(new RoleLink("owner", "issuer"));
contract.registerRole(new RoleLink("creator", "issuer"));
contract.addPermission(new ChangeOwnerPermission(r));
contract.addSignerKey(myPrivKey);
contract.seal();
System.out.println("contract.check(): " + contract.check());
contract.traceErrors();
// ItemResult itemResult = ts.client.register(contract.getPackedTransaction(), 5000);
ItemResult itemResult0 = ts.node.node.registerItem(contract);
// Thread.sleep(1000000000);
ItemResult itemResult = ts.node.node.waitItem(contract.getId(), 100);
assertEquals(ItemState.APPROVED, itemResult.state);
}
use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class MainTest method registerContractWithAnonymousId_bak.
@Test
public void registerContractWithAnonymousId_bak() throws Exception {
TestSpace ts = prepareTestSpace();
PrivateKey myPrivKey = TestKeys.privateKey(1);
PublicKey myPubKey = myPrivKey.getPublicKey();
byte[] myAnonId = myPrivKey.createAnonymousId();
Contract contract = new Contract();
contract.setExpiresAt(ZonedDateTime.now().plusDays(90));
Role r = contract.setIssuerKeys(myPubKey);
contract.registerRole(new RoleLink("owner", "issuer"));
contract.registerRole(new RoleLink("creator", "issuer"));
contract.addPermission(new ChangeOwnerPermission(r));
contract.addSignerKey(myPrivKey);
contract.seal();
ItemResult itemResult = ts.client.register(contract.getPackedTransaction(), 5000);
assertEquals(ItemState.APPROVED, itemResult.state);
}
use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class ParcelTest method setUp.
@Before
public void setUp() throws Exception {
TransactionPack payloadTpFromFile = new TransactionPack();
TransactionPack paymentTpFromFile = new TransactionPack();
TransactionPack payload_tp = new TransactionPack();
TransactionPack payment_tp = new TransactionPack();
// fill in with the values
Contract payload = new Contract();
payload.setIssuerKeys(TestKeys.publicKey(3));
payload.addSignerKey(TestKeys.privateKey(3));
payload.registerRole(new RoleLink("owner", "issuer"));
payload.registerRole(new RoleLink("creator", "issuer"));
payload.setExpiresAt(ZonedDateTime.now().plusDays(2));
payload.seal();
Contract payment = new Contract();
payment.setIssuerKeys(TestKeys.publicKey(3));
payment.addSignerKey(TestKeys.privateKey(3));
payment.registerRole(new RoleLink("owner", "issuer"));
payment.registerRole(new RoleLink("creator", "issuer"));
payment.setExpiresAt(ZonedDateTime.now().plusDays(2));
payment.seal();
payload_tp.setContract(payload);
payment_tp.setContract(payment);
parcel = new Parcel(payload_tp, payment_tp);
PrivateKey privateKey = TestKeys.privateKey(3);
Contract payloadFromFile = Contract.fromDslFile(ROOT_CONTRACT);
payloadFromFile.addSignerKey(privateKey);
payloadFromFile.seal();
Contract paymentFromFile = Contract.fromDslFile(ROOT_CONTRACT);
paymentFromFile.addSignerKey(privateKey);
paymentFromFile.seal();
payloadTpFromFile.setContract(payloadFromFile);
paymentTpFromFile.setContract(paymentFromFile);
parcelFromFile = new Parcel(payloadTpFromFile, paymentTpFromFile);
}
use of com.icodici.universa.contract.roles.RoleLink in project universa by UniversaBlockchain.
the class Node method checkIfContractContainsNetConfig.
private boolean checkIfContractContainsNetConfig(Contract contract) {
if (!contract.getStateData().containsKey("net_config")) {
return false;
}
// check if owner is list role
if (!(contract.getOwner() instanceof ListRole)) {
return false;
}
// TODO: network council criteria here
// check if quorum matches network concil criteria
ListRole owner = (ListRole) contract.getOwner();
if (owner.getQuorum() == 0 || owner.getQuorum() < owner.getRoles().size() - 1) {
return false;
}
// check if owner keys set equals to nodes key set
Object obj = DefaultBiMapper.getInstance().deserializeObject(contract.getStateData().get("net_config"));
if (!(obj instanceof List)) {
return false;
}
List contractNodes = (List) obj;
Set<PublicKey> ownerKeys = contract.getOwner().getKeys();
if (contractNodes.size() != ownerKeys.size() || !contractNodes.stream().allMatch(nodeInfo -> nodeInfo instanceof NodeInfo && ownerKeys.contains(((NodeInfo) nodeInfo).getPublicKey()))) {
return false;
}
for (Permission permission : contract.getPermissions().values()) {
if (permission instanceof ChangeOwnerPermission) {
if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
return false;
}
if (permission instanceof ModifyDataPermission) {
if (((ModifyDataPermission) permission).getFields().containsKey("net_config")) {
if (!(permission.getRole() instanceof RoleLink) || ((RoleLink) permission.getRole()).getRole() != contract.getOwner())
return false;
}
}
}
return true;
}
Aggregations