From a9c72a5bd7ef45e6923a5685d5b9f33432a56d68 Mon Sep 17 00:00:00 2001 From: Muhammad Abeer Date: Sun, 20 Mar 2022 01:41:12 +0500 Subject: [PATCH] handle accounts fixes --- constants.js | 6 +++ index.js | 82 +---------------------------- routes/accounts.js | 128 ++++++++++++++++++--------------------------- 3 files changed, 59 insertions(+), 157 deletions(-) diff --git a/constants.js b/constants.js index d64a0384..582d0733 100644 --- a/constants.js +++ b/constants.js @@ -46,6 +46,10 @@ const ethereumHost = 'ethereum.exokit.org'; const storageHost = 'https://ipfs.exokit.org'; const mainnetSignatureMessage = `Connecting mainnet address.`; const cacheHostUrl = 'cache.webaverse.com'; +const blockchainSyncServerUrl = "https://blockchain-sync.webaverse.com"; +const WebaverseERC20Address = '0x5D31B1BAc74A55A2D475d069E698Eb814EcD7eAe'; + + module.exports = { MAX_SIZE, accountKeys, @@ -58,4 +62,6 @@ module.exports = { storageHost, mainnetSignatureMessage, cacheHostUrl, + blockchainSyncServerUrl, + WebaverseERC20Address }; \ No newline at end of file diff --git a/index.js b/index.js index 1ff0d6b7..b589b542 100644 --- a/index.js +++ b/index.js @@ -1,41 +1,22 @@ require('dotenv').config(); -const path = require('path'); -const stream = require('stream'); const fs = require('fs'); const url = require('url'); const querystring = require('querystring'); const http = require('http'); const https = require('https'); -const dns = require('dns'); const crypto = require('crypto'); -const zlib = require('zlib'); -const os = require('os'); -const child_process = require('child_process'); -const mkdirp = require('mkdirp'); -const FormData = require('form-data'); -// const express = require('express'); const httpProxy = require('http-proxy'); const ws = require('ws'); -// const LRU = require('lru'); -const mime = require('mime'); const AWS = require('aws-sdk'); -const Stripe = require('stripe'); -// const puppeteer = require('puppeteer'); const namegen = require('./namegen.js'); -const Base64Encoder = require('./encoder.js').Encoder; -// const {JSONServer, CustomEvent} = require('./dist/sync-server.js'); const fetch = require('node-fetch'); -const {SHA3} = require('sha3'); const {default: formurlencoded} = require('form-urlencoded'); const bip39 = require('bip39'); const {hdkey} = require('ethereumjs-wallet'); -const {getDynamoItem, getDynamoAllItems, putDynamoItem} = require('./aws.js'); const {getRedisItem, getRedisAllItems, parseRedisItems} = require('./redis.js'); -const {getExt, makePromise} = require('./utils.js'); -const Timer = require('./timer.js'); +const { makePromise} = require('./utils.js'); const {getStoreEntries, getChainNft, getAllWithdrawsDeposits} = require('./tokens.js'); const {getBlockchain} = require('./blockchain.js'); -// const browserManager = require('./browser-manager.js'); const {accountKeys, ids, nftIndexName, redisPrefixes, mainnetSignatureMessage, cacheHostUrl} = require('./constants.js'); const {connect: redisConnect, getRedisClient} = require('./redis'); const ethereumJsUtil = require('./ethereumjs-util.js'); @@ -43,6 +24,7 @@ const gotNfts = require('got-nfts'); const OpenAI = require('openai-api'); const GPT3Encoder = require('gpt-3-encoder'); const Web3 = require('web3'); +const {_handleAccounts} = require('./routes/accounts'); const _jsonParse = s => { try { @@ -1028,66 +1010,6 @@ try { } }; -const _handleAccounts = chainName => async (req, res) => { - const _respond = (statusCode, body) => { - res.statusCode = statusCode; - _setCorsHeaders(res); - res.end(body); - }; - const _setCorsHeaders = res => { - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Headers', '*'); - res.setHeader('Access-Control-Allow-Methods', '*'); - }; - const _makeFakeAccount = address => { - const account = { - address, - }; - for (const k of accountKeys) { - account[k] = ''; - } - return account; - }; - const _getAccount = async address => getRedisItem(address, redisPrefixes.mainnetsidechainAccount) - .then(o => o.Item || _makeFakeAccount(address)); - -try { - const {method} = req; - let {pathname: p} = url.parse(req.url); - // console.log('ipfs request', {method, p}); - - if (method === 'OPTIONS') { - // res.statusCode = 200; - _setCorsHeaders(res); - res.end(); - } else if (method === 'GET') { - if (p === '/') { - let accounts = await getRedisAllItems(redisPrefixes.mainnetsidechainAccount); - accounts = accounts.filter(a => a.id !== ids.lastCachedBlockAccount); - _respond(200, JSON.stringify(accounts)); - } else { - const match = p.match(/^\/(0x[a-f0-9]+)$/i); - if (match) { - const address = match[1]; - const result = await _getAccount(address); - console.log('fetched account', address, result); - _respond(200, JSON.stringify(result)); - } else { - _respond(404, ''); - } - } - } else { - _respond(404, ''); - } -} catch(err) { - console.warn(err); - - _respond(500, JSON.stringify({ - error: err.stack, - })); -} -}; - /* const _handlePayments = async (req, res) => { const _respond = (statusCode, body) => { res.statusCode = statusCode; diff --git a/routes/accounts.js b/routes/accounts.js index b044ecf7..7f5c496c 100644 --- a/routes/accounts.js +++ b/routes/accounts.js @@ -1,84 +1,58 @@ -const url = require('url'); -const { _setCorsHeaders } = require('../utils.js'); -const blockchain = require('../blockchain.js'); -const accountManager = require('../account-manager.js'); +const url = require("url"); +const fetch = require("node-fetch"); -/* const _jsonParse = s => { - try { - return JSON.parse(s); - } catch(err) { - return null; - } -}; */ +const { + blockchainSyncServerUrl +} = require("../constants.js"); -const _handleAccountsRequest = async (req, res) => { - const request = url.parse(req.url); - const path = request.path.split('/')[1]; - let match; - try { - res = _setCorsHeaders(res); - const {method} = req; - if (method === 'OPTIONS') { - res.end(); - } else if (method === 'GET') { - if (path === 'latestBlock') { - const latestBlock = await blockchain.getLatestBlock(); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(latestBlock, null, 2)); - } else if (match = request.path.match(/^\/getEvents\/([^\/]+)\/([0-9]+)\/([0-9]+)$/)) { - const eventTypes = match[1].split(','); - const startBlock = parseInt(match[2], 10); - const endBlock = parseInt(match[3], 10); - let result = []; - await Promise.all(eventTypes.map(eventType => - blockchain.getEvents(eventType, startBlock, endBlock) - .then(events => { - result.push.apply(result, events); - }) - )); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(result, null, 2)); - } else { - res.statusCode = 404; - res.end(); - } - } else if (method === 'POST') { - const bs = []; - req.on('data', d => { - bs.push(d); - }); - req.on('end', async () => { - try { - const b = Buffer.concat(bs); - const s = b.toString('utf8'); - - if (path === 'sendTransaction') { - const spec = JSON.parse(s); - const transaction = await blockchain.runTransaction(spec); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(transaction, null, 2)); - } else { - const userKeys = await accountManager.getAccount(); - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify(userKeys, null, 2)); - } - } catch (err) { - console.log(err); - res.statusCode = 500; - res.end(err.stack); - } - }); +const _handleAccounts = (chainName) => async (req, res) => { + const _respond = (statusCode, body) => { + res.statusCode = statusCode; + _setCorsHeaders(res); + res.end(body); + }; + const _setCorsHeaders = (res) => { + res.setHeader("Access-Control-Allow-Origin", "*"); + res.setHeader("Access-Control-Allow-Headers", "*"); + res.setHeader("Access-Control-Allow-Methods", "*"); + }; + try { + const { method } = req; + let { pathname: p } = url.parse(req.url); + if (method === "OPTIONS") { + _setCorsHeaders(res); + res.end(); + } else if (method === "GET") { + if (p === "/") { + const accounts = await fetch(`${blockchainSyncServerUrl}/account/`).then(res => res.json()); + _respond(200, JSON.stringify(accounts)); + } else { + const match = p.match(/^\/(0x[a-f0-9]+)$/i); + if (match) { + const address = match[1]; + const account = await fetch(`${blockchainSyncServerUrl}/account/${address}`).then(res => res.json()); + _respond(200, JSON.stringify(account)); } else { - res.statusCode = 404; - res.end(); + _respond(404, ""); } - } catch (err) { - console.log(err); - res.statusCode = 500; - res.end(err.stack); + } + } else { + _respond(404, ""); } -} + } catch (err) { + console.warn(err); + + _respond( + 500, + JSON.stringify({ + error: err.stack, + }) + ); + } +}; + + module.exports = { - _handleAccountsRequest, -} \ No newline at end of file + _handleAccounts, +}; \ No newline at end of file