-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
InpageBridge and BackgroundBridge (#26)
- Loading branch information
Showing
17 changed files
with
545 additions
and
818 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export class InpageBridge { | ||
_engine; | ||
_webview; | ||
|
||
_onInpageRequest(payload) { | ||
const { current } = this._webview; | ||
const { provider } = this._engine.api.network; | ||
provider.sendAsync(payload, (error, response) => { | ||
current && | ||
current.postMessage( | ||
JSON.stringify({ | ||
type: 'INPAGE_RESPONSE', | ||
payload: { error, response, __mmID: payload.__mmID } | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
_sendStateUpdate = () => { | ||
const { current } = this._webview; | ||
const { network, selectedAddress } = this._engine.datamodel.flatState; | ||
current && | ||
current.postMessage({ | ||
type: 'STATE_UPDATE', | ||
payload: { network, selectedAddress } | ||
}); | ||
}; | ||
|
||
constructor(engine, webview) { | ||
this._engine = engine; | ||
this._webview = webview; | ||
engine.api.network.subscribe(this._sendStateUpdate); | ||
engine.api.preferences.subscribe(this._sendStateUpdate); | ||
} | ||
|
||
onMessage({ type, payload }) { | ||
switch (type) { | ||
case 'INPAGE_REQUEST': | ||
this._onInpageRequest(payload); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
export default InpageBridge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import BackgroundBridge from './BackgroundBridge'; | ||
|
||
const MOCK_ENGINE = { | ||
datamodel: { | ||
flatState: { | ||
selectedAddress: 'foo', | ||
network: 'bar' | ||
} | ||
}, | ||
api: { | ||
network: { | ||
provider: { | ||
sendAsync(payload, callback) { | ||
callback(undefined, true); | ||
} | ||
}, | ||
subscribe(callback) { | ||
callback(true); | ||
} | ||
}, | ||
preferences: { | ||
subscribe(callback) { | ||
callback(true); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const MOCK_WEBVIEW = { | ||
current: { | ||
postMessage() { | ||
/* eslint-disable-line no-empty */ | ||
} | ||
} | ||
}; | ||
|
||
describe('BackgroundBridge', () => { | ||
it('should subscribe to network store', () => { | ||
const { network, preferences } = MOCK_ENGINE.api; | ||
const stub1 = spyOn(network, 'subscribe'); | ||
const stub2 = spyOn(preferences, 'subscribe'); | ||
new BackgroundBridge(MOCK_ENGINE); | ||
expect(stub1).toBeCalled(); | ||
expect(stub2).toBeCalled(); | ||
}); | ||
|
||
it('should relay response from provider', () => { | ||
const bridge = new BackgroundBridge(MOCK_ENGINE, MOCK_WEBVIEW); | ||
bridge.onMessage({ type: 'FOO' }); | ||
const stub = spyOn(MOCK_WEBVIEW.current, 'postMessage'); | ||
bridge.onMessage({ type: 'INPAGE_REQUEST', payload: { method: 'net_version' } }); | ||
expect(stub).toBeCalledWith(JSON.stringify({ type: 'INPAGE_RESPONSE', payload: { response: true } })); | ||
}); | ||
|
||
it('should emit state update', () => { | ||
const stub = spyOn(MOCK_WEBVIEW.current, 'postMessage'); | ||
new BackgroundBridge(MOCK_ENGINE, MOCK_WEBVIEW); | ||
expect(stub).toBeCalledWith({ | ||
payload: { | ||
network: 'bar', | ||
selectedAddress: 'foo' | ||
}, | ||
type: 'STATE_UPDATE' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Engine from './Engine'; | ||
|
||
describe('Engine', () => { | ||
it('should expose an API', () => { | ||
const engine = new Engine(); | ||
expect(engine.api).toHaveProperty('accountTracker'); | ||
expect(engine.api).toHaveProperty('addressBook'); | ||
expect(engine.api).toHaveProperty('blockHistory'); | ||
expect(engine.api).toHaveProperty('currencyRate'); | ||
expect(engine.api).toHaveProperty('keyring'); | ||
expect(engine.api).toHaveProperty('network'); | ||
expect(engine.api).toHaveProperty('networkStatus'); | ||
expect(engine.api).toHaveProperty('phishing'); | ||
expect(engine.api).toHaveProperty('preferences'); | ||
expect(engine.api).toHaveProperty('shapeShift'); | ||
expect(engine.api).toHaveProperty('tokenRates'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
class InpageBridge { | ||
_onMessage(data) { | ||
try { | ||
const { payload, type } = JSON.parse(data); | ||
switch (type) { | ||
case 'STATE_UPDATE': | ||
this._onStateUpdate(payload); | ||
break; | ||
|
||
case 'INPAGE_RESPONSE': | ||
this._onBackgroundResponse(payload); | ||
break; | ||
} | ||
} catch (error) { | ||
/* eslint-disable-line no-empty */ | ||
} | ||
} | ||
|
||
_onBackgroundResponse({ __mmID, error, response }) { | ||
const callback = this._pending[__mmID]; | ||
callback && callback(error, response); | ||
delete this._pending[__mmID]; | ||
} | ||
|
||
_onStateUpdate(state) { | ||
this._selectedAddress = state.selectedAddress; | ||
this._network = state.network; | ||
} | ||
|
||
constructor() { | ||
this._pending = {}; | ||
this.isMetamask = true; | ||
document.addEventListener('message', ({ data }) => { | ||
this._onMessage(data); | ||
}); | ||
} | ||
|
||
isConnected() { | ||
return true; | ||
} | ||
|
||
send(payload) { | ||
let result; | ||
|
||
switch (payload.method) { | ||
case 'eth_accounts': | ||
result = this._selectedAddress ? [this._selectedAddress] : []; | ||
break; | ||
|
||
case 'eth_coinbase': | ||
result = this._selectedAddress; | ||
break; | ||
|
||
case 'eth_uninstallFilter': | ||
this.sendAsync(payload); | ||
break; | ||
|
||
case 'net_version': | ||
result = this._network; | ||
break; | ||
|
||
default: | ||
throw new Error( | ||
`This provider requires a callback to be passed when executing methods like ${ | ||
payload.method | ||
}. This is because all methods are always executed asynchonously. See https://git.io/fNi6S for more information.` | ||
); | ||
} | ||
|
||
return { | ||
id: payload.id, | ||
jsonrpc: payload.jsonrpc, | ||
result | ||
}; | ||
} | ||
|
||
sendAsync(payload, callback) { | ||
payload = { ...payload, ...{ __mmID: Date.now() } }; | ||
this._pending[payload.__mmID] = callback; | ||
window.postMessage( | ||
{ | ||
payload, | ||
type: 'INPAGE_REQUEST' | ||
}, | ||
'*' | ||
); | ||
} | ||
} | ||
|
||
window.ethereum = new InpageBridge(); | ||
|
||
window.originalPostMessage({ type: 'ETHEREUM_PROVIDER_SUCCESS' }, '*'); |
Oops, something went wrong.