Skip to content

Commit

Permalink
feat(react-wallet): Integrate wallet connection component (#3922)
Browse files Browse the repository at this point in the history
* feat(react-wallet): Integrate wallet-connection component

* feat(react-wallet): Get tests working with jest

* feat(react-wallet): DRY up some tests

* feat(react-wallet): Remove autodux dependency and refactor

* chore(react-wallet): clean up obselete comment
  • Loading branch information
samsiegart authored Oct 5, 2021
1 parent 724d629 commit 01a9118
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 93 deletions.
14 changes: 10 additions & 4 deletions packages/dapp-svelte-wallet/react-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
"license": "Apache-2.0",
"homepage": ".",
"type": "module",
"dependencies": {
"dependencies": {
"@agoric/assert": "*",
"@agoric/captp": "*",
"@agoric/eventual-send": "*",
"@agoric/notifier": "*",
"@material-ui/core": "^4.11.3",
"@material-ui/icons": "^4.11.2",
"enzyme-adapter-react-16": "^1.15.6",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"react-scripts": "4.0.3",
Expand All @@ -23,6 +24,7 @@
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"agoric": "*",
"enzyme": "^3.11.0",
"eslint": "^7.23.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.1.0",
Expand All @@ -33,9 +35,7 @@
"eslint-plugin-react": "^7.23.1",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"@babel/eslint-plugin": "^7.12.1",
"babel-eslint": "^10.1.0"
"rimraf": "^3.0.2"
},
"scripts": {
"start": "yarn build:ses && react-scripts start",
Expand All @@ -45,8 +45,14 @@
"lint-check": "eslint '**/*.{js,jsx}'",
"lint-fix": "eslint --fix '**/*.{js,jsx}'",
"test": "react-scripts test",
"test:debug": "react-scripts --inspect-brk test --runInBand --no-cache",
"eject": "react-scripts eject"
},
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!@lit-labs)/"
]
},
"eslintConfig": {
"ignorePatterns": [
"**/*.umd.js",
Expand Down
Binary file modified packages/dapp-svelte-wallet/react-ui/public/favicon.ico
Binary file not shown.
23 changes: 11 additions & 12 deletions packages/dapp-svelte-wallet/react-ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,26 @@
// Allow the React dev environment to extend the console for debugging
// features.
const consoleTaming = '%NODE_ENV%' === 'production' ? 'safe' : 'unsafe';

const { pow: mathPow } = Math;
Math.pow = (base, exp) =>
(typeof base === 'bigint' && typeof exp ==='bigint')
? base ** exp : mathPow(base, exp);

lockdown({
Math.pow = (base, exp) =>
typeof base === 'bigint' && typeof exp === 'bigint'
? base ** exp
: mathPow(base, exp);

lockdown({
__allowUnsafeMonkeyPatching__: 'unsafe',
errorTaming: 'unsafe',
overrideTaming: 'severe',
consoleTaming: consoleTaming });
consoleTaming: consoleTaming,
});

console.log('lockdown done.');
</script>
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="description" content="Agoric wallet" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
Expand All @@ -43,7 +42,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Agoric Wallet</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
24 changes: 0 additions & 24 deletions packages/dapp-svelte-wallet/react-ui/src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
Expand All @@ -23,16 +12,3 @@
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
27 changes: 11 additions & 16 deletions packages/dapp-svelte-wallet/react-ui/src/App.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
/* eslint-disable react/display-name */
import React from 'react';

import logo from './logo.svg';
import WalletConnection from './components/WalletConnection';

import './App.css';
import { withApplicationContext } from './contexts/Application';

function App() {
const App = ({ connectionState }) => {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
Connection Status: {connectionState}
</header>
<WalletConnection></WalletConnection>
</div>
);
}
};

export default App;
export default withApplicationContext(App, (context) => ({
connectionState: context.connectionState,
}));
8 changes: 0 additions & 8 deletions packages/dapp-svelte-wallet/react-ui/src/App.test.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable react/display-name */
import { makeReactAgoricWalletConnection } from '@agoric/wallet-connection/react.js';

import React, { useCallback } from 'react';
import { E } from '@agoric/eventual-send';
import { withApplicationContext } from '../contexts/Application';

// Create a wrapper for agoric-wallet-connection that is specific to
// the app's instance of React.
const AgoricWalletConnection = makeReactAgoricWalletConnection(React);

const getAccessToken = () => {
// Fetch the access token from the window's URL.
let accessTokenParams = `?${window.location.hash.slice(1)}`;
let accessToken = new URLSearchParams(accessTokenParams).get('accessToken');

try {
if (accessToken) {
// Store the access token for later use.
window.localStorage.setItem('accessTokenParams', accessTokenParams);
} else {
// Try reviving it from localStorage.
accessTokenParams =
window.localStorage.getItem('accessTokenParams') || '?';
accessToken = new URLSearchParams(accessTokenParams).get('accessToken');
}
} catch (e) {
console.log('Error fetching accessTokenParams', e);
}

// Now that we've captured it, clear out the access token from the URL bar.
window.location.hash = '';

window.addEventListener('hashchange', (_ev) => {
// See if we should update the access token params.
const atp = `?${window.location.hash.slice(1)}`;
const at = new URLSearchParams(atp).get('accessToken');

if (at) {
// We have new params, so replace them.
accessTokenParams = atp;
accessToken = at;
localStorage.setItem('accessTokenParams', accessTokenParams);
}

// Keep it clear.
window.location.hash = '';
});

return accessToken;
};

const WalletConnection = ({ setConnectionState }) => {
const onWalletState = useCallback((ev) => {
const { walletConnection, state } = ev.detail;
console.log('onWalletState', state);
setConnectionState(state);
switch (state) {
case 'idle': {
// This is one of the only methods that the wallet connection facet allows.
// It connects asynchronously, but you can use promise pipelining immediately.
/** @type {ERef<WalletBridge>} */
const bridge = E(walletConnection).getAdminBootstrap(getAccessToken());
// You should reconstruct all state here.
console.log('Got bridge', bridge);
break;
}
case 'error': {
console.log('error', ev.detail);
// In case of an error, reset to 'idle'.
// Backoff or other retry strategies would go here instead of immediate reset.
E(walletConnection).reset();
break;
}
default:
}
}, []);

return (
<span className="hidden">
<AgoricWalletConnection onState={onWalletState} />
</span>
);
};

export default withApplicationContext(WalletConnection, (context) => ({
setConnectionState: context.setConnectionState,
}));
Loading

0 comments on commit 01a9118

Please sign in to comment.