use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class CapsuleTest method testPartiallySigned.
@Test
public void testPartiallySigned() throws Exception {
Capsule c1 = new Capsule();
c1.setPublicData("hello", "world", "I'm", "the coffer");
PrivateKey k1 = TestKeys.privateKey(0);
PrivateKey k2 = TestKeys.privateKey(1);
c1.addSigners(k1, k2);
// Let's remove one signature
byte[] packed = c1.pack();
Binder b = Boss.unpack(packed);
ArrayList<Binder> ss = b.getBinders("signatures");
ss.remove(0);
b.put("signatures", ss);
packed = Boss.pack(b);
// Now it is only partially signed
Capsule c3 = new Capsule(packed, null, true, false);
assertFalse(c3.isSigned());
assertTrue(c3.isPartiallySigned());
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class PublicKey method unpack.
public void unpack(byte[] bytes, KeyInfo info) throws EncryptionError {
List parts = Boss.load(bytes);
if ((Integer) parts.get(0) != 1)
throw new EncryptionError("invalid packed public key");
try {
// e, n
Binder pp = new Binder("e", ((Bytes) parts.get(1)).toArray(), "n", ((Bytes) parts.get(2)).toArray());
setComponents(pp);
} catch (Exception error) {
error.printStackTrace();
throw new EncryptionError("failed to parse public key", error);
}
if (info == null)
setupInfo(bytes);
else
keyInfo = info;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Capsule method addSigner.
/**
* Add single signer key with associated data. Capsule format lets add any extra data to the
* signer key (e.g. role, restrictions and so on). Each signer receives an ID to refer which is
* returned.
*
* @param key
* @param signerData
*
* @return assigned ID of the signer
*/
public String addSigner(AbstractKey key, Binder signerData) {
// Compatibility: add to both ends
String id = String.valueOf(signers.size());
// signingKeys.add(key);
signers.put(id, new Binder("id", id, "key", key, "data", signerData));
return id;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Capsule method prepareSigners.
@NonNull
private HashMap<String, AbstractKey> prepareSigners(Binder payload) {
HashMap<String, AbstractKey> sigIds = new HashMap<>();
if (signers != null && !signers.isEmpty()) {
ArrayList<Binder> s = payload.set("signers", new ArrayList<>());
int i = 0;
for (Binder b : signers.values()) {
final String id = b.getStringOrThrow("id");
final AbstractKey key = (AbstractKey) b.get("key");
sigIds.put(id, key);
Binder signerData = new Binder("id", id, "key", key.getPublicKey().pack(), "data", b.getBinder("data"));
s.add(signerData);
}
}
return sigIds;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Capsule method preparePayload.
@NonNull
private Binder preparePayload() throws EncryptionError {
Binder payload = new Binder();
// Private data
if (hasPrivate()) {
if (encryptingKeys == null || encryptingKeys.isEmpty()) {
throw new IllegalStateException("missing encryption keys");
}
SymmetricKey mainKey = new SymmetricKey();
byte[] packedMainKey = mainKey.pack();
Binder data = payload.set("private", new Binder());
ArrayList keys = data.set("keys", new ArrayList<Binder>());
for (AbstractKey k : encryptingKeys) {
if (k == null)
throw new IllegalStateException("null is forbidden in encryption keys");
Binder b = new Binder("keyInfo", k.packedInfo(), "key", k.encrypt(packedMainKey));
keys.add(b);
}
// Private data are packed with random filler. The way it is donne
// fill will simply not read
byte[] packedPrivate = Boss.dumpToArray(privateData, CTRTransformer.randomBytes(0, 117));
data.put("data", mainKey.etaEncrypt(packedPrivate));
}
// public data
if (hasPublic()) {
payload.put("public", publicData);
}
payload.put("type", "capsule");
return payload;
}
Aggregations