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

Handle non-String web3 property access #9256

Merged
merged 1 commit into from Aug 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions app/scripts/lib/setupWeb3.js
Expand Up @@ -44,21 +44,22 @@ export default function setupWeb3 (log) {
}

if (shouldLogUsage) {
const name = stringifyKey(key)
window.ethereum.request({
method: 'metamask_logInjectedWeb3Usage',
params: [{ action: 'window.web3 get', name: key }],
params: [{ action: 'window.web3 get', name }],
})
}

// return value normally
return _web3[key]
},
set: (_web3, key, value) => {

const name = stringifyKey(key)
if (shouldLogUsage) {
window.ethereum.request({
method: 'metamask_logInjectedWeb3Usage',
params: [{ action: 'window.web3 set', name: key }],
params: [{ action: 'window.web3 set', name }],
})
}

Expand Down Expand Up @@ -120,3 +121,15 @@ export default function setupWeb3 (log) {
function triggerReset () {
global.location.reload()
}

/**
* Returns a "stringified" key. Keys that are already strings are returned
* unchanged, and any non-string values are returned as "typeof <type>".
*
* @param {any} key - The key to stringify
*/
function stringifyKey (key) {
return typeof key === 'string'
? key
: `typeof ${typeof key}`
}