Search in sources :

Example 6 with TransactionPack

use of com.icodici.universa.contract.TransactionPack in project universa by UniversaBlockchain.

the class CLIMainTest method testExportImportWithAddresses.

@Test
public void testExportImportWithAddresses() throws Exception {
    callMain2("-create", rootPath + "simple_root_contract_v2.yml", "-name", basePath + "contractWithAddresses.unicon", "-k", rootPath + "_xer0yfe2nn1xthc.private.unikey");
    Contract contract = CLIMain.loadContract(basePath + "contractWithAddresses.unicon", true);
    Set<KeyAddress> keyAddresses = new HashSet<>();
    keyAddresses.add(new KeyAddress(TestKeys.publicKey(0), 0, true));
    SimpleRole sr1 = new SimpleRole("owner", keyAddresses);
    contract.registerRole(sr1);
    contract.addSignerKey(TestKeys.privateKey(0));
    contract.seal();
    CLIMain.saveContract(contract, basePath + "contractWithAddresses.unicon");
    callMain("-e", basePath + "contractWithAddresses.unicon", "-name", basePath + "contractWithAddresses.json");
    System.out.println(output);
    assertTrue(output.indexOf("export as json ok") >= 0);
    assertEquals(0, errors.size());
    callMain("-i", basePath + "contractWithAddresses.json", "-name", basePath + "contractWithAddressesImported.unicon");
    System.out.println(output);
    assertTrue(output.indexOf("import from json ok") >= 0);
    assertEquals(1, errors.size());
    if (errors.size() > 0) {
        assertEquals(Errors.NOT_SIGNED.name(), errors.get(0).get("code"));
    }
    Contract contractImported = CLIMain.loadContract(basePath + "contractWithAddressesImported.unicon", true);
    assertTrue(contractImported.getOwner().getKeyAddresses().iterator().next().isMatchingKey(TestKeys.privateKey(0).getPublicKey()));
    PrivateKey creatorPrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    contractImported.addSignatureToSeal(creatorPrivateKey);
    contractImported.addSignatureToSeal(TestKeys.privateKey(0));
    assertTrue(contractImported.check());
    Set<PrivateKey> signKeys = new HashSet<>();
    signKeys.add(creatorPrivateKey);
    signKeys.add(TestKeys.privateKey(0));
    contractImported.setKeysToSignWith(signKeys);
    byte[] sealedContract = contractImported.sealAsV2();
    TransactionPack tp = new TransactionPack();
    tp.addKeys(creatorPrivateKey.getPublicKey());
    tp.addKeys(TestKeys.privateKey(0).getPublicKey());
    Contract restoredContract = new Contract(sealedContract, tp);
    assertTrue(restoredContract.check());
}
Also used : PrivateKey(com.icodici.crypto.PrivateKey) SimpleRole(com.icodici.universa.contract.roles.SimpleRole) KeyAddress(com.icodici.crypto.KeyAddress) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 7 with TransactionPack

use of com.icodici.universa.contract.TransactionPack in project universa by UniversaBlockchain.

the class WalletTest method shouldTransferAndSplitRest.

@Test
public void shouldTransferAndSplitRest() throws Exception {
    Decimal valueToSend = new Decimal(15);
    List<Contract> listOfCoinsWithAmount = createListOfCoinsWithAmount(Arrays.asList(1, 2, 3, 5, 8));
    List<Wallet> wallets = Wallet.determineWallets(listOfCoinsWithAmount);
    assertEquals(1, wallets.size());
    assertEquals(5, wallets.get(0).getContracts().size());
    Wallet wallet = wallets.get(0);
    Contract contract = wallet.buildContractWithValue(FIELD_NAME, valueToSend);
    contract.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    assertEquals(1, wallet.getContracts().size());
    sealCheckTrace(contract, true);
    Contract restContract = wallet.getContracts().get(0);
    assertEquals(4, restContract.getStateData().getIntOrThrow(FIELD_NAME));
    sealCheckTrace(restContract, true);
    TransactionPack tp = contract.getTransactionPack();
    Contract contractToSend = new Contract(contract.seal(), tp);
    contractToSend.addSignerKeyFromFile(PRIVATE_KEY_PATH);
    sealCheckTrace(contractToSend, true);
    assertEquals(valueToSend.intValue(), contractToSend.getStateData().getIntOrThrow(FIELD_NAME));
}
Also used : Decimal(com.icodici.universa.Decimal) Contract(com.icodici.universa.contract.Contract) TransactionPack(com.icodici.universa.contract.TransactionPack) Test(org.junit.Test)

Example 8 with TransactionPack

use of com.icodici.universa.contract.TransactionPack in project universa by UniversaBlockchain.

the class NetworkV2 method getItem.

// private final Map<NodeInfo,BasicHttpClient> httpClients = new HashMap<>();
@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);
        // 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;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) IOException(java.io.IOException) TransactionPack(com.icodici.universa.contract.TransactionPack)

Aggregations

TransactionPack (com.icodici.universa.contract.TransactionPack)8 Contract (com.icodici.universa.contract.Contract)6 Test (org.junit.Test)6 Decimal (com.icodici.universa.Decimal)5 KeyAddress (com.icodici.crypto.KeyAddress)1 PrivateKey (com.icodici.crypto.PrivateKey)1 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)1 Quantiser (com.icodici.universa.node2.Quantiser)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 BackingStoreException (java.util.prefs.BackingStoreException)1 OptionException (joptsimple.OptionException)1 Binder (net.sergeych.tools.Binder)1