Skip to content

Commit

Permalink
Merge branch 'main' into feature/pay-transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun46 committed Oct 27, 2023
2 parents baf00f0 + 3ad627f commit 98c0e9e
Show file tree
Hide file tree
Showing 52 changed files with 771 additions and 495 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- 'feature/**'

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "metalet",
"private": true,
"version": "2.0.4",
"version": "2.1.0",
"type": "module",
"scripts": {
"dev": "vite --config vite.dev.config.ts",
Expand Down
312 changes: 153 additions & 159 deletions public/content.js
Original file line number Diff line number Diff line change
@@ -1,161 +1,155 @@
const m = (n = 32) => {
let e = "";
const a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let r = 0; r < n; r++)
e += a.charAt(Math.floor(Math.random() * a.length));
return e;
};
async function t(n, e = "authorize", a) {
const r = `${e}-${n}`, u = m(16), l = window.location.host;
window.postMessage(
{
nonce: u,
channel: "to-metaidwallet",
action: r,
host: l,
icon: "",
params: a || {}
},
"*"
);
const h = (i) => {
const o = (c) => {
if (!(c.source !== window || c.data?.channel !== "from-metaidwallet")) {
if (c.data?.nonce === u) {
if (window.removeEventListener("message", o), c.data?.res?.error)
throw new Error(c.data.res.error);
i && typeof i == "function" && i(c.data);
;(function () {
'use strict'
const l = (n = 32) => {
let r = ''
const a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let t = 0; t < n; t++) r += a.charAt(Math.floor(Math.random() * a.length))
return r
}
async function e(n, r = 'authorize', a, t) {
const x = `${r}-${n}`,
f = l(16),
N = window.location.host
window.postMessage({ nonce: f, channel: 'to-metaidwallet', action: x, host: N, icon: '', params: a || {} }, '*')
const v = (c) => {
const o = (i) => {
if (!(i.source !== window || i.data?.channel !== 'from-metaidwallet')) {
if (i.data?.nonce === f) {
if ((window.removeEventListener('message', o), i.data?.res?.error)) throw new Error(i.data.res.error)
c && typeof c == 'function' && c(i.data)
}
return !0
}
return !0;
}
};
window.addEventListener("message", o);
};
return await new Promise((i) => {
h((o) => {
i(o.res);
});
});
}
async function s() {
return await t("Connect");
}
async function y() {
return await t("Disconnect");
}
async function T() {
return await t("IsConnected", "query");
}
async function B() {
return await t("GetNetwork", "query");
}
async function b() {
return await t("SwitchNetwork");
}
async function q(n) {
return await t("GetAddress", "query", n);
}
async function G(n) {
return await t("GetPublicKey", "query", n);
}
async function P() {
return await t("GetXPublicKey", "query");
}
async function w(n) {
return await t("GetBalance", "query", n);
}
async function S(n) {
return await t("GetUtxos", "query", n);
}
async function p(n) {
return await t("EciesEncrypt", "authorize", n);
}
async function z(n) {
return await t("EciesDecrypt", "authorize", n);
}
async function A(n) {
return await t("SignMessage", "authorize", n);
}
async function C(n) {
return await t("VerifySignature", "query", n);
}
async function E(n) {
return await t("PreviewTransaction", "query", n);
}
async function M(n) {
return await t("SignTransaction", "authorize", n);
}
async function K(n) {
return await t("SignTransactions", "authorize", n);
}
async function k(n) {
return await t("Pay", "authorize", n);
}
async function x(n) {
return await t("Transfer", "authorize", n);
}
async function N(n) {
return await t("Merge", "authorize", n);
}
async function g(n) {
return await t("GetTokenBalance", "query", n);
}
const f = {
query: [
{ name: "getBalance", action: "GetBTCBalance" },
{ name: "getAddress", action: "GetBTCAddress" },
{ name: "getPublicKey", action: "GetBTCPublicKey" },
{ name: "getUtxos", action: "GetBTCUtxos" }
],
authorize: [
{ name: "signPsbt", action: "SignBTCPsbt" },
{ name: "signMessage", action: "SignBTCMessage" }
]
}, d = {
connect: s,
isConnected: T,
disconnect: y,
getNetwork: B,
switchNetwork: b,
getAddress: q,
getPublicKey: G,
getXPublicKey: P,
getBalance: w,
getUtxos: S,
transfer: x,
merge: N,
previewTransaction: E,
signTransaction: M,
signTransactions: K,
pay: k,
signMessage: A,
verifySignature: C,
eciesEncrypt: p,
eciesDecrypt: z,
// signTransaction,
// transferAll,
token: {
getBalance: g
},
nft: {},
btc: {},
// btc: {
// getBalance: () => {},
// getAddress: () => {},
// getPublicKey: () => {},
// getUtxos: () => {},
// },
// Deprecating
requestAccount: s,
getAccount: s,
exitAccount: y,
getMvcBalance: w,
getSensibleFtBalance: g
};
Object.keys(f).forEach((n) => {
const e = n;
f[e].forEach((a) => {
d.btc[a.name] = async (r) => await t(a.action, e, r);
});
});
window.metaidwallet = d;
window.addEventListener('message', o)
}
return await new Promise((c) => {
v((o) => {
c(o.res)
})
})
}
async function s() {
return await e('Connect')
}
async function u() {
return await e('Disconnect')
}
async function m() {
return await e('IsConnected', 'query')
}
async function h() {
return await e('GetNetwork', 'query')
}
async function T() {
return await e('SwitchNetwork')
}
async function B(n) {
return await e('GetAddress', 'query', n)
}
async function b(n) {
return await e('GetPublicKey', 'query', n)
}
async function q() {
return await e('GetXPublicKey', 'query')
}
async function w(n) {
return await e('GetBalance', 'query', n)
}
async function E(n) {
return await e('GetUtxos', 'query', n)
}
async function G(n) {
return await e('EciesEncrypt', 'authorize', n)
}
async function S(n) {
return await e('EciesDecrypt', 'authorize', n)
}
async function p(n) {
return await e('SignMessage', 'authorize', n)
}
async function M(n) {
return await e('VerifySignature', 'query', n)
}
async function P(n) {
return await e('PreviewTransaction', 'query', n)
}
async function z(n) {
return await e('SignTransaction', 'authorize', n)
}
async function A(n) {
return await e('SignTransactions', 'authorize', n)
}
async function C(n) {
return await e('Transfer', 'authorize', n)
}
async function L(n) {
return await e('Merge', 'authorize', n)
}
async function y(n) {
return await e('GetTokenBalance', 'query', n)
}
async function K(n, r) {
const a = (t) => {
if (t.data?.channel === 'removeListener' && t.data?.eventName === n) {
window.removeEventListener('message', a)
return
}
t.source !== window || t.data?.channel !== 'from-metaidwallet' || t.data?.eventName !== n || r(...t.data.args)
}
window.addEventListener('message', a)
}
async function k(n) {
window.postMessage({ eventName: n, channel: 'removeListener' }, '*')
}
const d = {
query: [
{ name: 'getBalance', action: 'GetBTCBalance' },
{ name: 'getAddress', action: 'GetBTCAddress' },
{ name: 'getPublicKey', action: 'GetBTCPublicKey' },
{ name: 'getUtxos', action: 'GetBTCUtxos' },
],
authorize: [
{ name: 'signPsbt', action: 'SignBTCPsbt' },
{ name: 'signMessage', action: 'SignBTCMessage' },
],
},
g = {
connect: s,
isConnected: m,
disconnect: u,
getNetwork: h,
switchNetwork: T,
getAddress: B,
getPublicKey: b,
getXPublicKey: q,
getBalance: w,
getUtxos: E,
transfer: C,
merge: L,
previewTransaction: P,
signTransaction: z,
signTransactions: A,
signMessage: p,
verifySignature: M,
eciesEncrypt: G,
eciesDecrypt: S,
token: { getBalance: y },
nft: {},
btc: {},
on: K,
removeListener: k,
requestAccount: s,
getAccount: s,
exitAccount: u,
getMvcBalance: w,
getSensibleFtBalance: y,
}
Object.keys(d).forEach((n) => {
const r = n
d[r].forEach((a) => {
g.btc[a.name] = async (t) => await e(a.action, r, t)
})
}),
(window.metaidwallet = g)
})()
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Metalet",
"description": "An MVC Crypto Wallet Extension",
"version": "2.0.4",
"version": "2.1.0",
"manifest_version": 3,
"action": {
"default_popup": "index.html",
Expand Down
19 changes: 18 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import browser from 'webextension-polyfill'
import actions from './data/query-actions'
import exActions from './data/extension-actions'
import { NOTIFICATION_HEIGHT, NOTIFICATION_WIDTH } from './data/config'
import connector from './lib/connector'
import { getCurrentAccount } from './lib/account'
import { isLocked } from './lib/password'

browser.runtime.onMessage.addListener(async (msg, sender) => {
if (msg.channel === 'inter-extension') {
return await exActions[msg.fn](...msg.args)
}

const account = await getCurrentAccount()
const walletLocked = await isLocked()
const actionName = msg.action.replace('authorize-', '').replace('query-', '')
const actionName = msg.action.replace('authorize-', '').replace('query-', '').replace('event-', '')

// 如果连接状态为未连接,且请求的 action 不是connect或者IsConnected,则返回错误
let failedStatus: string = ''
Expand All @@ -33,6 +38,18 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {

return response
}

// event actions
// if (msg.action?.startsWith('event')) {

// if (msg.params?.type === 'on') {
// console.log(`register ${actionName}`)
// register(actionName)
// } else if (msg.params?.type === 'removeListener') {
// unregister(actionName)
// }
// return
// }

// authorize actions
if (msg.action?.startsWith('authorize')) {
Expand Down
Loading

0 comments on commit 98c0e9e

Please sign in to comment.