faucet-server-app 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -13,6 +13,27 @@ export {RemoteSQLStorage};
13
13
  export {verifyProsopoCaptcha} from './captcha.js';
14
14
  export type {CaptchaVerification} from './captcha.js';
15
15
  export {DEFAULT_CORS_ORIGINS, DEFAULT_CLAIM_COOLDOWN_SECONDS} from './env.js';
16
+ export {
17
+ getArbitraryCallsMode,
18
+ getCallAllowlist,
19
+ getCallLimits,
20
+ isCallAllowed,
21
+ parseCallRequest,
22
+ planGas,
23
+ selectorOf,
24
+ DEFAULT_MAX_CALL_GAS,
25
+ GAS_BUFFER_PERCENT,
26
+ SIMPLE_TRANSFER_GAS,
27
+ } from './calls.js';
28
+ export type {
29
+ ArbitraryCallsMode,
30
+ CallLimits,
31
+ ConfigIssues,
32
+ GasPlan,
33
+ } from './calls.js';
34
+ export {checkConfig} from './config.js';
35
+ export {parseChainId} from './chains.js';
36
+ export type {ChainConfig} from './chains.js';
16
37
 
17
38
  /**
18
39
  * Creates the tables the faucet needs. Idempotent, safe to call on every boot.
@@ -72,8 +93,10 @@ export function createServer<CustomEnv extends Env>(
72
93
  code: 'code' in err ? err.code : 5000,
73
94
  status: 'status' in err ? err.status : undefined,
74
95
  message: err.message,
75
- cause: env.DEV ? err.cause : undefined,
76
- stack: env.DEV ? err.stack : undefined,
96
+ // an explicit 'true', so that DEV=false does not read as
97
+ // truthy and leak stack traces
98
+ cause: env.DEV === 'true' ? err.cause : undefined,
99
+ stack: env.DEV === 'true' ? err.stack : undefined,
77
100
  },
78
101
  ],
79
102
  },
package/src/setup.ts CHANGED
@@ -2,6 +2,7 @@ import {MiddlewareHandler} from 'hono/types';
2
2
  import {ServerOptions} from './types.js';
3
3
  import {Env} from './env.js';
4
4
  import {RemoteSQLStorage} from './storage/RemoteSQLStorage.js';
5
+ import {checkConfig} from './config.js';
5
6
 
6
7
  export type SetupOptions<CustomEnv extends Env> = {
7
8
  serverOptions: ServerOptions<CustomEnv>;
@@ -18,6 +19,28 @@ declare module 'hono' {
18
19
  }
19
20
  }
20
21
 
22
+ // Cloudflare Workers has no boot hook, so the configuration is reported once
23
+ // per isolate instead: an operator gets the same errors and warnings the node
24
+ // cli prints at startup, on the first request the isolate serves.
25
+ let configReported = false;
26
+
27
+ function reportConfigOnce(env: Env): void {
28
+ if (configReported) {
29
+ return;
30
+ }
31
+ configReported = true;
32
+ const issues = checkConfig(env);
33
+ for (const line of issues.info) {
34
+ console.log(line);
35
+ }
36
+ for (const warning of issues.warnings) {
37
+ console.warn(`WARNING: ${warning}`);
38
+ }
39
+ for (const error of issues.errors) {
40
+ console.error(`ERROR: ${error}`);
41
+ }
42
+ }
43
+
21
44
  export function setup<CustomEnv extends Env>(
22
45
  options: SetupOptions<CustomEnv>,
23
46
  ): MiddlewareHandler {
@@ -25,6 +48,7 @@ export function setup<CustomEnv extends Env>(
25
48
 
26
49
  return async (c, next) => {
27
50
  const env = getEnv(c);
51
+ reportConfigOnce(env);
28
52
  const db = getDB(c);
29
53
  const storage = new RemoteSQLStorage(db);
30
54
 
@@ -73,7 +73,11 @@ export class RemoteSQLStorage {
73
73
  // Atomically reserves a claim slot: the row is only inserted when no recent
74
74
  // claim exists for that address or that ip. A single INSERT ... SELECT
75
75
  // WHERE NOT EXISTS statement is atomic in SQLite, so two concurrent requests
76
- // cannot both reserve. An empty `ip` disables the ip bucket.
76
+ // cannot both reserve. An empty `ip` disables the ip bucket, and an empty
77
+ // `address` disables the address bucket (used by /api/execute, whose `to` is
78
+ // a shared contract and so cannot be a rate limiting key: without the
79
+ // `address <> ''` guard every such row would collide with every other one
80
+ // and the first call would lock out every caller for a whole cooldown).
77
81
  async reserveClaim(params: {
78
82
  chainId: string;
79
83
  address: string;
@@ -92,7 +96,7 @@ export class RemoteSQLStorage {
92
96
  SELECT 1 FROM Claims
93
97
  WHERE chain_id = ?
94
98
  AND (
95
- (address = ? AND claimed_at > ?)
99
+ (address <> '' AND address = ? AND claimed_at > ?)
96
100
  OR (ip <> '' AND ip = ? AND claimed_at > ?)
97
101
  )
98
102
  )
@@ -143,7 +147,7 @@ export class RemoteSQLStorage {
143
147
  `SELECT address, ip, claimed_at FROM Claims
144
148
  WHERE chain_id = ?
145
149
  AND (
146
- (address = ? AND claimed_at > ?)
150
+ (address <> '' AND address = ? AND claimed_at > ?)
147
151
  OR (ip <> '' AND ip = ? AND claimed_at > ?)
148
152
  )
149
153
  ORDER BY claimed_at DESC LIMIT 1;`,
@@ -156,7 +160,10 @@ export class RemoteSQLStorage {
156
160
  return {reason: 'address', retryAfter: 1};
157
161
  }
158
162
  const row = results[0];
159
- const matchedAddress = row.address === address;
163
+ // `address !== ''` matters: an /api/execute row also has an empty address,
164
+ // so a plain `row.address === address` would call an ip hit an address hit
165
+ // and report the wrong cooldown.
166
+ const matchedAddress = address !== '' && row.address === address;
160
167
  const cooldown = matchedAddress ? addressCooldown : ipCooldown;
161
168
  const retryAfter = Math.max(1, row.claimed_at + cooldown - now);
162
169
  return {reason: matchedAddress ? 'address' : 'ip', retryAfter};