flexbiz-server
Version:
Flexible Server
13 lines (12 loc) • 6.07 kB
JavaScript
const model=global.getModel("asskey"),forever=require("async/forever"),CryptoJS=require("crypto-js"),hexToBinary=$s$$=>{let $ret$$="";const $lookupTable$$={0:"0000",1:"0001",2:"0010",3:"0011",4:"0100",5:"0101",6:"0110",7:"0111",8:"1000",9:"1001",a:"1010",b:"1011",c:"1100",d:"1101",e:"1110",f:"1111"};for(let $i$$=0;$i$$<$s$$.length;$i$$+=1)if($lookupTable$$[$s$$[$i$$]])$ret$$+=$lookupTable$$[$s$$[$i$$]];else return null;return $ret$$},getHashById=async($id_app$$,$id$$)=>global.getModel("assfindhash").findOne({id_app:$id_app$$,
id:$id$$}).lean(),getLatestHash=async $id_app$$=>await global.getModel("assfindhash").findOne({id_app:$id_app$$}).sort({id:-1}).lean(),getAdjustedDifficulty=async($id_app$$,$lastestHash_timeTaken$$)=>{if(!$lastestHash_timeTaken$$)return 1;let {DIFFICULTY_ADJUSTMENT_INTERVAL:$DIFFICULTY_ADJUSTMENT_INTERVAL$$,BLOCK_GENERATION_INTERVAL:$BLOCK_GENERATION_INTERVAL$$}=configs;$DIFFICULTY_ADJUSTMENT_INTERVAL$$||=10;$BLOCK_GENERATION_INTERVAL$$||=60;if(Number($lastestHash_timeTaken$$.id)%$DIFFICULTY_ADJUSTMENT_INTERVAL$$===
0&&Number($lastestHash_timeTaken$$.id)!==0){$id_app$$=await getHashById($id_app$$,Number($lastestHash_timeTaken$$.id)+1-$DIFFICULTY_ADJUSTMENT_INTERVAL$$);const $timeExpected$$=$BLOCK_GENERATION_INTERVAL$$*$DIFFICULTY_ADJUSTMENT_INTERVAL$$;$lastestHash_timeTaken$$=Number($lastestHash_timeTaken$$.timestamp)-Number($id_app$$.timestamp);Logger.info("adjusted difficulty",{DIFFICULTY_ADJUSTMENT_INTERVAL:$DIFFICULTY_ADJUSTMENT_INTERVAL$$,BLOCK_GENERATION_INTERVAL:$BLOCK_GENERATION_INTERVAL$$,timeExpected:$timeExpected$$,
timeTaken:$lastestHash_timeTaken$$});return $lastestHash_timeTaken$$<$timeExpected$$/2?Number($id_app$$.difficulty)+1:$lastestHash_timeTaken$$>$timeExpected$$*2?Math.max(1,Number($id_app$$.difficulty)-1):Number($id_app$$.difficulty)}return Number($lastestHash_timeTaken$$.difficulty)},calculateHash=($index$$,$previousHash$$,$timestamp$$,$data$$,$difficulty$$,$nonce$$)=>CryptoJS.SHA256($index$$+$previousHash$$+$timestamp$$+$data$$+$difficulty$$+$nonce$$).toString(),hashMatchesDifficulty=($hash_hashInBinary$$,
$difficulty$$)=>{$hash_hashInBinary$$=hexToBinary($hash_hashInBinary$$);$difficulty$$="0".repeat($difficulty$$);return $hash_hashInBinary$$.startsWith($difficulty$$)},findHash=async($id_app$$,$lastestHash$$)=>{let $id$$,$previousHash$$,$difficulty$$,$timestamp$$=Math.roundBy((new Date).getTime()/1E3);$lastestHash$$||=await getLatestHash($id_app$$);$lastestHash$$?($id$$=Number($lastestHash$$.id)+1,$previousHash$$=$lastestHash$$.hash,$difficulty$$=await getAdjustedDifficulty($id_app$$,$lastestHash$$)):
($id$$=0,$previousHash$$="",$difficulty$$=1);let $nonce$$=0;return new Promise($rs$$=>{forever($cb$$=>{const $hash$$=calculateHash($id$$,$previousHash$$,$timestamp$$,"",$difficulty$$,$nonce$$);if(hashMatchesDifficulty($hash$$,$difficulty$$))return $cb$$({id:$id$$,hash:$hash$$,previousHash:$previousHash$$,timestamp:$timestamp$$,data:"",difficulty:$difficulty$$,nonce:$nonce$$});$nonce$$++;$cb$$()},$h$$=>{$rs$$($h$$)})})},validHash=async($difficulty$jscomp$3_id_app$$,$findhash$$={})=>{let $id$$;var $lastestHash$$;
if($lastestHash$$=await getLatestHash($difficulty$jscomp$3_id_app$$)){$id$$=Number($lastestHash$$.id)+1;var $currnetTimestamp_previousHash$$=$lastestHash$$.hash;$difficulty$jscomp$3_id_app$$=await getAdjustedDifficulty($difficulty$jscomp$3_id_app$$,$lastestHash$$);$lastestHash$$=$lastestHash$$.timestamp}else $id$$=0,$currnetTimestamp_previousHash$$="",$difficulty$jscomp$3_id_app$$=1,$lastestHash$$=0;if($currnetTimestamp_previousHash$$!=$findhash$$.previousHash)throw"PreviousHash is not correct";$currnetTimestamp_previousHash$$=
(new Date).getTime();if($findhash$$.timestamp>$currnetTimestamp_previousHash$$+6E4)throw"Timestamp is not valid";if($lastestHash$$>0&&$findhash$$.timestamp<$lastestHash$$)throw"Timestamp must be greater than the previous timestamp ";if($difficulty$jscomp$3_id_app$$!=$findhash$$.difficulty)throw"Difficulty is not correct";if(calculateHash($id$$,$findhash$$.previousHash,$findhash$$.timestamp,$findhash$$.data,$findhash$$.difficulty,$findhash$$.nonce)!=$findhash$$.hash)throw"The hash is wrong";if(!hashMatchesDifficulty($findhash$$.hash,
$findhash$$.difficulty))throw"The hash does not match with difficulty";return{id:$id$$}},mining=async($id_app$$,$fn$$)=>{let $found$$;Logger.info("find hash...");try{$found$$=await findHash($id_app$$),$fn$$(null,$found$$)}catch($e$$){return Logger.error($e$$),$fn$$($e$$.message||$e$$.error||$e$$)}},getAccount=async($account_id_app$$,$address$$)=>{if($account_id_app$$=await global.getModel("asskey").findOne({id_app:$account_id_app$$,address:$address$$}).lean())delete $account_id_app$$.privateKey,
delete $account_id_app$$.private_key,delete $account_id_app$$.mnemonic;return $account_id_app$$},getWallet=async($id_app$$,$username$$,$address$$)=>{$id_app$$=await model.findOne({id_app:$id_app$$,username:$username$$,address:$address$$}).lean();if(!$id_app$$)throw{message:`User ${$username$$} has not the wallet ${$address$$}`};return $id_app$$},getKeyInfo=async($balance_id_app$$,$account$jscomp$1_username$$,$address$$)=>{$account$jscomp$1_username$$=await getAccount($balance_id_app$$,$address$$);
$balance_id_app$$=await bankBalancesGet($balance_id_app$$,$address$$);return{account:$account$jscomp$1_username$$,balance:$balance_id_app$$}},bankBalancesGet=async($id_app$$,$address$$)=>await global.getModel("assbank").getBalances($id_app$$,$address$$),denomBalancesGet=async($id_app$$,$denom$$)=>(await global.getModel("assbank").find({id_app:$id_app$$,denom:$denom$$}).lean()).filter($b$$=>$b$$.amount),spend=async($id_app$$,$access_token$$,$from_address$$,$to_address$$,$data$$)=>{try{return await global.getModel("assbank").sendCoins($id_app$$,
$from_address$$,$to_address$$,$data$$.amount,$data$$.denom,$data$$),{message:"\u0110\u00e3 chuy\u1ec3n th\u00e0nh c\u00f4ng"}}catch($e$$){return{code:5,message:$e$$.message||$e$$.error||$e$$}}};module.exports={getAccount,getWallet,getKeyInfo,bankBalancesGet,denomBalancesGet,spend,getLatestHash,getHashById,findHash,validHash,mining};