@Override public KeyInfo info() { if (keyInfo == null) { KeyInfo i = getPublicKey().info(); keyInfo = new KeyInfo(KeyInfo.Algorythm.RSAPrivate, i.getTag(), privateKey .getBitStrength() / 8); } return super.info(); }
@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()))); }
static Contract createSimpleContract(PrivateKey key) { Contract result = new Contract(); // default expiration date result.setExpiresAt(ZonedDateTime.now().plusDays(90)); // issuer role is a key for a new contract result.setIssuerKeys(key.getPublicKey().getShortAddress()); // issuer is owner, link roles result.registerRole(new RoleLink("owner", "issuer")); result.registerRole(new RoleLink("creator", "issuer")); return result; }
@Test public void addWhiteListAddressTest() { Config config = new Config(); PrivateKey randomKey = new PrivateKey(4096); KeyAddress keyAddr = new KeyAddress(randomKey.getPublicKey(), 0, true); config.getAddressesWhiteList().add(keyAddr); assertTrue(config.getAddressesWhiteList().contains(keyAddr)); } }
@Test public void testCloneAsAddressRole() throws Exception { Set<KeyAddress> keyAddresses = new HashSet<>(); keyAddresses.add(new KeyAddress(keys.get(0).getPublicKey(), 0, true)); SimpleRole sr = new SimpleRole("tr1", keyAddresses); SimpleRole r1 = sr.cloneAs("tr2"); SimpleRole r2 = r1.cloneAs("tr1"); assertEquals(sr, r2); } }
@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()); }
public synchronized Parcel createParcelWithFreshU(Contract c, Set<PrivateKey> keys) throws Exception { Set<PublicKey> ownerKeys = new HashSet(); keys.stream().forEach(key -> ownerKeys.add(key.getPublicKey())); Contract stepaU = InnerContractsService.createFreshU(100000000, ownerKeys); stepaU.check(); //stepaU.setIsU(true); stepaU.traceErrors(); node.registerItem(stepaU); ItemResult itemResult = node.waitItem(stepaU.getId(), 18000); assertEquals(ItemState.APPROVED, itemResult.state); return ContractsService.createParcel(c, stepaU, 150, keys); }
@Test public void shouldPerformRoleWithAnyMode() { Contract c = new Contract(); SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(1).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); c.registerRole(s1); c.registerRole(s2); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertTrue(roleList.isAllowedForKeys(new HashSet<>(Do.listOf(keys.get(1).getPublicKey())))); }
@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"); }
@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 serializeAny() throws Exception { SimpleRole s1 = new SimpleRole("owner"); SimpleRole s2 = new SimpleRole("owner2"); s1.addKeyRecord(new KeyRecord(keys.get(0).getPublicKey())); s2.addKeyRecord(new KeyRecord(keys.get(2).getPublicKey())); ListRole roleList = new ListRole("listAnyMode", ListRole.Mode.ANY, Do.listOf(s1, s2)); Binder serialized = DefaultBiMapper.serialize(roleList); Role r1 = DefaultBiMapper.deserialize(serialized); assertEquals(r1, roleList); }
public Contract createUnsPayment() throws IOException { PrivateKey ownerKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")); Set<PublicKey> keys = new HashSet(); keys.add(ownerKey.getPublicKey()); Contract stepaU = InnerContractsService.createFreshU(100000000, keys); Contract paymentDecreased = stepaU.createRevision(ownerKey); paymentDecreased.getStateData().set("transaction_units", stepaU.getStateData().getIntOrThrow("transaction_units") - 2000); paymentDecreased.seal(); return paymentDecreased; } }
public Contract createSlotPayment() throws IOException { PrivateKey ownerKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey")); Set<PublicKey> keys = new HashSet(); keys.add(ownerKey.getPublicKey()); Contract stepaU = InnerContractsService.createFreshU(100000000, keys); Contract paymentDecreased = stepaU.createRevision(ownerKey); paymentDecreased.getStateData().set("transaction_units", stepaU.getStateData().getIntOrThrow("transaction_units") - 100); paymentDecreased.seal(); return paymentDecreased; } }
@Test public void goodRevoke() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); c.addSignerKeyFromFile(rootPath+"_xer0yfe2nn1xthc.private.unikey"); PrivateKey goodKey = c.getKeysToSignWith().iterator().next(); c.setOwnerKeys(new KeyRecord(goodKey.getPublicKey())); c.seal(); Contract revokeContract = c.createRevocation(goodKey); revokeContract.check(); assertTrue(revokeContract.isOk()); // tc.traceErrors(); }
@Test public void sign() throws Exception { byte[] data = "Hello world".getBytes(); PrivateKey k = TestKeys.privateKey(3); byte [] signature = ExtendedSignature.sign(k, data); PublicKey pubKey = k.getPublicKey(); ExtendedSignature es = ExtendedSignature.verify(pubKey, signature, data); assertNotNull(es); assertAlmostSame(es.getCreatedAt(), ZonedDateTime.now()); assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.keyId(pubKey)); assertEquals(ExtendedSignature.keyId(k), ExtendedSignature.extractKeyId(signature)); }
@Test public void fingerprint() throws Exception { PrivateKey k = TestKeys.privateKey(0); byte[] f1 = k.getPublicKey().fingerprint(); assertEquals(33, f1.length); byte[] f2 = k.fingerprint(); assertArrayEquals(f1, f2); assertEquals(AbstractKey.FINGERPRINT_SHA256, f1[0]); }
@Test public void testError() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); assertThrows(CommandFailedException.class, ()->c.command("test_error")); }
@Test public void shouldNotSplitWithAnotherIssuerSerialize() throws Exception { Contract c = createCoin(); c.addSignerKeyFromFile(PRIVATE_KEY_PATH); sealCheckTrace(c, true); Contract c2 = c.splitValue(FIELD_NAME, new Decimal(50)); c2.setIssuerKeys(ownerKey1.getPublicKey()); sealCheckTrace(c2, false); }
@Test public void publicKeyMustHaveInfo() throws Exception { AbstractKey k = TestKeys.privateKey(3).getPublicKey(); KeyInfo h = k.info(); assertEquals(KeyInfo.Algorythm.RSAPublic, h.getAlgorythm()); assertEquals(KeyInfo.PRF.None, h.getPRF()); assertEquals(5, h.getTag().length); // Bytes.dump(h.pack()); }
@Test public void unpackKey() throws Exception { AbstractKey k1 = TestKeys.privateKey(3).getPublicKey(); AbstractKey kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); k1 = SymmetricKey.fromPassword("helluva", 4096); kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); k1 = TestKeys.privateKey(2); kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); }