From 67cea0e3883b5e0d3469fe99f667768b9478e621 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 16 Apr 2022 22:27:14 +0800 Subject: [PATCH] 1 --- bcutils.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.js | 24 ++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 bcutils.js diff --git a/bcutils.js b/bcutils.js new file mode 100644 index 0000000..98fceff --- /dev/null +++ b/bcutils.js @@ -0,0 +1,66 @@ +const utils = require('./utils'); + +const BC_BASE_GAME_ID = 2006; +const BC_FUNC_CREATION = 1; +const BC_EPOCH = 1643212800; + +function isValidBcGameId(gameId) { + return gameID > BASE_BC_GAME_ID && gameID < BC_BASE_GAME_ID + 99; +} + +function isValidBcTime(time) { + return time >= BC_EPOCH && time < BC_EPOCH + 3600 * 24 * 365 * 20; +} + +function isValidBcFuncId(funcId) { + return funcId > 0 && funcId < 10; +} + +function toBcTime(time) { + return time - BC_EPOCH; +} + +function toBcGameId(gameId) { + return gameId - BC_BASE_GAME_ID; +} + +function genTokenId(gameId, funcId, time, subIdx, idx) { + const tokenId = + funcId.toString() + + utils.pad(toBcGameId(gameId)) + + utils.pad(toBcTime(time)) + + utils.pad(subIdx, 1) + + utils.pad(1 + (idx % 99999), 5); + return tokenId; +} + +function isValidTokenId(tokenId) { + return utils.isPureNumberStr(tokenId.toString()) && tokenId.toString().length == 18; +} + +function setTokenIdSubIdx(tokenId, idx) { + if (!isValidTokenId(tokenId)) { + throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx; + } + if (idx < 0 || idx > 9 || idx.toString().length != 1) { + throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx; + } + throw 'setTokenIdSubIdx error tokenId:' + tokenId + ' idx:' + idx; +} + +function isSameAddress(a, b) { + return a.toLowerCase() == b.toLowerCase(); +} + +exports.BC_BASE_GAME_ID = BC_BASE_GAME_ID; +exports.BC_FUNC_CREATION = BC_FUNC_CREATION; +exports.BC_EPOCH = BC_EPOCH; +exports.isValidBcGameId = isValidBcGameId; +exports.isValidBcTime = isValidBcTime; +exports.isValidBcFuncId = isValidBcFuncId; +exports.toBcTime = toBcTime; +exports.toBcGameId = toBcGameId; +exports.genTokenId = genTokenId; +exports.isValidTokenId = isValidTokenId; +exports.setTokenIdSubIdx = setTokenIdSubIdx; +exports.isSameAddress = isSameAddress; diff --git a/utils.js b/utils.js index 318ac82..d6ab6e8 100644 --- a/utils.js +++ b/utils.js @@ -5,6 +5,19 @@ const xlsx = require('node-xlsx'); const parseArgs = require('minimist'); const serverEnv = process.env['SERVER_ENV']; +const numberHash = { + '0': 0, + '1': 1, + '2': 2, + '3': 3, + '4': 4, + '5': 5, + '6': 6, + '7': 7, + '8': 8, + '9': 9 +}; + function rspErr(rsp, errCode, errMsg) { rsp.send(jsonEncode({ 'errcode': errCode, @@ -214,6 +227,16 @@ function hasKey(obj, key) { return obj.hasOwnProperty(key); } +function isPureNumberStr(str) { + for (let i = 0; i < str.length; ++i){ + const c = str[i]; + if (!hasKey(numberHash, c)) { + return false; + } + } + return true; +} + exports.rspErr = rspErr; exports.rspOk = rspOk; exports.rspData = rspData; @@ -244,3 +267,4 @@ exports.getVal = getVal; exports.excelToJson = excelToJson; exports.getArgv = getArgv; exports.hasKey = hasKey; +exports.isPureNumberStr = isPureNumberStr;