use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class KeyRecord method setupKey.
private void setupKey() {
try {
Object x = getOrThrow("key");
remove("key");
if (x instanceof PublicKey) {
publicKey = (PublicKey) x;
} else if (x instanceof PrivateKey) {
publicKey = ((PrivateKey) x).getPublicKey();
} else if (x instanceof String) {
publicKey = new PublicKey(Base64u.decodeCompactString((String) x));
} else {
if (x instanceof Bytes)
x = ((Bytes) x).toArray();
if (x instanceof byte[]) {
publicKey = new PublicKey((byte[]) x);
} else {
throw new IllegalArgumentException("unsupported key object: " + x.getClass().getName());
}
}
put("key", publicKey);
} catch (EncryptionError e) {
throw new IllegalArgumentException("unsupported key, failed to construct", e);
}
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class BiMapperTest method processBytes.
@Test
public void processBytes() throws Exception {
byte[] x = Do.randomBytes(10);
Binder s = DefaultBiMapper.serialize(x);
byte[] result = ((Bytes) DefaultBiMapper.deserialize(s)).getData();
assertArrayEquals(x, result);
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class UnikeyFactoryTest method testUnikeyStructure.
@Test
public void testUnikeyStructure() throws Exception {
assertEquals(524, pk1Bytes.length);
assertEquals(3, pk1E.length);
assertEquals(256, pk1P.length);
assertEquals(256, pk1Q.length);
// Private key structure
// It contains 4 components...
final ArrayList unpackedFromBoss = Boss.load(pk1Bytes);
assertEquals(4, unpackedFromBoss.size());
// First one is Integer, other ones are Bytes.
assertTrue(unpackedFromBoss.get(0) instanceof Integer);
assertTrue(unpackedFromBoss.get(1) instanceof Bytes);
assertTrue(unpackedFromBoss.get(2) instanceof Bytes);
assertTrue(unpackedFromBoss.get(3) instanceof Bytes);
// Now their contents...
assertEquals(0, unpackedFromBoss.get(0));
assertArrayEquals(pk1E, ((Bytes) unpackedFromBoss.get(1)).toArray());
assertArrayEquals(pk1P, ((Bytes) unpackedFromBoss.get(2)).toArray());
assertArrayEquals(pk1Q, ((Bytes) unpackedFromBoss.get(3)).toArray());
}
use of net.sergeych.utils.Bytes in project universa by UniversaBlockchain.
the class Boss method traceObject.
private static String traceObject(String prefix, Object obj) {
if (obj instanceof Bytes || obj instanceof byte[]) {
Bytes bb = obj instanceof Bytes ? (Bytes) obj : new Bytes((byte[]) obj);
if (bb.size() > 30)
return prefix + bb.part(0, 30).toHex() + String.format(" ...(%d bytes)\n", bb.size());
return prefix + bb.toHex() + "\n";
}
if (obj instanceof Object[] || obj instanceof List<?>) {
return traceArray(prefix, Do.collection(obj));
}
if (obj instanceof Binder) {
Binder i = (Binder) obj;
StringBuilder b = new StringBuilder();
for (Map.Entry<String, Object> e : i.entrySet()) {
b.append(prefix + e.getKey() + ":\n");
b.append(traceObject(prefix + " ", e.getValue()));
}
return b.toString();
}
return prefix + (obj == null ? "null" : "\"" + obj.toString() + "\"") + "\n";
}
Aggregations