public Binder getBinderOrThrow(String key) { Binder result = getBinder(key, null); if (result == null) throw new IllegalArgumentException("map not found at key: " + key); return result; }
@NonNull private Binder unpackPayload(byte[] packedCoffer, boolean allowPartiallySigned) throws EncryptionError { signed = false; Binder outer = Boss.unpack(packedCoffer); Collection<Binder> signatures = outer.getBinders("signatures"); final byte[] source = outer.getBinary("content"); Binder payload = Boss.unpack(source); if (!payload.get("type").equals("capsule")) throw new FormatException("not capsule/unknown type"); checkSignatures(source, signatures, payload, allowPartiallySigned); publicData = payload.getBinder("public"); return payload; }
public void deserealizeWith(Binder data, BiDeserializer d) { createdAt = data.getZonedDateTimeOrThrow("created_at"); expiresAt = data.getZonedDateTime("expires_at", null); revision = data.getIntOrThrow("revision"); this.references = d.deserialize(data.getList("references", null)); if (revision <= 0) throw new IllegalArgumentException("illegal revision number: " + revision); Role r = registerRole(d.deserialize(data.getBinderOrThrow("owner"))); if (!r.getName().equals("owner")) throw new IllegalArgumentException("bad owner role name"); r = registerRole(d.deserialize(data.getBinderOrThrow("created_by"))); if (!r.getName().equals("creator")) throw new IllegalArgumentException("bad creator role name"); this.data = data.getBinder("data", Binder.EMPTY); branchId = data.getString("branch_id", null); parent = d.deserialize(data.get("parent")); origin = d.deserialize(data.get("origin")); }
public Binder getSignerData(String signerKeyId) { return signers.get(signerKeyId).getBinder("data"); }
public void deserializeWith(Binder data, BiDeserializer d) { registerRole(d.deserialize(data.getBinderOrThrow("issuer"))); createdAt = data.getZonedDateTimeOrThrow("created_at"); expiresAt = data.getZonedDateTime("expires_at", null); extendedType = data.getString("extended_type", null); this.data = d.deserialize(data.getBinder("data", Binder.EMPTY)); references = d.deserialize(data.getList("references", null)); Map<String, Permission> perms = d.deserialize(data.getOrThrow("permissions")); perms.forEach((id, perm) -> { perm.setId(id); addPermission(perm); }); }
/** * Method calls from {@link SlotContract#fromDslFile(String)} and initialize contract from given binder. * @param root id binder with initialized data * @return created and ready {@link SlotContract} contract. * @throws EncryptionError if something went wrong */ protected SlotContract initializeWithDsl(Binder root) throws EncryptionError { super.initializeWithDsl(root); int numRevisions = root.getBinder("state").getBinder("data").getInt(KEEP_REVISIONS_FIELD_NAME, -1); if(numRevisions > 0) keepRevisions = numRevisions; return this; }
/** * Retreive extra information of the signer. * * @param key * * @return any extra data associated to the key. Empty {@link Binder} instance if there is no * associated information * * @throws IllegalArgumentException * if key is not found */ public Binder getSignerData(AbstractKey key) { return getSigner(key).getBinder("data"); }
private static void doGetURate() throws IOException { BasicHttpClient httpClient = new BasicHttpClient(URS_ROOT_URL); BasicHttpClient.Answer answer = httpClient.commonRequest("uutn/info"); System.out.println("Current U per UTN rate is " + answer.data.getBinder("rates").getString("U_UTN")); System.out.println("Minimum U to reserve " + answer.data.getBinder("limits").getBinder("U").getString("min")); System.out.println("Maximum U to reserve " + answer.data.getBinder("limits").getBinder("U").getString("max")); finish(); }
protected UnsContract initializeWithDsl(Binder root) throws EncryptionError { super.initializeWithDsl(root); ArrayList<?> arrayNames = root.getBinder("state").getBinder("data").getArray(NAMES_FIELD_NAME); for (Object name: arrayNames) { Binder binder; if (name.getClass().getName().endsWith("Binder")) binder = (Binder) name; else binder = new Binder((Map) name); UnsName unsName = new UnsName().initializeWithDsl(binder); storedNames.add(unsName); } return this; }
public List<String> extractJSNames() { List<String> res = new ArrayList<>(); Binder scriptBinder = getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder == null) scriptBinder = getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null); if (scriptBinder != null) res.addAll(JSApiHelpers.getFileNamesFromScriptBinder(scriptBinder)); return res; }
public void updateThisEnvironmentByName(Contract newContract, JSApiExecOptions execOptions) throws Exception { JSApiEnvironment env = execJSByName( newContract.getDefinition().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), newContract.getState().getData().getBinder(Contract.JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, newContract, stringParams ); this.jsApi = env.jsApi; this.scriptEngine = env.scriptEngine; this.currentContract = env.currentContract; this.result = env.result; }
/** * Executes attached javascript if only one js is attached. Also, it should be attached with putContentIntoContract == true. */ public Object execJSByName(JSApiExecOptions execOptions, String jsFileName, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByName( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, jsFileName, this, params ); return env.callEvent("main", true); }
/** * Look for state data of slot contract * @param slotId slot contract id * @return {@link Binder} containing state data of slot contract * @throws ClientError */ public Binder querySlotInfo(HashId slotId) throws ClientError { return protect(() -> { Binder result = httpClient.command("querySlotInfo", "slot_id", slotId.getDigest()); Binder binder = result.getBinder("slot_state", null); return binder; }); }
/** * Look for state data of follower contract * @param followerId follower contract id * @return {@link Binder} containing state data of follower contract * @throws ClientError */ public Binder queryFollowerInfo(HashId followerId) throws ClientError { return protect(() -> { Binder result = httpClient.command("queryFollowerInfo", "follower_id", followerId.getDigest()); Binder binder = result.getBinder("follower_state", null); return binder; }); } }
/** * Executes javascript, like {@link Contract#execJS(JSApiExecOptions, byte[], String...)}, * but searches script body in contract. It should be saved previously with {@link Definition#setJS(byte[], String, JSApiScriptParameters, boolean)} * or {@link State#setJS(byte[], String, JSApiScriptParameters, boolean)} with putContentIntoContract == true. * @param scriptHash is {@link HashId} from js-file content. */ public Object execJSByScriptHash(JSApiExecOptions execOptions, HashId scriptHash, String... params) throws Exception { JSApiEnvironment env = JSApiEnvironment.execJSByScriptHash( getDefinition().getData().getBinder(JSAPI_SCRIPT_FIELD, null), getState().getData().getBinder(JSAPI_SCRIPT_FIELD, null), execOptions, scriptHash, this, params ); return env.callEvent("main", true); }
public void deserializeWith(Binder data, BiDeserializer d) { if(data != null) { id = data.getString("id", null); List refs = data.getList("references", null); if(refs != null) { references = d.deserializeCollection(refs); } try { validUntil = data.getLongOrThrow("valid_until"); } catch (IllegalArgumentException e) { validUntil = null; } this.data = data.getBinder("data", null); } }
/** * Get processing state of given parcel * @param parcelId id of the parcel to get state of * @return processing state of the parcel * @throws ClientError */ public Node.ParcelProcessingState getParcelProcessingState(HashId parcelId) throws ClientError { return protect(() -> { Binder result = httpClient.command("getParcelProcessingState", "parcelId", parcelId); Object ps = result.getOrThrow("processingState"); if (ps instanceof Node.ParcelProcessingState) return (Node.ParcelProcessingState) ps; return Node.ParcelProcessingState.valueOf(result.getBinder("processingState").getStringOrThrow("state")); }); }
/** * Saves client's javascript in contract's state. It can be executed with {@link Contract#execJS(JSApiExecOptions, byte[], String...)} */ public void setJS(byte[] jsFileContent, String jsFileName, JSApiScriptParameters scriptParameters, boolean putContentIntoContract) { String fileNameKey = JSApiHelpers.fileName2fileKey(jsFileName); Binder scriptBinder = JSApiHelpers.createScriptBinder(jsFileContent, jsFileName, scriptParameters, putContentIntoContract); Binder scripts = getData().getBinder(JSAPI_SCRIPT_FIELD, new Binder()); scripts.set(fileNameKey, scriptBinder); getData().put(JSAPI_SCRIPT_FIELD, scripts); }
/** * Saves client's javascript in contract's definition. It can be executed with {@link Contract#execJS(JSApiExecOptions, byte[], String...)} * @param putContentIntoContract pass here true to save js body in contract, in this case it's not necessary to store attached * js-files separately, needs only {@link HashId} of its. */ public void setJS(byte[] jsFileContent, String jsFileName, JSApiScriptParameters scriptParameters, boolean putContentIntoContract) { String fileNameKey = JSApiHelpers.fileName2fileKey(jsFileName); Binder scriptBinder = JSApiHelpers.createScriptBinder(jsFileContent, jsFileName, scriptParameters, putContentIntoContract); Binder scripts = getData().getBinder(JSAPI_SCRIPT_FIELD, new Binder()); scripts.set(fileNameKey, scriptBinder); getData().put(JSAPI_SCRIPT_FIELD, scripts); }
@Override public void deserialize(Binder data, BiDeserializer deserializer) { name = data.getStringOrThrow("name"); comment = data.getString("comment", null); contract = deserializer.getContext(); Binder required = data.getBinder("required"); if(required != null) { if(required.containsKey(ALL_OF.name())) { requiredAllReferences.addAll(deserializer.deserialize(required.getArray(ALL_OF.name()))); } if(required.containsKey(ANY_OF.name())) { requiredAnyReferences.addAll(deserializer.deserialize(required.getArray(ANY_OF.name()))); } } }