Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 472f713

Browse files
axelchalonamaury1093
authored andcommitted
Use persistent storage (#177)
* Use persistent storage for webviews * Update .gitlab-ci.yml enable gitsync * Retrieve dapp token internally * Remove shell_requestNewToken RPC method. The token must be retrieved internally (by importing DappsRequestsStore in Parity UI), this way we can be sure that the appId is coming from a trusted source. The token, along with the appId, are now passed as query string parameters to the dapp (and to subsequent pages during in-dapp navigation) * Use new version of @parity/api stripped of shell_requestNewToken methods that are now unused * Use synchronous blocking methods in preload.js to maximize chances of web3/parity/ethereum objects being available directly to the dapp, before its own js executes.
1 parent 1b1c17a commit 472f713

File tree

9 files changed

+63
-84
lines changed

9 files changed

+63
-84
lines changed

.gitlab-ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,4 @@ publish:snap:
103103
- scripts/publish-snap.sh
104104
tags:
105105
- rust-stable
106+
#gitsync enabled

electron/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ function createWindow () {
111111

112112
let baseUrl;
113113
let appId;
114+
let token;
114115

115116
// Derive the dapp baseUrl (.../my-dapp/) from the first URL of the webview
116117
// (.../my-dapp/index.html). The baseUrl defines what files the webview is
@@ -120,7 +121,8 @@ function createWindow () {
120121
webContents.once('did-navigate', (e, initialUrl) => {
121122
const initialURL = new URL(initialUrl);
122123

123-
appId = initialURL.searchParams.get('appId');
124+
appId = initialURL.searchParams.get('shellAppId');
125+
token = initialURL.searchParams.get('shellToken');
124126

125127
initialURL.hash = '';
126128
initialURL.search = '';
@@ -138,7 +140,8 @@ function createWindow () {
138140

139141
const newURL = new URL(targetUrl);
140142

141-
newURL.searchParams.set('appId', appId);
143+
newURL.searchParams.set('shellAppId', appId);
144+
newURL.searchParams.set('shellToken', token);
142145

143146
webContents.loadURL(newURL.href);
144147
} else {

package-lock.json

+34-36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
"yauzl": "2.9.1"
159159
},
160160
"dependencies": {
161-
"@parity/api": "2.1.20",
161+
"@parity/api": "2.1.24",
162162
"@parity/mobx": "1.1.2",
163163
"@parity/plugin-signer-account": "github:parity-js/plugin-signer-account#05294dce59b7cd7c4f1b26dc604fc6a04dd02bc8",
164164
"@parity/plugin-signer-default": "github:parity-js/plugin-signer-default#dcf8cf23bb050070b6a691413b974b5c2d7d1ce6",

src/Dapp/dapp.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export default class Dapp extends Component {
4242

4343
state = {
4444
app: null,
45-
loading: true
45+
loading: true,
46+
token: null
4647
};
4748

4849
store = DappsStore.get(this.context.api);
@@ -111,7 +112,7 @@ export default class Dapp extends Component {
111112
this.store
112113
.loadApp(id)
113114
.then((app) => {
114-
this.setState({ loading: false, app });
115+
this.setState({ loading: false, app, token: this.requestsStore.createToken(app.id) });
115116
})
116117
.catch(() => {
117118
this.setState({ loading: false });
@@ -144,13 +145,14 @@ export default class Dapp extends Component {
144145
preload={ preload }
145146
ref={ this.handleWebview }
146147
src={ `${src}${hash}` }
148+
partition={ `persist:${this.state.app.id}` }
147149
webpreferences='contextIsolation'
148150
/>;
149151
}
150152

151153
render () {
152154
const { params } = this.props;
153-
const { app, loading } = this.state;
155+
const { app, loading, token } = this.state;
154156

155157
if (loading) {
156158
return (
@@ -178,7 +180,7 @@ export default class Dapp extends Component {
178180
);
179181
}
180182

181-
let src = `${app.localUrl}?appId=${app.id}`;
183+
let src = `${app.localUrl}?shellAppId=${app.id}&shellToken=${token}`;
182184

183185
let hash = '';
184186

src/DappRequests/store.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,13 @@ export default class Store {
146146
}
147147

148148
createToken = appId => {
149-
const token = sha3(`${appId}:${Date.now()}`);
149+
const token = sha3(`${appId}:${Math.random()}:${Date.now()}`);
150150

151151
this.tokens[token] = appId;
152152
return token;
153153
};
154154

155155
hasValidToken = (method, appId, token) => {
156-
if (!token) {
157-
return method === 'shell_requestNewToken';
158-
}
159-
160156
return this.tokens[token] === appId;
161157
};
162158

src/inject.js

+11-23
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,24 @@
1717
import Api from '@parity/api';
1818
import qs from 'query-string';
1919

20-
function getAppId () {
21-
// Local dapps: file:///home/username/.config/parity-ui/dapps/mydapp/index.html?appId=LOCAL-dapp-name
22-
// Local dapps served in development mode on a dedicated port: http://localhost:3001/?appId=LOCAL-dapp-name
23-
// Built-in dapps: file://path-to-shell/.build/dapps/0x0587.../index.html?appId=dapp-name
24-
// Built-in dapps when running Electron in dev mode: http://127.0.0.1:3000/dapps/v1/index.html?appId=dapp-name
25-
// Network dapps: file:///home/username/.config/parity-ui/hashfetch/files/0x8075.../index.html?appId=dapp-name
26-
const fromQuery = qs.parse(window.location.search).appId;
27-
28-
if (fromQuery) { return fromQuery; }
29-
30-
console.error('Could not find appId');
31-
}
32-
3320
function initProvider () {
34-
const appId = getAppId();
21+
const queryParams = qs.parse(window.location.search);
22+
23+
// Local dapps: file:///home/username/.config/parity-ui/dapps/mydapp/index.html?appId=LOCAL-dapp-name&token=0x...
24+
// Local dapps served in development mode on a dedicated port: http://localhost:3001/?appId=LOCAL-dapp-name&token=0x...
25+
// Built-in dapps: file://path-to-shell/.build/dapps/0x0587.../index.html?appId=dapp-name&token=0x...
26+
// Built-in dapps when running Electron in dev mode: http://127.0.0.1:3000/dapps/v1/index.html?appId=dapp-name&token=0x...
27+
// Network dapps: file:///home/username/.config/parity-ui/hashfetch/files/0x8075.../index.html?appId=dapp-name&token=0x...
28+
const appId = queryParams.shellAppId;
29+
const token = queryParams.shellToken;
3530

3631
// The dapp will use the PostMessage provider, send postMessages to
3732
// preload.js, and preload.js will relay those messages to the shell.
33+
console.log(`Initializing provider with appId ${appId} and token ${token}`);
3834
const ethereum = new Api.Provider.PostMessage(appId);
3935

40-
console.log(`Requesting API communications token for ${appId}`);
41-
4236
ethereum
43-
.requestNewToken()
44-
.then((tokenId) => {
45-
console.log(`Received API communications token ${tokenId}`);
46-
})
47-
.catch((error) => {
48-
console.error('Unable to retrieve communications token', error);
49-
});
37+
.setToken(token);
5038

5139
window.ethereum = ethereum;
5240
window.isParity = true;

0 commit comments

Comments
 (0)