jr: network id switching seems to work

todo: change explorer links based on network id
This commit is contained in:
2024-03-19 17:59:20 -06:00
parent bbf46d78ea
commit 4be9626a43
3 changed files with 188 additions and 77 deletions
+98 -5
View File
@@ -75,6 +75,7 @@ export {
p_delete_key,
p_get_gphrase,
p_gphrase_recover,
p_netid_switch,
// background script api
b_main
};
@@ -243,6 +244,18 @@ p_gphrase_recover
name : name}}}) as void;
}
async function
p_netid_switch
(new_netid : string)
: Promise<void>
{
console.log('b_lib p_netid_switch', {new_netid: new_netid});
return await browser.runtime.sendMessage({frum: 'popup',
data: {method : 'p_netid_switch',
args : {new_netid : new_netid}}}) as void;
}
//=============================================================================
//=============================================================================
// API: BACKGROUND SCRIPT
@@ -483,7 +496,7 @@ bi_msg_handler_content
case "connection.open":
return w2a_ok({id : "jr",
name : "JR",
networkId : "gajumaru_devnet",
networkId : i_state.netid_cur,
origin : browser.runtime.getURL('/'),
type : "extension"});
@@ -514,7 +527,7 @@ bi_msg_handler_content
console.log('jr bg content message handler transaction sign');
let tx_str : string = msg.data.params.tx;
console.log('transaction: ', tx_str);
let result = await tx_sign(tx_str, secret_key)
let result = await tx_sign(i_state.netid_cur, tx_str, secret_key)
if
('good' === result.result) {
return w2a_ok({signedTransaction: result.signedTransaction});
@@ -701,7 +714,8 @@ msg_sign
*/
async function
tx_sign
(tx_str : string,
(netid_cur : bi_netid,
tx_str : string,
secret_key : Uint8Array)
: Promise< {result : 'good',
signedTransaction : string}
@@ -722,8 +736,7 @@ tx_sign
// we sign <<NetworkId, SerializedObject>>
// SerializedObject can either be the object or the hash of the object
// let's stick with hash for now
// FIXME: get network id from application state
let network_id : Uint8Array = vdk_binary.encode_utf8('gajumaru_devnet');
let network_id : Uint8Array = vdk_binary.encode_utf8(netid_cur);
// let tx_hash_bytes : Uint8Array = hash(tx_bytes);
let sign_data : Uint8Array = vdk_binary.bytes_concat(network_id, tx_bytes);
// @ts-ignore yes nacl is stupid
@@ -990,6 +1003,7 @@ type bi_popup_calldata
| bi_pc_p_delete_key
| bi_pc_p_get_gphrase
| bi_pc_p_gphrase_recover
| bi_pc_p_netid_switch
;
type bi_pc_p_get_data
@@ -1021,6 +1035,10 @@ type bi_pc_p_gphrase_recover
args : {gphrase : string,
name : string}};
type bi_pc_p_netid_switch
= {method : 'p_netid_switch',
args : {new_netid : string}};
type bi_popup_return_data
= p_data
@@ -1058,6 +1076,8 @@ bi_msg_handler_popup
return await bi_p_get_gphrase(msg.args);
case 'p_gphrase_recover':
return await bi_p_gphrase_recover(msg.args);
case 'p_netid_switch':
return await bi_p_netid_switch(msg.args);
default:
throw new Error('unsupported popup call method');
}
@@ -1361,6 +1381,79 @@ bi_p_gphrase_recover
async function
bi_p_netid_switch
(args: {new_netid : string})
: Promise<void>
{
console.log('b_lib bi_p_netid_switch', {new_netid: args.new_netid});
// get state
let istate : bi_state = await bi_get_state();
// type bi_state
// = {rich_keypairs : Array<bi_rich_keypair>,
// netid_cur : bi_netid,
// netids_alt : Array<bi_netid>};
//
// let NETIDS_ALL : Array<bi_netid> = [NETID_GDEV, NETID_AETEST, NETID_AEMAIN];
// if this is already the network id, do nothing
if
(istate.netid_cur === args.new_netid)
return;
// next make sure it's allowed
else if
(!elem(args.new_netid, NETIDS_ALL))
throw new Error('invalid network id: ' + args.new_netid);
// ok now we know we're definitely switching to a valid network id
else {
// set the current network id to the one the user asked for
istate.netid_cur = args.new_netid as bi_netid;
// new alts are all of them minus the new current one
istate.netids_alt = exclude(args.new_netid, NETIDS_ALL) as Array<bi_netid>;
await bi_set_state(istate);
}
}
function
elem
<t>
(item : t,
list : Array<t>)
: boolean
{
for (let im of list)
if (item === im)
return true;
return false;
}
function
exclude
<t>
(item : t,
list : Array<t>)
: Array<t>
{
// accumulator
let new_list : Array<t> = [];
for (let im of list) {
if (im === item)
continue;
else
new_list.push(im);
}
return new_list;
}
//=============================================================================
//=============================================================================