function getSearchUrl(_search) { if (_search.length == 42) { return `/address/${_search}`; } return `/tx/${_search}`; } class RaptorChainInterface { // class Transaction { // constructor(txjson) { // this.txdata = JSON.parse(txjson); // this.type = this.txdata.type; // if (this.type == 0) { // this.sender = this.txdata.from; // this.recipient = this.txdata.to; // this.value = txdata.tokens; // } // else if this.type == 1 { // this.sender = this.txdata.from; // this.recipient = this.txdata.to; // this.value = txdata.tokens; // } // } // } constructor(nodeaddr) { this.node = nodeaddr; } async fetchJSON(url) { return (await (await fetch(url))).json(); } async getBlock(bkid) { if (bkid.length == 66) { return (await this.fetchJSON(`${this.node}/chain/blockByHash/${bkid}`)).result; } if (bkid.length == 64) { return (await this.fetchJSON(`${this.node}/chain/blockByHash/0x${bkid}`)).result; } if (Number.isInteger(Number(bkid)) && Number(bkid) >= 0) { return (await this.fetchJSON(`${this.node}/chain/block/${bkid}`)).result; } } async getChainLength() { return (await this.fetchJSON(`${this.node}/chain/length`)).result; } async getRawTransaction(txid) { return (await this.fetchJSON(`${this.node}/get/transactions/${txid}`)).result[0]; } async getAccount(address) { return (await this.fetchJSON(`${this.node}/accounts/accountInfo/${address}`)); } async getNLastBlocks(N) { let _chainlength = (await this.getChainLength()); let blocks = []; for (let n=_chainlength-1; n>=Math.max(_chainlength-N, 0); n--) { blocks.push(await this.getBlock(n)); } return blocks; } async systemRoot() { return (await (await fetch(`https://${this.node}/chain/getlastblock`)).json()).result.txsRoot; } } function handleSearch() { _search = document.getElementById("searchInput").value; _url = getSearchUrl(_search); window.location.replace(_url); // open(_url); } class Rendering { constructor(puller) { this.puller = puller; } leftPadding(num, paddingValue) { let strnum = String(num); return (("0").repeat(paddingValue - strnum.length) + strnum) } formatTimestamp(inSeconds) { const dte = (new Date(inSeconds*1000)); let time = `${dte.getHours()}:${this.leftPadding(dte.getMinutes(), 2)}:${this.leftPadding(dte.getSeconds(),2)}`; let date = `${this.leftPadding(dte.getDate())}/${this.leftPadding(dte.getMonth() + 1, 2)}/${dte.getFullYear()}`; return [time,date] } renderTable(lines) { let fmtLines = []; for (let n=0; n${lines[n][o]}`); } fmtLines.push("" + (fmtItems.join("")) + "") } return `${fmtLines.join("")}
` } renderBlockList(blocks) { let _fmtList = [["Height", "Hash", "Time", "Date", "Miner"]]; for (let n=0; n${bk.height}`, `${bk.miningData.proof}`, _time, _date, `${bk.miningData.miner}`]); } return this.renderTable(_fmtList); } }