// check proof of work data constcheckPow = (randomTxt, timestamp) => { let [numStr] = getTimeState(timestamp) let randMd5Str = md5(randomTxt) return randMd5Str.indexOf(numStr) != -1
}
/** * 获取 [截取字符串,时间戳] * * @param {long} fixedTimestamp 固定时间戳,可选,如果设置了则 numStr 以该时间戳截取 * @returns [numStr,timestamp] */ constgetTimeState = (fixedTimestamp) => { let timestamp = !!fixedTimestamp ? fixedTimestamp : newDate().getTime() if(timestamp >>> 38 < 0){ thrownewError(`${timestamp} is don't look like timestamp type`) } let timestampStr = timestamp.toString() let cutTimestampStr = timestampStr.substr(-LENGTH) let num = Number.parseInt(cutTimestampStr) let numStr = num.toString(16) return [numStr, timestamp] }
/** * generate proof of work data */ constpow = () => { let [numStr, timestamp] = getTimeState()
let md5Str = "" let count = 0, maxCount = 2000 let randomStr while (md5Str.indexOf(numStr) == -1) { randomStr = Math.random().toString(36).substr(3) md5Str = md5(randomStr) if (count++ > maxCount) { [numStr, timestamp] = getTimeState() count = 0 } }