135 lines
3.0 KiB
JavaScript
135 lines
3.0 KiB
JavaScript
const ethUtil = require('ethereumjs-util');
|
|
const sigUtil = require('@metamask/eth-sig-util');
|
|
|
|
const app = require('j7/app');
|
|
const utils = require('j7/utils');
|
|
const bcutils = require('j7/bcutils');
|
|
const bc = require('../blockchain');
|
|
const metaFactory = require('../metadata/factory');
|
|
|
|
let lastIdx = 0;
|
|
let tokenTime = utils.getUtcTime();
|
|
|
|
function genIdx() {
|
|
let newIdx = lastIdx;
|
|
if (newIdx < bcutils.BC_MAX_TOKEN_IDX) {
|
|
newIdx = lastIdx;
|
|
++lastIdx;
|
|
} else {
|
|
lastIdx = 0;
|
|
newIdx = lastidx;
|
|
++tokenTime;
|
|
}
|
|
return newIdx;
|
|
}
|
|
|
|
function genTokenId() {
|
|
const tokenId = bcutils.genTokenId(
|
|
1006,
|
|
bcutils.BC_FUNC_PRESENT,
|
|
utils.getUtcTime(),
|
|
1,
|
|
genIdx()
|
|
);
|
|
return tokenId;
|
|
}
|
|
|
|
async function isValidItem(session) {
|
|
const itemId = session.request('item_id');
|
|
const itemMeta = metaFactory.getMetaByKey('Item', itemId);
|
|
if (!itemMeta) {
|
|
session.rspErr(1, 'is not nft');
|
|
return;
|
|
}
|
|
if (itemMeta.getNftType() == bcutils.NONE_TYPE) {
|
|
session.rspErr(1, 'is not nft');
|
|
return;
|
|
}
|
|
session.rspOk();
|
|
}
|
|
|
|
async function isValidAccount(session) {
|
|
const account = session.request('account');
|
|
const valid = bc.isAddress(account);
|
|
if (!valid) {
|
|
session.rspErr(1, 'is invalid account');
|
|
return;
|
|
}
|
|
session.rspOk();
|
|
}
|
|
|
|
async function presentItem(session) {
|
|
const {err, conn} = await app.getDbConn('MarketDb0');
|
|
if (err) {
|
|
session.rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
const account = bcutils.toNormalAddress(session.request('account'));
|
|
const uniKey = session.request('unikey');
|
|
const itemId = session.request('item_id');
|
|
const itemMeta = metaFactory.getMetaByKey('Item', itemId);
|
|
if (!itemMeta) {
|
|
session.rspErr(1, 'is not nft');
|
|
return;
|
|
}
|
|
const tokenType = itemMeta.getNftType();
|
|
if (tokenType == bcutils.NONE_TYPE) {
|
|
session.rspErr(1, 'is not nft');
|
|
return;
|
|
}
|
|
const valid = bc.isAddress(account);
|
|
if (!valid) {
|
|
session.rspErr(2, 'is invalid account');
|
|
return;
|
|
}
|
|
|
|
{
|
|
const {err, row} = await conn.ormSelectOne(
|
|
't_mint',
|
|
[
|
|
['unikey', uniKey],
|
|
]
|
|
);
|
|
if (err) {
|
|
session.rspErr(500, 'server internal error');
|
|
return;
|
|
}
|
|
if (row) {
|
|
session.rspOk();
|
|
return;
|
|
}
|
|
}
|
|
|
|
{
|
|
const nowTime = utils.getUtcTime();
|
|
const tokenId = genTokenId();
|
|
const fieldsList = [
|
|
['bc_mint_tokenid', tokenId],
|
|
['unikey', uniKey],
|
|
['account', bcutils.toNormalAddress(account)],
|
|
['game_id', 2006],
|
|
|
|
['bc_mint_itemid', itemId],
|
|
['bc_mint_token_type', tokenType],
|
|
['bc_mint_tags', ''],
|
|
|
|
['createtime', nowTime],
|
|
['modifytime', nowTime]
|
|
];
|
|
const {err} = await conn.insert(
|
|
't_mint',
|
|
fieldsList
|
|
);
|
|
}
|
|
|
|
session.rspOk();
|
|
}
|
|
|
|
function init() {
|
|
app.registerHandler('BcService', 'isValidAccount', isValidAccount);
|
|
app.registerHandler('BcService', 'isValidItem', isValidItem);
|
|
app.registerHandler('BcService', 'presentItem', presentItem);
|
|
}
|
|
|
|
exports.init = init;
|