diff options
author | bitpshr <mail@bitpshr.net> | 2018-09-28 02:19:09 +0800 |
---|---|---|
committer | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2018-11-06 07:07:09 +0800 |
commit | c76c9ca2c86317f902f443db2c5704d4bf6311c0 (patch) | |
tree | e6b182dd8b0eb849d93143ecbd1217640aa8f490 /app/scripts/controllers/provider-approval.js | |
parent | 2d4ff1dd82252cbe1abe06206fb2664f96905d91 (diff) | |
download | tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar.gz tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar.bz2 tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar.lz tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar.xz tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.tar.zst tangerine-wallet-browser-c76c9ca2c86317f902f443db2c5704d4bf6311c0.zip |
EIP-1102: updated implementation
Diffstat (limited to 'app/scripts/controllers/provider-approval.js')
-rw-r--r-- | app/scripts/controllers/provider-approval.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/app/scripts/controllers/provider-approval.js b/app/scripts/controllers/provider-approval.js new file mode 100644 index 000000000..e9b1b8e16 --- /dev/null +++ b/app/scripts/controllers/provider-approval.js @@ -0,0 +1,84 @@ +const ObservableStore = require('obs-store') + +/** + * A controller that services user-approved requests for a full Ethereum provider API + */ +class ProviderApprovalController { + /** + * Creates a ProviderApprovalController + * + * @param {Object} [config] - Options to configure controller + */ + constructor ({ closePopup, openPopup, platform, publicConfigStore } = {}) { + this.store = new ObservableStore() + this.closePopup = closePopup + this.openPopup = openPopup + this.platform = platform + this.publicConfigStore = publicConfigStore + this.approvedOrigins = {} + platform && platform.addMessageListener && platform.addMessageListener(({ action, origin }) => { + action && action === 'init-provider-request' && this.handleProviderRequest(origin) + }) + } + + /** + * Called when a tab requests access to a full Ethereum provider API + * + * @param {string} origin - Origin of the window requesting full provider access + */ + handleProviderRequest (origin) { + this.store.updateState({ providerRequests: [{ origin }] }) + if (this.approvedOrigins[origin]) { + this.approveProviderRequest(origin) + return + } + this.openPopup && this.openPopup() + } + + /** + * Called when a user approves access to a full Ethereum provider API + * + * @param {string} origin - Origin of the target window to approve provider access + */ + approveProviderRequest (origin) { + this.closePopup && this.closePopup() + const requests = this.store.getState().providerRequests || [] + this.platform && this.platform.sendMessage({ action: 'approve-provider-request' }, { active: true }) + this.publicConfigStore.emit('update', this.publicConfigStore.getState()) + const providerRequests = requests.filter(request => request.origin !== origin) + this.store.updateState({ providerRequests }) + this.approvedOrigins[origin] = true + } + + /** + * Called when a tab rejects access to a full Ethereum provider API + * + * @param {string} origin - Origin of the target window to reject provider access + */ + rejectProviderRequest (origin) { + this.closePopup && this.closePopup() + const requests = this.store.getState().providerRequests || [] + this.platform && this.platform.sendMessage({ action: 'reject-provider-request' }, { active: true }) + const providerRequests = requests.filter(request => request.origin !== origin) + this.store.updateState({ providerRequests }) + } + + /** + * Clears any cached approvals for user-approved origins + */ + clearApprovedOrigins () { + this.approvedOrigins = {} + } + + /** + * Determines if a given origin has been approved + * + * @param {string} origin - Domain origin to check for approval status + * @returns {boolean} - True if the origin has been approved + */ + isApproved (origin) { + return this.approvedOrigins[origin] + } +} + +module.exports = ProviderApprovalController |