tapimo 0.0.2 → 0.1.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/README.md +11 -2
- package/dist/ApiKey.d.ts +12 -11
- package/dist/ApiKey.d.ts.map +1 -1
- package/dist/ApiKey.js +10 -12
- package/dist/ApiKey.js.map +1 -1
- package/dist/ApiKeys.d.ts +139 -0
- package/dist/ApiKeys.d.ts.map +1 -0
- package/dist/ApiKeys.js +166 -0
- package/dist/ApiKeys.js.map +1 -0
- package/dist/App.d.ts +920 -910
- package/dist/App.d.ts.map +1 -1
- package/dist/App.js +36 -12
- package/dist/App.js.map +1 -1
- package/dist/Handler.d.ts +86 -0
- package/dist/Handler.d.ts.map +1 -0
- package/dist/Handler.js +83 -0
- package/dist/Handler.js.map +1 -0
- package/dist/Scope.d.ts +4 -1
- package/dist/Scope.d.ts.map +1 -1
- package/dist/Scope.js +5 -1
- package/dist/Scope.js.map +1 -1
- package/dist/Webhooks.d.ts +3 -5
- package/dist/Webhooks.d.ts.map +1 -1
- package/dist/Webhooks.js +9 -2
- package/dist/Webhooks.js.map +1 -1
- package/dist/admin/Access.d.ts +1 -3
- package/dist/admin/Access.d.ts.map +1 -1
- package/dist/admin/Access.js +1 -3
- package/dist/admin/Access.js.map +1 -1
- package/dist/admin/App.d.ts +98 -209
- package/dist/admin/App.d.ts.map +1 -1
- package/dist/admin/App.js +2 -4
- package/dist/admin/App.js.map +1 -1
- package/dist/admin/apps/api-keys.d.ts +7 -5
- package/dist/admin/apps/api-keys.d.ts.map +1 -1
- package/dist/admin/apps/api-keys.js +8 -9
- package/dist/admin/apps/api-keys.js.map +1 -1
- package/dist/admin/apps/scopes.d.ts +3 -0
- package/dist/admin/apps/scopes.d.ts.map +1 -1
- package/dist/admin/apps/verified-tokens.d.ts +101 -215
- package/dist/admin/apps/verified-tokens.d.ts.map +1 -1
- package/dist/admin/apps/verified-tokens.js +17 -38
- package/dist/admin/apps/verified-tokens.js.map +1 -1
- package/dist/admin/ui.generated.d.ts +1 -1
- package/dist/admin/ui.generated.js +1 -1
- package/dist/apps/balances.d.ts +2 -2
- package/dist/apps/receipts.d.ts +43 -43
- package/dist/apps/tokens.d.ts +454 -454
- package/dist/apps/transactions.d.ts +43 -43
- package/dist/apps/transfers.d.ts +29 -29
- package/dist/apps/webhooks.d.ts +5 -2
- package/dist/apps/webhooks.d.ts.map +1 -1
- package/dist/apps/webhooks.js +38 -34
- package/dist/apps/webhooks.js.map +1 -1
- package/dist/{Cli.d.ts → cli/Cli.d.ts} +13 -2
- package/dist/cli/Cli.d.ts.map +1 -0
- package/dist/cli/Cli.js +19 -0
- package/dist/cli/Cli.js.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/bin.js +4 -0
- package/dist/cli/bin.js.map +1 -0
- package/dist/cli/commands/admin.d.ts +15 -0
- package/dist/cli/commands/admin.d.ts.map +1 -0
- package/dist/cli/commands/admin.js +60 -0
- package/dist/cli/commands/admin.js.map +1 -0
- package/dist/cloudflare.d.ts +7 -0
- package/dist/cloudflare.d.ts.map +1 -1
- package/dist/cloudflare.js +9 -0
- package/dist/cloudflare.js.map +1 -1
- package/dist/db/Db.d.ts +103 -0
- package/dist/db/Db.d.ts.map +1 -0
- package/dist/db/Db.js +274 -0
- package/dist/db/Db.js.map +1 -0
- package/dist/db/Schema.d.ts +88 -0
- package/dist/db/Schema.d.ts.map +1 -0
- package/dist/db/Schema.js +130 -0
- package/dist/db/Schema.js.map +1 -0
- package/dist/db/tables/organizations.d.ts +79 -0
- package/dist/db/tables/organizations.d.ts.map +1 -0
- package/dist/db/tables/organizations.js +81 -0
- package/dist/db/tables/organizations.js.map +1 -0
- package/dist/db/tables/verifiedTokens.d.ts +53 -0
- package/dist/db/tables/verifiedTokens.d.ts.map +1 -0
- package/dist/db/tables/verifiedTokens.js +74 -0
- package/dist/db/tables/verifiedTokens.js.map +1 -0
- package/dist/db/tables/webhookDeliveries.d.ts +71 -0
- package/dist/db/tables/webhookDeliveries.d.ts.map +1 -0
- package/dist/db/tables/webhookDeliveries.js +119 -0
- package/dist/db/tables/webhookDeliveries.js.map +1 -0
- package/dist/db/tables/webhookSubscriptions.d.ts +158 -0
- package/dist/db/tables/webhookSubscriptions.d.ts.map +1 -0
- package/dist/db/tables/webhookSubscriptions.js +272 -0
- package/dist/db/tables/webhookSubscriptions.js.map +1 -0
- package/dist/handlers/internal/executionError.d.ts +25 -0
- package/dist/handlers/internal/executionError.d.ts.map +1 -0
- package/dist/handlers/internal/executionError.js +184 -0
- package/dist/handlers/internal/executionError.js.map +1 -0
- package/dist/handlers/internal/multisig.d.ts +237 -0
- package/dist/handlers/internal/multisig.d.ts.map +1 -0
- package/dist/handlers/internal/multisig.js +567 -0
- package/dist/handlers/internal/multisig.js.map +1 -0
- package/dist/handlers/internal/requestListener.d.ts +124 -0
- package/dist/handlers/internal/requestListener.d.ts.map +1 -0
- package/dist/handlers/internal/requestListener.js +173 -0
- package/dist/handlers/internal/requestListener.js.map +1 -0
- package/dist/handlers/internal/sponsorship.d.ts +83 -0
- package/dist/handlers/internal/sponsorship.d.ts.map +1 -0
- package/dist/handlers/internal/sponsorship.js +103 -0
- package/dist/handlers/internal/sponsorship.js.map +1 -0
- package/dist/handlers/internal/types.d.ts +284 -0
- package/dist/handlers/internal/types.d.ts.map +1 -0
- package/dist/handlers/internal/types.js +2 -0
- package/dist/handlers/internal/types.js.map +1 -0
- package/dist/handlers/internal/utils.d.ts +34 -0
- package/dist/handlers/internal/utils.d.ts.map +1 -0
- package/dist/handlers/internal/utils.js +131 -0
- package/dist/handlers/internal/utils.js.map +1 -0
- package/dist/handlers/relay.d.ts +119 -0
- package/dist/handlers/relay.d.ts.map +1 -0
- package/dist/handlers/relay.js +1264 -0
- package/dist/handlers/relay.js.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/internal/Auth.d.ts +16 -10
- package/dist/internal/Auth.d.ts.map +1 -1
- package/dist/internal/Auth.js +72 -12
- package/dist/internal/Auth.js.map +1 -1
- package/dist/internal/RpcSchema.d.ts +10 -0
- package/dist/internal/RpcSchema.d.ts.map +1 -0
- package/dist/internal/RpcSchema.js +2 -0
- package/dist/internal/RpcSchema.js.map +1 -0
- package/dist/internal/Store.d.ts +42 -3
- package/dist/internal/Store.d.ts.map +1 -1
- package/dist/internal/Store.js +65 -3
- package/dist/internal/Store.js.map +1 -1
- package/dist/internal/VerifiedTokens.d.ts +15 -34
- package/dist/internal/VerifiedTokens.d.ts.map +1 -1
- package/dist/internal/VerifiedTokens.js +115 -159
- package/dist/internal/VerifiedTokens.js.map +1 -1
- package/dist/internal/Webhooks.d.ts +49 -35
- package/dist/internal/Webhooks.d.ts.map +1 -1
- package/dist/internal/Webhooks.js +92 -205
- package/dist/internal/Webhooks.js.map +1 -1
- package/package.json +7 -2
- package/src/ApiKey.test.ts +3 -3
- package/src/ApiKey.ts +10 -12
- package/src/ApiKeys.test.ts +175 -0
- package/src/ApiKeys.ts +205 -0
- package/src/App.test-d.ts +1 -1
- package/src/App.test.ts +10768 -351
- package/src/App.ts +79 -36
- package/src/Client.test-d.ts +1 -1
- package/src/Handler.ts +154 -0
- package/src/Scope.ts +5 -1
- package/src/Webhooks.test.ts +8 -7
- package/src/Webhooks.ts +11 -5
- package/src/admin/Access.ts +1 -3
- package/src/admin/App.ts +19 -24
- package/src/admin/apps/api-keys.test.ts +19 -18
- package/src/admin/apps/api-keys.ts +8 -9
- package/src/admin/apps/scopes.test.ts +4 -0
- package/src/admin/apps/verified-tokens.test.ts +3 -30
- package/src/admin/apps/verified-tokens.ts +17 -36
- package/src/admin/ui.generated.ts +1 -1
- package/src/apps/balances.test.ts +4 -3
- package/src/apps/blocks.test.ts +1 -1
- package/src/apps/exchanges.test.ts +1 -1
- package/src/apps/indexer.test.ts +1 -1
- package/src/apps/receipts.test.ts +1 -1
- package/src/apps/rpc.test.ts +100 -10
- package/src/apps/tokenlist.test.ts +5 -7
- package/src/apps/tokens.test.ts +4 -3
- package/src/apps/transactions.test.ts +1 -1
- package/src/apps/transfers.test.ts +1 -1
- package/src/apps/verified-tokens.test.ts +24 -12
- package/src/apps/webhooks.test.ts +130 -143
- package/src/apps/webhooks.ts +45 -47
- package/src/{Cli.test.ts → cli/Cli.test.ts} +10 -9
- package/src/{Cli.ts → cli/Cli.ts} +16 -9
- package/src/cli/bin.ts +4 -0
- package/src/cli/commands/admin.test.ts +71 -0
- package/src/cli/commands/admin.ts +60 -0
- package/src/cloudflare.ts +10 -0
- package/src/db/Db.test.ts +84 -0
- package/src/db/Db.ts +383 -0
- package/src/db/Schema.ts +164 -0
- package/src/db/tables/organizations.test.ts +124 -0
- package/src/db/tables/organizations.ts +127 -0
- package/src/db/tables/verifiedTokens.ts +111 -0
- package/src/db/tables/webhookDeliveries.ts +159 -0
- package/src/db/tables/webhookSubscriptions.ts +358 -0
- package/src/handlers/internal/executionError.ts +205 -0
- package/src/handlers/internal/multisig.localnet.test.ts +146 -0
- package/src/handlers/internal/multisig.test.ts +744 -0
- package/src/handlers/internal/multisig.ts +858 -0
- package/src/handlers/internal/relay.localnet.test.ts +2035 -0
- package/src/handlers/internal/requestListener.ts +273 -0
- package/src/handlers/internal/sponsorship.ts +186 -0
- package/src/handlers/internal/types.ts +378 -0
- package/src/handlers/internal/utils.ts +153 -0
- package/src/handlers/relay.ts +1605 -0
- package/src/index.ts +4 -2
- package/src/internal/Auth.test.ts +21 -21
- package/src/internal/Auth.ts +92 -19
- package/src/internal/RateLimit.test.ts +1 -1
- package/src/internal/RpcSchema.ts +10 -0
- package/src/internal/Store.test.ts +98 -10
- package/src/internal/Store.ts +112 -6
- package/src/internal/VerifiedTokens.test.ts +99 -115
- package/src/internal/VerifiedTokens.ts +123 -175
- package/src/internal/Webhooks.test.ts +107 -107
- package/src/internal/Webhooks.ts +102 -220
- package/dist/ApiKeySource.d.ts +0 -166
- package/dist/ApiKeySource.d.ts.map +0 -1
- package/dist/ApiKeySource.js +0 -234
- package/dist/ApiKeySource.js.map +0 -1
- package/dist/Cli.d.ts.map +0 -1
- package/dist/Cli.js +0 -13
- package/dist/Cli.js.map +0 -1
- package/seeds/31318/icons/0x20c0000000000000000000000000000000000000.svg +0 -1
- package/seeds/31318/icons/0x20c0000000000000000000000000000000000001.svg +0 -1
- package/seeds/31318/icons/0x20c0000000000000000000000000000000000002.svg +0 -1
- package/seeds/31318/icons/0x20c0000000000000000000000000000000000003.svg +0 -1
- package/seeds/31318/verified-tokens.json +0 -30
- package/seeds/4217/icons/0x20c0000000000000000000000000000000000000.svg +0 -25
- package/seeds/4217/icons/0x20c000000000000000000000048c8f36df1c9a4a.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000000520792dcccccccc.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000000a6da882d075a4c3.svg +0 -4
- package/seeds/4217/icons/0x20c000000000000000000000111111111e910f0f.svg +0 -30
- package/seeds/4217/icons/0x20c00000000000000000000014f22ca97301eb73.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000001621e21f71cf12fb.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000002f52d5cc21a3207b.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000003158081efd85bfc2.svg +0 -11
- package/seeds/4217/icons/0x20c0000000000000000000003554d28269e0f3c2.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000383a23bacb546ab9.svg +0 -1
- package/seeds/4217/icons/0x20c00000000000000000000042109aef2f8b28e1.png +0 -0
- package/seeds/4217/icons/0x20c0000000000000000000005c0bac7cef389a11.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000006fd9a167923ba194.png +0 -0
- package/seeds/4217/icons/0x20c0000000000000000000007f7ba549dd0251b9.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000008191667423f70e67.svg +0 -5
- package/seeds/4217/icons/0x20c0000000000000000000008ee4fcff88888888.svg +0 -1
- package/seeds/4217/icons/0x20c0000000000000000000009a4a4b17e0dc6651.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000ab02d39df30bd17e.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000ae247a1130450f09.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000aeed2ec36a54d0e5.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000b9537d11c60e8b50.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000bd95bfb69fbe6ce3.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000c412ec89d0c08be5.svg +0 -1
- package/seeds/4217/icons/0x20c000000000000000000000f047dd7018e50367.png +0 -0
- package/seeds/4217/verified-tokens.json +0 -177
- package/seeds/42431/icons/0x20c0000000000000000000000000000000000000.svg +0 -25
- package/seeds/42431/icons/0x20c0000000000000000000000000000000000001.svg +0 -1
- package/seeds/42431/icons/0x20c0000000000000000000000000000000000002.svg +0 -1
- package/seeds/42431/icons/0x20c0000000000000000000000000000000000003.svg +0 -1
- package/seeds/42431/icons/0x20c0000000000000000000009e8d7eb59b783726.svg +0 -1
- package/seeds/42431/icons/0x20c000000000000000000000d72572838bbee59c.svg +0 -1
- package/seeds/42431/verified-tokens.json +0 -44
- package/seeds/openrpc/README.md +0 -27
- package/seeds/openrpc/execution-apis.json +0 -8989
- package/src/ApiKeySource.test-d.ts +0 -11
- package/src/ApiKeySource.test.ts +0 -295
- package/src/ApiKeySource.ts +0 -303
package/README.md
CHANGED
|
@@ -33,15 +33,24 @@ Install the package:
|
|
|
33
33
|
pnpm i tapimo
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
Stateful features (webhook subscriptions, verified tokens) live in Postgres.
|
|
37
|
+
Provision a database and apply migrations with the CLI:
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
DATABASE_URL=postgresql://… tapimo admin migrate
|
|
41
|
+
```
|
|
42
|
+
|
|
36
43
|
Compose an app with `App.create`. It returns a handler with a Web `fetch`
|
|
37
44
|
method and a Node.js `listener`, so you can serve it from any runtime or
|
|
38
45
|
framework:
|
|
39
46
|
|
|
40
47
|
<!-- prettier-ignore -->
|
|
41
48
|
```ts
|
|
42
|
-
import { App } from 'tapimo'
|
|
49
|
+
import { App, Db } from 'tapimo'
|
|
43
50
|
|
|
44
|
-
const handler = App.create(
|
|
51
|
+
const handler = App.create({
|
|
52
|
+
db: Db.postgres({ connectionString: process.env.DATABASE_URL }),
|
|
53
|
+
})
|
|
45
54
|
|
|
46
55
|
createServer(handler.listener) // Node.js
|
|
47
56
|
Bun.serve({ fetch: handler.fetch }) // Bun
|
package/dist/ApiKey.d.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The resolved API-key resource and the reusable credential primitives
|
|
3
|
-
* source backend shares.
|
|
2
|
+
* The resolved API-key resource and the reusable credential primitives.
|
|
4
3
|
*
|
|
5
4
|
* Holds the resolved {@link ApiKey} type and its {@link schema}, the token
|
|
6
|
-
* format/generation ({@link generateToken}/{@link generateId}), the
|
|
7
|
-
* derivation ({@link keyFor}/{@link recordPrefix}), and log
|
|
8
|
-
* These live here — not in
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* `
|
|
13
|
-
* persistence.
|
|
5
|
+
* format/generation ({@link generateToken}/{@link generateId}), the
|
|
6
|
+
* storage-key derivation ({@link keyFor}/{@link recordPrefix}), and log
|
|
7
|
+
* {@link redact}ion. These live here — not in the persistence module — so
|
|
8
|
+
* issued and parsed credentials are byte-identical and cannot drift. The
|
|
9
|
+
* persisted record/mint-input shapes and provisioning (mint/list/revoke) live
|
|
10
|
+
* in `./ApiKeys.js`, which composes these primitives with the app's KV state
|
|
11
|
+
* store (`App.create({ kv })`).
|
|
14
12
|
*/
|
|
15
13
|
import * as z from 'zod/mini';
|
|
16
14
|
/** Zod schemas owned by the API-key module. */
|
|
17
15
|
export declare namespace schema {
|
|
18
16
|
/**
|
|
19
17
|
* Canonical schema for resolved API-key metadata, and the single source of
|
|
20
|
-
* truth for the {@link ApiKey} type. `
|
|
18
|
+
* truth for the {@link ApiKey} type. `ApiKeys` derives its persisted
|
|
21
19
|
* `Record` and `MintInput` schemas from this one, so the resolved and
|
|
22
20
|
* persisted shapes cannot drift.
|
|
23
21
|
*/
|
|
@@ -36,6 +34,7 @@ export declare namespace schema {
|
|
|
36
34
|
"*": "*";
|
|
37
35
|
"data:read": "data:read";
|
|
38
36
|
"indexer:query": "indexer:query";
|
|
37
|
+
"rpc-relay": "rpc-relay";
|
|
39
38
|
"webhooks:read": "webhooks:read";
|
|
40
39
|
"webhooks:write": "webhooks:write";
|
|
41
40
|
}>>>;
|
|
@@ -60,6 +59,8 @@ export declare function generateToken(environment?: ApiKey['environment']): stri
|
|
|
60
59
|
export declare function generateId(): string;
|
|
61
60
|
/** Storage-key prefix under which key records are persisted, keyed by token hash. */
|
|
62
61
|
export declare const recordPrefix = "apikey:";
|
|
62
|
+
/** Hashes a plaintext token to its at-rest lookup hash (lowercase hex, no `0x`). */
|
|
63
|
+
export declare function hash(token: string): string;
|
|
63
64
|
/** Storage key under which the record for a plaintext token is persisted. */
|
|
64
65
|
export declare function keyFor(token: string): string;
|
|
65
66
|
//# sourceMappingURL=ApiKey.d.ts.map
|
package/dist/ApiKey.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiKey.d.ts","sourceRoot":"","sources":["../src/ApiKey.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ApiKey.d.ts","sourceRoot":"","sources":["../src/ApiKey.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAIH,OAAO,KAAK,CAAC,MAAM,UAAU,CAAA;AAK7B,+CAA+C;AAC/C,yBAAiB,MAAM,CAAC;IACtB;;;;;OAKG;IACI,MAAM,MAAM;;;;;;;;;;;;;;;;;;;qBAiBjB,CAAA;CACH;AAED,mDAAmD;AACnD,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,MAAM,CAAC,MAAM,CAAC,CAAA;AAEnD;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAI3C;AAiCD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,WAAW,GAAE,MAAM,CAAC,aAAa,CAAgB,GAAG,MAAM,CAEvF;AAED,mDAAmD;AACnD,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,qFAAqF;AACrF,eAAO,MAAM,YAAY,YAAY,CAAA;AAErC,oFAAoF;AACpF,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE1C;AAED,6EAA6E;AAC7E,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE5C"}
|
package/dist/ApiKey.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The resolved API-key resource and the reusable credential primitives
|
|
3
|
-
* source backend shares.
|
|
2
|
+
* The resolved API-key resource and the reusable credential primitives.
|
|
4
3
|
*
|
|
5
4
|
* Holds the resolved {@link ApiKey} type and its {@link schema}, the token
|
|
6
|
-
* format/generation ({@link generateToken}/{@link generateId}), the
|
|
7
|
-
* derivation ({@link keyFor}/{@link recordPrefix}), and log
|
|
8
|
-
* These live here — not in
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* `
|
|
13
|
-
* persistence.
|
|
5
|
+
* format/generation ({@link generateToken}/{@link generateId}), the
|
|
6
|
+
* storage-key derivation ({@link keyFor}/{@link recordPrefix}), and log
|
|
7
|
+
* {@link redact}ion. These live here — not in the persistence module — so
|
|
8
|
+
* issued and parsed credentials are byte-identical and cannot drift. The
|
|
9
|
+
* persisted record/mint-input shapes and provisioning (mint/list/revoke) live
|
|
10
|
+
* in `./ApiKeys.js`, which composes these primitives with the app's KV state
|
|
11
|
+
* store (`App.create({ kv })`).
|
|
14
12
|
*/
|
|
15
13
|
import { nanoid } from 'nanoid';
|
|
16
14
|
import { Hash, Hex } from 'ox';
|
|
@@ -22,7 +20,7 @@ export var schema;
|
|
|
22
20
|
(function (schema) {
|
|
23
21
|
/**
|
|
24
22
|
* Canonical schema for resolved API-key metadata, and the single source of
|
|
25
|
-
* truth for the {@link ApiKey} type. `
|
|
23
|
+
* truth for the {@link ApiKey} type. `ApiKeys` derives its persisted
|
|
26
24
|
* `Record` and `MintInput` schemas from this one, so the resolved and
|
|
27
25
|
* persisted shapes cannot drift.
|
|
28
26
|
*/
|
|
@@ -95,7 +93,7 @@ export function generateId() {
|
|
|
95
93
|
/** Storage-key prefix under which key records are persisted, keyed by token hash. */
|
|
96
94
|
export const recordPrefix = 'apikey:';
|
|
97
95
|
/** Hashes a plaintext token to its at-rest lookup hash (lowercase hex, no `0x`). */
|
|
98
|
-
function hash(token) {
|
|
96
|
+
export function hash(token) {
|
|
99
97
|
return Hash.sha256(Hex.fromString(token)).slice(2);
|
|
100
98
|
}
|
|
101
99
|
/** Storage key under which the record for a plaintext token is persisted. */
|
package/dist/ApiKey.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiKey.js","sourceRoot":"","sources":["../src/ApiKey.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"ApiKey.js","sourceRoot":"","sources":["../src/ApiKey.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,IAAI,CAAA;AAC9B,OAAO,KAAK,CAAC,MAAM,UAAU,CAAA;AAE7B,OAAO,KAAK,SAAS,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAEnC,+CAA+C;AAC/C,MAAM,KAAW,MAAM,CAyBtB;AAzBD,W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+
*
|
|
4
|
+
* Records are keyed by token hash (`ApiKey.keyFor`) — the plaintext token is
|
|
5
|
+
* never stored — and validated against the scope catalog on read; unknown,
|
|
6
|
+
* corrupt, expired, or invalid records resolve to `null`. An `id → recordKey`
|
|
7
|
+
* index makes revoke-by-id O(1). The persisted {@link Record}/{@link
|
|
8
|
+
* MintInput} shapes live here (derived from `ApiKey.schema` so they cannot
|
|
9
|
+
* drift from the resolved shape), while the shared credential primitives
|
|
10
|
+
* (token format/generation, storage-key derivation, redaction) live in
|
|
11
|
+
* `./ApiKey.js`.
|
|
12
|
+
*/
|
|
13
|
+
import * as z from 'zod/mini';
|
|
14
|
+
import * as ApiKey from './ApiKey.js';
|
|
15
|
+
import type * as Store from './internal/Store.js';
|
|
16
|
+
/** Zod schemas owned by the API-keys module. */
|
|
17
|
+
export declare namespace schema {
|
|
18
|
+
/**
|
|
19
|
+
* Fields accepted when minting a key, and the single source of truth for the
|
|
20
|
+
* {@link MintInput} type and `mint` validation. Derived from the resolved
|
|
21
|
+
* `ApiKey.schema` (minus the generated `id`) so the scope vocabulary stays in
|
|
22
|
+
* lockstep with the resolved shape.
|
|
23
|
+
*/
|
|
24
|
+
const MintInput: z.ZodMiniObject<{
|
|
25
|
+
environment: z.ZodMiniDefault<z.ZodMiniEnum<{
|
|
26
|
+
production: "production";
|
|
27
|
+
sandbox: "sandbox";
|
|
28
|
+
}>>;
|
|
29
|
+
name: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
30
|
+
rateLimits: z.ZodMiniOptional<z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniObject<{
|
|
31
|
+
perMinute: z.ZodMiniNumber<number>;
|
|
32
|
+
}, z.core.$strip>>>;
|
|
33
|
+
scopes: z.ZodMiniReadonly<z.ZodMiniArray<z.ZodMiniEnum<{
|
|
34
|
+
"*": "*";
|
|
35
|
+
"data:read": "data:read";
|
|
36
|
+
"indexer:query": "indexer:query";
|
|
37
|
+
"rpc-relay": "rpc-relay";
|
|
38
|
+
"webhooks:read": "webhooks:read";
|
|
39
|
+
"webhooks:write": "webhooks:write";
|
|
40
|
+
}>>>;
|
|
41
|
+
createdBy: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
42
|
+
expiresAt: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
43
|
+
orgId: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
44
|
+
}, z.core.$strip>;
|
|
45
|
+
/**
|
|
46
|
+
* A persisted API-key record: the resolved `ApiKey.schema` plus
|
|
47
|
+
* persistence/display fields. Extending the resolved schema keeps the resolved
|
|
48
|
+
* and persisted shapes in lockstep.
|
|
49
|
+
*/
|
|
50
|
+
const Record: z.ZodMiniObject<{
|
|
51
|
+
environment: z.ZodMiniDefault<z.ZodMiniEnum<{
|
|
52
|
+
production: "production";
|
|
53
|
+
sandbox: "sandbox";
|
|
54
|
+
}>>;
|
|
55
|
+
id: z.ZodMiniString<string>;
|
|
56
|
+
name: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
57
|
+
orgId: z.ZodMiniString<string>;
|
|
58
|
+
rateLimits: z.ZodMiniOptional<z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniObject<{
|
|
59
|
+
perMinute: z.ZodMiniNumber<number>;
|
|
60
|
+
}, z.core.$strip>>>;
|
|
61
|
+
scopes: z.ZodMiniReadonly<z.ZodMiniArray<z.ZodMiniEnum<{
|
|
62
|
+
"*": "*";
|
|
63
|
+
"data:read": "data:read";
|
|
64
|
+
"indexer:query": "indexer:query";
|
|
65
|
+
"rpc-relay": "rpc-relay";
|
|
66
|
+
"webhooks:read": "webhooks:read";
|
|
67
|
+
"webhooks:write": "webhooks:write";
|
|
68
|
+
}>>>;
|
|
69
|
+
createdAt: z.ZodMiniString<string>;
|
|
70
|
+
createdBy: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
71
|
+
expiresAt: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
72
|
+
tokenLast4: z.ZodMiniString<string>;
|
|
73
|
+
}, z.core.$strip>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* A durable API-key record as persisted. A superset of the resolved
|
|
77
|
+
* `ApiKey.ApiKey` with provisioning/display metadata. The plaintext token is
|
|
78
|
+
* never stored — records are keyed by its hash (`ApiKey.keyFor`) and carry only
|
|
79
|
+
* `tokenLast4` for display.
|
|
80
|
+
*/
|
|
81
|
+
export type Record = z.output<typeof schema.Record>;
|
|
82
|
+
/**
|
|
83
|
+
* Fields accepted when minting a key via {@link mint}. Uses the schema's
|
|
84
|
+
* *input* type so defaulted fields (e.g. `environment`) are optional for
|
|
85
|
+
* callers — the default is applied during `mint`'s validation.
|
|
86
|
+
*/
|
|
87
|
+
export type MintInput = z.input<typeof schema.MintInput>;
|
|
88
|
+
/** Result of {@link mint}: the persisted record and its one-time token. */
|
|
89
|
+
export type MintResult = {
|
|
90
|
+
/** The persisted key record (metadata only — never the token). */
|
|
91
|
+
record: Record;
|
|
92
|
+
/** The plaintext token, shown once and unrecoverable afterward. */
|
|
93
|
+
token: string;
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* Mints a key: validates the input (e.g. rejects an unknown scope at the write
|
|
97
|
+
* boundary), persists its hashed record, and returns the record plus the
|
|
98
|
+
* one-time plaintext token (shown once, unrecoverable afterward).
|
|
99
|
+
*
|
|
100
|
+
* @param state - The KV state store holding key records.
|
|
101
|
+
* @param input - The key to mint.
|
|
102
|
+
* @returns The persisted record and its one-time token.
|
|
103
|
+
*/
|
|
104
|
+
export declare function mint(state: Store.State, input: MintInput): Promise<MintResult>;
|
|
105
|
+
/**
|
|
106
|
+
* Resolves a plaintext API key token to its metadata, or `null`. Expiry is
|
|
107
|
+
* enforced on read — not just via the store's native TTL — so it holds on
|
|
108
|
+
* backends that evict lazily and regardless of TTL granularity.
|
|
109
|
+
*
|
|
110
|
+
* @param state - The KV state store holding key records.
|
|
111
|
+
* @param token - The presented plaintext token.
|
|
112
|
+
* @returns The resolved key metadata, or `null`.
|
|
113
|
+
*/
|
|
114
|
+
export declare function resolve(state: Store.State, token: string): Promise<ApiKey.ApiKey | null>;
|
|
115
|
+
/**
|
|
116
|
+
* Lists key records (metadata only — never tokens), newest first. Expired or
|
|
117
|
+
* catalog-invalid records are skipped.
|
|
118
|
+
*
|
|
119
|
+
* @param state - The KV state store holding key records.
|
|
120
|
+
* @param options - Optional owner filter.
|
|
121
|
+
* @returns The records.
|
|
122
|
+
*/
|
|
123
|
+
export declare function list(state: Store.State, options?: list.Options): Promise<readonly Record[]>;
|
|
124
|
+
export declare namespace list {
|
|
125
|
+
/** Options for {@link list}. */
|
|
126
|
+
type Options = {
|
|
127
|
+
/** Restrict results to keys owned by this organization. */
|
|
128
|
+
orgId?: string | undefined;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Revokes a key by id, deleting its record and index entry.
|
|
133
|
+
*
|
|
134
|
+
* @param state - The KV state store holding key records.
|
|
135
|
+
* @param id - The key id (`key_…`).
|
|
136
|
+
* @returns Whether a matching record was deleted.
|
|
137
|
+
*/
|
|
138
|
+
export declare function revoke(state: Store.State, id: string): Promise<boolean>;
|
|
139
|
+
//# sourceMappingURL=ApiKeys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ApiKeys.d.ts","sourceRoot":"","sources":["../src/ApiKeys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,CAAC,MAAM,UAAU,CAAA;AAE7B,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,KAAK,KAAK,MAAM,qBAAqB,CAAA;AAEjD,gDAAgD;AAChD,yBAAiB,MAAM,CAAC;IACtB;;;;;OAKG;IACI,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;qBASpB,CAAA;IAEF;;;;OAIG;IACI,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;qBAKjB,CAAA;CACH;AA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
|
4
|
+
* Records are keyed by token hash (`ApiKey.keyFor`) — the plaintext token is
|
|
5
|
+
* never stored — and validated against the scope catalog on read; unknown,
|
|
6
|
+
* corrupt, expired, or invalid records resolve to `null`. An `id → recordKey`
|
|
7
|
+
* index makes revoke-by-id O(1). The persisted {@link Record}/{@link
|
|
8
|
+
* MintInput} shapes live here (derived from `ApiKey.schema` so they cannot
|
|
9
|
+
* drift from the resolved shape), while the shared credential primitives
|
|
10
|
+
* (token format/generation, storage-key derivation, redaction) live in
|
|
11
|
+
* `./ApiKey.js`.
|
|
12
|
+
*/
|
|
13
|
+
import * as z from 'zod/mini';
|
|
14
|
+
import * as ApiKey from './ApiKey.js';
|
|
15
|
+
/** Zod schemas owned by the API-keys module. */
|
|
16
|
+
export var schema;
|
|
17
|
+
(function (schema) {
|
|
18
|
+
/**
|
|
19
|
+
* Fields accepted when minting a key, and the single source of truth for the
|
|
20
|
+
* {@link MintInput} type and `mint` validation. Derived from the resolved
|
|
21
|
+
* `ApiKey.schema` (minus the generated `id`) so the scope vocabulary stays in
|
|
22
|
+
* lockstep with the resolved shape.
|
|
23
|
+
*/
|
|
24
|
+
schema.MintInput = z.extend(z.omit(ApiKey.schema.ApiKey, { id: true }), {
|
|
25
|
+
createdBy: z
|
|
26
|
+
.optional(z.string())
|
|
27
|
+
.check(z.describe('Identity creating the key (e.g. admin email), recorded for audit.')),
|
|
28
|
+
expiresAt: z
|
|
29
|
+
.optional(z.string())
|
|
30
|
+
.check(z.describe('ISO 8601 expiry timestamp. Omit for a non-expiring key.')),
|
|
31
|
+
// TODO: make required once orgs exist.
|
|
32
|
+
orgId: z.optional(z.string()).check(z.describe('Owning organization id.')),
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* A persisted API-key record: the resolved `ApiKey.schema` plus
|
|
36
|
+
* persistence/display fields. Extending the resolved schema keeps the resolved
|
|
37
|
+
* and persisted shapes in lockstep.
|
|
38
|
+
*/
|
|
39
|
+
schema.Record = z.extend(ApiKey.schema.ApiKey, {
|
|
40
|
+
createdAt: z.string().check(z.describe('ISO 8601 creation timestamp.')),
|
|
41
|
+
createdBy: z.optional(z.string()).check(z.describe('Identity that created the key.')),
|
|
42
|
+
expiresAt: z.optional(z.string()).check(z.describe('ISO 8601 expiry timestamp.')),
|
|
43
|
+
tokenLast4: z.string().check(z.describe('Last 4 chars of the plaintext token.')),
|
|
44
|
+
});
|
|
45
|
+
})(schema || (schema = {}));
|
|
46
|
+
/**
|
|
47
|
+
* Mints a key: validates the input (e.g. rejects an unknown scope at the write
|
|
48
|
+
* boundary), persists its hashed record, and returns the record plus the
|
|
49
|
+
* one-time plaintext token (shown once, unrecoverable afterward).
|
|
50
|
+
*
|
|
51
|
+
* @param state - The KV state store holding key records.
|
|
52
|
+
* @param input - The key to mint.
|
|
53
|
+
* @returns The persisted record and its one-time token.
|
|
54
|
+
*/
|
|
55
|
+
export async function mint(state, input) {
|
|
56
|
+
const valid = schema.MintInput.safeParse(input);
|
|
57
|
+
if (!valid.success)
|
|
58
|
+
throw new Error(`invalid API key input: ${z.prettifyError(valid.error)}`);
|
|
59
|
+
const data = valid.data;
|
|
60
|
+
const time = new Date();
|
|
61
|
+
const token = ApiKey.generateToken(data.environment);
|
|
62
|
+
const recordKey = ApiKey.keyFor(token);
|
|
63
|
+
const id = ApiKey.generateId();
|
|
64
|
+
const record = {
|
|
65
|
+
createdAt: time.toISOString(),
|
|
66
|
+
environment: data.environment,
|
|
67
|
+
id,
|
|
68
|
+
// TODO: drop fallback once a real org concept exists.
|
|
69
|
+
orgId: data.orgId ?? id,
|
|
70
|
+
scopes: data.scopes,
|
|
71
|
+
tokenLast4: token.slice(-4),
|
|
72
|
+
...(data.createdBy === undefined ? {} : { createdBy: data.createdBy }),
|
|
73
|
+
...(data.expiresAt === undefined ? {} : { expiresAt: data.expiresAt }),
|
|
74
|
+
...(data.name === undefined ? {} : { name: data.name }),
|
|
75
|
+
...(data.rateLimits === undefined ? {} : { rateLimits: data.rateLimits }),
|
|
76
|
+
};
|
|
77
|
+
// Bound the stored record by its expiry so the backend evicts it natively.
|
|
78
|
+
const ttl = data.expiresAt === undefined
|
|
79
|
+
? undefined
|
|
80
|
+
: Math.max(1, Date.parse(data.expiresAt) - time.getTime());
|
|
81
|
+
await state.put(recordKey, JSON.stringify(record), { ttl });
|
|
82
|
+
// Index `id → recordKey` so revoke-by-id is O(1) (records are keyed by
|
|
83
|
+
// token hash, unrecoverable from the id alone). Shares the record's TTL so
|
|
84
|
+
// both entries expire together.
|
|
85
|
+
await state.put(idKeyFor(record.id), recordKey, { ttl });
|
|
86
|
+
return { record, token };
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Resolves a plaintext API key token to its metadata, or `null`. Expiry is
|
|
90
|
+
* enforced on read — not just via the store's native TTL — so it holds on
|
|
91
|
+
* backends that evict lazily and regardless of TTL granularity.
|
|
92
|
+
*
|
|
93
|
+
* @param state - The KV state store holding key records.
|
|
94
|
+
* @param token - The presented plaintext token.
|
|
95
|
+
* @returns The resolved key metadata, or `null`.
|
|
96
|
+
*/
|
|
97
|
+
export async function resolve(state, token) {
|
|
98
|
+
return read(state, ApiKey.keyFor(token), Date.now());
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Lists key records (metadata only — never tokens), newest first. Expired or
|
|
102
|
+
* catalog-invalid records are skipped.
|
|
103
|
+
*
|
|
104
|
+
* @param state - The KV state store holding key records.
|
|
105
|
+
* @param options - Optional owner filter.
|
|
106
|
+
* @returns The records.
|
|
107
|
+
*/
|
|
108
|
+
export async function list(state, options = {}) {
|
|
109
|
+
const { keys } = await state.list({ prefix: ApiKey.recordPrefix });
|
|
110
|
+
const records = [];
|
|
111
|
+
for (const { name } of keys) {
|
|
112
|
+
const record = await read(state, name, Date.now());
|
|
113
|
+
if (record && (!options.orgId || record.orgId === options.orgId))
|
|
114
|
+
records.push(record);
|
|
115
|
+
}
|
|
116
|
+
// Newest first; ids are random, so sort by creation time.
|
|
117
|
+
return records.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Revokes a key by id, deleting its record and index entry.
|
|
121
|
+
*
|
|
122
|
+
* @param state - The KV state store holding key records.
|
|
123
|
+
* @param id - The key id (`key_…`).
|
|
124
|
+
* @returns Whether a matching record was deleted.
|
|
125
|
+
*/
|
|
126
|
+
export async function revoke(state, id) {
|
|
127
|
+
const recordKey = await state.get(idKeyFor(id));
|
|
128
|
+
if (!recordKey)
|
|
129
|
+
return false;
|
|
130
|
+
await state.delete(recordKey);
|
|
131
|
+
await state.delete(idKeyFor(id));
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Reads and validates a stored record by storage key. Invalid, corrupt, or
|
|
136
|
+
* expired records (`expiresAt <= now`) resolve to `null`.
|
|
137
|
+
*/
|
|
138
|
+
async function read(state, key, now) {
|
|
139
|
+
const raw = await state.get(key);
|
|
140
|
+
if (!raw)
|
|
141
|
+
return null;
|
|
142
|
+
const value = (() => {
|
|
143
|
+
try {
|
|
144
|
+
return JSON.parse(raw);
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
})();
|
|
150
|
+
if (value === undefined)
|
|
151
|
+
return null;
|
|
152
|
+
const parsed = schema.Record.safeParse(value);
|
|
153
|
+
if (!parsed.success)
|
|
154
|
+
return null;
|
|
155
|
+
const record = parsed.data;
|
|
156
|
+
if (record.expiresAt !== undefined && Date.parse(record.expiresAt) <= now)
|
|
157
|
+
return null;
|
|
158
|
+
return record;
|
|
159
|
+
}
|
|
160
|
+
/** Storage-key prefix for the `id → recordKey` index. */
|
|
161
|
+
const idPrefix = 'apikey_id:';
|
|
162
|
+
/** Storage key under which a key's `id → recordKey` index entry is persisted. */
|
|
163
|
+
function idKeyFor(id) {
|
|
164
|
+
return `${idPrefix}${id}`;
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=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