This commit is contained in:
aozhiwei 2024-06-26 17:47:16 +08:00
parent c81ae2780c
commit 5dce28c31e
6 changed files with 33 additions and 214 deletions

View File

@ -1,24 +1,32 @@
const app = require('j7/app');
const utils = require('j7/utils');
const base64url = require('base64url');
const fastJwt = require('fast-jwt');
const publicKey = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAySgE/YiiI2fzpXaco+OWeDAKymEoqqLYYb6RKOEU1n8=
-----END PUBLIC KEY-----`;
async function verify(session) {
const postData = session.getBody();
console.log('aaaaaaaaaaaaaa', postData);
const {err, decoded} = await asyncVerify(jwksUri, postData['data']);
console.log('fffff', err, decoded);
if (err != null) {
//console.log(postData);
session.rspErr(500, err);
return;
try {
const postData = session.getBody();
const strArr = postData['data'].split('.');
strArr.splice(strArr.length - 1, 1);
const token = strArr.join('.');
const verifier = fastJwt.createVerifier({
algorithms: ['EdDSA'],
key: publicKey
});
console.log(verifier(token));
session.rspData({
'decoded': verifier(token)
});
} catch (e) {
session.rspErr(1, e);
}
session.rspData({
'decoded': decoded
});
}
function init() {
refreshKey();
app.registerHandler('MetaMaskJwt', 'verify', verify);
}

View File

@ -12,7 +12,7 @@
"base64url": "^3.0.1",
"common": "file:../common",
"express": "^4.17.2",
"fast-jwt": "^4.0.1",
"fast-jwt": "1.7.1",
"j7": "file:../../third_party/j7",
"jsonwebtoken": "^9.0.2",
"jwks-rsa": "^3.1.0",
@ -497,14 +497,6 @@
"@ethersproject/strings": "^5.7.0"
}
},
"node_modules/@lukeed/ms": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@lukeed/ms/-/ms-2.0.2.tgz",
"integrity": "sha512-9I2Zn6+NJLfaGoz9jN3lpwDgAYvfGeNYdbAIjJOqzs4Tpc+VU3Jqq4IofSUBKajiDS8k9fZIg18/z13mpk1bsA==",
"engines": {
"node": ">=8"
}
},
"node_modules/@metamask/eth-sig-util": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@metamask/eth-sig-util/-/eth-sig-util-4.0.1.tgz",
@ -1628,17 +1620,16 @@
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="
},
"node_modules/fast-jwt": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-4.0.1.tgz",
"integrity": "sha512-+mdSoH0QdOdFSbbGBctJu7L1yfXRtbmjbVJ4W/PEjyvivobDena0RKwihtBkOML1P+kUJ1QuewnH8u+mROsR1w==",
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-1.7.1.tgz",
"integrity": "sha512-HdDR/k2d3qgUHDVwyhzYdHG/EK+IGhDGZFNidlSvCpVIhWMce8UYXUjx8hWEDhvOA032qoCtO1f/d/VcuzWhzA==",
"dependencies": {
"@lukeed/ms": "^2.0.1",
"asn1.js": "^5.4.1",
"asn1.js": "^5.3.0",
"ecdsa-sig-formatter": "^1.0.11",
"mnemonist": "^0.39.5"
"mnemonist": "^0.39.0"
},
"engines": {
"node": ">=16"
"node": "^18 || ^17 || ^16 || ^14 || ^12"
}
},
"node_modules/finalhandler": {
@ -4499,11 +4490,6 @@
"@ethersproject/strings": "^5.7.0"
}
},
"@lukeed/ms": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@lukeed/ms/-/ms-2.0.2.tgz",
"integrity": "sha512-9I2Zn6+NJLfaGoz9jN3lpwDgAYvfGeNYdbAIjJOqzs4Tpc+VU3Jqq4IofSUBKajiDS8k9fZIg18/z13mpk1bsA=="
},
"@metamask/eth-sig-util": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@metamask/eth-sig-util/-/eth-sig-util-4.0.1.tgz",
@ -5457,14 +5443,13 @@
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="
},
"fast-jwt": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-4.0.1.tgz",
"integrity": "sha512-+mdSoH0QdOdFSbbGBctJu7L1yfXRtbmjbVJ4W/PEjyvivobDena0RKwihtBkOML1P+kUJ1QuewnH8u+mROsR1w==",
"version": "1.7.1",
"resolved": "https://registry.npmjs.org/fast-jwt/-/fast-jwt-1.7.1.tgz",
"integrity": "sha512-HdDR/k2d3qgUHDVwyhzYdHG/EK+IGhDGZFNidlSvCpVIhWMce8UYXUjx8hWEDhvOA032qoCtO1f/d/VcuzWhzA==",
"requires": {
"@lukeed/ms": "^2.0.1",
"asn1.js": "^5.4.1",
"asn1.js": "^5.3.0",
"ecdsa-sig-formatter": "^1.0.11",
"mnemonist": "^0.39.5"
"mnemonist": "^0.39.0"
}
},
"finalhandler": {

View File

@ -9,7 +9,7 @@
"base64url": "^3.0.1",
"common": "file:../common",
"express": "^4.17.2",
"fast-jwt": "^4.0.1",
"fast-jwt": "1.7.1",
"j7": "file:../../third_party/j7",
"jsonwebtoken": "^9.0.2",
"jwks-rsa": "^3.1.0",

View File

@ -1,105 +0,0 @@
const util = require('util');
const Web3 = require('web3');
const utils = require('j7/utils');
const event = require('j7/event');
const sync = require("j7/sync");
const log = require("j7/log");
const bcutils = require("j7/bcutils");
const contract = require('common/contract');
const bcconst = require('common/bcconst');
const metaFactory = require('./metadata/factory');
class BlockChain {
constructor(netId) {
this.netId = netId;
}
async initInstance(user, address, jsonUrl) {
const json = utils.readJsonFromFile(jsonUrl);
return new this.web3.eth.Contract(
json.abi,
address,
{ from: user }
);
}
async init() {
this.web3Conf = metaFactory.getWeb3Conf(this.netId);
this.contractsConf = metaFactory.getContractsConf(this.netId);
this.netDir = metaFactory.getNetDir(this.netId);
this.web3 = new Web3(this.getRpcUrl());
this.web3.eth.handleRevert = true;
this.web3.eth.accounts.wallet.add(this.getPrivateKey());
for (const data of this.contractsConf) {
this[`${data.name}Instance`] = await this.initInstance
(this.getUserAddress(), data.address, this.netDir + data.json);
}
this.currencyHash = {};
/*
this.currencyHash['CEG'] = bcutils.toNormalAddress(
this.getContractByName(contract.CEG_INSTANCE_NAME)['address']);
if (this.netId == bcconst.ARBITRUM_NET_ID) {
this.currencyHash['USDT'] = bcutils.toNormalAddress('0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9');
this.currencyHash['USDC'] = bcutils.toNormalAddress('0xaf88d065e77c8cC2239327C5EDb3A432268e5831');
} else if (this.netId == bcconst.ARBITRUM_TEST_NET_ID) {
this.currencyHash['USDT'] = bcutils.toNormalAddress('0xaa34B79A0Ab433eaC900fB3CB9f191F5Cd27501D');
this.currencyHash['USDC'] = bcutils.toNormalAddress('0x944D0A8463B2c955F90F7252bBb99A3395087155');
}*/
const chainId = await this.web3.eth.getChainId();
if (chainId != this.netId) {
log.warning(util.format('net id error %s %s',
chainId,
this.netId
));
}
log.info(util.format('local.net_id:%s remote_net_id:%s',
this.netId,
chainId
));
}
getNetId() {
return this.netId;
}
getRpcUrl() {
return this.web3Conf['block_server'];
}
getUserAddress() {
return this.web3Conf['user_address'];
}
getPrivateKey() {
return this.web3Conf['private_key'];
}
getContractByName(name) {
let contract = null;
this.contractsConf.forEach((item) => {
if (item['name'] == name) {
contract = item;
}
});
return contract;
}
getNftAddress(tokenType) {
const name = contract.getNftNameByTokenType(tokenType);
const c = this.getContractByName(name);
return c ? c['address'] : '';
}
isValidAddress(address) {
return bcutils.isValidAddress(address);
}
getCurrencyAddressByName(currencyName) {
return utils.hasKey(this.currencyHash, currencyName) ? this.currencyHash[currencyName] : '';
}
}
module.exports = BlockChain;

View File

@ -1,68 +0,0 @@
const utils = require('j7/utils');
const BaseService = require('./baseservice');
const metaFactory = require('../metadata/factory');
const bcClass = require('../blockchain');
const netIdHash = {};
function getBc(netId) {
return utils.hasKey(netIdHash, netId) ? netIdHash[netId] : null;
}
class BlockChain extends BaseService {
#bc = null;
static async staticInit() {
metaFactory.getNetList().forEach(async (netId) => {
const bc = new bcClass(netId);
netIdHash[netId] = bc;
await bc.init();
});
}
async init(netId) {
this.#bc = getBc(netId);
return this.#bc != null;
}
getNftAddress(tokenType) {
return this.#bc.getNftAddress(tokenType);
}
getUserAddress() {
return this.#bc.getUserAddress();
}
isValidAddress(address) {
return this.#bc.isValidAddress(address);
}
getContractAddressByName(name) {
const c = this.#bc.getContractByName(name);
return c ? c['address'] : '';
}
getContractByName(name) {
const c = this.#bc.getContractByName(name);
return c;
}
getCurrencyAddressByName(currencyName) {
return this.#bc.getCurrencyAddressByName(currencyName);
}
getInstanceByName(name) {
return this.#bc[`${name}Instance`];
}
async soliditySha3Sign(...args) {
const signStr = await this.#bc.web3.utils.soliditySha3(...args);
let signature = await this.#bc.web3.eth.sign(signStr, this.getUserAddress());
signature = signature.replace(/00$/, "1b").replace(/01$/, "1c");
return signature;
}
}
module.exports = BlockChain;

View File

@ -22,7 +22,6 @@ async function addSingle(clsName, modName) {
}
async function init() {
await add("BlockChain", 'blockchain');
}
function create(name, session = null) {