Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add EIP-1102 convenience methods #602

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/components/Views/Browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ export class Browser extends Component {
const { AssetsController } = Engine.context;
const suggestionResult = await AssetsController.watchAsset({ address, symbol, decimals, image }, type);
return suggestionResult.result;
}
},
metamask_isApproved: async ({ hostname }) => ({
isApproved: !!this.props.approvedHosts[hostname]
})
});

const entryScriptWeb3 =
Expand Down
50 changes: 50 additions & 0 deletions app/core/InpageBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,54 @@ class InpageBridge {
}

window.ethereum = new InpageBridge();

/**
* Expose nonstandard convenience methods at an application-specific namespace.
* A Proxy is used so developers can be warned about the use of these methods.
*/
window.ethereum._metamask = new Proxy(
{
/**
* Determines if user accounts are enabled for this domain
*
* @returns {boolean} - true if accounts are enabled for this domain
*/
isEnabled: () => !!window.ethereum._selectedAddress,

/**
* Determines if user accounts have been previously enabled for this
* domain in the past. This is useful for determining if a user has
* previously whitelisted a given dapp.
*
* @returns {Promise<boolean>} - Promise resolving to true if accounts have been previously enabled for this domain
*/
isApproved: async () => {
const { isApproved } = await window.ethereum.send('metamask_isApproved');
return isApproved;
},

/**
* Determines if MetaMask is unlocked by the user. The mobile application
* is always unlocked, so this method exists only for symmetry with the
* browser extension.
*
* @returns {Promise<boolean>} - Promise resolving to true
*/
isUnlocked: () => Promise.resolve(true)
},
{
get: (obj, prop) => {
!window.ethereum._warned &&
// eslint-disable-next-line no-console
console.warn(
'Heads up! ethereum._metamask exposes methods that have ' +
'not been standardized yet. This means that these methods may not be implemented ' +
'in other dapp browsers and may be removed from MetaMask in the future.'
);
window.ethereum._warned = true;
return obj[prop];
}
}
);

window.postMessage({ type: 'ETHEREUM_PROVIDER_SUCCESS' }, '*');
3 changes: 2 additions & 1 deletion app/core/InpageBridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe('InpageBridge', () => {
set(instance) {
expect(instance).toBeDefined();
INSTANCE = instance;
}
},
get: () => ({})
});
require('./InpageBridge');
});
Expand Down