/** * Create new revision to be changed, signed sealed and then ready to approve. Created "revision" contract is a copy * of this contract, with all fields and references correctly set. After this call one need to change mutable * fields, add signing keys, seal it and then apss to Universa network for approval. * * @param keys initially added and signer keys. Role "creator" is set to these keys for new revision * @param transactional is {@link Transactional} section to create new revision with * @return new revision of this contract, identical to this one, to be modified. */ public synchronized Contract createRevision(Collection<PrivateKey> keys, Transactional transactional) { Contract newRevision = createRevision(transactional); Set<KeyRecord> krs = new HashSet<>(); keys.forEach(k -> { krs.add(new KeyRecord(k.getPublicKey())); newRevision.addSignerKey(k); }); newRevision.setCreator(krs); return newRevision; }
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 + ": "+(x.getClass().getName())); if (anonId != null) anonymousIds.add(anonId); else if (kr != null) keyRecords.put(kr.getPublicKey(), kr); }); }
private void setupKey() { try { Object x = getOrThrow("key"); remove("key"); if (x instanceof PublicKey) { publicKey = (PublicKey) x; } else if (x instanceof PrivateKey) { publicKey = ((PrivateKey) x).getPublicKey(); } else if (x instanceof String) { publicKey = new PublicKey(Base64u.decodeCompactString((String) x)); } else { if (x instanceof Bytes) x = ((Bytes) x).toArray(); if (x instanceof byte[]) { publicKey = new PublicKey((byte[]) x); } else { throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName()); } } put("key", publicKey); } catch (EncryptionError e) { throw new IllegalArgumentException("unsupported key, failed to construct", e); } }
/** * Adds {@link KeyRecord} to role. * * @param keyRecord is {@link KeyRecord} */ public void addKeyRecord(KeyRecord keyRecord) { keyRecords.put(keyRecord.getPublicKey(), keyRecord); }
assertThat(issuer.getPublicKey(), is(instanceOf(PublicKey.class))); assertEquals(issuer.getStringOrThrow("name"), "Universa"); KeyRecord owner = c.testGetOwner(); assertNotNull(owner); assertEquals("Pupkin", owner.getBinderOrThrow("name").getStringOrThrow("last"));
public KeyRecord(PublicKey key) { this.publicKey = key; put("key", key); }
/** * Create new {@link SimpleRole} and add one {@link KeyRecord} associated with role. * * @param name is name of role * @param keyRecord is {@link KeyRecord} associated with role */ public SimpleRole(String name, @NonNull KeyRecord keyRecord) { super(name); keyRecords.put(keyRecord.getPublicKey(), keyRecord); }
public void setPublicKey(PublicKey publicKey) { put("key", publicKey); this.publicKey = publicKey; }
@Test public void serializeNone() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(r1.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertTrue(r1.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); }
/** * Checks if permission of given type exists and is allowed for given key record * @param permissionName type of permission to check for * @param keyRecord key record to check permission with * @return permission allowed for keyRecord is found * @throws Quantiser.QuantiserException if quantas limit was reached during check */ public boolean isPermitted(String permissionName, KeyRecord keyRecord) throws Quantiser.QuantiserException { return isPermitted(permissionName, keyRecord.getPublicKey()); }
@Test public void serializeMany() throws Exception { SimpleRole sr = new SimpleRole("tr1"); keys.forEach(k-> sr.addKeyRecord(new KeyRecord(k.getPublicKey()))); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); Set<PublicKey> kk = ((SimpleRole)r1).getSimpleKeys(); keys.forEach(k->assertTrue(kk.contains(k.getPublicKey()))); }
/** * Check role is allowed to keys * * @param keys is set of keys * @return true if role is allowed to keys */ @Override public boolean isAllowedForKeys(Set<? extends AbstractKey> keys) { if(!super.isAllowedForKeys(keys)) { return false; } boolean allMatch1 = anonymousIds.stream().allMatch(anonId -> keys.stream().anyMatch(key -> { try { return key.matchAnonymousId(anonId.getBytes()); } catch (IOException e) { return false; } })); boolean allMatch2 = keyRecords.values().stream().allMatch( kr -> keys.stream().anyMatch(k -> k.getPublicKey().equals(kr.getPublicKey()))); boolean allMatch3 = keyAddresses.stream().allMatch(address -> keys.stream().anyMatch(key -> key.isMatchingKeyAddress(address))); return allMatch1 && allMatch2 && allMatch3; }
@Test public void serializeAny() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ANY_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ALL_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ANY_OF).iterator().next(),"ref"); }
KeyRecord checkingIssuer = checkingContract.getIssuer().getKeyRecords().iterator().next(); assertNotNull(checkingIssuer); assertThat(checkingIssuer.getPublicKey(), is(instanceOf(PublicKey.class))); assertEquals(checkingIssuer, originIssuer); KeyRecord checkingCreator = checkingContract.getCreator().getKeyRecords().iterator().next(); assertNotNull(checkingCreator); assertThat(checkingCreator.getPublicKey(), is(instanceOf(PublicKey.class))); assertEquals(checkingCreator, originCreator); KeyRecord checkingOwner = checkingContract.getOwner().getKeyRecords().iterator().next(); assertNotNull(checkingOwner); assertThat(checkingOwner.getPublicKey(), is(instanceOf(PublicKey.class))); assertEquals(checkingOwner, originOwner);
@Test public void serializeAll() throws Exception { SimpleRole sr = new SimpleRole("tr1"); sr.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); sr.addRequiredReference("ref", Role.RequiredMode.ALL_OF); Binder serialized = DefaultBiMapper.serialize(sr); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(sr, r1); assertTrue(sr.getReferences(Role.RequiredMode.ANY_OF).isEmpty()); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).size(),1); assertEquals(sr.getReferences(Role.RequiredMode.ALL_OF).iterator().next(),"ref"); }
@Test public void probeFile() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); c.addSignerKeyFromFile(rootPath+"_xer0yfe2nn1xthc.private.unikey"); c.addSignerKeyFromFile(rootPath + "keys/u_key.private.unikey"); PrivateKey goodKey = c.getKeysToSignWith().iterator().next(); // let's make this key among owners ((SimpleRole)c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey())); c.seal(); Files.write(Paths.get(basePath + "probeFile.unicon"),c.getPackedTransaction(), StandardOpenOption.CREATE, StandardOpenOption.WRITE); System.out.println("---"); System.out.println("register contract"); System.out.println("---"); CLIMain.registerContract(c); Thread.sleep(1500); System.out.println("---"); System.out.println("check contract"); System.out.println("---"); callMain("--probe-file", basePath + "probeFile.unicon"); System.out.println(output); assertTrue (output.indexOf(ItemState.APPROVED.name()) >= 0); }
@Test public void serializeQuorum() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); SimpleRole s3 = new SimpleRole("owner3"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); s3.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", 2, Do.listOf(s1, s2, s3)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
krs.add(new KeyRecord(k));
PrivateKey goodKey = c.getKeysToSignWith().iterator().next(); ((SimpleRole)c.getRole("owner")).addKeyRecord(new KeyRecord(goodKey.getPublicKey())); c.seal();