-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this prevents the need for 'unsafe-inline' in the CSP to talk to MM on Firefox
- Loading branch information
1 parent
dddb885
commit 265d59e
Showing
6 changed files
with
213 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
declare module 'metamask-inpage-provider' { | ||
import type PostMessageStream from 'post-message-stream' | ||
|
||
export function initProvider({ | ||
connectionStream, | ||
maxEventListeners = 100, | ||
preventPropertyDeletion = true, | ||
shouldSendMetadata = true, | ||
shouldSetOnWindow = true | ||
}: { | ||
connectionStream: PostMessageStream | ||
maxEventListeners?: number | ||
preventPropertyDeletion?: boolean | ||
shouldSendMetadata?: boolean | ||
shouldSetOnWindow?: boolean | ||
}): unknown | ||
} |
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,103 @@ | ||
/// <reference path="./metamaskInpageProvider.d.ts" /> | ||
|
||
import { initProvider } from 'metamask-inpage-provider' | ||
import PostMessageStream from 'post-message-stream' | ||
|
||
function universalProxy(pseudoTarget: object) { | ||
return { | ||
proxy: new Proxy( | ||
{}, | ||
new Proxy( | ||
{}, | ||
{ | ||
get(_, p) { | ||
return (_t: any, p2: any, r: any) => { | ||
switch (p) { | ||
case 'get': { | ||
const out = Reflect.get(pseudoTarget, p2, r) | ||
if (typeof out === 'function') return out.bind(pseudoTarget) | ||
return out | ||
} | ||
case 'getOwnPropertyDescriptor': { | ||
const out = Reflect.getOwnPropertyDescriptor(pseudoTarget, p2) | ||
if (out) out.configurable = true | ||
return out | ||
} | ||
case 'isExtensible': | ||
return true | ||
case 'preventExtensions': | ||
return false | ||
default: | ||
return (Reflect as any)[p](pseudoTarget, p2, r) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
), | ||
getPseudoTarget() { | ||
return pseudoTarget | ||
}, | ||
setPseudoTarget(value: object) { | ||
pseudoTarget = value | ||
} | ||
} | ||
} | ||
|
||
if (typeof window !== 'undefined') { | ||
const initialPseudoTarget = Object.freeze({}) | ||
const { proxy, getPseudoTarget, setPseudoTarget } = universalProxy( | ||
window.ethereum ?? initialPseudoTarget | ||
) | ||
try { | ||
Object.defineProperty(window, 'ethereum', { | ||
configurable: true, | ||
enumerable: true, | ||
get() { | ||
return proxy | ||
}, | ||
set(value: unknown) { | ||
if (!(value && ['object', 'function'].includes(typeof value))) throw new TypeError() | ||
setPseudoTarget(value as object) | ||
} | ||
}) | ||
|
||
// Allow MM time to try to sucessfully execute its own injected stub | ||
setTimeout(() => { | ||
try { | ||
// Don't clobber an existing MM stub | ||
if (getPseudoTarget() !== initialPseudoTarget) { | ||
console.info('metamaskInpageProvider: MM stub was already injected') | ||
return | ||
} | ||
initProvider({ | ||
connectionStream: new PostMessageStream({ | ||
name: 'inpage', | ||
target: 'contentscript' | ||
}) | ||
}) | ||
if (window.ethereum.isMetaMask) { | ||
console.info('metamaskInpageProvider: injected MM stub') | ||
} else { | ||
console.info( | ||
'metamaskInpageProvider: MM stub injection failed:', | ||
window.ethereum, | ||
getPseudoTarget() | ||
) | ||
} | ||
} catch (e) { | ||
console.error('metamaskInpageProvider callback:', e) | ||
} finally { | ||
Object.defineProperty(window, 'ethereum', { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: getPseudoTarget() | ||
}) | ||
console.info('metamaskInpageProvider: window.ethereum proxy reset') | ||
} | ||
}, 0) | ||
} catch (e) { | ||
console.error('metamaskInpageProvider:', e) | ||
} | ||
} |
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