revisions: for new version, new metamask API, refactoring, new method of injecting for sync mv3

This commit is contained in:
Andrei O 2022-10-31 01:34:21 +02:00
parent 3a5683b589
commit a0a198c8e4
No known key found for this signature in database
GPG Key ID: B961E5B68389457E
19 changed files with 2160 additions and 1001 deletions

2
.gitignore vendored
View File

@ -30,4 +30,6 @@ npm-debug.log*
/plugins
/www
/src/extension/inject.js
/src/extension/content.js
/src/extension/webInject.js
releases

View File

@ -4,51 +4,55 @@
"private": true,
"scripts": {
"dev": "vite",
"build": "tsc --out src/extension/inject.js src/extension/inject.ts && vue-tsc --noEmit && vite build",
"webInject": "tsc --out src/extension/webInject.js src/extension/webInject.ts",
"inject": "tsc --out src/extension/inject.js src/extension/inject.ts",
"content": "tsc --out src/extension/content.js src/extension/content.ts",
"manifestContent" : "ts-node ./release-scripts/replace-content-manifest.ts",
"build": "yarn inject && yarn webInject && yarn content && vue-tsc --noEmit && vite build && yarn manifestContent",
"preview": "vite preview",
"release": "yarn config set version-tag-prefix clear-wallet@v && yarn config set version-git-message 'clear-wallet@v%s' && yarn version --patch && yarn postversion",
"postversion": "git push",
"pub": "yarn build && yarn release && ts-node ./release-scripts/create-release.ts"
},
"dependencies": {
"@capacitor/app": "^4.0.1",
"@capacitor/core": "^4.3.0",
"@capacitor/app": "^4.1.0",
"@capacitor/core": "^4.4.0",
"@capacitor/haptics": "^4.0.1",
"@capacitor/keyboard": "^4.0.1",
"@capacitor/status-bar": "^4.0.1",
"@ionic/vue": "^6.3.0",
"@ionic/vue-router": "^6.3.0",
"@types/chrome": "^0.0.197",
"core-js": "^3.25.2",
"ethers": "^5.7.1",
"vue": "^3.2.39",
"vue-router": "^4.1.5"
"@ionic/vue": "^6.3.3",
"@ionic/vue-router": "^6.3.3",
"@types/chrome": "^0.0.200",
"core-js": "^3.26.0",
"ethers": "^5.7.2",
"vue": "^3.2.41",
"vue-router": "^4.1.6"
},
"devDependencies": {
"@capacitor/cli": "^4.3.0",
"@capacitor/cli": "^4.4.0",
"@crxjs/vite-plugin": "^1.0.14",
"@types/archiver": "^5.3.1",
"@types/jest": "^29.0.3",
"@types/node": "^18.7.19",
"@typescript-eslint/eslint-plugin": "^5.38.0",
"@typescript-eslint/parser": "^5.38.0",
"@vitejs/plugin-vue": "^3.1.0",
"@types/jest": "^29.2.0",
"@types/node": "^18.11.7",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"@vitejs/plugin-vue": "^3.2.0",
"@vue/eslint-config-typescript": "^11.0.2",
"archiver": "^5.3.1",
"eslint": "^8.23.1",
"eslint-plugin-vue": "^9.5.1",
"eslint": "^8.26.0",
"eslint-plugin-vue": "^9.6.0",
"http-browserify": "^1.7.0",
"https-browserify": "^1.0.0",
"jest": "^29.0.3",
"rollup-plugin-polyfill-node": "^0.10.2",
"jest": "^29.2.2",
"rollup-plugin-polyfill-node": "^0.11.0",
"sass": "^1.55.0",
"stream-browserify": "^3.0.0",
"ts-jest": "^29.0.1",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.8.3",
"util": "^0.12.4",
"vite": "^3.1.3",
"vue-tsc": "^0.40.13",
"typescript": "^4.8.4",
"util": "^0.12.5",
"vite": "^3.2.0",
"vue-tsc": "^1.0.9",
"yarn-upgrade-all": "^0.7.1"
},
"description": "An Ionic project"

View File

@ -0,0 +1,7 @@
(async () => {
const fs = (await import('fs')).default
const pkg = JSON.parse(fs.readFileSync('dist/manifest.json').toString());
pkg.content_scripts[0].js[0] = 'src/extension/content.js'
fs.writeFileSync('dist/manifest.json', JSON.stringify(pkg, null, 2))
})();

View File

@ -6,9 +6,9 @@
<script lang="ts">
import { IonApp, IonRouterOutlet } from "@ionic/vue";
import { defineComponent, onBeforeMount } from "vue";
import { defineComponent, onBeforeMount, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { getSettings } from '@/utils/platform'
import { getSettings } from "@/utils/platform";
export default defineComponent({
name: "App",
@ -16,49 +16,57 @@ export default defineComponent({
IonApp,
IonRouterOutlet,
},
setup () {
const route = useRoute()
const router = useRouter()
const { param, rid } = route.query;
setup() {
const route = useRoute();
const router = useRouter();
const { param, rid } = route.query;
onBeforeMount( () => {
getSettings().then((settings) => {
if(settings.theme !== 'system') {
document.body.classList.remove(settings.theme === 'dark' ? 'light': 'dark')
document.body.classList.add(settings.theme)
}
})
})
onBeforeMount(() => {
getSettings().then((settings) => {
if (settings.theme !== "system") {
document.body.classList.remove(settings.theme === "dark" ? "light" : "dark");
document.body.classList.add(settings.theme);
}
});
});
switch (route?.query?.route ?? "") {
case "sign-msg": {
router.push({
path: `/sign-msg/${rid}/${param}`
onMounted(() => {
switch (route?.query?.route ?? "") {
case "sign-msg": {
router.push({
path: `/sign-msg/${rid}/${param}`,
});
break;
}
case "sign-tx": {
router.push({
path: `/sign-tx/${rid}/${param}`,
});
break;
}
case "switch-network": {
router.push({
path: `/switch-network/${rid}/${param}`,
});
break;
}
case "request-network": {
router.push({
path: `/request-network/${rid}/${param}`,
});
break;
}
case "wallet-error": {
router.push({
path: `/wallet-error/${rid}/${param}`,
});
break;
}
default: {
router.push({ path: "/" });
}
}
});
break;
}
case "sign-tx": {
router.push({
path: `/sign-tx/${rid}/${param}`
});
break;
}
case "switch-network": {
router.push({
path: `/switch-network/${rid}/${param}`
});
break;
}
case "wallet-error": {
router.push({
path: `/wallet-error/${rid}/${param}`
});
break;
}
default: {
router.push({ path: "/", })
}
}
}
},
});
</script>

View File

@ -1,5 +1,28 @@
import { getSelectedNetwork, numToHexStr } from "@/utils/platform";
import type { RequestArguments } from '@/extension/types'
(() =>{
try {
const metamaskStub = `
// Add MetamaskAPI STUB for wallets lib to detect wallet exists
window.ethereum = {
isMetaMask: true,
isConnected: () => false
}`;
document.documentElement.setAttribute('onreset', metamaskStub);
document.documentElement.dispatchEvent(new CustomEvent('reset'));
document.documentElement.removeAttribute('onreset');
const container = document.documentElement;
const script = document.createElement('script');
script.setAttribute('async', "false")
script.setAttribute('fetchpriority', "high")
script.src = chrome.runtime.getURL('src/extension/webInject.js')
container.prepend(script)
} catch (error) {
console.error('MetaMask: Provider injection failed.', error);
}
chrome.runtime.connect({
name: 'content'
})
})()
const allowedMethods = {
'eth_accounts': true,
@ -27,41 +50,46 @@ const allowedMethods = {
'eth_signTypedData_V3': true,
'signTypedData_v4': true,
'eth_signTypedData_v4': true,
'web3_clientVersion': true,
'wallet_getPermissions': true,
'net_listening': true,
'eth_coinbase': true,
'wallet_addEthereumChain': true
}
window.addEventListener("message", (event) => {
if (event.source != window)
return;
console.log(event)
if (event.data.type && (event.data.type === "CLWALLET_CONTENT")) {
event.data.data.resId = event.data.resId
event.data.data.type = "CLWALLET_CONTENT_MSG"
event.data.data.website = document?.location?.href ?? ''
if((event?.data?.data?.method ?? 'x') in allowedMethods) {
chrome.runtime.sendMessage(event.data.data, (res) => {
const data = { type: "CLWALLET_PAGE", data: res, resId: event.data.resId, website: window?.location?.href ?? '' };
// console.log('data back', data)
const data = { type: "CLWALLET_PAGE", data: res, resId: event.data.resId };
console.log('data back', data)
window.postMessage(data, "*");
})
}
else {
const data = { type: "CLWALLET_PAGE", data: { error: true, message: 'Unknown method requested'}, resId: event.data.resId };
const data = { type: "CLWALLET_PAGE", data: { error: true, message: 'ClearWallet: Unknown method requested ' + event?.data?.data?.method ?? ''}, resId: event.data.resId };
window.postMessage(data, "*");
}
} else if (event.data.type && (event.data.type === "CLWALLET_PING")) {
getSelectedNetwork().then(network => {
const data = { type: "CLWALLET_PAGE_LISTENER", data: {
listner: 'connect',
data: {
chainId: numToHexStr(network.chainId ?? 0)
}
}};
window.postMessage(data, "*");
event.data.data.resId = event.data.resId
event.data.data.type = "CLWALLET_CONTENT_MSG"
event.data.data.method = "wallet_connect"
event.data.data.params = Array(0)
chrome.runtime.sendMessage(event.data.data , async (res) => {
window.postMessage(res, "*");
})
}
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
chrome.runtime.onMessage.addListener((message: RequestArguments , sender, sendResponse) => {
chrome.runtime.onMessage.addListener((message: any , sender, sendResponse) => {
if(message.type === "CLWALLET_EXT_LISTNER") {
const data = { type: "CLWALLET_PAGE_LISTENER", data: message.data };
// console.log('data listner', data)
@ -70,12 +98,3 @@ chrome.runtime.onMessage.addListener((message: RequestArguments , sender, sendRe
return true
});
(function() {
chrome.runtime.connect({
name: 'content'
})
const script = document.createElement('script')
script.src = chrome.runtime.getURL('src/extension/inject.js')
document.documentElement.appendChild(script)
})()

View File

@ -1,4 +1,5 @@
interface RequestArguments {
id?: string | undefined
method: string;
params?: unknown[] | object;
}
@ -6,41 +7,37 @@ interface RequestArguments {
const listners = {
accountsChanged: new Set<(p?: any) => void>(),
connect: new Set<(p?: any) => void>(),
disconnect: new Set<(p?: any) => void>,
disconnect: new Set<(p?: any) => void>(),
chainChanged: new Set<(p?: any) => void>(),
once: {
accountsChanged: new Set<(p?: any) => void>(),
connect: new Set<(p?: any) => void>(),
disconnect: new Set<(p?: any) => void>(),
chainChanged: new Set<(p?: any) => void>(),
}
}
const promResolvers = {} as any
const promResolvers = new Map()
const listner = function(event: any) {
if (event.source != window) return;
if (event.data.type && (event.data.type === "CLWALLET_PAGE")) {
if(event?.data?.data?.error){
promResolvers[event.data.resId].reject(event.data.data);
}else {
promResolvers[event.data.resId].resolve(event.data.data);
}
promResolvers[event.data.resId] = undefined;
} else if( event.data.type && (event.data.type === "CLWALLET_PAGE_LISTENER")) {
if((event?.data?.data?.listner ?? 'x') in listners ) {
try {
const listnerName = event?.data?.data?.listner as ('accountsChanged' | 'connect' | 'disconnect' | 'chainChanged')
listners[listnerName].forEach(listner => listner(event?.data?.data?.data));
} catch {
// ignore
const getListnersCount = (): number => {
let count = 0
for(const key of Object.keys(listners)) {
if(key === 'once'){
for(const onceKey of Object.keys(listners[key])) {
count += (<any>listners)[key][onceKey]?.length
}
}else {
count += (<any>listners)[key].length
}
}
}
window.addEventListener("message",listner)
return count
}
const sendMessage = (args: RequestArguments, ping = false) => {
if(Object.values(promResolvers).filter(r=> r).length < 10 ) {
return new Promise((resolve, reject) => {
const resId = crypto.randomUUID()
promResolvers[resId] = { resolve, reject }
promResolvers.set(resId, { resolve, reject })
const data = { type: "CLWALLET_CONTENT", data: args, resId};
if (ping) {
data.type = 'CLWALLET_PING'
@ -55,28 +52,51 @@ if(Object.values(promResolvers).filter(r=> r).length < 10 ) {
}
}
const eth = new Proxy({
isConnected: () => {
return true
},
class MetaMaskAPI {
isMetaMask = true
_state = {accounts: Array(1), isConnected: true, isUnlocked: true, initialized: true, isPermanentlyDisconnected: false}
_sentWarnings = {enable: false, experimentalMethods: false, send: false, events: {}}
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
chainId = "0x89"
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
networkVersion = "137"
selectedAddress = null
autoRefreshOnNetworkChange = false
// Internal Simulate Metamask
_events = {}
_eventsCount = 2
_jsonRpcConnection = {}
_log = {}
_maxListeners= 100
_metamask = new Proxy({
isUnlocked: () => {
return Promise.resolve(true)
},
requestBatch: () => {
// empty
},
}, {})
_rpcEngine = {
_events: {}, _eventsCount: 0, _maxListeners: undefined, _middleware: Array(4)
}
isConnected() {
return false
}
// for maximum compatibility since is cloning the same API
isMetaMask: true,
enable: () => {
enable() {
return sendMessage({ method: 'eth_requestAccounts', params: Array(0)})
},
request: (args: RequestArguments): Promise<unknown> => {
}
request(args: RequestArguments): Promise<unknown> {
return sendMessage(args)
},
}
// Deprecated
sendAsync: (arg1: RequestArguments, arg2: any): void => {
sendMessage(arg1 as RequestArguments).then(result => {
if (typeof arg2 === 'function'){
(arg2 as (r?: any) => any )(result)
}
})
},
sendAsync (arg1: any, arg2: any): void {
return this.send(arg1, arg2) as any
}
// Deprecated
send: (arg1: unknown, arg2: unknown): unknown => {
send (arg1: unknown, arg2: unknown): unknown {
if( typeof arg1 === 'string' ) {
return sendMessage({
method: arg1,
@ -87,12 +107,31 @@ const eth = new Proxy({
}else {
sendMessage(arg1 as RequestArguments).then(result => {
if (typeof arg2 === 'function'){
(arg2 as (r?: any) => any )(result)
(arg2 as (e?: any, r?: any) => any )(undefined, {
id: (arg1 as RequestArguments)?.id,
jsonrpc: '2.0',
method: (arg1 as RequestArguments).method,
result
}
)
}
}).catch( e => {
(arg2 as (er?: any, r?: any) => any )(new Error(e), {
id: (arg1 as RequestArguments)?.id,
jsonrpc: '2.0',
method: (arg1 as RequestArguments).method,
error: new Error(e)
}
)
})
}
},
on: (eventName: string, callback: () => void) => {
}
on (eventName: string, callback: () => void) {
this.addListener(eventName, callback)
return this
}
addListener (eventName: string, callback: () => void) {
switch (eventName) {
case 'accountsChanged':
listners.accountsChanged.add(callback)
@ -104,6 +143,7 @@ const eth = new Proxy({
}, true)
break;
case 'disconnect':
case 'close':
listners.disconnect.add(callback)
break;
// Deprecated - chainIdChanged -networkChanged
@ -113,8 +153,38 @@ const eth = new Proxy({
listners.chainChanged.add(callback)
break;
}
},
removeListener: (eventName: string, callback: () => void) => {
return this
}
once (eventName: string, callback: () => void) {
switch (eventName) {
case 'accountsChanged':
listners.once.accountsChanged.add(callback)
break
case 'connect':
listners.once.connect.add(callback)
sendMessage({
method: 'wallet_ready'
}, true)
break;
case 'disconnect':
case 'close':
listners.once.disconnect.add(callback)
break;
// Deprecated - chainIdChanged -networkChanged
case 'chainChanged':
case 'chainIdChanged':
case 'networkChanged':
listners.once.chainChanged.add(callback)
break;
}
return this
}
off (eventName: string, callback: () => void) {
(this).removeListener(eventName, callback)
return this
}
removeListener (eventName: string, callback: () => void) {
switch (eventName) {
case 'accountsChanged':
listners.accountsChanged.delete(callback)
@ -123,6 +193,7 @@ const eth = new Proxy({
listners.connect.delete(callback)
break;
case 'disconnect':
case 'close':
listners.disconnect.delete(callback)
break;
// Deprecated - chainIdChanged -networkChanged
@ -134,68 +205,185 @@ const eth = new Proxy({
default:
return
}
},
return this
}
removeAllListeners() {
listners.accountsChanged.clear()
listners.chainChanged.clear()
listners.disconnect.clear()
listners.connect.clear()
return this
}
getMaxListeners() {
return 100
}
_getExperimentalApi () {
return this._metamask
}
eventNames () {
return []
}
listenerCount () {
return getListnersCount()
}
listners() { return [] }
rawListners() { return [] }
// Internal Simulate Metamask
_warnOfDeprecation: () => null,
_state: {},
_sentWarnings: () => null,
_rpcRequest: () => null,
_handleAccountsChanged: () => null,
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
chainId: "0xa",
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
networkVersion: 10,
selectedAddress: null,
autoRefreshOnNetworkChange: false,
// Internal Simulate Metamask
_events: {},
_eventsCount: 0,
_handleChainChanged: () => null,
_handleConnect: () => null,
_handleDisconnect: () => null,
_handleStreamDisconnect: () => null,
_handleUnlockStateChanged: () => null,
_jsonRpcConnection: {},
_log: {},
_maxListeners: 100,
_metamask: new Proxy({}, {}),
_rpcEngine: {}
}, {
set: () => { return true },
_warnOfDeprecation() { return true }
_rpcRequest() { return true }
_handleAccountsChanged() { return true }
_handleChainChanged() { return true }
_handleConnect() { return true }
_handleDisconnect() { return true }
_handleStreamDisconnect() { return true }
_handleUnlockStateChanged() { return true }
_sendSync () {
console.error('Clear Wallet: Sync calling is deprecated and not supported')
}
}
const eth = new Proxy( new MetaMaskAPI(), {
// set: () => { return true },
// get: function(target, name, receiver) {
// if (!(name in target)) {
// console.log(`Getting non-existant property '" + ${name.toString()} + "'`);
// return undefined;
// if (typeof (<any>target)[name] == 'function') {
// return function (...args: any) {
// console.dir({ call: [name, ...args] });
// return undefined;
// }
// }
// let check = true
// setTimeout(() => check = false, 400)
// while(check){
// // igmore
// }
// console.log(target, name, receiver)
// },
deleteProperty: () => { return false },
})
const injectWallet = (win: any) => {
Object.defineProperty(win, 'ethereum', {
get: function () {
return eth
},
set: function () {
return true
const listner = function(event: any) {
if (event.source != window) return;
if (event.data.type && (event.data.type === "CLWALLET_PAGE")) {
try {
if(event?.data?.data?.error){
promResolvers.get(event.data.resId)?.reject(event.data.data);
console.error(event?.data?.data)
}else {
promResolvers.get(event.data.resId)?.resolve(event.data.data);
}
promResolvers.delete(event.data.resId)
} catch (e) {
console.log('Failed to connect resolve msg', e)
}
});
// console.log('Clear wallet injected', (window as any).ethereum, win)
} else if( event.data.type && (event.data.type === "CLWALLET_PAGE_LISTENER")) {
if((event?.data?.data?.listner ?? 'x') in listners ) {
try {
const listnerName = event?.data?.data?.listner as ('accountsChanged' | 'connect' | 'disconnect' | 'chainChanged')
if( listnerName === 'connect' && event?.data?.data?.data) {
(<any>eth).networkVersion = event?.data?.data?.data?.chainId?.toString(10) ?? '137';
(<any>eth).chainId = event?.data?.data?.data?.chainId ?? '0x89';
(<any>eth).selectedAddress = event?.data?.data?.address ?? null;
(<any>eth).isConnected = () => true;
} else if( listnerName === 'chainChanged' ) {
console.log(event?.data?.data?.data);
(<any>eth).networkVersion = event?.data?.data?.data.toString(10) ?? '137';
(<any>eth).chainId = event?.data?.data?.data ?? '0x89';
} else if ( listnerName === 'accountsChanged' ) {
(<any>eth).selectedAddress = event?.data?.data?.data?.address ?? 'dummy-string';
}
listners[listnerName].forEach(listner => listner(event?.data?.data?.data));
listners.once[listnerName].forEach(listner => {
listner(event?.data?.data?.data)
listners.once[listnerName].delete(listner)
});
} catch (e) {
console.error(e)
// ignore
}
}
}
}
window.addEventListener("message",listner)
const proxy1 = new Proxy({
// on: (event: any, callback:any) => { if (event === 'message') {
// debugger;
// callback(true, true)
// } }
}, {
get: function(target, name, receiver) {
if (typeof (<any>target)[name] == 'function') {
return function (...args: any) {
console.dir({ call: [name, ...args] });
}
}
console.log('ETH', name.toString() , target, receiver);
return undefined
}
})
const proxy2 = new Proxy({
// on: (event: any, callback:any) => { if (event === 'message') {
// debugger;
// callback(true, true)
// } }
}, {
get: function(target, name, receiver) {
if (typeof (<any>target)[name] == 'function') {
return function (...args: any) {
console.dir({ call: [name, ...args] });
}
}
console.log('web3', name.toString() , target, receiver);
return undefined
}
})
const web3Shim = {
currentProvider: eth,
__isMetaMaskShim__: true
}
injectWallet(this)
const injectWallet = (win: any) => {
Object.defineProperty(win, 'ethereum', {
value: eth,
});
Object.defineProperty(win, 'web3', {
value: web3Shim
});
sendMessage({
method: 'wallet_ready'
}, true)
console.log('Clear wallet injected', (window as any).ethereum, win)
}
injectWallet(this);
// setTimeout(() => {
// console.log('Metamask clone test');
// // console.log('Metamask clone test');
// // (<any>window).ethereum.request({method: 'eth_requestAccounts', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'eth_accounts', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'eth_chainId', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'wallet_requestPermissions', params: [{eth_accounts: {}}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'net_version', params: []}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'wallet_switchEthereumChain', params: [{chainId: "0x99"}]}).then((res: any) => { console.log(res, '111111111')});
// (<any>window).ethereum.on('connect', ((a: any, b: any) => console.log('connect', a, b)));
// (<any>window).ethereum.on('accountsChanged', ((a: any, b: any) => console.log('accountsChanged', a, b)));
// (<any>window).ethereum.on('chainChanged', ((a: any) => console.log('chainChanged', a, typeof a)));
// // (<any>window).ethereum.request({method: 'wallet_switchEthereumChain', params: [{chainId: "0x89"}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum2.request({method: 'wallet_switchEthereumChain', params: [{chainId: "0x89"}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.on('connect', ((a: any, b: any) => console.log('connect', a, b)));
// // (<any>window).ethereum.on('accountsChanged', ((a: any, b: any) => console.log('accountsChanged', a, b)));
// // (<any>window).ethereum.on('chainChanged', ((a: any) => console.log('chainChanged', a, typeof a)));
// // console.log((<any>window).ethereum.on('message', (a: any, b:any) => console.log(a,b)))
// console.log((<any>window).ethereum.toString())
// console.log((<any>window).ethereum2.toString())
// console.log((<any>window).ethereum.Symbold)
// }, 3500)
// console.log( (window as any).ethereum.request({method: 'eth_chainId'}))

View File

@ -21,7 +21,6 @@
}
},
"minimum_chrome_version": "93",
"permissions": [
"tabs",
"notifications",
@ -50,8 +49,9 @@
"js": ["/src/extension/content.ts"]
}
],
"web_accessible_resources": [{
"resources": ["src/extension/inject.js"],
"resources": ["src/extension/inject.js", "src/extension/webInject.js", "src/extension/content.js"],
"matches": ["<all_urls>"]
}]
}

View File

@ -1,25 +1,26 @@
import { getSelectedAccount, getSelectedNetwork, smallRandomString, getSettings, clearPk, openTab, getUrl, addToHistory } from '@/utils/platform';
import { getSelectedAccount, getSelectedNetwork, smallRandomString, getSettings, clearPk, openTab, getUrl, addToHistory, getNetworks, strToHex } from '@/utils/platform';
import { userApprove, userReject, rIdWin, rIdData } from '@/extension/userRequest'
import { signMsg, getBalance, getBlockNumber, estimateGas, sendTransaction, getGasPrice, getBlockByNumber, evmCall, getTxByHash, getTxReceipt, signTypedData } from '@/utils/wallet'
import type { RequestArguments } from '@/extension/types'
import { rpcError } from '@/extension/rpcConstants'
import { updatePrices } from '@/utils/gecko'
import { mainNets, testNets } from '@/utils/networks'
let notificationUrl: string
chrome.runtime.onInstalled.addListener(() => {
console.log('Service worker installed');
chrome.runtime.onConnect.addListener(port => port.onDisconnect.addListener(() =>
{
console.log('Service worker connected');
}))
chrome.runtime.connect(null as unknown as string, {
name:'sw-connection'
})
})
chrome.runtime.onConnect.addListener(port => port.onDisconnect.addListener(() =>
{
console.log('Service worker connected');
}))
chrome.runtime.onStartup.addListener(() => {
console.log('Service worker startup');
})
@ -78,12 +79,12 @@ if (!chrome.notifications.onButtonClicked.hasListener(viewTxListner)){
chrome.notifications.onButtonClicked.addListener(viewTxListner)
}
chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendResponse) => {
const mainListner = (message: RequestArguments, sender:any, sendResponse: (a: any) => any) => {
if(message?.type !== "CLWALLET_CONTENT_MSG") {
return true
}
(async () => {
if (!('method' in message)) {
if (!(message?.method)) {
sendResponse({
code: 500,
message: 'Invalid request method'
@ -245,7 +246,7 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
await chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${encodeURIComponent('No account is selected you need to have an account selected before trying to make a transaction')}&rid=${String(message?.resId ?? '')}`),
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${strToHex('No account is selected you need to have an account selected before trying to make a transaction')}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
})
return
@ -254,13 +255,13 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
await chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${encodeURIComponent('No network is selected you need to have a network selected before trying to make a transaction')}&rid=${String(message?.resId ?? '')}`),
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${strToHex('No network is selected you need to have a network selected before trying to make a transaction')}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
})
return
}
params.from = account.address
const serializeParams = encodeURIComponent(JSON.stringify(params)) ?? ''
const serializeParams = strToHex(JSON.stringify(params)) ?? ''
const pEstimateGas = estimateGas({
to: params?.to ?? '',
from: params?.from ?? '',
@ -332,7 +333,7 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${encodeURIComponent(String(err))}&rid=${String(message?.resId ?? '')}`),
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${strToHex(String(err))}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
})
chrome.notifications.create({
@ -370,7 +371,7 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
await chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${encodeURIComponent('No account is selected you need to have an account selected before trying sign a message')}&rid=${String(message?.resId ?? '')}`),
url: chrome.runtime.getURL(`index.html?route=wallet-error&param=${strToHex('No account is selected you need to have an account selected before trying sign a message')}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
})
return
@ -391,7 +392,7 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=sign-msg&param=${signMsgData}&rid=${String(message?.resId ?? '')}`),
url: chrome.runtime.getURL(`index.html?route=sign-msg&param=${strToHex(signMsgData)}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
}).then((win) => {
userReject[String(win.id)] = reject
@ -419,19 +420,34 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
}
break
}
// NON Standard metamask API
// NON Standard / metamask API
case 'eth_coinbase': {
const account = await getSelectedAccount()
const address = account?.address ?? null
sendResponse(address)
break
}
case 'net_listening': {
sendResponse(true)
break
}
case 'web3_clientVersion': {
sendResponse("MetaMask/v10.20.0")
break
}
case 'wallet_getPermissions':
case 'wallet_requestPermissions': {
const account = await getSelectedAccount()
const address = account?.address ? [account?.address] : []
sendResponse([{
caveats: {
type:'',
id: smallRandomString(21),
parentCapability: 'eth_accounts',
invoker: message?.website?.split('/').slice(0,3).join('/') ?? '',
caveats: [{
type:'restrictReturnedAccounts',
value: address
},
invoker: '',
}],
date: Date.now(),
id: smallRandomString(),
parentCapability: Object.keys(message?.params?.[0] ?? {})?.[0] ?? 'unknown'
}])
break
}
@ -443,6 +459,10 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
}
case 'wallet_switchEthereumChain': {
try {
const currentChainId = `0x${((await getSelectedNetwork())?.chainId ?? 0).toString(16)}`
if(currentChainId === String(message?.params?.[0]?.chainId ?? '' )) {
sendResponse(null)
}else {
await new Promise((resolve, reject) => {
chrome.windows.create({
height: 450,
@ -454,19 +474,89 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
userApprove[String(win.id)] = resolve
rIdWin[String(win.id)] = String(message.resId)
})
})
sendResponse(null)
}
} catch {
sendResponse({
error: true,
code: rpcError.USER_REJECTED,
message: 'User Rejected Signature'
message: 'User Rejected chain switch'
})
}
break
}
case 'wallet_addEthereumChain': {
const userNetworks = await getNetworks()
const networks = {...mainNets, ...testNets, ...userNetworks}
const chainId = Number(message?.params?.[0]?.chainId ?? '0')
if(!chainId) {
sendResponse({
error: true,
code: rpcError.USER_REJECTED,
message: 'Invalid Network'
})
}
if( chainId in networks ) {
mainListner({...message, method:'wallet_switchEthereumChain', params: [{
chainId: `0x${(chainId).toString(16)}`
}] }, sender, sendResponse)
} else {
if ( !message?.params?.[0]?.chainId ||
!message?.params?.[0]?.chainName ||
!message?.params?.[0]?.rpcUrls ||
!message?.params?.[0]?.blockExplorerUrls ||
!message?.params?.[0]?.nativeCurrency?.symbol
){
sendResponse({
error: true,
code: rpcError.USER_REJECTED,
message: 'Invalid Network params chainId, chainName, rpcUrls, blockExplorerUrls, and nativeCurrency are required'
})
}else {
try {
await new Promise((resolve, reject) => {
chrome.windows.create({
height: 450,
width: 400,
url: chrome.runtime.getURL(`index.html?route=request-network&param=${strToHex(JSON.stringify({...{website: message?.website ?? ''}, ...(message?.params?.[0] ?? {})}) ?? '')}&rid=${String(message?.resId ?? '')}`),
type: 'popup'
}).then((win) => {
userReject[String(win.id)] = reject
userApprove[String(win.id)] = resolve
rIdWin[String(win.id)] = String(message.resId)
})
})
sendResponse(null)
} catch (err) {
console.log('err')
sendResponse({
error: true,
code: rpcError.USER_REJECTED,
message: 'User Rejected adding network'
})
}
}
}
break
}
// internal messeges
case 'wallet_connect': {
const pNetwork = getSelectedNetwork()
const pAccount = getSelectedAccount()
const [network, account] = await Promise.all([pNetwork, pAccount])
const address = account?.address ? [account?.address] : []
const chainId = `0x${(network?.chainId ?? 0).toString(16)}`
const data = { type: "CLWALLET_PAGE_LISTENER", data: {
listner: 'connect',
data: {
chainId
},
address
}};
sendResponse(data)
break
}
case 'wallet_approve': {
if(String(sender.tab?.windowId) in rIdWin){
userApprove[String(sender.tab?.windowId)]?.(true)
@ -500,7 +590,7 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
sendResponse({
error: true,
code: rpcError.INVALID_PARAM,
message: 'Invalid request method'
message: 'ClearWallet: Invalid request method ' + message?.method ?? ''
})
break
}
@ -509,4 +599,6 @@ chrome.runtime.onMessage.addListener((message: RequestArguments, sender, sendRes
}
)();
return true;
});
}
chrome.runtime.onMessage.addListener(mainListner);

399
src/extension/webInject.ts Normal file
View File

@ -0,0 +1,399 @@
const container = document.head
const script = document.createElement('script');
script.setAttribute('async', "false")
script.textContent = `
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var listners = {
accountsChanged: new Set(),
connect: new Set(),
disconnect: new Set(),
chainChanged: new Set(),
once: {
accountsChanged: new Set(),
connect: new Set(),
disconnect: new Set(),
chainChanged: new Set()
}
};
var promResolvers = new Map();
var getListnersCount = function () {
var _a;
var count = 0;
for (var _i = 0, _b = Object.keys(listners); _i < _b.length; _i++) {
var key = _b[_i];
if (key === 'once') {
for (var _c = 0, _d = Object.keys(listners[key]); _c < _d.length; _c++) {
var onceKey = _d[_c];
count += (_a = listners[key][onceKey]) === null || _a === void 0 ? void 0 : _a.length;
}
}
else {
count += listners[key].length;
}
}
return count;
};
var sendMessage = function (args, ping) {
if (ping === void 0) { ping = false; }
if (Object.values(promResolvers).filter(function (r) { return r; }).length < 10) {
return new Promise(function (resolve, reject) {
var resId = crypto.randomUUID();
promResolvers.set(resId, { resolve: resolve, reject: reject });
var data = { type: "CLWALLET_CONTENT", data: args, resId: resId };
if (ping) {
data.type = 'CLWALLET_PING';
}
// console.log('data in', data)
window.postMessage(data, "*");
});
}
else {
return new Promise(function (resolve, reject) {
reject(new Error("You have reached the maximum number of concurent wallet messeges."));
});
}
};
var MetaMaskAPI = /** @class */ (function () {
function MetaMaskAPI() {
this.isMetaMask = true;
this._state = { accounts: Array(1), isConnected: true, isUnlocked: true, initialized: true, isPermanentlyDisconnected: false };
this._sentWarnings = { enable: false, experimentalMethods: false, send: false, events: {} };
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
this.chainId = "0x89";
// Deprecated - hardcoded for now, websites should not access this directly since is deprecated for a long time
this.networkVersion = "137";
this.selectedAddress = null;
this.autoRefreshOnNetworkChange = false;
// Internal Simulate Metamask
this._events = {};
this._eventsCount = 2;
this._jsonRpcConnection = {};
this._log = {};
this._maxListeners = 100;
this._metamask = new Proxy({
isUnlocked: function () {
return Promise.resolve(true);
},
requestBatch: function () {
// empty
}
}, {});
this._rpcEngine = {
_events: {}, _eventsCount: 0, _maxListeners: undefined, _middleware: Array(4)
};
}
MetaMaskAPI.prototype.isConnected = function () {
return false;
};
// for maximum compatibility since is cloning the same API
MetaMaskAPI.prototype.enable = function () {
return sendMessage({ method: 'eth_requestAccounts', params: Array(0) });
};
MetaMaskAPI.prototype.request = function (args) {
return sendMessage(args);
};
// Deprecated
MetaMaskAPI.prototype.sendAsync = function (arg1, arg2) {
return this.send(arg1, arg2);
};
// Deprecated
MetaMaskAPI.prototype.send = function (arg1, arg2) {
if (typeof arg1 === 'string') {
return sendMessage({
method: arg1,
params: arg2
});
}
else if (arg2 === undefined) {
console.error('Clear Wallet: Sync calling is deprecated and not supported');
}
else {
sendMessage(arg1).then(function (result) {
if (typeof arg2 === 'function') {
arg2(undefined, {
id: arg1 === null || arg1 === void 0 ? void 0 : arg1.id,
jsonrpc: '2.0',
method: arg1.method,
result: result
});
}
})["catch"](function (e) {
arg2(new Error(e), {
id: arg1 === null || arg1 === void 0 ? void 0 : arg1.id,
jsonrpc: '2.0',
method: arg1.method,
error: new Error(e)
});
});
}
};
MetaMaskAPI.prototype.on = function (eventName, callback) {
this.addListener(eventName, callback);
return this;
};
MetaMaskAPI.prototype.addListener = function (eventName, callback) {
switch (eventName) {
case 'accountsChanged':
listners.accountsChanged.add(callback);
break;
case 'connect':
listners.connect.add(callback);
sendMessage({
method: 'wallet_ready'
}, true);
break;
case 'disconnect':
case 'close':
listners.disconnect.add(callback);
break;
// Deprecated - chainIdChanged -networkChanged
case 'chainChanged':
case 'chainIdChanged':
case 'networkChanged':
listners.chainChanged.add(callback);
break;
}
return this;
};
MetaMaskAPI.prototype.once = function (eventName, callback) {
switch (eventName) {
case 'accountsChanged':
listners.once.accountsChanged.add(callback);
break;
case 'connect':
listners.once.connect.add(callback);
sendMessage({
method: 'wallet_ready'
}, true);
break;
case 'disconnect':
case 'close':
listners.once.disconnect.add(callback);
break;
// Deprecated - chainIdChanged -networkChanged
case 'chainChanged':
case 'chainIdChanged':
case 'networkChanged':
listners.once.chainChanged.add(callback);
break;
}
return this;
};
MetaMaskAPI.prototype.off = function (eventName, callback) {
(this).removeListener(eventName, callback);
return this;
};
MetaMaskAPI.prototype.removeListener = function (eventName, callback) {
switch (eventName) {
case 'accountsChanged':
listners.accountsChanged["delete"](callback);
break;
case 'connect':
listners.connect["delete"](callback);
break;
case 'disconnect':
case 'close':
listners.disconnect["delete"](callback);
break;
// Deprecated - chainIdChanged -networkChanged
case 'chainChanged':
case 'chainIdChanged':
case 'networkChanged':
listners.chainChanged["delete"](callback);
break;
default:
return;
}
return this;
};
MetaMaskAPI.prototype.removeAllListeners = function () {
listners.accountsChanged.clear();
listners.chainChanged.clear();
listners.disconnect.clear();
listners.connect.clear();
return this;
};
MetaMaskAPI.prototype.getMaxListeners = function () {
return 100;
};
MetaMaskAPI.prototype._getExperimentalApi = function () {
return this._metamask;
};
MetaMaskAPI.prototype.eventNames = function () {
return [];
};
MetaMaskAPI.prototype.listenerCount = function () {
return getListnersCount();
};
MetaMaskAPI.prototype.listners = function () { return []; };
MetaMaskAPI.prototype.rawListners = function () { return []; };
// Internal Simulate Metamask
MetaMaskAPI.prototype._warnOfDeprecation = function () { return true; };
MetaMaskAPI.prototype._rpcRequest = function () { return true; };
MetaMaskAPI.prototype._handleAccountsChanged = function () { return true; };
MetaMaskAPI.prototype._handleChainChanged = function () { return true; };
MetaMaskAPI.prototype._handleConnect = function () { return true; };
MetaMaskAPI.prototype._handleDisconnect = function () { return true; };
MetaMaskAPI.prototype._handleStreamDisconnect = function () { return true; };
MetaMaskAPI.prototype._handleUnlockStateChanged = function () { return true; };
MetaMaskAPI.prototype._sendSync = function () {
console.error('Clear Wallet: Sync calling is deprecated and not supported');
};
return MetaMaskAPI;
}());
var eth = new Proxy(new MetaMaskAPI(), {
// set: () => { return true },
// get: function(target, name, receiver) {
// if (typeof (<any>target)[name] == 'function') {
// return function (...args: any) {
// console.dir({ call: [name, ...args] });
// return undefined;
// }
// }
// let check = true
// setTimeout(() => check = false, 400)
// while(check){
// // igmore
// }
// },
deleteProperty: function () { return false; }
});
var listner = function (event) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11;
if (event.source != window)
return;
if (event.data.type && (event.data.type === "CLWALLET_PAGE")) {
try {
if ((_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error) {
(_c = promResolvers.get(event.data.resId)) === null || _c === void 0 ? void 0 : _c.reject(event.data.data);
console.error((_d = event === null || event === void 0 ? void 0 : event.data) === null || _d === void 0 ? void 0 : _d.data);
}
else {
(_e = promResolvers.get(event.data.resId)) === null || _e === void 0 ? void 0 : _e.resolve(event.data.data);
}
promResolvers["delete"](event.data.resId);
}
catch (e) {
console.log('Failed to connect resolve msg', e);
}
}
else if (event.data.type && (event.data.type === "CLWALLET_PAGE_LISTENER")) {
if (((_h = (_g = (_f = event === null || event === void 0 ? void 0 : event.data) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.listner) !== null && _h !== void 0 ? _h : 'x') in listners) {
try {
var listnerName_1 = (_k = (_j = event === null || event === void 0 ? void 0 : event.data) === null || _j === void 0 ? void 0 : _j.data) === null || _k === void 0 ? void 0 : _k.listner;
if (listnerName_1 === 'connect' && ((_m = (_l = event === null || event === void 0 ? void 0 : event.data) === null || _l === void 0 ? void 0 : _l.data) === null || _m === void 0 ? void 0 : _m.data)) {
eth.networkVersion = (_s = (_r = (_q = (_p = (_o = event === null || event === void 0 ? void 0 : event.data) === null || _o === void 0 ? void 0 : _o.data) === null || _p === void 0 ? void 0 : _p.data) === null || _q === void 0 ? void 0 : _q.chainId) === null || _r === void 0 ? void 0 : _r.toString(10)) !== null && _s !== void 0 ? _s : '137';
eth.chainId = (_w = (_v = (_u = (_t = event === null || event === void 0 ? void 0 : event.data) === null || _t === void 0 ? void 0 : _t.data) === null || _u === void 0 ? void 0 : _u.data) === null || _v === void 0 ? void 0 : _v.chainId) !== null && _w !== void 0 ? _w : '0x89';
eth.selectedAddress = (_z = (_y = (_x = event === null || event === void 0 ? void 0 : event.data) === null || _x === void 0 ? void 0 : _x.data) === null || _y === void 0 ? void 0 : _y.address) !== null && _z !== void 0 ? _z : null;
eth.isConnected = function () { return true; };
}
else if (listnerName_1 === 'chainChanged') {
console.log((_1 = (_0 = event === null || event === void 0 ? void 0 : event.data) === null || _0 === void 0 ? void 0 : _0.data) === null || _1 === void 0 ? void 0 : _1.data);
eth.networkVersion = (_4 = (_3 = (_2 = event === null || event === void 0 ? void 0 : event.data) === null || _2 === void 0 ? void 0 : _2.data) === null || _3 === void 0 ? void 0 : _3.data.toString(10)) !== null && _4 !== void 0 ? _4 : '137';
eth.chainId = (_7 = (_6 = (_5 = event === null || event === void 0 ? void 0 : event.data) === null || _5 === void 0 ? void 0 : _5.data) === null || _6 === void 0 ? void 0 : _6.data) !== null && _7 !== void 0 ? _7 : '0x89';
}
else if (listnerName_1 === 'accountsChanged') {
eth.selectedAddress = (_11 = (_10 = (_9 = (_8 = event === null || event === void 0 ? void 0 : event.data) === null || _8 === void 0 ? void 0 : _8.data) === null || _9 === void 0 ? void 0 : _9.data) === null || _10 === void 0 ? void 0 : _10.address) !== null && _11 !== void 0 ? _11 : 'dummy-string';
}
listners[listnerName_1].forEach(function (listner) { var _a, _b; return listner((_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.data); });
listners.once[listnerName_1].forEach(function (listner) {
var _a, _b;
listner((_b = (_a = event === null || event === void 0 ? void 0 : event.data) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.data);
listners.once[listnerName_1]["delete"](listner);
});
}
catch (e) {
console.error(e);
// ignore
}
}
}
};
window.addEventListener("message", listner);
var proxy1 = new Proxy({
// on: (event: any, callback:any) => { if (event === 'message') {
// debugger;
// callback(true, true)
// } }
}, {
get: function (target, name, receiver) {
if (typeof target[name] == 'function') {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
console.dir({ call: __spreadArray([name], args, true) });
};
}
console.log('ETH', name.toString(), target, receiver);
return undefined;
}
});
var proxy2 = new Proxy({
// on: (event: any, callback:any) => { if (event === 'message') {
// debugger;
// callback(true, true)
// } }
}, {
get: function (target, name, receiver) {
if (typeof target[name] == 'function') {
return function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
console.dir({ call: __spreadArray([name], args, true) });
};
}
console.log('web3', name.toString(), target, receiver);
return undefined;
}
});
var web3Shim = {
currentProvider: eth,
__isMetaMaskShim__: true
};
var injectWallet = function (win) {
Object.defineProperty(win, 'ethereum', {
value: eth
});
Object.defineProperty(win, 'web3', {
value: web3Shim
});
sendMessage({
method: 'wallet_ready'
}, true);
console.log('Clear wallet injected', window.ethereum, win);
};
injectWallet(this);
// setTimeout(() => {
// // console.log('Metamask clone test');
// // (<any>window).ethereum.request({method: 'eth_requestAccounts', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'eth_accounts', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'eth_chainId', params: Array(0)}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'wallet_requestPermissions', params: [{eth_accounts: {}}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'net_version', params: []}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.request({method: 'wallet_switchEthereumChain', params: [{chainId: "0x89"}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum2.request({method: 'wallet_switchEthereumChain', params: [{chainId: "0x89"}]}).then((res: any) => { console.log(res, '111111111')});
// // (<any>window).ethereum.on('connect', ((a: any, b: any) => console.log('connect', a, b)));
// // (<any>window).ethereum.on('accountsChanged', ((a: any, b: any) => console.log('accountsChanged', a, b)));
// // (<any>window).ethereum.on('chainChanged', ((a: any) => console.log('chainChanged', a, typeof a)));
// // console.log((<any>window).ethereum.on('message', (a: any, b:any) => console.log(a,b)))
// console.log((<any>window).ethereum.toString())
// console.log((<any>window).ethereum2.toString())
// console.log((<any>window).ethereum.Symbold)
// }, 3500)
// console.log( (window as any).ethereum.request({method: 'eth_chainId'}))
`
container.prepend(script);
script.parentElement?.removeChild(script)

View File

@ -1,7 +1,6 @@
import { createApp } from 'vue'
import App from './App.vue'
import router from './router';
import { IonicVue } from '@ionic/vue';
/* Core CSS required for Ionic components to work properly */

View File

@ -27,6 +27,10 @@ const routes: Array<RouteRecordRaw> = [
path: '/wallet-error/:rid/:param',
component: () => import('@/views/WalletError.vue'),
},
{
path: '/request-network/:rid/:param',
component: () => import('@/views/RequestNetwork.vue'),
},
{
path: '/tabs/',
component: AppTabs,

View File

@ -137,8 +137,16 @@ export const getRandomPk = () => {
).substring(0, 66)
}
export const smallRandomString = () => {
return (Math.random() + 1).toString(36).substring(7);
export const smallRandomString = (size = 7) => {
if(size <= 7) {
return (Math.random() + 1).toString(36).substring(0,7);
} else {
let str = ''
for(let i = 0; i < (size / 7) << 0; i++){
str+=(Math.random() + i).toString(36).substring(0,7);
}
return str.substring(0, size)
}
}
export const clearPk = async (): Promise<void> => {

View File

@ -0,0 +1,218 @@
<template>
<ion-page>
<ion-header>
<ion-toolbar>
<ion-title>Add and Switch Network</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-item>
<ion-text>{{ website }}</ion-text>
</ion-item>
<ion-item>
<ion-text>has requested network you to add and switch to the following</ion-text>
</ion-item>
<ion-list>
<ion-item>
<ion-list>
<ion-item><b>NETWORK:</b></ion-item>
<ion-item>
<ion-list>
<ion-item>
<ion-label>Name:</ion-label>
<ion-input
style="margin-left: 0.5rem"
v-model="name"
readonly
placeholder="ex: Polygon"
></ion-input>
</ion-item>
<ion-item>
<ion-label>ChainId: </ion-label>
<ion-input
style="margin-left: 0.5rem"
v-model="chainId"
readonly
placeholder="137"
></ion-input>
</ion-item>
<ion-item button>
<ion-label>RPC URL: </ion-label>
<ion-input
style="margin-left: 0.5rem"
readonly
placeholder="https://polygon-mainnet.g.alchemy.com/..."
v-model="rpc"
></ion-input>
</ion-item>
<ion-item button>
<ion-label>Native Token Symbol: </ion-label>
<ion-input
style="margin-left: 0.5rem"
readonly
placeholder="MATIC"
v-model="symbol"
></ion-input>
</ion-item>
<ion-item button>
<ion-label>Explorer: </ion-label>
<ion-input
style="margin-left: 0.5rem"
readonly
placeholder="https://polygonscan.com"
v-model="explorer"
></ion-input>
</ion-item>
</ion-list>
</ion-item>
<ion-item>
<ion-button @click="onCancel">Cancel</ion-button>
<ion-button @click="onAddSwitch">Add Network and Switch</ion-button>
</ion-item>
</ion-list>
</ion-item>
<ion-item>Auto-reject Timer: {{ timerReject }}</ion-item>
</ion-list>
<ion-loading
:is-open="loading"
cssClass="my-custom-class"
message="Please wait..."
:duration="4000"
@didDismiss="loading = false"
/>
</ion-content>
</ion-page>
</template>
<script lang="ts">
import { defineComponent, ref } from "vue";
import {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonItem,
IonLabel,
IonButton,
IonText,
IonLoading,
onIonViewWillEnter,
IonList,
IonInput,
} from "@ionic/vue";
import { useRoute } from "vue-router";
import { getUrl, saveSelectedNetwork, saveNetwork, hexTostr } from "@/utils/platform";
import type { Network } from "@/extension/types";
import { mainNets, testNets } from "@/utils/networks";
import { approve, walletPing } from "@/extension/userRequest";
import { triggerListner } from "@/extension/listners";
export default defineComponent({
components: {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonItem,
IonLabel,
IonButton,
IonText,
IonLoading,
IonList,
IonInput,
},
setup: () => {
const route = useRoute();
const loading = ref(true);
const rid = (route?.params?.rid as string) ?? "";
const networkData = hexTostr((route.params?.param as string) ?? "");
const alertOpen = ref(false);
const templateNetworks = Object.assign({}, mainNets, testNets) ?? {};
const timerReject = ref(140);
let interval: any;
const website = ref("");
const name = ref("");
const chainId = ref("");
const rpc = ref("");
const symbol = ref("");
const explorer = ref("");
const onCancel = () => {
window.close();
if (interval) {
try {
clearInterval(interval);
} catch {
// ignore
}
}
};
onIonViewWillEnter(async () => {
(window as any)?.resizeTo?.(600, 600);
try {
if (!networkData) {
onCancel();
} else {
const data = JSON.parse(networkData);
name.value = data.chainName;
chainId.value = data.chainId;
rpc.value = data.rpcUrls[0];
symbol.value = data.nativeCurrency.symbol;
explorer.value = data.blockExplorerUrls[0];
website.value = data.website;
}
} catch {
onCancel();
}
loading.value = false;
interval = setInterval(async () => {
if (timerReject.value <= 0) {
onCancel();
return;
}
timerReject.value -= 1;
walletPing();
}, 1000) as any;
});
const onAddSwitch = async () => {
loading.value = true;
const network: Network = {
chainId: Number(chainId.value),
name: name.value,
explorer: explorer.value,
rpc: rpc.value,
symbol: symbol.value,
};
await saveNetwork(network);
await saveSelectedNetwork(network);
triggerListner("chainChanged", chainId.value);
approve(rid);
loading.value = false;
};
return {
onCancel,
alertOpen,
loading,
templateNetworks,
getUrl,
onAddSwitch,
timerReject,
website,
chainId,
name,
rpc,
explorer,
symbol,
};
},
});
</script>

View File

@ -78,7 +78,7 @@ export default defineComponent({
const route = useRoute();
const loading = ref(false);
const rid = (route?.params?.rid as string) ?? "";
const signMsg = ref(hexTostr((route?.params?.param as string) ?? ""));
const signMsg = ref(hexTostr(hexTostr((route?.params?.param as string) ?? "")));
const alertOpen = ref(false);
const alertMsg = ref("");
const timerReject = ref(140);
@ -130,7 +130,7 @@ export default defineComponent({
const modalResult = await openModal();
if (modalResult) {
unBlockLockout();
loading.value = true
loading.value = true;
approve(rid);
} else {
onCancel();

View File

@ -187,6 +187,7 @@ import {
unBlockLockout,
getSelectedAccount,
strToHex,
hexTostr,
} from "@/utils/platform";
import { getBalance, getGasPrice, estimateGas } from "@/utils/wallet";
import type { Network } from "@/extension/types";
@ -216,7 +217,7 @@ export default defineComponent({
const route = useRoute();
const rid = (route?.params?.rid as string) ?? "";
let isError = false;
const decodedParam = decodeURIComponent((route.params?.param as string) ?? "");
const decodedParam = hexTostr((route.params?.param as string) ?? "");
const params = JSON.parse(decodedParam);
const signTxData = ref("");
const alertOpen = ref(false);

View File

@ -12,64 +12,74 @@
</ion-item>
<ion-list>
<ion-item v-if="networkCase === 'exists' || networkCase === 'inTemplates'">
<ion-list>
<ion-item><b>Switch</b></ion-item>
<ion-item>From:</ion-item>
<ion-item>
<ion-list>
<ion-item><b>Switch</b></ion-item>
<ion-item>From:</ion-item>
<ion-item>Network Name: {{ selectedNetwork?.name }}</ion-item>
<ion-item>
<ion-list>
<ion-item>Network Name: {{ selectedNetwork?.name }}</ion-item>
<ion-item>
<ion-avatar
v-if="(templateNetworks as any)[selectedNetwork?.chainId]?.icon"
style="margin-right: 1rem; width: 1.8rem; height: 1.8rem"
>
<img
:alt="selectedNetwork?.name"
:src="getUrl('assets/chain-icons/' + (templateNetworks as any)[selectedNetwork?.chainId]?.icon)"
/>
</ion-avatar>
<ion-label>Network ID: {{ selectedNetwork?.chainId }}</ion-label>
</ion-item>
</ion-list>
</ion-item>
<ion-item>To</ion-item>
<ion-item>
<ion-list>
<ion-item>Network Name: {{ (templateNetworks as any)[networkId]?.name }}</ion-item>
<ion-item>
<ion-avatar
v-if="(templateNetworks as any)[networkId]?.icon"
style="margin-right: 1rem; width: 1.8rem; height: 1.8rem"
>
<img
:alt="selectedNetwork?.name"
:src="getUrl('assets/chain-icons/' + (templateNetworks as any)[networkId]?.icon)"
/>
</ion-avatar>
<ion-label>Network ID: {{ (templateNetworks as any)[networkId]?.chainId }}</ion-label>
</ion-item>
</ion-list>
</ion-item>
<ion-item>
<ion-button @click="onCancel">Cancel</ion-button>
<ion-button v-if="networkCase === 'exists'" @click="onSwitchExists">Switch</ion-button>
<ion-button v-else @click="onSwitchTemplates">Add Network and Switch</ion-button>
<ion-avatar
v-if="(templateNetworks as any)[selectedNetwork?.chainId]?.icon"
style="margin-right: 1rem; width: 1.8rem; height: 1.8rem"
>
<img
:alt="selectedNetwork?.name"
:src="getUrl('assets/chain-icons/' + (templateNetworks as any)[selectedNetwork?.chainId]?.icon)"
/>
</ion-avatar>
<ion-label>Network ID: {{ selectedNetwork?.chainId }}</ion-label>
</ion-item>
</ion-list>
</ion-item>
<ion-item v-else>
<ion-list>
<ion-item>Request to change to unknown network ID: {{ networkId }}</ion-item>
<ion-item>Do you want to go to {{ addChainUrl }}</ion-item>
<ion-item>To add it manually.</ion-item>
<ion-item>
<ion-button @click="onCancel">No</ion-button>
<ion-button @click="onSwitchNotExisting">Yes</ion-button>
<ion-item>To</ion-item>
<ion-item>
<ion-list>
<ion-item
>Network Name:
{{ (existingNetworks as any)[networkId]?.name }}</ion-item
>
<ion-item>
<ion-avatar
v-if="(existingNetworks as any)[networkId]?.icon"
style="margin-right: 1rem; width: 1.8rem; height: 1.8rem"
>
<img
:alt="(existingNetworks as any)[networkId]?.name"
:src="getUrl('assets/chain-icons/' + (existingNetworks as any)[networkId].icon)"
/>
</ion-avatar>
<ion-label
>Network ID:
{{ (existingNetworks as any)[networkId]?.chainId }}</ion-label
>
</ion-item>
</ion-list>
</ion-list>
</ion-item>
<ion-item>
<ion-button @click="onCancel">Cancel</ion-button>
<ion-button v-if="networkCase === 'exists'" @click="onSwitchExists"
>Switch</ion-button
>
<ion-button v-else @click="onSwitchTemplates"
>Add Network and Switch</ion-button
>
</ion-item>
<ion-item>Auto-reject Timer: {{ timerReject }}</ion-item>
</ion-list>
</ion-item>
<ion-item v-else>
<ion-list>
<ion-item>Request to change to unknown network ID: {{ networkId }}</ion-item>
<ion-item>Do you want to go to {{ addChainUrl }}</ion-item>
<ion-item>To add it manually.</ion-item>
<ion-item>
<ion-button @click="onCancel">No</ion-button>
<ion-button @click="onSwitchNotExisting">Yes</ion-button>
</ion-item>
</ion-list>
</ion-item>
<ion-item>Auto-reject Timer: {{ timerReject }}</ion-item>
</ion-list>
<ion-alert
:is-open="alertOpen"
@ -107,13 +117,23 @@ import {
IonLoading,
onIonViewWillEnter,
IonList,
IonAvatar,
} from "@ionic/vue";
// import { ethers } from "ethers";
import { useRoute } from "vue-router";
import { getSelectedNetwork, getNetworks, getUrl, saveSelectedNetwork, saveNetwork, openTab} from "@/utils/platform";
import {
getSelectedNetwork,
getNetworks,
getUrl,
saveSelectedNetwork,
saveNetwork,
openTab,
numToHexStr,
} from "@/utils/platform";
import type { Network, Networks } from "@/extension/types";
import { mainNets, testNets } from "@/utils/networks";
import { approve, walletPing } from '@/extension/userRequest'
import { approve, walletPing } from "@/extension/userRequest";
import { triggerListner } from "@/extension/listners";
export default defineComponent({
components: {
@ -129,27 +149,29 @@ export default defineComponent({
IonText,
IonLoading,
IonList,
IonAvatar,
},
setup: () => {
const route = useRoute();
const loading = ref(true);
const rid = (route?.params?.rid as string) ?? "";
const networkId = ref(String(Number(route?.params?.param as string ?? "")));
const networkId = ref(String(Number((route?.params?.param as string) ?? "")));
const alertOpen = ref(false);
const selectedNetwork = (ref(null) as unknown) as Ref<Network>;
const alertMsg = ref("");
const networkCase = ref("");
let pnetworks: Promise<Networks>;
const templateNetworks = Object.assign({}, mainNets, testNets) ?? {};
const addChainUrl = `${chainListPage}${networkId.value}`
const addChainUrl = `${chainListPage}${networkId.value}`;
const timerReject = ref(140);
let interval: any
let interval: any;
const existingNetworks = ref({});
const onCancel = () => {
window.close();
if(interval) {
if (interval) {
try {
clearInterval(interval)
clearInterval(interval);
} catch {
// ignore
}
@ -157,14 +179,15 @@ export default defineComponent({
};
onIonViewWillEnter(async () => {
(window as any)?.resizeTo?.(600, 600)
(window as any)?.resizeTo?.(600, 600);
pnetworks = getNetworks();
selectedNetwork.value = await getSelectedNetwork();
console.log(networkId.value)
const existingNetworks = await pnetworks;
if ((networkId.value ?? "0") in existingNetworks ?? {}) {
console.log(networkId.value);
existingNetworks.value = await pnetworks;
if ((networkId.value ?? "0") in existingNetworks.value ?? {}) {
networkCase.value = "exists";
} else if ((networkId.value ?? "0") in templateNetworks) {
existingNetworks.value = templateNetworks;
networkCase.value = "inTemplates";
} else {
networkCase.value = "doesNotExist";
@ -172,38 +195,40 @@ export default defineComponent({
loading.value = false;
interval = setInterval(async () => {
if(timerReject.value <= 0) {
onCancel()
if (timerReject.value <= 0) {
onCancel();
return;
}
timerReject.value -= 1
walletPing()
}, 1000) as any
timerReject.value -= 1;
walletPing();
}, 1000) as any;
});
const onSwitchExists = async () => {
loading.value = true;
const existingNetworks = await pnetworks;
selectedNetwork.value = existingNetworks[Number(networkId.value)]
await saveSelectedNetwork(selectedNetwork.value)
selectedNetwork.value = existingNetworks[Number(networkId.value)];
await saveSelectedNetwork(selectedNetwork.value);
triggerListner("chainChanged", numToHexStr(selectedNetwork.value?.chainId ?? 0));
approve(rid);
loading.value = false;
};
const onSwitchTemplates = async () => {
loading.value = true;
selectedNetwork.value = templateNetworks[Number(networkId.value)]
await saveNetwork(templateNetworks[Number(networkId.value)])
await saveSelectedNetwork(templateNetworks[Number(networkId.value)])
selectedNetwork.value = templateNetworks[Number(networkId.value)];
await saveNetwork(templateNetworks[Number(networkId.value)]);
await saveSelectedNetwork(templateNetworks[Number(networkId.value)]);
triggerListner("chainChanged", numToHexStr(selectedNetwork.value?.chainId ?? 0));
approve(rid);
loading.value = false;
};
const onSwitchNotExisting = async () => {
loading.value = true;
openTab(addChainUrl)
onCancel()
loading.value = true;
openTab(addChainUrl);
onCancel();
};
return {
@ -220,7 +245,8 @@ export default defineComponent({
onSwitchTemplates,
onSwitchNotExisting,
addChainUrl,
timerReject
timerReject,
existingNetworks,
};
},
});

View File

@ -52,7 +52,7 @@ import {
IonLoading,
} from "@ionic/vue";
import { useRoute } from "vue-router";
import { hexTostr } from "@/utils/platform";
export default defineComponent({
components: {
@ -69,7 +69,7 @@ export default defineComponent({
},
setup: () => {
const route = useRoute();
const error = decodeURIComponent((route.params?.param as string) ?? "");
const error = hexTostr((route.params?.param as string) ?? "");
const loading = ref(true);
const onCancel = () => {

View File

@ -31,7 +31,7 @@ export default defineConfig({
chunkSizeWarningLimit: 1000,
commonjsOptions: {
transformMixedEsModules: true
}
},
},
plugins: [
!production &&

1592
yarn.lock

File diff suppressed because it is too large Load Diff