2022-04-02 01:15:21 +08:00

85 lines
2.5 KiB
JavaScript

const ethUtil = require('ethereumjs-util');
const sigUtil = require('@metamask/eth-sig-util');
const app = require('j7/app');
const http = require('j7/http');
const metamgr = require('../metamgr');
const bc = require('../blockchain');
const cmdHash = {};
async function login(session, params) {
let nonce = '';
const netId = await bc.getNetId();
{
const {err, data} = await http.get('https://market-test.kingsome.cn/webapp/index.php',
{
'c': 'Market',
'a': 'getNonce',
'account': metamgr.getUserAddress(),
'net_id': netId
});
nonce = '' + data['nonce'];
console.log(err, data);
}
{
const tips = 'hello world';
const msgParams = {
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
],
set: [
{ name: 'tips', type: 'string' },
{ name: 'nonce', type: 'string' },
]
},
primaryType: 'set',
domain: {
name: 'Auth',
version: '1',
},
message: {
tips: tips,
nonce: nonce
}
};
const signature = sigUtil.signTypedData({
'privateKey': Buffer.from(metamgr.getPrivateKey(), 'hex'),
'data': msgParams,
'version': "V4"
});
const {err, data} = await http.get('https://market-test.kingsome.cn/webapp/index.php',
{
'c': 'Market',
'a': 'auth',
'account': metamgr.getUserAddress(),
'tips': tips,
'nonce': nonce,
'signature': signature,
'net_id': netId
});
console.log(err, data);
}
session.rspOk();
}
async function execCmd(session) {
const cmdLine = session.request('cmd').split('.');
const cmd = cmdLine[0];
const params = cmdLine.length > 1 ? [] : cmdLine.slice(1);
if (cmdHash[cmd]) {
cmdHash[cmd](session, params);
} else {
session.rspErr(404, 'not found');
}
}
function init() {
app.registerHandler('GM', 'execCmd', execCmd);
cmdHash['login'] = login;
}
exports.init = init;