Search in sources :

Example 1 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class TestBoss method testStringsAndBinaries.

@Test
public void testStringsAndBinaries() {
    assertEquals("Hello", Boss.load(Bytes.fromHex("2B 48 65 6C 6C 6F")));
    assertEquals("2B 48 65 6C 6C 6F", Boss.dump("Hello").toHex());
    Bytes bb = Bytes.fromHex("00 01 02 03 04 05");
    Bytes rr = (Bytes) Boss.load(Bytes.fromHex("34 00 01 02 03 04 05"));
    assertEquals(bb.toHex(), rr.toHex());
    Bytes encoded = Boss.dump(rr);
    assertEquals("34 00 01 02 03 04 05", encoded.toHex());
    byte[] ba = new byte[] { 0, 1, 2, 3, 4, 5 };
    assertEquals("34 00 01 02 03 04 05", Boss.dump(ba).toHex());
    // Should pach utf8
    assertEquals("Абвгд", Boss.load(Boss.dump("Абвгд")));
}
Also used : Bytes(net.sergeych.utils.Bytes) Test(org.junit.Test)

Example 2 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class TestBoss method testCache.

@Test
public void testCache() {
    List<Byte> res = Boss.load(Bytes.fromHex("36 2B 48 65 6C 6C 6F 2B 57 6F 72 6C 64 15 15 15 1D"));
    Object[] source = new Object[] { "Hello", "World", "Hello", "Hello", "Hello", "World" };
    assertArrayEquals(source, res.toArray());
    assertEquals("36 2B 48 65 6C 6C 6F 2B 57 6F 72 6C 64 15 15 15 1D", Boss.dump(source).toHex());
    List sub = asList("Hello", 1, 2);
    List a = asList(10, null, sub, "Hello", null, sub, sub);
    Bytes packed = Boss.dump(a);
    assertEquals("3E 50 05 1E 2B 48 65 6C 6C 6F 08 10 1D 05 15 15", packed.toHex());
    assertEquals(a, Boss.load(packed));
    @SuppressWarnings("unchecked") Map<String, String> smap = (Map<String, String>) Boss.load(Bytes.fromHex("0F 2B 68 65 6C 6C 6F 2B 77 6F 72 6C 64"));
    assertEquals(1, smap.size());
    assertEquals("world", smap.get("hello"));
    assertEquals("0F 2B 68 65 6C 6C 6F 2B 77 6F 72 6C 64", Boss.dump(smap).toHex());
}
Also used : Bytes(net.sergeych.utils.Bytes) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class TestBoss method testBytes.

@Test
public void testBytes() {
    Bytes src = Bytes.fromHex("01 02 03 04 05 06 07 08 09 10");
    Bytes p1 = src.part(-3);
    assertEquals("08 09 10", p1.toHex());
    p1 = src.part(8);
    assertEquals("09 10", p1.toHex());
    try {
        p1 = src.part(-20);
        fail("Exception must be thrown");
    } catch (IndexOutOfBoundsException e) {
    }
    assertEquals("02 03", src.part(1, 2).toHex());
    assertEquals("09 10", src.part(-2, 20).toHex());
    assertEquals("08 09", src.part(-3, 2).toHex());
}
Also used : Bytes(net.sergeych.utils.Bytes) Test(org.junit.Test)

Example 4 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class TestBoss method emptyStrings.

@Test
public void emptyStrings() throws Exception {
    Object xo = JsonTool.fromJson("{\"text\":\"\",\"type\":\"p2pchat\",\"from_party_id\":\"1271\"}");
    Binder b = Binder.from(xo);
    Bytes bb = Boss.dump(b);
    String x = (String) Boss.unpack(bb.toArray()).getStringOrThrow("text");
    assertEquals("", x);
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) Test(org.junit.Test)

Example 5 with Bytes

use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.

the class ContractTest method checkSealedBytes.

@Test
public void checkSealedBytes() throws Exception {
    PrivateKey martyPrivateKey = new PrivateKey(Do.read(rootPath + "keys/marty_mcfly.private.unikey"));
    PrivateKey stepaPrivateKey = new PrivateKey(Do.read(rootPath + "keys/stepan_mamontov.private.unikey"));
    PrivateKey manufacturePrivateKey = new PrivateKey(Do.read(rootPath + "_xer0yfe2nn1xthc.private.unikey"));
    Contract delorean = Contract.fromDslFile(rootPath + "DeLoreanOwnership.yml");
    delorean.addSignerKey(manufacturePrivateKey);
    delorean.seal();
    System.out.println("----");
    delorean = delorean.createRevision(manufacturePrivateKey);
    delorean.addSignerKey(manufacturePrivateKey);
    delorean.seal();
    byte[] firstSeal = delorean.getLastSealedBinary();
    System.out.println("--delorean 1--");
    System.out.println(delorean.getRevoking().size());
    TransactionPack tp_before = delorean.getTransactionPack();
    byte[] data = tp_before.pack();
    System.out.println("----");
    TransactionPack tp_after = TransactionPack.unpack(data);
    Contract delorean2 = tp_after.getContract();
    System.out.println("----");
    delorean2.addSignerKey(stepaPrivateKey);
    delorean2.seal();
    System.out.println("--delorean 2--");
    System.out.println(delorean2.getRevoking().size());
    byte[] secondSeal = delorean2.getLastSealedBinary();
    Binder data1 = Boss.unpack(firstSeal);
    byte[] contractBytes1 = data1.getBinaryOrThrow("data");
    for (Object signature : (List) data1.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println(ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes1));
    }
    System.out.println("----");
    Binder data2 = Boss.unpack(secondSeal);
    byte[] contractBytes2 = data2.getBinaryOrThrow("data");
    for (Object signature : (List) data2.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println("m: " + ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes2));
        System.out.println("s: " + ExtendedSignature.verify(stepaPrivateKey.getPublicKey(), s, contractBytes2));
    }
    System.out.println("----");
    for (Object signature : (List) data1.getOrThrow("signatures")) {
        byte[] s = ((Bytes) signature).toArray();
        System.out.println(ExtendedSignature.verify(manufacturePrivateKey.getPublicKey(), s, contractBytes2));
    }
}
Also used : Binder(net.sergeych.tools.Binder) Bytes(net.sergeych.utils.Bytes) PrivateKey(com.icodici.crypto.PrivateKey) Test(org.junit.Test)

Aggregations

Bytes (net.sergeych.utils.Bytes)14 Binder (net.sergeych.tools.Binder)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 PrivateKey (com.icodici.crypto.PrivateKey)2 PublicKey (com.icodici.crypto.PublicKey)2 EncryptionError (com.icodici.crypto.EncryptionError)1 Sha3_384 (com.icodici.crypto.digest.Sha3_384)1 Sha512 (com.icodici.crypto.digest.Sha512)1 HashId (com.icodici.universa.HashId)1 Quantiser (com.icodici.universa.node2.Quantiser)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 SecureRandom (java.security.SecureRandom)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1