diff --git a/examples/micro/README.md b/examples/micro/README.md index 5cd096e..d7301d4 100644 --- a/examples/micro/README.md +++ b/examples/micro/README.md @@ -4,7 +4,7 @@ This backend implementation uses [Micro](https://github.com/vercel/micro) which ## Usage -First, you need an Arweave wallet. Provide the path to the wallet with the `WALLET_PATH` environment variable. +First, you need an Arweave wallet. Provide the path to the wallet with the `WALLET` environment variable. > [!NOTE] > @@ -23,14 +23,14 @@ yarn install Finally, start the server with: ```sh -CONTRACT_TXID="your-contract" yarn start +CONTRACT="your-contract" yarn start ``` ### Configurations There are several environment variables to configure the server. You can provide them within the command line, or via `.env` file. An example is given [here](./.env.example). -- `WALLET_PATH=path/to/wallet.json`
HollowDB requires an Arweave wallet, specified by this variable. If none is given, it defaults to `./config/wallet.json`. +- `WALLET=path/to/wallet.json`
HollowDB requires an Arweave wallet, specified by this variable. If none is given, it defaults to `./config/wallet.json`. - `REDIS_URL=`
You need a Redis server running before you start the server, the URL to the server can be provided with a `REDIS_URL` environment variable. The connection URL defaults to `redis://default:redispw@localhost:6379`. diff --git a/examples/micro/package.json b/examples/micro/package.json index 664336e..33c8de1 100644 --- a/examples/micro/package.json +++ b/examples/micro/package.json @@ -17,10 +17,10 @@ }, "main": "build/src/index.js", "scripts": { - "precompile": "yarn clean", - "compile": "npx tsc", - "prestart": "yarn compile", - "start": "npx micro", + "prebuild": "yarn clean", + "build": "npx tsc", + "prestart": "yarn build", + "start": "node ./build/src/index.js", "test": "npx mocha --bail", "clean": "rm -rf ./build" }, @@ -29,7 +29,7 @@ "@types/mocha": "^10.0.1", "@types/node": "^18.16.3", "@types/test-listen": "^1.1.0", - "arlocal": "^1.1.60", + "arlocal": "1.1.62", "chai": "^4.3.7", "hollowdb-prover": "^0.1.4", "mocha": "^10.2.0", diff --git a/examples/micro/src/config/index.ts b/examples/micro/src/config/index.ts index 5d0b774..d519200 100644 --- a/examples/micro/src/config/index.ts +++ b/examples/micro/src/config/index.ts @@ -4,7 +4,7 @@ export default { /** Redis URL to connect to. Defaults to `redis://default:redispw@localhost:6379`. */ REDIS_URL: process.env.REDIS_URL || "redis://default:redispw@localhost:6379", /** Path to Arweave wallet. */ - WALLET_PATH: process.env.WALLET_PATH || "./config/wallet.json", + WALLET_PATH: process.env.WALLET || "./config/wallet.json", /** Log level for underlying Warp. */ WARP_LOG_LEVEL: (process.env.WARP_LOG_LEVEL || "info") as Parameters[0], /** Arweave port for `arlocal`. */ diff --git a/examples/micro/src/index.ts b/examples/micro/src/index.ts index ea5ac41..01fd305 100644 --- a/examples/micro/src/index.ts +++ b/examples/micro/src/index.ts @@ -1,9 +1,11 @@ // load env variables (put this at top) import "dotenv/config"; +import http from "http"; import { Redis } from "ioredis"; -import { readFileSync } from "fs"; +import { existsSync, readFileSync } from "fs"; import { SDK } from "hollowdb"; +import { serve } from "micro"; import type { JWKInterface } from "warp-contracts"; import makeServer from "./server"; @@ -11,17 +13,25 @@ import config from "./config"; import { makeWarp } from "./util"; import { createCaches } from "./cache"; -const contractTxId = process.env.CONTRACT_TXID; +const contractTxId = process.env.CONTRACT; if (!contractTxId) { throw new Error("Please provide CONTRACT_TXID environment variable."); } +if (Buffer.from(contractTxId, "base64").toString("hex").length !== 64) { + throw new Error("Invalid CONTRACT_TXID."); +} const redisClient = new Redis(config.REDIS_URL, { lazyConnect: false, // explicitly connect }); const caches = createCaches(contractTxId, redisClient); +if (!existsSync(config.WALLET_PATH)) { + throw new Error("No wallet found at: " + config.WALLET_PATH); +} const wallet = JSON.parse(readFileSync(config.WALLET_PATH, "utf-8")) as JWKInterface; const warp = makeWarp(caches); + const hollowdb = new SDK(wallet, contractTxId, warp); -// module.exports needed by Micro -module.exports = makeServer(hollowdb, contractTxId); +const server = new http.Server(serve(makeServer(hollowdb, contractTxId))); + +server.listen(3000); diff --git a/examples/micro/test/index.test.ts b/examples/micro/test/index.test.ts index 5464ff0..504c70e 100644 --- a/examples/micro/test/index.test.ts +++ b/examples/micro/test/index.test.ts @@ -45,8 +45,8 @@ describe("crud operations", () => { const { contractTxId } = await deploy(owner, warp); // start the server & connect to the contract - const circuitsDir = __dirname + "../../../config/circuits/hollow-authz-plonk"; - prover = new Prover(circuitsDir + "/circuit/hollow-authz.wasm", circuitsDir + "/circuit/prover_key.zkey", "plonk"); + const circuitsDir = __dirname + "/../../../config/circuits/hollow-authz-plonk"; + prover = new Prover(circuitsDir + "/hollow-authz.wasm", circuitsDir + "/prover_key.zkey", "plonk"); caches = createCaches(contractTxId, redisClient); warp = makeLocalWarp(config.ARWEAVE_PORT, caches); const hollowdb = new SDK(owner, contractTxId, warp); diff --git a/examples/micro/yarn.lock b/examples/micro/yarn.lock index a9cb2d9..c772835 100644 --- a/examples/micro/yarn.lock +++ b/examples/micro/yarn.lock @@ -1933,7 +1933,36 @@ argparse@^2.0.1: resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38" integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q== -arlocal@^1.1.59, arlocal@^1.1.60: +arlocal@1.1.62: + version "1.1.62" + resolved "https://registry.yarnpkg.com/arlocal/-/arlocal-1.1.62.tgz#aecb7e8bc2e915a7e4be8c82b74a0a694c5a080a" + integrity sha512-BnpolFe3obbQBGKvvkvrAZ4oleLHox7Xcly2l896cHzysPqvo8C9lYofUiQKYcprHIhHH1F7AFLrxAvZq5yQLQ== + dependencies: + "@koa/cors" "^3.1.0" + apollo-server-koa "^3.6.2" + arbundles "^0.6.19" + arweave-bundles "^1.0.3" + asn1.js "5.4.1" + bignumber.js "^9.0.2" + bytes "^3.1.2" + graphql "^16.2.0" + knex "^0.95.6" + koa "^2.13.4" + koa-body "^4.2.0" + koa-bodyparser "^4.3.0" + koa-json "^2.0.2" + koa-logger "^3.2.1" + koa-router "^10.1.1" + lodash "^4.17.21" + mime "^3.0.0" + minimist "^1.2.5" + moment "^2.29.1" + rfc4648 "^1.5.1" + smartweave "^0.4.46" + sqlite3 "^5.0.3" + tsc-watch "^4.6.0" + +arlocal@^1.1.59: version "1.1.64" resolved "https://registry.yarnpkg.com/arlocal/-/arlocal-1.1.64.tgz#153f6bdb8fdc8840813f4d147ac60d4c50d1b81a" integrity sha512-623LT99n+TVYYO7USXaETN+uTOcZOWxMbMtdnlQ6uGWiroGJzu13jSsB0w3Q0JHsaiOwIHG5tZFNVUpNCplrNQ==