@thirdweb-dev/service-utils 0.1.1-nightly-7dc685bb-20230714041905 → 0.2.0-nightly-5eb6fc1b-20230714082745

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.
Files changed (45) hide show
  1. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts +2 -0
  2. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.d.ts.map +1 -0
  3. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.dev.js +115 -0
  4. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.js +7 -0
  5. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.prod.js +115 -0
  6. package/cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js +105 -0
  7. package/cf-worker/package.json +4 -0
  8. package/dist/declarations/src/cf-worker/index.d.ts +18 -0
  9. package/dist/declarations/src/cf-worker/index.d.ts.map +1 -0
  10. package/dist/declarations/src/core/api.d.ts +31 -0
  11. package/dist/declarations/src/core/api.d.ts.map +1 -0
  12. package/dist/declarations/src/core/authorize/index.d.ts +18 -0
  13. package/dist/declarations/src/core/authorize/index.d.ts.map +1 -0
  14. package/dist/declarations/src/core/authorize/types.d.ts +11 -0
  15. package/dist/declarations/src/core/authorize/types.d.ts.map +1 -0
  16. package/dist/declarations/src/core/services.d.ts.map +1 -0
  17. package/dist/declarations/src/core/types.d.ts +5 -0
  18. package/dist/declarations/src/core/types.d.ts.map +1 -0
  19. package/dist/declarations/src/index.d.ts +1 -2
  20. package/dist/declarations/src/index.d.ts.map +1 -1
  21. package/dist/declarations/src/node/index.d.ts +16 -0
  22. package/dist/declarations/src/node/index.d.ts.map +1 -0
  23. package/dist/index-294e111f.esm.js +252 -0
  24. package/dist/index-3060948e.cjs.prod.js +254 -0
  25. package/dist/index-fcf69a55.cjs.dev.js +254 -0
  26. package/dist/services-86283509.esm.js +44 -0
  27. package/dist/services-9e185105.cjs.prod.js +49 -0
  28. package/dist/services-a3f36057.cjs.dev.js +49 -0
  29. package/dist/thirdweb-dev-service-utils.cjs.dev.js +5 -284
  30. package/dist/thirdweb-dev-service-utils.cjs.prod.js +5 -284
  31. package/dist/thirdweb-dev-service-utils.esm.js +1 -282
  32. package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts +2 -0
  33. package/node/dist/thirdweb-dev-service-utils-node.cjs.d.ts.map +1 -0
  34. package/node/dist/thirdweb-dev-service-utils-node.cjs.dev.js +113 -0
  35. package/node/dist/thirdweb-dev-service-utils-node.cjs.js +7 -0
  36. package/node/dist/thirdweb-dev-service-utils-node.cjs.prod.js +113 -0
  37. package/node/dist/thirdweb-dev-service-utils-node.esm.js +102 -0
  38. package/node/package.json +4 -0
  39. package/package.json +22 -10
  40. package/dist/declarations/src/auth/index.d.ts +0 -4
  41. package/dist/declarations/src/auth/index.d.ts.map +0 -1
  42. package/dist/declarations/src/auth/types.d.ts +0 -62
  43. package/dist/declarations/src/auth/types.d.ts.map +0 -1
  44. package/dist/declarations/src/services.d.ts.map +0 -1
  45. /package/dist/declarations/src/{services.d.ts → core/services.d.ts} +0 -0
@@ -0,0 +1,113 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var node_crypto = require('node:crypto');
6
+ var index = require('../../dist/index-3060948e.cjs.prod.js');
7
+ var services = require('../../dist/services-9e185105.cjs.prod.js');
8
+
9
+ async function authorizeNode(authInput, serviceConfig) {
10
+ let authData;
11
+ try {
12
+ authData = extractAuthorizationData(authInput);
13
+ } catch (e) {
14
+ if (e instanceof Error && e.message === "KEY_CONFLICT") {
15
+ return {
16
+ authorized: false,
17
+ status: 400,
18
+ errorMessage: "Please pass either a client id or a secret key.",
19
+ errorCode: "KEY_CONFLICT"
20
+ };
21
+ }
22
+ return {
23
+ authorized: false,
24
+ status: 500,
25
+ errorMessage: "Internal Server Error",
26
+ errorCode: "INTERNAL_SERVER_ERROR"
27
+ };
28
+ }
29
+ return await index.authorize(authData, serviceConfig);
30
+ }
31
+ function getHeader(headers, headerName) {
32
+ const header = headers[headerName];
33
+ if (Array.isArray(header)) {
34
+ return header[0];
35
+ }
36
+ return header ?? null;
37
+ }
38
+ function extractAuthorizationData(authInput) {
39
+ if (!authInput.req.url) {
40
+ throw new Error("no req.url in authInput.req");
41
+ }
42
+ const requestUrl = new URL(authInput.req.url, authInput.req.headers.host);
43
+ const headers = authInput.req.headers;
44
+ const secretKey = getHeader(headers, "x-secret-key");
45
+ // prefer clientId that is explicitly passed in
46
+ let clientId = authInput.clientId ?? null;
47
+ if (!clientId) {
48
+ // next preference is clientId from header
49
+ clientId = getHeader(headers, "x-client-id");
50
+ }
51
+
52
+ // next preference is search param
53
+ if (!clientId) {
54
+ clientId = requestUrl.searchParams.get("clientId");
55
+ }
56
+ // bundle id from header is first preference
57
+ let bundleId = getHeader(headers, "x-bundle-id");
58
+
59
+ // next preference is search param
60
+ if (!bundleId) {
61
+ bundleId = requestUrl.searchParams.get("bundleId");
62
+ }
63
+ let origin = getHeader(headers, "origin");
64
+ // if origin header is not available we'll fall back to referrer;
65
+ if (!origin) {
66
+ origin = getHeader(headers, "referer");
67
+ }
68
+ // if we have an origin at this point, normalize it
69
+ if (origin) {
70
+ try {
71
+ origin = new URL(origin).hostname;
72
+ } catch (e) {
73
+ console.warn("failed to parse origin", origin, e);
74
+ }
75
+ }
76
+
77
+ // handle if we a secret key is passed in the headers
78
+ let secretKeyHash = null;
79
+ if (secretKey) {
80
+ // hash the secret key
81
+ secretKeyHash = hashSecretKey(secretKey);
82
+ // derive the client id from the secret key hash
83
+ const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
84
+ // if we already have a client id passed in we need to make sure they match
85
+ if (clientId && clientId !== derivedClientId) {
86
+ throw new Error("KEY_CONFLICT");
87
+ }
88
+ // otherwise set the client id to the derived client id (client id based off of secret key)
89
+ clientId = derivedClientId;
90
+ }
91
+ return {
92
+ secretKeyHash,
93
+ secretKey,
94
+ clientId,
95
+ origin,
96
+ bundleId
97
+ };
98
+ }
99
+ function hashSecretKey(secretKey) {
100
+ return node_crypto.createHash("sha256").update(secretKey).digest("hex");
101
+ }
102
+ function deriveClientIdFromSecretKeyHash(secretKeyHash) {
103
+ return secretKeyHash.slice(0, 32);
104
+ }
105
+
106
+ exports.SERVICES = services.SERVICES;
107
+ exports.SERVICE_DEFINITIONS = services.SERVICE_DEFINITIONS;
108
+ exports.SERVICE_NAMES = services.SERVICE_NAMES;
109
+ exports.getServiceByName = services.getServiceByName;
110
+ exports.authorizeNode = authorizeNode;
111
+ exports.deriveClientIdFromSecretKeyHash = deriveClientIdFromSecretKeyHash;
112
+ exports.extractAuthorizationData = extractAuthorizationData;
113
+ exports.hashSecretKey = hashSecretKey;
@@ -0,0 +1,102 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { a as authorize } from '../../dist/index-294e111f.esm.js';
3
+ export { b as SERVICES, S as SERVICE_DEFINITIONS, a as SERVICE_NAMES, g as getServiceByName } from '../../dist/services-86283509.esm.js';
4
+
5
+ async function authorizeNode(authInput, serviceConfig) {
6
+ let authData;
7
+ try {
8
+ authData = extractAuthorizationData(authInput);
9
+ } catch (e) {
10
+ if (e instanceof Error && e.message === "KEY_CONFLICT") {
11
+ return {
12
+ authorized: false,
13
+ status: 400,
14
+ errorMessage: "Please pass either a client id or a secret key.",
15
+ errorCode: "KEY_CONFLICT"
16
+ };
17
+ }
18
+ return {
19
+ authorized: false,
20
+ status: 500,
21
+ errorMessage: "Internal Server Error",
22
+ errorCode: "INTERNAL_SERVER_ERROR"
23
+ };
24
+ }
25
+ return await authorize(authData, serviceConfig);
26
+ }
27
+ function getHeader(headers, headerName) {
28
+ const header = headers[headerName];
29
+ if (Array.isArray(header)) {
30
+ return header[0];
31
+ }
32
+ return header ?? null;
33
+ }
34
+ function extractAuthorizationData(authInput) {
35
+ if (!authInput.req.url) {
36
+ throw new Error("no req.url in authInput.req");
37
+ }
38
+ const requestUrl = new URL(authInput.req.url, authInput.req.headers.host);
39
+ const headers = authInput.req.headers;
40
+ const secretKey = getHeader(headers, "x-secret-key");
41
+ // prefer clientId that is explicitly passed in
42
+ let clientId = authInput.clientId ?? null;
43
+ if (!clientId) {
44
+ // next preference is clientId from header
45
+ clientId = getHeader(headers, "x-client-id");
46
+ }
47
+
48
+ // next preference is search param
49
+ if (!clientId) {
50
+ clientId = requestUrl.searchParams.get("clientId");
51
+ }
52
+ // bundle id from header is first preference
53
+ let bundleId = getHeader(headers, "x-bundle-id");
54
+
55
+ // next preference is search param
56
+ if (!bundleId) {
57
+ bundleId = requestUrl.searchParams.get("bundleId");
58
+ }
59
+ let origin = getHeader(headers, "origin");
60
+ // if origin header is not available we'll fall back to referrer;
61
+ if (!origin) {
62
+ origin = getHeader(headers, "referer");
63
+ }
64
+ // if we have an origin at this point, normalize it
65
+ if (origin) {
66
+ try {
67
+ origin = new URL(origin).hostname;
68
+ } catch (e) {
69
+ console.warn("failed to parse origin", origin, e);
70
+ }
71
+ }
72
+
73
+ // handle if we a secret key is passed in the headers
74
+ let secretKeyHash = null;
75
+ if (secretKey) {
76
+ // hash the secret key
77
+ secretKeyHash = hashSecretKey(secretKey);
78
+ // derive the client id from the secret key hash
79
+ const derivedClientId = deriveClientIdFromSecretKeyHash(secretKeyHash);
80
+ // if we already have a client id passed in we need to make sure they match
81
+ if (clientId && clientId !== derivedClientId) {
82
+ throw new Error("KEY_CONFLICT");
83
+ }
84
+ // otherwise set the client id to the derived client id (client id based off of secret key)
85
+ clientId = derivedClientId;
86
+ }
87
+ return {
88
+ secretKeyHash,
89
+ secretKey,
90
+ clientId,
91
+ origin,
92
+ bundleId
93
+ };
94
+ }
95
+ function hashSecretKey(secretKey) {
96
+ return createHash("sha256").update(secretKey).digest("hex");
97
+ }
98
+ function deriveClientIdFromSecretKeyHash(secretKeyHash) {
99
+ return secretKeyHash.slice(0, 32);
100
+ }
101
+
102
+ export { authorizeNode, deriveClientIdFromSecretKeyHash, extractAuthorizationData, hashSecretKey };
@@ -0,0 +1,4 @@
1
+ {
2
+ "main": "dist/thirdweb-dev-service-utils-node.cjs.js",
3
+ "module": "dist/thirdweb-dev-service-utils-node.esm.js"
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thirdweb-dev/service-utils",
3
- "version": "0.1.1-nightly-7dc685bb-20230714041905",
3
+ "version": "0.2.0-nightly-5eb6fc1b-20230714082745",
4
4
  "main": "dist/thirdweb-dev-service-utils.cjs.js",
5
5
  "module": "dist/thirdweb-dev-service-utils.esm.js",
6
6
  "exports": {
@@ -8,6 +8,14 @@
8
8
  "module": "./dist/thirdweb-dev-service-utils.esm.js",
9
9
  "default": "./dist/thirdweb-dev-service-utils.cjs.js"
10
10
  },
11
+ "./node": {
12
+ "module": "./node/dist/thirdweb-dev-service-utils-node.esm.js",
13
+ "default": "./node/dist/thirdweb-dev-service-utils-node.cjs.js"
14
+ },
15
+ "./cf-worker": {
16
+ "module": "./cf-worker/dist/thirdweb-dev-service-utils-cf-worker.esm.js",
17
+ "default": "./cf-worker/dist/thirdweb-dev-service-utils-cf-worker.cjs.js"
18
+ },
11
19
  "./package.json": "./package.json"
12
20
  },
13
21
  "repository": "https://github.com/thirdweb-dev/js/tree/main/packages/pay",
@@ -17,29 +25,32 @@
17
25
  },
18
26
  "author": "thirdweb eng <eng@thirdweb.com>",
19
27
  "files": [
20
- "dist/"
28
+ "dist/",
29
+ "node/",
30
+ "cf-worker/"
21
31
  ],
22
32
  "preconstruct": {
23
33
  "entrypoints": [
24
- "index.ts"
34
+ "index.ts",
35
+ "cf-worker/index.ts",
36
+ "node/index.ts"
25
37
  ],
26
38
  "exports": true
27
39
  },
28
40
  "sideEffects": false,
29
41
  "devDependencies": {
30
42
  "@cloudflare/workers-types": "^4.20230710.0",
43
+ "@preconstruct/cli": "^2.8.1",
44
+ "@thirdweb-dev/tsconfig": "^0.1.7",
45
+ "@types/jest": "^29.5.2",
31
46
  "@types/node": "^20.4.1",
32
47
  "@typescript-eslint/eslint-plugin": "^6.0.0",
33
48
  "@typescript-eslint/parser": "^6.0.0",
34
49
  "eslint": "^8.44.0",
35
50
  "eslint-config-prettier": "^8.8.0",
36
- "lint-staged": "^13.2.3",
37
- "prettier": "^3.0.0",
38
- "sort-package-json": "^2.5.1",
39
- "ts-node": "^10.9.1",
40
- "@preconstruct/cli": "^2.7.0",
41
- "@thirdweb-dev/tsconfig": "^0.1.7",
42
51
  "eslint-config-thirdweb": "^0.1.5",
52
+ "jest": "^29.4.3",
53
+ "prettier": "^3.0.0",
43
54
  "typescript": "^5.1.6"
44
55
  },
45
56
  "scripts": {
@@ -48,6 +59,7 @@
48
59
  "fix": "eslint src/ --fix",
49
60
  "clean": "rm -rf dist/",
50
61
  "build": "tsc && preconstruct build",
51
- "push": "yalc push"
62
+ "push": "yalc push",
63
+ "test": "jest"
52
64
  }
53
65
  }
@@ -1,4 +0,0 @@
1
- import type { AuthorizationResponse, AuthorizeCFWorkerOptions, AuthorizeNodeServiceOptions } from "./types";
2
- export declare function authorizeCFWorkerService(options: AuthorizeCFWorkerOptions): Promise<AuthorizationResponse>;
3
- export declare function authorizeNodeService(options: AuthorizeNodeServiceOptions): Promise<AuthorizationResponse>;
4
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"../../../../src/auth","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAIV,qBAAqB,EAErB,wBAAwB,EACxB,2BAA2B,EAE5B,MAAM,SAAS,CAAC;AAEjB,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,wBAAwB,kCAkClC;AAED,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,2BAA2B,kCASrC"}
@@ -1,62 +0,0 @@
1
- import type { KVNamespace, ExecutionContext } from "@cloudflare/workers-types";
2
- import type { ServiceName } from "../services";
3
- export interface AuthOptions {
4
- clientId: string;
5
- secretHash?: string;
6
- bundleId?: string;
7
- origin?: string;
8
- }
9
- export interface AuthorizeCFWorkerOptions {
10
- ctx: ExecutionContext;
11
- kvStore: KVNamespace<string>;
12
- authOptions: AuthOptions;
13
- serviceConfig: ServiceConfiguration;
14
- validations: AuthorizationValidations;
15
- }
16
- export interface AuthorizeNodeServiceOptions {
17
- authOptions: AuthOptions;
18
- serviceConfig: ServiceConfiguration;
19
- validations: AuthorizationValidations;
20
- }
21
- export interface ServiceConfiguration {
22
- apiUrl: string;
23
- scope: ServiceName;
24
- serviceKey: string;
25
- cachedKey?: ApiKey;
26
- cacheTtl?: number;
27
- onRefetchComplete?: (key: ApiKey) => void;
28
- }
29
- export interface AuthorizationValidations {
30
- serviceTargetAddresses?: string[];
31
- serviceAction?: string;
32
- }
33
- export interface AuthorizationResponse {
34
- authorized: boolean;
35
- errorMessage?: string;
36
- errorCode?: string;
37
- statusCode?: number;
38
- data?: ApiKey;
39
- }
40
- export interface ApiResponse {
41
- data: ApiKey | null;
42
- error: {
43
- code: string;
44
- statusCode: number;
45
- message: string;
46
- };
47
- }
48
- export interface ApiKey {
49
- id: string;
50
- key: string;
51
- creatorWalletAddress: string;
52
- secretHash: string;
53
- walletAddresses: string[];
54
- domains: string[];
55
- bundleIds: string[];
56
- services: {
57
- name: string;
58
- targetAddresses: string[];
59
- actions: string[];
60
- }[];
61
- }
62
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"../../../../src/auth","sources":["types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,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