1
This commit is contained in:
parent
bd48043b89
commit
a83c7f56fa
@ -94,9 +94,9 @@ async function init() {
|
|||||||
'',
|
'',
|
||||||
'BlockChainDb'
|
'BlockChainDb'
|
||||||
);
|
);
|
||||||
registerMetaClass(configDir + 'web3service.cluster.json',
|
registerMetaClass(configDir + 'web3sign.cluster.json',
|
||||||
'',
|
'',
|
||||||
'Web3Service'
|
'Web3Sign'
|
||||||
);
|
);
|
||||||
|
|
||||||
// registerMetaClass(resDir + 'item@item.json',
|
// registerMetaClass(resDir + 'item@item.json',
|
||||||
@ -108,7 +108,7 @@ async function init() {
|
|||||||
traverseMetaList('BlockChainDb', (dbConf, idx) => {
|
traverseMetaList('BlockChainDb', (dbConf, idx) => {
|
||||||
app.registerDb('BlockChainDb' + idx, dbConf);
|
app.registerDb('BlockChainDb' + idx, dbConf);
|
||||||
});
|
});
|
||||||
traverseMetaList('Web3Service', (item, idx) => {
|
traverseMetaList('Web3Sign', (item, idx) => {
|
||||||
if (item['instance_id'] == app.getInstanceId()) {
|
if (item['instance_id'] == app.getInstanceId()) {
|
||||||
web3ServiceConf = item;
|
web3ServiceConf = item;
|
||||||
item['nets'].forEach((netId) => {
|
item['nets'].forEach((netId) => {
|
||||||
@ -163,7 +163,7 @@ function getWeb3Conf(netId) {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWeb3ServiceConf() {
|
function getWeb3SignConf() {
|
||||||
return web3ServiceConf;
|
return web3ServiceConf;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ exports.traverseMetaList = traverseMetaList;
|
|||||||
exports.callMetaStatic = callMetaStatic;
|
exports.callMetaStatic = callMetaStatic;
|
||||||
|
|
||||||
exports.getWeb3Conf = getWeb3Conf;
|
exports.getWeb3Conf = getWeb3Conf;
|
||||||
exports.getWeb3ServiceConf = getWeb3ServiceConf;
|
exports.getWeb3SignConf = getWeb3SignConf;
|
||||||
exports.getContractsConf = getContractsConf;
|
exports.getContractsConf = getContractsConf;
|
||||||
exports.getNetList = getNetList;
|
exports.getNetList = getNetList;
|
||||||
exports.getNetDir = getNetDir;
|
exports.getNetDir = getNetDir;
|
||||||
|
17
server/web3sign/services/baseservice.js
Normal file
17
server/web3sign/services/baseservice.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
class BaseService {
|
||||||
|
|
||||||
|
constructor(session) {
|
||||||
|
this.session = session;
|
||||||
|
}
|
||||||
|
|
||||||
|
getSession() {
|
||||||
|
return this.session;
|
||||||
|
}
|
||||||
|
|
||||||
|
getUser() {
|
||||||
|
return this.session.user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = BaseService;
|
46
server/web3sign/services/blockchain.js
Normal file
46
server/web3sign/services/blockchain.js
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
34
server/web3sign/services/factory.js
Normal file
34
server/web3sign/services/factory.js
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
const services = {};
|
||||||
|
|
||||||
|
async function internalAdd(clsName, modName, isSingle) {
|
||||||
|
const modClass = require('./' + modName);
|
||||||
|
services[clsName] = {
|
||||||
|
'clsName': clsName,
|
||||||
|
'modName': modName,
|
||||||
|
'class': modClass,
|
||||||
|
'isSingle': isSingle
|
||||||
|
};
|
||||||
|
if (modClass.staticInit) {
|
||||||
|
await modClass.staticInit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function add(clsName, modName) {
|
||||||
|
await internalAdd(clsName, modName, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addSingle(clsName, modName) {
|
||||||
|
await internalAdd(clsName, modName, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
await add("BlockChain", 'blockchain');
|
||||||
|
}
|
||||||
|
|
||||||
|
function create(name, session = null) {
|
||||||
|
const service = services[name];
|
||||||
|
return new service['class'](session);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.init = init;
|
||||||
|
exports.create = create;
|
130
server/web3sign/session.js
Normal file
130
server/web3sign/session.js
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
const utils = require('j7/utils');
|
||||||
|
const app = require('j7/app');
|
||||||
|
const error = require('j7/error');
|
||||||
|
const modelsFactory = require('./models/factory');
|
||||||
|
const serviceFactory = require('./services/factory');
|
||||||
|
const metaFactory = require('./metadata/factory');
|
||||||
|
|
||||||
|
class Session {
|
||||||
|
|
||||||
|
constructor(req, rsp) {
|
||||||
|
this.req = req;
|
||||||
|
this.rsp = rsp;
|
||||||
|
this.nowTime = utils.getUtcTime();
|
||||||
|
this.useConns = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
async destory() {
|
||||||
|
if (this.user) {
|
||||||
|
await this.user.destory();
|
||||||
|
}
|
||||||
|
for (let key in this.useConns) {
|
||||||
|
this.useConns[key].release();
|
||||||
|
}
|
||||||
|
//console.log(new Error().stack);
|
||||||
|
this.useConns = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getNowTime() {
|
||||||
|
return this.nowTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
getNowDaySeconds() {
|
||||||
|
return this.getNowDaySeconds(this.getNowTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
getDaySeconds(utcTime) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
rspErr(errCode, errMsg) {
|
||||||
|
utils.rspErr(this.rsp, errCode, errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
rspOk() {
|
||||||
|
utils.rspOk(this.rsp);
|
||||||
|
}
|
||||||
|
|
||||||
|
rspData(data) {
|
||||||
|
utils.rspData(this.rsp, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
dieErr(errCode, errMsg) {
|
||||||
|
this.rspErr(errCode, errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
throwError(errCode, errMsg) {
|
||||||
|
throw new error.InternalError(errCode, errMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
request(name, defVal = null) {
|
||||||
|
return name in this.req.query ? this.req.query[name] : defVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
requestToJson() {
|
||||||
|
return utils.jsonEncode(this.req.query);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getBlockChainDb() {
|
||||||
|
const idx = 0;
|
||||||
|
const dbKey = 'BlockChainDb' + idx;
|
||||||
|
if (this.useConns[dbKey]) {
|
||||||
|
return this.useConns[dbKey];
|
||||||
|
}
|
||||||
|
const {err, conn} = await app.getDbConn(dbKey);
|
||||||
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
//console.log(new Error().stack);
|
||||||
|
if (!err && conn) {
|
||||||
|
this.useConns[dbKey] = conn;
|
||||||
|
}
|
||||||
|
return conn;
|
||||||
|
}
|
||||||
|
|
||||||
|
createModel(name) {
|
||||||
|
return modelsFactory.create(name, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
createService(name) {
|
||||||
|
return serviceFactory.create(name, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
getMeta(name, key) {
|
||||||
|
return metaFactory.getMetaByKey(name, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
callMetaStatic(name, method, ...args) {
|
||||||
|
return metaFactory.callMetaStatic(name, method, this, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
traverseMetaList(name, cb) {
|
||||||
|
return metaFactory.traverseMetaList(name, cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
callMetaFactory(name, ...args) {
|
||||||
|
return metaFactory[name](this, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
async blockChainConn(method, ...args) {
|
||||||
|
const conn = await this.getBlockChainDb();
|
||||||
|
const ret = await conn[method](...args);
|
||||||
|
if (ret.err){
|
||||||
|
this.throwError(500, 'internal error');
|
||||||
|
log.error(ret.err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (utils.hasKey(ret, 'row')) {
|
||||||
|
return ret['row'];
|
||||||
|
} else if (utils.hasKey(ret, 'lastId')) {
|
||||||
|
return ret['lastId'];
|
||||||
|
} else if (utils.hasKey(ret, 'rows')) {
|
||||||
|
return ret['rows'];
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Session;
|
41
server/web3sign/user.js
Normal file
41
server/web3sign/user.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
const utils = require('j7/utils');
|
||||||
|
const app = require('j7/app');
|
||||||
|
const db = require('j7/db');
|
||||||
|
const log = require('j7/log');
|
||||||
|
|
||||||
|
class User {
|
||||||
|
|
||||||
|
constructor(session) {
|
||||||
|
this.session = session;
|
||||||
|
this.accountId = session.request('account_id');
|
||||||
|
this.sessionId = session.request('session_id');
|
||||||
|
this.useConns = {};
|
||||||
|
this.selfDb = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async destory() {
|
||||||
|
for (let key in this.useConns) {
|
||||||
|
this.useConns[key].release();
|
||||||
|
}
|
||||||
|
this.useConns = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAccountId() {
|
||||||
|
return this.accountId;
|
||||||
|
}
|
||||||
|
|
||||||
|
getChannel() {
|
||||||
|
return utils.extractChannel(this.getAccountId());
|
||||||
|
}
|
||||||
|
|
||||||
|
getSessionId() {
|
||||||
|
return this.sessionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRegisterTime() {
|
||||||
|
return utils.extractRegisterTime(this.getAccountId(), this.getSessionId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.User = User;
|
Loading…
x
Reference in New Issue
Block a user