@nfps.dev/runtime
Version:
Runtime library for NFPs
54 lines • 1.77 kB
JavaScript
import { uuid_v4 } from '@blake.regalia/belt';
import { create_html } from './dom';
export const XC_CMD_CONNECT = 1;
export const XC_CMD_DISCONNECT = 2;
export const XC_CMD_ACCOUNT_CHANGED = 3;
export const XC_CMD_SIGN_AUTO = 4;
export const XC_CMD_EXEC_CONTRACT = 5;
export const XC_CMD_SECRET_ENCRYPT = 6;
export const XC_CMD_SECRET_DECRYPT = 7;
export const XC_CMD_STORE_DATA = 8;
export const XC_CMD_FETCH_DATA = 9;
export const comcPortal = (p_host, dm_root) => new Promise((fk_resolve) => {
const dm_iframe = create_html('iframe', {
src: p_host,
style: 'display:none',
});
dm_iframe.onload = () => {
fk_resolve(dm_iframe);
};
dm_root.append(dm_iframe);
});
export const comcClient = (dm_iframe) => {
const d_window = dm_iframe.contentWindow;
if (!d_window)
throw new Error('Unable to access iframe content window at ' + dm_iframe.src);
// requests dict
const h_requests = {};
// response handler
addEventListener('message', (d_event) => {
try {
// destructure response
const [si_req, xc_result, w_value,] = d_event.data;
// route
h_requests[si_req][xc_result](w_value);
// clean
delete h_requests[si_req];
}
catch (e_process) { }
});
// client instance
return {
post: (xc_cmd, w_arg, si_req = uuid_v4()) => new Promise((fk_resolve, fe_reject) => {
// save resolver to request dict
h_requests[si_req] = [fk_resolve, fe_reject];
// post message to frame
d_window.postMessage([
si_req,
xc_cmd,
w_arg,
], '*');
}),
};
};
//# sourceMappingURL=comc.js.map