use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class ContractsService method createNotaryContract.
/**
* Creates a simple notary contract for given keys.
*<br><br>
* The service creates a notary contract with issuer, creator and owner roles;
* with change_owner permission for owner and revoke permissions for owner and issuer.
* By default expires at time is set to 60 months from now.
*<br><br>
* @param issuerKeys is issuer private keys.
* @param ownerKeys is owner public keys.
* @return signed and sealed contract, ready for register.
*/
public static synchronized Contract createNotaryContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys) {
Contract notaryContract = new Contract();
notaryContract.setApiLevel(3);
Contract.Definition cd = notaryContract.getDefinition();
cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
Binder data = new Binder();
data.set("name", "Default notary");
data.set("description", "Default notary description.");
cd.setData(data);
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : issuerKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
notaryContract.registerRole(issuerRole);
notaryContract.createRole("issuer", issuerRole);
notaryContract.createRole("creator", issuerRole);
notaryContract.registerRole(ownerRole);
notaryContract.createRole("owner", ownerRole);
ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
notaryContract.addPermission(changeOwnerPerm);
RevokePermission revokePerm1 = new RevokePermission(ownerRole);
notaryContract.addPermission(revokePerm1);
RevokePermission revokePerm2 = new RevokePermission(issuerRole);
notaryContract.addPermission(revokePerm2);
notaryContract.seal();
notaryContract.addSignatureToSeal(issuerKeys);
return notaryContract;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class ContractsService method createRevocation.
/**
* Implementing revoking procedure.
* <br><br>
* Service create temp contract with given contract in revoking items and return it.
* That temp contract should be send to Universa and given contract will be revoked.
* <br><br>
* @param c is contract should revoked be
* @param keys is keys from owner of c
* @return working contract that should be register in the Universa to finish procedure.
*/
public static synchronized Contract createRevocation(Contract c, PrivateKey... keys) {
Contract tc = new Contract();
Contract.Definition cd = tc.getDefinition();
// by default, transactions expire in 30 days
cd.setExpiresAt(tc.getCreatedAt().plusDays(30));
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : keys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
tc.addSignerKey(k);
}
tc.registerRole(issuerRole);
tc.createRole("owner", issuerRole);
tc.createRole("creator", issuerRole);
if (!tc.getRevokingItems().contains(c)) {
Binder data = tc.getDefinition().getData();
List<Binder> actions = data.getOrCreateList("actions");
tc.getRevokingItems().add(c);
actions.add(Binder.fromKeysValues("action", "remove", "id", c.getId()));
}
tc.seal();
return tc;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class ContractsService method createShareContract.
/**
* Creates a share contract for given keys.
*<br><br>
* The service creates a simple share contract with issuer, creator and owner roles;
* with change_owner permission for owner, revoke permissions for owner and issuer and split_join permission for owner.
* Split_join permission has by default following params: 1 for min_value, 1 for min_unit, "amount" for field_name,
* "state.origin" for join_match_fields.
* By default expires at time is set to 60 months from now.
*<br><br>
* @param issuerKeys is issuer private keys.
* @param ownerKeys is owner public keys.
* @param amount is maximum shares number.
* @return signed and sealed contract, ready for register.
*/
public static synchronized Contract createShareContract(Set<PrivateKey> issuerKeys, Set<PublicKey> ownerKeys, String amount) {
Contract shareContract = new Contract();
shareContract.setApiLevel(3);
Contract.Definition cd = shareContract.getDefinition();
cd.setExpiresAt(ZonedDateTime.now().plusMonths(60));
Binder data = new Binder();
data.set("name", "Default share name");
data.set("currency_code", "DSH");
data.set("currency_name", "Default share name");
data.set("description", "Default share description.");
cd.setData(data);
SimpleRole issuerRole = new SimpleRole("issuer");
for (PrivateKey k : issuerKeys) {
KeyRecord kr = new KeyRecord(k.getPublicKey());
issuerRole.addKeyRecord(kr);
}
SimpleRole ownerRole = new SimpleRole("owner");
for (PublicKey k : ownerKeys) {
KeyRecord kr = new KeyRecord(k);
ownerRole.addKeyRecord(kr);
}
shareContract.registerRole(issuerRole);
shareContract.createRole("issuer", issuerRole);
shareContract.createRole("creator", issuerRole);
shareContract.registerRole(ownerRole);
shareContract.createRole("owner", ownerRole);
shareContract.getStateData().set("amount", amount);
ChangeOwnerPermission changeOwnerPerm = new ChangeOwnerPermission(ownerRole);
shareContract.addPermission(changeOwnerPerm);
Binder params = new Binder();
params.set("min_value", 1);
params.set("min_unit", 1);
params.set("field_name", "amount");
List<String> listFields = new ArrayList<>();
listFields.add("state.origin");
params.set("join_match_fields", listFields);
SplitJoinPermission splitJoinPerm = new SplitJoinPermission(ownerRole, params);
shareContract.addPermission(splitJoinPerm);
RevokePermission revokePerm1 = new RevokePermission(ownerRole);
shareContract.addPermission(revokePerm1);
RevokePermission revokePerm2 = new RevokePermission(issuerRole);
shareContract.addPermission(revokePerm2);
shareContract.seal();
shareContract.addSignatureToSeal(issuerKeys);
return shareContract;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class BasicHttpClient method start.
/**
* Authenticate self to the remote party. Blocks until the handshake is done. It is important to start() connection
* before any use.
*
* @param privateKey is client's {@link PrivateKey}
* @param nodePublicKey is Node's {@link PublicKey}
* @param session is {@link BasicHttpClientSession} object, can be null
* @return created or already using {@link BasicHttpClientSession}
* @throws IOException is something went wrong
*/
public BasicHttpClientSession start(PrivateKey privateKey, PublicKey nodePublicKey, BasicHttpClientSession session) throws IOException {
synchronized (this) {
if (session != null) {
this.session = session;
this.session.setNodePublicKey(nodePublicKey);
this.session.setPrivateKey(privateKey);
// this.session.setNodePublicKey(nodePublicKey);
//
// this.session.setPrivateKey(privateKey);
// Answer a = requestOrThrow("connect", "client_key", privateKey.getPublicKey().pack());
//
// this.session.setSessionId(a.data.getLongOrThrow("session_id"));
// byte[] server_nonce = a.data.getBinaryOrThrow("server_nonce");
// byte[] client_nonce = Do.randomBytes(47);
// byte[] data = Boss.pack(Binder.fromKeysValues(
// "client_nonce", client_nonce,
// "server_nonce", server_nonce
// ));
//
// a = requestOrThrow("get_token",
// "signature", privateKey.sign(data, HashType.SHA512),
// "data", data,
// "session_id", this.session.getSessionId()
// );
//
// data = a.data.getBinaryOrThrow("data");
//
// if (!nodePublicKey.verify(data, a.data.getBinaryOrThrow("signature"), HashType.SHA512))
// throw new IOException("node signature failed");
//
// Binder params = Boss.unpack(data);
//
// if (!Arrays.equals(client_nonce, params.getBinaryOrThrow("client_nonce")))
// throw new IOException("client nonce mismatch");
//
// byte[] key = Boss.unpack(
// privateKey.decrypt(
// params.getBinaryOrThrow("encrypted_token")
// )
// )
// .getBinaryOrThrow("sk");
// this.session.sessionKey = new SymmetricKey(key);
Binder result = command("hello");
this.session.setConnectMessage(result.getStringOrThrow("message"));
if (!result.getStringOrThrow("status").equals("OK"))
throw new ConnectionFailedException("" + result);
// throw new IllegalStateException("session already started");
} else {
this.session = new BasicHttpClientSession();
this.session.setNodePublicKey(nodePublicKey);
this.session.setPrivateKey(privateKey);
Answer a = requestOrThrow("connect", "client_key", privateKey.getPublicKey().pack());
this.session.setSessionId(a.data.getLongOrThrow("session_id"));
byte[] server_nonce = a.data.getBinaryOrThrow("server_nonce");
byte[] client_nonce = Do.randomBytes(47);
byte[] data = Boss.pack(Binder.fromKeysValues("client_nonce", client_nonce, "server_nonce", server_nonce));
a = requestOrThrow("get_token", "signature", privateKey.sign(data, HashType.SHA512), "data", data, "session_id", this.session.getSessionId());
data = a.data.getBinaryOrThrow("data");
if (!nodePublicKey.verify(data, a.data.getBinaryOrThrow("signature"), HashType.SHA512))
throw new IOException("node signature failed");
Binder params = Boss.unpack(data);
if (!Arrays.equals(client_nonce, params.getBinaryOrThrow("client_nonce")))
throw new IOException("client nonce mismatch");
byte[] key = Boss.unpack(privateKey.decrypt(params.getBinaryOrThrow("encrypted_token"))).getBinaryOrThrow("sk");
this.session.setSessionKey(new SymmetricKey(key));
Binder result = command("hello");
this.session.setConnectMessage(result.getStringOrThrow("message"));
if (!result.getStringOrThrow("status").equals("OK"))
throw new ConnectionFailedException("" + result);
}
return this.session;
}
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class BasicHttpServer method extractParams.
public Binder extractParams(BasicHTTPService.Request request) {
Binder rp = request.getParams();
String sparams = rp.getString("requestData64", null);
if (sparams != null) {
byte[] x = Base64.decode(sparams);
return Boss.unpack(x);
} else {
BasicHTTPService.FileUpload rd = (BasicHTTPService.FileUpload) rp.get("requestData");
if (rd != null) {
byte[] data = rd.getBytes();
return Boss.unpack(data);
}
}
return Binder.EMPTY;
}
Aggregations