Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -58,4 +62,6 @@ module.exports = {
storageHost,
mainnetSignatureMessage,
cacheHostUrl,
blockchainSyncServerUrl,
WebaverseERC20Address
};
82 changes: 2 additions & 80 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,30 @@
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');
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 {
Expand Down Expand Up @@ -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;
Expand Down
128 changes: 51 additions & 77 deletions routes/accounts.js
Original file line number Diff line number Diff line change
@@ -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,
}
_handleAccounts,
};