Skip to content

Commit

Permalink
wip: adapt to next layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016x committed Dec 29, 2022
1 parent 0bb9b4d commit 04b2ddb
Show file tree
Hide file tree
Showing 38 changed files with 726 additions and 192 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ dist
dist-*
.yarn
.DS_Store
yarn-error
yarn-error

tmp
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,
}
15 changes: 12 additions & 3 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,9 +187,9 @@ const config = {
],
},
plugins: [
new ESLintWebpackPlugin({
extensions: ['ts', 'tsx', 'js', 'jsx'],
}),
// new ESLintWebpackPlugin({
// extensions: ['ts', 'tsx', 'js', 'jsx'],
// }),
// new AntdDayjsWebpackPlugin(),
new HtmlWebpackPlugin({
inject: true,
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
43 changes: 43 additions & 0 deletions scripts/pack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env sh

script_dir="$( cd "$( dirname "$0" )" && pwd )"
project_dir=$(dirname "$script_dir")

RABBY_DESKTOP_REPO=$( cd "$project_dir/../RabbyDesktop" && pwd )

export VERSION=0.60.1;
export RABBYX_GIT_HASH=$(git rev-parse --short HEAD);

TARGET_FILE=$project_dir/tmp/RabbyX-v${VERSION}-${RABBYX_GIT_HASH}.zip;

echo "[pack] RABBY_DESKTOP_REPO is $RABBY_DESKTOP_REPO";

# for windows, download zip.exe from http://stahlworks.com/dev/index.php?tool=zipunzip and add to your path

# rm -rf $RABBY_DESKTOP_REPO/assets/chrome_exts/rabby;
if [ -z $NO_BUILD ]; then
yarn build:pro;
fi
echo "[pack] built finished";

cd $RABBY_DESKTOP_REPO/assets/chrome_exts/rabby;

rm -rf $project_dir/tmp/ && mkdir -p $project_dir/tmp/ && \
zip -r $TARGET_FILE ./*

cd $project_dir;
cp $TARGET_FILE $project_dir/tmp/RabbyX-latest.zip

# upload to storage
if [ -z $NO_UPLOAD ]; then
aws s3 cp $project_dir/tmp/ s3://$RABBY_BUILD_BUCKET/rabby/_tools/ --recursive --exclude="*" --include "*.zip" --acl public-read
aws cloudfront create-invalidation --distribution-id E1F7UQCCQWLXXZ --paths '/_tools/*.zip'
echo "[pack] uploaded.";
fi

# cp to RabbyDesktop
rm -rf $RABBY_DESKTOP_REPO/release/rabbyx/ && mkdir -p $RABBY_DESKTOP_REPO/release/rabbyx/;
cp $TARGET_FILE $RABBY_DESKTOP_REPO/release/rabbyx/
cp $TARGET_FILE $RABBY_DESKTOP_REPO/release/rabbyx/RabbyX-latest.zip

echo "[pack] finished.";
2 changes: 2 additions & 0 deletions src/background/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { default as providerController } from './provider';
export { default as walletController } from './wallet';

import './wallet-bridge';
140 changes: 140 additions & 0 deletions src/background/controller/wallet-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import walletController from "./wallet";

type LooseErrorObj = {
code?: string;
message?: string;
stack?: string;
}
async function runAndCatchErr<T = any>(proc: Function, mark?: string): Promise<{
result: T | null
error?: LooseErrorObj
}> {
try {
console.debug('[debug] runAndCatchErr:: mark', mark);
const result = await proc();
// console.debug('[debug] runAndCatchErr:: result', result);

return {
result: result
};
} catch (err) {
console.error(err)

return {
result: null,
error: {
code: (err as any).code,
message: err.message,
stack: err.stack,
}
};
}
}

type BridgePayload = {
rpcId: string
method: string
params: any[]
}

async function onRabbyxRpcQuery (payload: BridgePayload) {
if (!payload.rpcId) {
throw new Error('[rabbyx-rpc-query] rpcId is required');
}

let retPayload = {
result: null,
error: null
} as any;

switch (payload.method) {
case 'walletController.boot': {
const [password] = payload.params;
retPayload = await runAndCatchErr(() => {
return walletController.boot(password);
}, payload.method)
break;
}
case 'walletController.isBooted': {
retPayload = await runAndCatchErr(() => {
return walletController.isBooted.apply(walletController);
}, payload.method)
break;
}
case 'walletController.isUnlocked': {
retPayload = await runAndCatchErr(() => {
return walletController.isUnlocked.apply(walletController);
}, payload.method)
break;
}
case 'walletController.lockWallet': {
retPayload = await runAndCatchErr(() => {
return walletController.lockWallet.apply(walletController);
}, payload.method)
break;
}
case 'walletController.unlock': {
retPayload = await runAndCatchErr(() => {
return walletController.unlock.apply(walletController, payload.params as any);
}, payload.method)
break;
}
case 'walletController.getConnectedSites': {
retPayload = await runAndCatchErr(() => {
return walletController.getConnectedSites.apply(walletController);
}, payload.method)
break;
}
case 'walletController.importPrivateKey': {
retPayload = await runAndCatchErr(() => {
const [password] = payload.params || [];
return walletController.importPrivateKey.apply(walletController, [password]);
}, payload.method)
break;
}
case 'walletController.getAlianName': {
retPayload = await runAndCatchErr(() => {
const [address] = payload.params || [];
return walletController.getAlianName.apply(walletController, [address]);
}, payload.method)
break;
}
case 'walletController.updateAlianName': {
retPayload = await runAndCatchErr(() => {
const [address, name] = payload.params || [];
return walletController.updateAlianName.apply(walletController, [address, name]);
}, payload.method)
break;
}
default: {
const [ns, method] = payload.method.split('.');

if (ns === 'walletController' && typeof walletController[method] === 'function') {
retPayload = await runAndCatchErr(() => {
return walletController[method].apply(walletController, payload.params);
}, payload.method)
} else {
retPayload.error = {
message: `[rabbyx-rpc-query] method ${payload.method} is not supported`
}
}
}
}

// console.debug('[debug] retPayload', retPayload);

window.rabbyDesktop.ipcRenderer.sendMessage('rabbyx-rpc-respond', JSON.stringify({
rpcId: payload.rpcId,
result: retPayload?.result,
error: retPayload?.error,
}));
}

if (window.rabbyDesktop?.ipcRenderer.on) {
console.warn('[debug] window.rabbyDesktop?.ipcRenderer.on', window.rabbyDesktop?.ipcRenderer.on);
window.rabbyDesktop?.ipcRenderer.on('rabbyx-rpc-query', onRabbyxRpcQuery);
} else {
document.addEventListener('rabbyx-rpc-query', (e: any) => {
onRabbyxRpcQuery(e.detail);
});
}
Loading

0 comments on commit 04b2ddb

Please sign in to comment.