public byte[] sealAsV2() { byte[] theContract = Boss.pack( BossBiMapper.serialize( Binder.of( "contract", this, "revoking", revokingItems.stream() .map(i -> i.getLastSealedBinary()) .collect(Collectors.toList()), "new", newItems.stream() .map(i -> i.seal()) .collect(Collectors.toList()) ) ) ); //redundand code. already executed here newItems.stream().map(i -> i.seal()) //newItems.forEach(c -> c.seal()); Binder result = Binder.of( "type", "unicapsule", "version", 2, "data", theContract ); List<byte[]> signatures = new ArrayList<>(); keysToSignWith.forEach(key -> { signatures.add(ExtendedSignature.sign(key, theContract)); }); result.put("signatures", signatures); setOwnBinary(result); return sealedBinary; }
/** * Seal contract to binary. This call adds signatures from {@link #getKeysToSignWith()} * @return contract's sealed unicapsule */ public byte[] seal() { Object forPack = BossBiMapper.serialize( Binder.of( "contract", this, "revoking", revokingItems.stream() .map(i -> i.getId()) .collect(Collectors.toList()), "new", newItems.stream() .map(i -> i.getId(true)) .collect(Collectors.toList()) ) ); byte[] theContract = Boss.pack( forPack ); List<byte[]> signatures = new ArrayList<>(); Binder result = Binder.of( "type", "unicapsule", "version", 3, "data", theContract, "signatures", signatures ); setOwnBinary(result); addSignatureToSeal(keysToSignWith); return sealedBinary; }
fin.close(); hashBinder = BossBiMapper.serialize(HashId.of(buffer)); } catch (IOException e) { e.printStackTrace();
@Test public void serializeToBinder() throws Exception { Contract c = Contract.fromDslFile(rootPath + "simple_root_contract.yml"); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); // System.out.println(yaml.dump(b)); // System.out.println(yaml.dump(c1.serializeToBinder())); assertProperSimpleRootContract(c1); Contract c2 = c.copy(); assertProperSimpleRootContract(c2); }
@Test public void serializeWithMoreRoles() { SimpleRole s1 = new SimpleRole("s1"); SimpleRole s2 = new SimpleRole("s2"); ListRole lr1 = new ListRole("lr1", ListRole.Mode.ALL, Do.listOf(s1, s2)); ListRole lr2 = new ListRole("lr2", ListRole.Mode.ANY, Do.listOf(s1, s2)); assertEquals(lr1.getRoles(), lr2.getRoles()); Binder blr = BossBiMapper.serialize(lr1); ListRole slr1 = DefaultBiMapper.deserialize(blr); blr = BossBiMapper.serialize(lr1); ListRole slr2 = DefaultBiMapper.deserialize(blr); assertEquals(slr1.getRoles(), slr2.getRoles()); }
@Test public void serializeContractWithListRole() throws Exception { Contract c = Contract.fromDslFile("./src/test_contracts/simple_root_contract.yml"); SimpleRole s1 = new SimpleRole("role"); ListRole listRole = new ListRole("owner", 1, Do.listOf(s1)); c.registerRole(listRole); Binder b = BossBiMapper.serialize(c); Contract c1 = DefaultBiMapper.deserialize(b); assertEquals(c.getRole("owner"), c1.getRole("owner")); }
"ReferencedConditions_contract2.yml - description"); Contract notaryDeserialized = DefaultBiMapper.deserialize(BossBiMapper.serialize(notaryContract)); assertTrue(notaryContract.getDefinition().getData().equals(notaryDeserialized.getDefinition().getData()));
assertEquals(files.getBinder("coin100_yml").getString("file_description"), "coin100.yml - description"); Contract notaryDeserialized = DefaultBiMapper.deserialize(BossBiMapper.serialize(notaryContract)); assertTrue(notaryContract.getDefinition().getData().equals(notaryDeserialized.getDefinition().getData()));
"ReferencedConditions_contract2.yml - description"); Contract notaryDeserialized = DefaultBiMapper.deserialize(BossBiMapper.serialize(notaryContract)); assertTrue(notaryContract.getDefinition().getData().equals(notaryDeserialized.getDefinition().getData()));
/** * Check serialization and deserialization contract with references * @throws Exception */ @Test public void checkReferenceSerialization() throws Exception { Contract contract = Contract.fromDslFile(rootPath + "simple_root_contract_with_references.yml"); assertEquals(contract.findReferenceByName("ref_cont").getComment(), "ref_comment"); contract.findReferenceByName("ref_cont3").setComment("ref_comment3"); contract.seal(); Binder b = BossBiMapper.serialize(contract); Contract desContract = DefaultBiMapper.deserialize(b); assertEquals(desContract.findReferenceByName("ref_cont").getComment(), "ref_comment"); assertEquals(desContract.findReferenceByName("ref_cont3").getComment(), "ref_comment3"); assertNull(desContract.findReferenceByName("ref_cont2").getComment()); for (Reference ref: contract.getReferences().values()) { Reference desRef = desContract.findReferenceByName(ref.getName()); assertTrue(desRef != null); assertEquals(ref.getConditions(), desRef.getConditions()); } }
assertTrue(smartContract.isOk()); Binder b = BossBiMapper.serialize(smartContract);
assertTrue(smartContract.isOk()); Binder b = BossBiMapper.serialize(smartContract); Contract desContract = DefaultBiMapper.deserialize(b); assertSameContracts(smartContract, desContract);
assertTrue(uns.isOk()); Binder b = BossBiMapper.serialize(uns); Contract desUns = DefaultBiMapper.deserialize(b);
@Test public void serializeSmartContract() throws Exception { final PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey")); Contract smartContract = new NSmartContract(key); smartContract.seal(); smartContract.check(); smartContract.traceErrors(); assertTrue(smartContract.isOk()); Binder b = BossBiMapper.serialize(smartContract); Contract desContract = DefaultBiMapper.deserialize(b); assertSameContracts(smartContract, desContract); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), desContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), desContract.get("definition.extended_type")); assertTrue(desContract instanceof NSmartContract); assertTrue(desContract instanceof NContract); Contract copiedContract = smartContract.copy(); assertSameContracts(smartContract, copiedContract); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), copiedContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), copiedContract.get("definition.extended_type")); assertTrue(copiedContract instanceof NSmartContract); assertTrue(copiedContract instanceof NContract); } }
@Test public void serializeNSmartContract() throws Exception { final PrivateKey key = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey")); Contract smartContract = new NSmartContract(key); smartContract.seal(); smartContract.check(); smartContract.traceErrors(); assertTrue(smartContract.isOk()); Binder b = BossBiMapper.serialize(smartContract); Contract desContract = DefaultBiMapper.deserialize(b); assertSameContracts(smartContract, desContract); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), desContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), desContract.get("definition.extended_type")); assertTrue(desContract instanceof NSmartContract); assertTrue(smartContract instanceof NContract); Contract copiedContract = smartContract.copy(); assertSameContracts(smartContract, copiedContract); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), copiedContract.getDefinition().getExtendedType()); assertEquals(NSmartContract.SmartContractType.N_SMART_CONTRACT.name(), copiedContract.get("definition.extended_type")); assertTrue(copiedContract instanceof NSmartContract); assertTrue(smartContract instanceof NContract); } }