Skip to content

Commit

Permalink
feat: used as desktop's rabbyx.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Dec 5, 2022
1 parent 6456873 commit 0847679
Show file tree
Hide file tree
Showing 37 changed files with 670 additions and 441 deletions.
14 changes: 11 additions & 3 deletions build/paths.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
const path = require('path');
const fs = require('fs');

const appRoot = fs.realpathSync(process.cwd());
const appRoot = fs.realpathSync(path.resolve(__dirname, '..'));

const desktopRepo = process.env.RABBY_DESKTOP_REPO || path.join(appRoot, '../RabbyDesktop');
if (!fs.existsSync(desktopRepo)) {
console.log('RabbyDesktop repo not found at ' + desktopRepo);
desktopRepo = null;
} else {
console.log('Using RabbyDesktop repo at ' + desktopRepo);
};

const rootResolve = path.resolve.bind(path, appRoot);

module.exports = {
root: appRoot,
src: rootResolve('src'),
popupHtml: rootResolve('src/ui/popup.html'),
popupHtml: rootResolve('src/ui/popup.ejs'),
notificationHtml: rootResolve('src/ui/notification.html'),
indexHtml: rootResolve('src/ui/index.html'),
backgroundHtml: rootResolve('src/background/background.html'),
dist: rootResolve('dist'),
dist: desktopRepo ? path.resolve(desktopRepo, './assets/chrome_exts/rabby') : rootResolve('dist'),

rootResolve,
}
39 changes: 24 additions & 15 deletions build/webpack.common.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const config = {
sideEffects: true,
test: /[\\/]pageProvider[\\/]index.ts/,
loader: 'ts-loader',
options: {
compilerOptions: {
outDir: paths.dist,
},
}
},
{
test: /[\\/]ui[\\/]index.tsx/,
Expand All @@ -58,6 +63,7 @@ const config = {
}),
compilerOptions: {
module: 'es2015',
outDir: paths.dist,
},
},
},
Expand Down Expand Up @@ -96,6 +102,9 @@ const config = {
}),
],
}),
compilerOptions: {
outDir: paths.dist,
}
},
},
],
Expand Down Expand Up @@ -178,28 +187,28 @@ const config = {
],
},
plugins: [
new ESLintWebpackPlugin({
extensions: ['ts', 'tsx', 'js', 'jsx'],
}),
// new ESLintWebpackPlugin({
// extensions: ['ts', 'tsx', 'js', 'jsx'],
// }),
// new AntdDayjsWebpackPlugin(),
new HtmlWebpackPlugin({
inject: true,
template: paths.popupHtml,
chunks: ['ui'],
filename: 'popup.html',
}),
new HtmlWebpackPlugin({
inject: true,
template: paths.notificationHtml,
chunks: ['ui'],
filename: 'notification.html',
}),
new HtmlWebpackPlugin({
inject: true,
template: paths.indexHtml,
chunks: ['ui'],
filename: 'index.html',
}),
// new HtmlWebpackPlugin({
// inject: true,
// template: paths.notificationHtml,
// chunks: ['ui'],
// filename: 'notification.html',
// }),
// new HtmlWebpackPlugin({
// inject: true,
// template: paths.indexHtml,
// chunks: ['ui'],
// filename: 'index.html',
// }),
new HtmlWebpackPlugin({
inject: true,
template: paths.backgroundHtml,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "A browser plugin for DeFi users",
"scripts": {
"postinstall": "patch-package",
"clean": "mkdir -p dist && rm -rf dist/* && cp -r _raw/* dist",
"clean": "./scripts/clean-assets.sh",
"make-vars": "node scripts/make-vars.js",
"build:dev": "npm run clean && npm run make-vars && TAILWIND_MODE=watch webpack --progress --env config=dev",
"build:pro": "npm run clean && npm run make-vars && webpack --progress --env config=pro",
"build:debug": "npm run clean && npm run make-vars && webpack --progress --env config=debug",
"build:sourcemap": "npm run clean && npm run make-vars && webpack --progress --env config=sourcemap",
"build:dev": "npm run clean && TAILWIND_MODE=watch webpack --progress --env config=dev",
"build:pro": "npm run clean && webpack --progress --env config=pro",
"build:debug": "npm run clean && webpack --progress --env config=debug",
"build:sourcemap": "npm run clean && webpack --progress --env config=sourcemap",
"lint:fix": "eslint --fix --ext .js,.jsx,.ts,.tsx ./src",
"test": "jest",
"pub": "node build/release.js"
Expand Down
11 changes: 11 additions & 0 deletions scripts/clean-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
script_dir="$( cd "$( dirname "$0" )" && pwd )"
project_dir=$(dirname "$script_dir")

if [ -z $RABBY_DESKTOP_REPO ]; then
export RABBY_DESKTOP_REPO=$( cd "$project_dir/../RabbyDesktop" && pwd )
echo "[clean-assets] RABBY_DESKTOP_REPO is not set, use default: $RABBY_DESKTOP_REPO"
else
echo "[clean-assets] RABBY_DESKTOP_REPO is set to: $RABBY_DESKTOP_REPO"
fi

mkdir -p $RABBY_DESKTOP_REPO/assets/chrome_exts/rabby && rm -rf $RABBY_DESKTOP_REPO/assets/chrome_exts/rabby/* && cp -r _raw/* $RABBY_DESKTOP_REPO/assets/chrome_exts/rabby
104 changes: 77 additions & 27 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,16 +344,16 @@ export class WalletController extends BaseController {
Sentry.captureException(
new Error(
'postGasStationOrder failed, params: ' +
JSON.stringify({
userAddr: account.address,
fromChainId: others.chainServerId,
fromTxId: txId,
toChainId: toChainId,
toTokenAmount,
fromTokenId: others.tokenId,
fromTokenAmount: fromTokenAmount,
fromUsdValue,
})
JSON.stringify({
userAddr: account.address,
fromChainId: others.chainServerId,
fromTxId: txId,
toChainId: toChainId,
toTokenAmount,
fromTokenId: others.tokenId,
fromTokenAmount: fromTokenAmount,
fromUsdValue,
})
)
);
}
Expand Down Expand Up @@ -500,11 +500,11 @@ export class WalletController extends BaseController {
$ctx:
needApprove && pay_token_id !== chain.nativeTokenAddress
? {
ga: {
...$ctx?.ga,
source: 'approvalAndSwap|swap',
},
}
ga: {
...$ctx?.ga,
source: 'approvalAndSwap|swap',
},
}
: $ctx,
method: 'eth_sendTransaction',
params: [swapParam],
Expand Down Expand Up @@ -2279,18 +2279,18 @@ export class WalletController extends BaseController {
}: {
list: (
| {
chainServerId: string;
contractId: string;
spender: string;
abi: 'ERC721' | 'ERC1155' | '';
tokenId: string | null | undefined;
isApprovedForAll: boolean;
}
chainServerId: string;
contractId: string;
spender: string;
abi: 'ERC721' | 'ERC1155' | '';
tokenId: string | null | undefined;
isApprovedForAll: boolean;
}
| {
chainServerId: string;
id: string;
spender: string;
}
chainServerId: string;
id: string;
spender: string;
}
)[];
}) => {
const queue = new PQueue({
Expand Down Expand Up @@ -2325,4 +2325,54 @@ export class WalletController extends BaseController {
};
}

export default new WalletController();
const walletController = new WalletController();

export default walletController;

window.rabbyDesktop.ipcRenderer.on('rabbyx-rpc-query', (payload) => {
if (!payload.rpcId) {
throw new Error('[rabbyx-rpc-query] rpcId is required');
}

switch (payload.method) {
case 'walletController.boot': {
const [password] = payload.params;
window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', {
rpcId: payload.rpcId,
result: walletController.boot(password),
});
break;
}
case 'walletController.isBooted': {
window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', {
rpcId: payload.rpcId,
result: walletController.isBooted(),
});
break;
}
case 'walletController.isUnlocked': {
window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', {
rpcId: payload.rpcId,
result: walletController.isUnlocked(),
});
break;
}
case 'walletController.lockWallet': {
window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', {
rpcId: payload.rpcId,
result: walletController.lockWallet(),
});
break;
}
case 'walletController.getConnectedSites': {
window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', {
rpcId: payload.rpcId,
result: walletController.getConnectedSites(),
});
break;
}
default: {
throw new Error(`[rabbyx-rpc-query] method ${payload.method} is not supported`);
}
}
});
30 changes: 30 additions & 0 deletions src/background/desktop-inject/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
interface Window {
rabbyDesktop: {
ipcRenderer: {
sendMessage<T extends string>(
channel: T,
...args: any[]
): void;
on: {
<T extends string>(
channel: T,
func: (...args: any[]) => void
): (() => void) | undefined;
<T extends string>(
channel: T,
func: (event: any) => void
): (() => void) | undefined;
};
once: {
<T extends string>(
channel: T,
func: (...args: any[]) => void
): (() => void) | undefined;
<T extends string>(
channel: T,
func: (event: any) => void
): (() => void) | undefined;
};
};
};
}
2 changes: 2 additions & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path="desktop-inject/type.d.ts" />

import { groupBy } from 'lodash';
import 'reflect-metadata';
import * as Sentry from '@sentry/browser';
Expand Down
Loading

0 comments on commit 0847679

Please sign in to comment.