gasgasgas/js/run.js

79 lines
1.9 KiB
JavaScript

var STATE = {
LOCAL_KEY: 1 << 0,
LOCAL_KEY_PROBE: 1 << 1,
LOCAL_KEY_REQUEST: 1 << 2,
LOCAL_KEY_REJECT: 1 << 3,
LOCAL_KEY_UNLOCK: 1 << 4,
LOCAL_KEY_LOCK: 1 << 5,
RPC_CONNECT: 1 << 6,
RPC_CHANGE: 1 << 7,
RPC_GONE: 1 << 8,
RPC_PING: 1 << 9,
EVM_BALANCE_LOW: 1 << 10,
EVM_REGISTRY: 1 << 11,
PROMISE_NEW: 1 << 12,
PROMISE_PERSIST: 1 << 13,
};
const STATE_KEYS = Object.keys(STATE);
var g_state = 0;
var g_chainId = 0;
var g_balance = 0;
var g_minBalance = 10000000;
var g_poll_wallet = 3000;
var g_poll_rpc = 1000;
var g_registry = '0xb708175e3f6cd850643aaf7b32212afad50e2549';
var g_persist = []
var g_persistIndex = [];
const DEBUG = true;
const SESSION_ID = 'gasgasgas';
const KEY_STORAGEKEY = 'sessionKey_' + SESSION_ID;
const PERSIST_STORAGEKEY = 'persistKey_' + SESSION_ID; // replace with addressdeclarator
const qp = new URLSearchParams(window.location.search);
console.debug('uuid', qp.get("u"));
// borrowed from forro
async function stateChange(s, set_states, rst_states) {
if (!set_states) {
set_states = [];
} else if (!Array.isArray(set_states)) {
set_states = [set_states];
}
if (!rst_states) {
rst_states = [];
} else if (!Array.isArray(rst_states)) {
rst_states = [rst_states];
}
let new_state = g_state;
for (let i = 0; i < set_states.length; i++) {
let state = parseInt(set_states[i]);
new_state |= state;
}
for (let i = 0; i < rst_states.length; i++) {
let state = parseInt(set_states[i]);
new_state = new_state & (0xffffffff & ~rst_states[i]);
}
old_state = g_state;
g_state = new_state;
const ev = new CustomEvent('messagestatechange', {
bubbles: true,
cancelable: false,
composed: true,
detail: {
s: s,
old_state: old_state,
state: new_state,
},
});
window.dispatchEvent(ev);
}
function checkState(bit_check, bit_field) {
if (bit_field != 0 && !bit_field) {
bit_field = g_state;
}
return (bit_check & bit_field) > 0;
}