Skip to content

Commit

Permalink
more code reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 17, 2024
1 parent a2f55e7 commit 56417da
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/services/ui/website/scripts/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// gameConnect does everything to connect the browser to the wallet and
// to the game engine. If successful, a JWT is returned that is needed
// for other game engine API calls.
async function gameConnect(url) {

// Get configuration information from the game engine.
var [cfg, err] = await config(url);
if (err != null) {
return [null, err];
}

// Ask the user's wallet is talking to the same blockchain as
// the game engine.
var [_, err] = await switchChain(cfg.chainId);
if (err != null) {

// The blockchain does not exist in the user's wallet so
// let's try to help them.
var [_, err] = await addEthereumChain(cfg);
if (err != null) {
return [null, err];
}

// Try one more time to switch the wallet.
var [_, err] = await switchChain(cfg.chainId);
if (err != null) {
return [null, err];
}
}

// Request permission to use the wallet. The user will select an
// account to use.
var [rp, err] = await requestPermissions();
if (err != null) {
return [null, err];
}

// Capture the account that the user selected.
if (rp.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats.length != 1) {
return [null, "user didn't select one account"];
}
if (rp[0].caveats[0].value.length != 1) {
return [null, "user didn't select one account"];
}
const address = rp[0].caveats[0].value[0];

// Get the current time to sign data to send to the game engine.
const dateTime = currentDateTime();

// Sign the arbitrary data.
var [sig, err] = await personalSign(address, cfg.chainId, dateTime);
if (err != null) {
return [null, err];
}

// Connect to the game engine to get a token for game play.
var [cge, err] = await connectGameEngine(url, address, cfg.chainId, dateTime, sig);
if (err != null) {
return [null, err];
}

return [cge.token, null];
}
89 changes: 89 additions & 0 deletions app/services/ui/website/scripts/wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
async function switchChain(chainId) {
try {
const result = await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [
{
"chainId": '0x' + chainId.toString(16)
}
],
});

return [result, null];
}

catch (e) {
return [null, e.message];
}
}

async function addEthereumChain(cfg) {
try {
const result = await ethereum.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: '0x' + cfg.chainId.toString(16),
chainName: "Liars Dice Local",
rpcUrls: [
cfg.network,
],
iconUrls: [],
nativeCurrency: {
"name": "Ethereum",
"symbol": "ETH",
"decimals": 18
},
blockExplorerUrls: [
cfg.network
]
}
],
});

return [result, null];
}

catch (e) {
return [null, e.message];
}
}

async function requestPermissions() {
try {
const result = await ethereum.request({
method: 'wallet_requestPermissions',
params: [
{
"eth_accounts": {}
}
],
});

return [result, null];
}

catch (e) {
return [null, e.message];
}
}

async function personalSign(address, chainId, dateTime) {
const data = `{"address":"${address}","chainId":${chainId},"dateTime":"${dateTime}"}`;

try {
const signature = await ethereum.request({
method: 'personal_sign',
params: [
hexer(data),
address
],
});

return [signature, null];
}

catch (e) {
return [null, e.message];
}
}

0 comments on commit 56417da

Please sign in to comment.