/** * Unpack either old contract binary (all included), or newer transaction pack. Could be used to load old contracts * to perform a transaction. * * @param packOrContractBytes binary that was packed by {@link TransactionPack#pack()} * * @return transaction, either unpacked or reconstructed from the self-contained v2 contract * @throws IOException if something went wrong */ public static TransactionPack unpack(byte[] packOrContractBytes) throws IOException { return unpack(packOrContractBytes, true); }
/** * Main .unicon read routine. Load any .unicon version and construct a linked Contract with counterparts (new and * revoking items if present) and corresponding {@link TransactionPack} instance to pack it to store or send to * approval. * <p> * The supported file variants are: * <p> * - v2 legacy unicon. Is loaded with packed counterparts if any. Only for compatibility, avoid using it. * <p> * - v3 compacted unicon. Is loaded without counterparts, should be added later if need with {@link * #addNewItems(Contract...)} and {@link #addRevokingItems(Contract...)}. This is a good way to keep the long * contract chain. * <p> * - packed {@link TransactionPack}. This is a preferred way to keep current contract state. * <p> * To pack and write corresponding .unicon file use {@link #getPackedTransaction()}. * * @param packedItem some packed from of the universa contract * @return unpacked {@link Contract} * @throws IOException if the packedItem is broken */ public static Contract fromPackedTransaction(@NonNull byte[] packedItem) throws IOException { TransactionPack tp = TransactionPack.unpack(packedItem); return tp.getContract(); }
@Override public Approvable getItem(HashId itemId, NodeInfo nodeInfo, Duration maxTimeout) throws InterruptedException { try { // URL url = new URL("http://localhost:8080/contracts/" + itemId.toBase64String()); URL url = new URL(nodeInfo.publicUrlString() + "/contracts/" + itemId.toBase64String()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("User-Agent", "Universa JAVA API Client"); connection.setRequestProperty("Connection", "close"); connection.setRequestMethod("GET"); if (200 != connection.getResponseCode()) return null; byte[] data = Do.read(connection.getInputStream()); TransactionPack tp = TransactionPack.unpack(data, true); // tp.trace(); // Contract c = Contract.fromPackedTransaction(data); return tp.getContract(); } catch (Exception e) { report(getLabel(), "download failure. from: " + nodeInfo.getNumber() + " by: " + myInfo.getNumber() +" reason: " + e, DatagramAdapter.VerboseLevel.BASE); e.printStackTrace(); return null; } }
private static void checkFile(File f) { try { TransactionPack tp = TransactionPack.unpack(Do.read(f), true); if (tp.isReconstructed()) { report("file " + f + " is a single contract"); } else { report("file " + f + " is a transaction pack"); } System.out.println(); checkContract(tp.getContract()); } catch (Quantiser.QuantiserException e) { addError("QUANTIZER_COST_LIMIT", f.getPath(), e.toString()); } catch (IOException e) { addError("READ_ERROR", f.getPath(), e.toString()); } catch (Exception e) { addError("UNKNOWN_ERROR", f.getPath(), e.toString()); } }
@Test public void deserializeOldContract() throws Exception { TransactionPack tp = TransactionPack.unpack(c.sealAsV2()); checkSimplePack(tp); }
@Override synchronized public Approvable getItem(HashId itemId, NodeInfo nodeInfo, Duration maxTimeout) throws InterruptedException { Node node = nodes.get(nodeInfo); Approvable item = node.getItem(itemId); if(item instanceof Contract) { TransactionPack tp_before = ((Contract) item).getTransactionPack(); byte[] data = tp_before.pack(); // here we "send" data and "got" it TransactionPack tp_after = null; try { tp_after = TransactionPack.unpack(data); Contract gotMainContract = tp_after.getContract(); return gotMainContract; } catch (IOException e) { e.printStackTrace(); } } return item; }
/** * Imitate of sending contract from one part of swappers to another. * * Method packs sending contracts with main swap contract (can be blank - doesn't matter) into TransactionPack. * Then restore from packed binary main swap contract, contracts sending with. * * @param mainContract * @return * @throws Exception */ public synchronized Contract imitateSendingTransactionToPartner(Contract mainContract) throws Exception { TransactionPack tp_before = mainContract.getTransactionPack(); byte[] data = tp_before.pack(); // here we "send" data and "got" it TransactionPack tp_after = TransactionPack.unpack(data); Contract gotMainContract = tp_after.getContract(); return gotMainContract; }
assertTrue (new File(basePath + "origin_1.unicon").exists()); Contract main = TransactionPack.unpack(Files.readAllBytes(Paths.get(basePath + "origin_main.unicon"))).getContract(); main.check(); assertTrue(main.isOk()); Contract part1 = TransactionPack.unpack(Files.readAllBytes(Paths.get(basePath + "origin_0.unicon"))).getContract(); Contract part2 = TransactionPack.unpack(Files.readAllBytes(Paths.get(basePath + "origin_1.unicon"))).getContract(); HashSet<PublicKey> part1Keys = new HashSet<>(); part1Keys.add(splitJoinKey1.getPublicKey()); assertTrue (new File(basePath + "join_good.unicon").exists()); Contract joinBad = TransactionPack.unpack(Files.readAllBytes(Paths.get(basePath + "join_bad.unicon"))).getContract(); joinBad.check(); assertFalse(joinBad.isOk()); Contract joinGood = TransactionPack.unpack(Files.readAllBytes(Paths.get(basePath + "join_good.unicon"))).getContract(); assertTrue(joinGood.getOwner().isAllowedForKeys(part1Keys)); assertEquals(joinGood.getStateData().getString("amount"),"1300.5");
TransactionPack tp1 = TransactionPack.unpack(packedTp); checkSimplePack(tp1);
@Test public void packAndUnpackWithKeys() throws Exception { TransactionPack tp = new TransactionPack(); tp.setContract(c); // tp.addKeys(publicKey); checkPackWithKeys(tp); assertSame(tp,c.getTransactionPack()); byte[] packedTp = tp.pack(); TransactionPack tp1 = TransactionPack.unpack(packedTp); checkPackWithKeys(tp1); }
byte[] data = tp_before.pack(); System.out.println("----"); TransactionPack tp_after = TransactionPack.unpack(data); Contract delorean2 = tp_after.getContract();
tp_before.addReferencedItem(jobCertificate); byte[] data = tp_before.pack(); TransactionPack tp_after = TransactionPack.unpack(data);
TransactionPack tp2 = TransactionPack.unpack(new TransactionPack(c1).pack());
assertEquals(simpleContract.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId());
assertEquals(simpleContract.getId(), TransactionPack.unpack(((SlotContract) smartContract).getPackedTrackingContract()).getContract().getId());