@arcblock/did-connect-js 1.30.0 → 4.0.0-beta.10

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 (124) hide show
  1. package/{lib/adapters/detect.d.cts → dist/adapters/detect.d.ts} +3 -5
  2. package/dist/adapters/detect.d.ts.map +1 -0
  3. package/dist/adapters/detect.js +13 -0
  4. package/dist/adapters/detect.js.map +1 -0
  5. package/dist/adapters/express.d.ts +40 -0
  6. package/dist/adapters/express.d.ts.map +1 -0
  7. package/dist/adapters/express.js +30 -0
  8. package/dist/adapters/express.js.map +1 -0
  9. package/dist/adapters/hono.d.ts +49 -0
  10. package/dist/adapters/hono.d.ts.map +1 -0
  11. package/dist/adapters/hono.js +176 -0
  12. package/dist/adapters/hono.js.map +1 -0
  13. package/dist/authenticator/base.d.ts +21 -0
  14. package/dist/authenticator/base.d.ts.map +1 -0
  15. package/dist/authenticator/base.js +109 -0
  16. package/dist/authenticator/base.js.map +1 -0
  17. package/dist/authenticator/wallet.d.ts +252 -0
  18. package/dist/authenticator/wallet.d.ts.map +1 -0
  19. package/dist/authenticator/wallet.js +590 -0
  20. package/dist/authenticator/wallet.js.map +1 -0
  21. package/dist/handlers/base.d.ts +25 -0
  22. package/dist/handlers/base.d.ts.map +1 -0
  23. package/dist/handlers/base.js +48 -0
  24. package/dist/handlers/base.js.map +1 -0
  25. package/dist/handlers/util.d.ts +48 -0
  26. package/dist/handlers/util.d.ts.map +1 -0
  27. package/dist/handlers/util.js +805 -0
  28. package/dist/handlers/util.js.map +1 -0
  29. package/dist/handlers/wallet.d.ts +96 -0
  30. package/dist/handlers/wallet.d.ts.map +1 -0
  31. package/dist/handlers/wallet.js +129 -0
  32. package/dist/handlers/wallet.js.map +1 -0
  33. package/dist/index.d.ts +11 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +9 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/protocol.d.ts +14 -0
  38. package/dist/protocol.d.ts.map +1 -0
  39. package/dist/protocol.js +37 -0
  40. package/dist/protocol.js.map +1 -0
  41. package/dist/schema/claims.d.ts +11 -0
  42. package/dist/schema/claims.d.ts.map +1 -0
  43. package/dist/schema/claims.js +226 -0
  44. package/dist/schema/claims.js.map +1 -0
  45. package/dist/schema/index.d.ts +4 -0
  46. package/dist/schema/index.d.ts.map +1 -0
  47. package/dist/schema/index.js +89 -0
  48. package/dist/schema/index.js.map +1 -0
  49. package/dist/storage/kv.d.ts +31 -0
  50. package/dist/storage/kv.d.ts.map +1 -0
  51. package/dist/storage/kv.js +54 -0
  52. package/dist/storage/kv.js.map +1 -0
  53. package/dist/types.d.ts +53 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +2 -0
  56. package/dist/types.js.map +1 -0
  57. package/dist/utils/helpers.d.ts +19 -0
  58. package/dist/utils/helpers.d.ts.map +1 -0
  59. package/dist/utils/helpers.js +77 -0
  60. package/dist/utils/helpers.js.map +1 -0
  61. package/package.json +51 -107
  62. package/LICENSE +0 -13
  63. package/README.md +0 -210
  64. package/esm/_virtual/rolldown_runtime.mjs +0 -8
  65. package/esm/adapters/detect.d.mts +0 -11
  66. package/esm/adapters/detect.mjs +0 -16
  67. package/esm/adapters/express.d.mts +0 -48
  68. package/esm/adapters/express.mjs +0 -39
  69. package/esm/adapters/hono.d.mts +0 -57
  70. package/esm/adapters/hono.mjs +0 -164
  71. package/esm/authenticator/base.d.mts +0 -23
  72. package/esm/authenticator/base.mjs +0 -88
  73. package/esm/authenticator/wallet.d.mts +0 -270
  74. package/esm/authenticator/wallet.mjs +0 -556
  75. package/esm/did/did-connect/package.mjs +0 -118
  76. package/esm/handlers/base.d.mts +0 -32
  77. package/esm/handlers/base.mjs +0 -37
  78. package/esm/handlers/util.d.mts +0 -70
  79. package/esm/handlers/util.mjs +0 -739
  80. package/esm/handlers/wallet.d.mts +0 -120
  81. package/esm/handlers/wallet.mjs +0 -139
  82. package/esm/index.d.mts +0 -8
  83. package/esm/index.mjs +0 -8
  84. package/esm/node_modules/valibot/dist/index.mjs +0 -736
  85. package/esm/protocol.d.mts +0 -15
  86. package/esm/protocol.mjs +0 -40
  87. package/esm/schema/claims.d.mts +0 -16
  88. package/esm/schema/claims.mjs +0 -216
  89. package/esm/schema/index.d.mts +0 -29
  90. package/esm/schema/index.mjs +0 -75
  91. package/esm/storage/kv.d.mts +0 -33
  92. package/esm/storage/kv.mjs +0 -55
  93. package/esm/types.d.mts +0 -55
  94. package/esm/types.mjs +0 -1
  95. package/lib/_virtual/rolldown_runtime.cjs +0 -31
  96. package/lib/adapters/detect.cjs +0 -18
  97. package/lib/adapters/express.cjs +0 -41
  98. package/lib/adapters/express.d.cts +0 -48
  99. package/lib/adapters/hono.cjs +0 -167
  100. package/lib/adapters/hono.d.cts +0 -57
  101. package/lib/authenticator/base.cjs +0 -91
  102. package/lib/authenticator/base.d.cts +0 -23
  103. package/lib/authenticator/wallet.cjs +0 -565
  104. package/lib/authenticator/wallet.d.cts +0 -270
  105. package/lib/did/did-connect/package.cjs +0 -121
  106. package/lib/handlers/base.cjs +0 -40
  107. package/lib/handlers/base.d.cts +0 -32
  108. package/lib/handlers/util.cjs +0 -759
  109. package/lib/handlers/util.d.cts +0 -70
  110. package/lib/handlers/wallet.cjs +0 -139
  111. package/lib/handlers/wallet.d.cts +0 -119
  112. package/lib/index.cjs +0 -16
  113. package/lib/index.d.cts +0 -8
  114. package/lib/node_modules/valibot/dist/index.cjs +0 -756
  115. package/lib/protocol.cjs +0 -46
  116. package/lib/protocol.d.cts +0 -15
  117. package/lib/schema/claims.cjs +0 -218
  118. package/lib/schema/claims.d.cts +0 -16
  119. package/lib/schema/index.cjs +0 -79
  120. package/lib/schema/index.d.cts +0 -29
  121. package/lib/storage/kv.cjs +0 -57
  122. package/lib/storage/kv.d.cts +0 -33
  123. package/lib/types.cjs +0 -0
  124. package/lib/types.d.cts +0 -55
@@ -1,48 +0,0 @@
1
- import { ConnectRequest, ConnectResponse, NextFunction } from "../types.cjs";
2
-
3
- //#region src/adapters/express.d.ts
4
- interface ExpressLikeApp {
5
- use(path: string, ...handlers: any[]): void;
6
- get(path: string, ...handlers: any[]): void;
7
- post(path: string, ...handlers: any[]): void;
8
- }
9
- interface HandlerFunctions {
10
- generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
11
- checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
12
- expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
13
- onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
14
- onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
15
- ensureContext: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => Promise<void>;
16
- ensureSignedJson: (req: ConnectRequest, res: ConnectResponse, next: NextFunction) => void;
17
- }
18
- interface AttachExpressOptions {
19
- app: ExpressLikeApp;
20
- prefix: string;
21
- action: string;
22
- handlers: HandlerFunctions;
23
- }
24
- /**
25
- * Register DID Connect routes on an Express-compatible app.
26
- *
27
- * Routes registered:
28
- * - GET/POST `{prefix}/{action}/token` — generate session
29
- * - GET `{prefix}/{action}/status` — check session status
30
- * - GET `{prefix}/{action}/timeout` — expire session
31
- * - GET `{prefix}/{action}/auth` — wallet fetches auth request
32
- * - POST `{prefix}/{action}/auth` — wallet submits auth response
33
- * - GET `{prefix}/{action}/auth/submit` — web wallet submit
34
- */
35
- declare function attachExpress({
36
- app,
37
- prefix,
38
- action,
39
- handlers
40
- }: AttachExpressOptions): {
41
- generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
42
- expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
43
- checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
44
- onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
45
- onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
46
- };
47
- //#endregion
48
- export { AttachExpressOptions, ExpressLikeApp, HandlerFunctions, attachExpress };
@@ -1,167 +0,0 @@
1
-
2
- //#region src/adapters/hono.ts
3
- /**
4
- * Parse a raw Cookie header string into a key-value map.
5
- */
6
- function parseCookieHeader(header) {
7
- const cookies = {};
8
- if (!header) return cookies;
9
- for (const pair of header.split(";")) {
10
- const idx = pair.indexOf("=");
11
- if (idx > 0) {
12
- const key = pair.slice(0, idx).trim();
13
- const val = pair.slice(idx + 1).trim();
14
- cookies[key] = decodeURIComponent(val);
15
- }
16
- }
17
- return cookies;
18
- }
19
- /**
20
- * Simple Accept-Language negotiation matching Express's req.acceptsLanguages().
21
- * Only needs to handle the two languages used by DID Connect: 'en-US' and 'zh-CN'.
22
- */
23
- function negotiateLanguage(acceptHeader, ...langs) {
24
- if (!acceptHeader || langs.length === 0) return langs[0] || false;
25
- const lower = acceptHeader.toLowerCase();
26
- for (const lang of langs) {
27
- const prefix = lang.toLowerCase().split("-")[0];
28
- if (lower.includes(lang.toLowerCase()) || lower.includes(prefix)) return lang;
29
- }
30
- return langs[0] || false;
31
- }
32
- /**
33
- * Create a ConnectRequest from a Hono-like Context.
34
- *
35
- * @param c - Hono Context object
36
- * @param bodyCache - Pre-parsed request body (since c.req.json() is async)
37
- */
38
- function createHonoRequest(c, bodyCache = {}) {
39
- const url = new URL(c.req.url);
40
- const rawHeaders = {};
41
- if (c.req.raw?.headers) c.req.raw.headers.forEach((value, key) => {
42
- rawHeaders[key.toLowerCase()] = value;
43
- });
44
- return {
45
- body: bodyCache,
46
- query: Object.fromEntries(url.searchParams.entries()),
47
- params: typeof c.req.param === "function" ? c.req.param() : {},
48
- headers: rawHeaders,
49
- cookies: parseCookieHeader(rawHeaders.cookie || ""),
50
- protocol: url.protocol.replace(":", ""),
51
- originalUrl: url.pathname + url.search,
52
- get(name) {
53
- return rawHeaders[name.toLowerCase()];
54
- },
55
- acceptsLanguages(...langs) {
56
- return negotiateLanguage(rawHeaders["accept-language"] || "", ...langs);
57
- },
58
- raw: c
59
- };
60
- }
61
- /**
62
- * Create a ConnectResponse that buffers the handler's response.
63
- * The Hono route wrapper reads the buffer via _getResult() to return c.json().
64
- */
65
- function createHonoResponse() {
66
- let result = null;
67
- return {
68
- jsonp(data) {
69
- result = {
70
- statusCode: 200,
71
- body: data
72
- };
73
- },
74
- json(data) {
75
- result = {
76
- statusCode: 200,
77
- body: data
78
- };
79
- },
80
- status(code) {
81
- return { json(data) {
82
- result = {
83
- statusCode: code,
84
- body: data
85
- };
86
- } };
87
- },
88
- _getResult() {
89
- return result;
90
- }
91
- };
92
- }
93
- /**
94
- * Wrap a middleware chain + handler into a single Hono route handler.
95
- * Runs middlewares sequentially with manual next() control,
96
- * then runs the handler, then returns the buffered response.
97
- */
98
- function wrapHandler(middlewares, handler) {
99
- return async (c) => {
100
- let body = {};
101
- try {
102
- const text = await c.req.text();
103
- if (text) {
104
- const contentType = c.req.header("content-type") || "";
105
- if (contentType.includes("json")) body = JSON.parse(text);
106
- else if (contentType.includes("urlencoded")) body = Object.fromEntries(new URLSearchParams(text).entries());
107
- }
108
- } catch {}
109
- const req = createHonoRequest(c, body);
110
- const res = createHonoResponse();
111
- for (const mw of middlewares) {
112
- let nextCalled = false;
113
- await mw(req, res, () => {
114
- nextCalled = true;
115
- });
116
- if (!nextCalled) {
117
- const r$1 = res._getResult();
118
- if (r$1) return c.json(r$1.body, r$1.statusCode);
119
- return c.json({ error: "Unknown error" }, 500);
120
- }
121
- }
122
- await handler(req, res);
123
- const r = res._getResult();
124
- return c.json(r?.body ?? {}, r?.statusCode ?? 200);
125
- };
126
- }
127
- /**
128
- * Register DID Connect routes on a Hono-compatible app.
129
- *
130
- * Routes registered are the same as attachExpress:
131
- * - GET/POST `{prefix}/{action}/token`
132
- * - GET `{prefix}/{action}/status`
133
- * - GET `{prefix}/{action}/timeout`
134
- * - GET `{prefix}/{action}/auth`
135
- * - POST `{prefix}/{action}/auth`
136
- * - GET `{prefix}/{action}/auth/submit`
137
- */
138
- function attachHono({ app, prefix, action, handlers }) {
139
- const pathname = `${prefix}/${action}/auth`;
140
- const { generateSession, checkSession, expireSession, onAuthRequest, onAuthResponse, ensureContext, ensureSignedJson } = handlers;
141
- app.use(`${prefix}/${action}/*`, async (c, next) => {
142
- c.header("Access-Control-Allow-Origin", "*");
143
- c.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
144
- c.header("Access-Control-Allow-Headers", "Content-Type, Authorization");
145
- if (c.req.method === "OPTIONS") return c.text("", 204);
146
- await next();
147
- });
148
- app.get(`${prefix}/${action}/token`, wrapHandler([], generateSession));
149
- app.post(`${prefix}/${action}/token`, wrapHandler([], generateSession));
150
- app.get(`${prefix}/${action}/status`, wrapHandler([ensureContext], checkSession));
151
- app.get(`${prefix}/${action}/timeout`, wrapHandler([ensureContext], expireSession));
152
- app.get(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthRequest));
153
- app.post(pathname, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
154
- app.get(`${pathname}/submit`, wrapHandler([ensureContext, ensureSignedJson], onAuthResponse));
155
- return {
156
- generateSession,
157
- expireSession,
158
- checkSession,
159
- onAuthRequest,
160
- onAuthResponse
161
- };
162
- }
163
-
164
- //#endregion
165
- exports.attachHono = attachHono;
166
- exports.createHonoRequest = createHonoRequest;
167
- exports.createHonoResponse = createHonoResponse;
@@ -1,57 +0,0 @@
1
- import { ConnectRequest, ConnectResponse } from "../types.cjs";
2
- import { HandlerFunctions } from "./express.cjs";
3
-
4
- //#region src/adapters/hono.d.ts
5
- interface HonoLikeApp {
6
- get(path: string, handler: (c: any) => any): void;
7
- post(path: string, handler: (c: any) => any): void;
8
- use(path: string, handler: any): void;
9
- }
10
- interface AttachHonoOptions {
11
- app: HonoLikeApp;
12
- prefix: string;
13
- action: string;
14
- handlers: HandlerFunctions;
15
- }
16
- /**
17
- * Create a ConnectRequest from a Hono-like Context.
18
- *
19
- * @param c - Hono Context object
20
- * @param bodyCache - Pre-parsed request body (since c.req.json() is async)
21
- */
22
- declare function createHonoRequest(c: any, bodyCache?: any): ConnectRequest;
23
- /**
24
- * Create a ConnectResponse that buffers the handler's response.
25
- * The Hono route wrapper reads the buffer via _getResult() to return c.json().
26
- */
27
- declare function createHonoResponse(): ConnectResponse & {
28
- _getResult(): {
29
- statusCode: number;
30
- body: any;
31
- } | null;
32
- };
33
- /**
34
- * Register DID Connect routes on a Hono-compatible app.
35
- *
36
- * Routes registered are the same as attachExpress:
37
- * - GET/POST `{prefix}/{action}/token`
38
- * - GET `{prefix}/{action}/status`
39
- * - GET `{prefix}/{action}/timeout`
40
- * - GET `{prefix}/{action}/auth`
41
- * - POST `{prefix}/{action}/auth`
42
- * - GET `{prefix}/{action}/auth/submit`
43
- */
44
- declare function attachHono({
45
- app,
46
- prefix,
47
- action,
48
- handlers
49
- }: AttachHonoOptions): {
50
- generateSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
51
- expireSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
52
- checkSession: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
53
- onAuthRequest: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
54
- onAuthResponse: (req: ConnectRequest, res: ConnectResponse) => Promise<void>;
55
- };
56
- //#endregion
57
- export { AttachHonoOptions, HonoLikeApp, attachHono, createHonoRequest, createHonoResponse };
@@ -1,91 +0,0 @@
1
- Object.defineProperty(exports, '__esModule', { value: true });
2
- const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
- const require_package$1 = require('../did/did-connect/package.cjs');
4
- let _ocap_wallet = require("@ocap/wallet");
5
- let _arcblock_jwt = require("@arcblock/jwt");
6
- _arcblock_jwt = require_rolldown_runtime.__toESM(_arcblock_jwt);
7
-
8
- //#region src/authenticator/base.ts
9
- const debug = require("debug")(`${require_package$1.default.name}:authenticator:base`);
10
- const DEFAULT_CHAIN_INFO = {
11
- id: "none",
12
- host: "none",
13
- type: "arcblock"
14
- };
15
- var BaseAuthenticator = class {
16
- _validateWallet(wallet, canSign = true) {
17
- if (!wallet) throw new Error("WalletAuthenticator cannot work without wallet");
18
- if (typeof wallet === "function") return wallet;
19
- if ((0, _ocap_wallet.isValid)(wallet, canSign)) {
20
- if (!wallet.pk && wallet.publicKey) wallet.pk = wallet.publicKey;
21
- if (!wallet.sk && wallet.secretKey) wallet.sk = wallet.secretKey;
22
- return wallet;
23
- }
24
- if (canSign && !wallet.sk) throw new Error("WalletAuthenticator cannot work without wallet.sk");
25
- if (!wallet.pk) throw new Error("WalletAuthenticator cannot work without wallet.pk");
26
- if (!wallet.address) throw new Error("WalletAuthenticator cannot work without wallet.address");
27
- try {
28
- const fullWallet = (0, _ocap_wallet.fromJSON)(wallet);
29
- if (!fullWallet.pk && fullWallet.publicKey) fullWallet.pk = fullWallet.publicKey;
30
- if (!fullWallet.sk && fullWallet.secretKey) fullWallet.sk = fullWallet.secretKey;
31
- return fullWallet;
32
- } catch (_err) {
33
- return wallet;
34
- }
35
- }
36
- /**
37
- * Verify a DID auth response sent from DID Wallet
38
- *
39
- * @method
40
- * @param {object} data
41
- * @param {string} [locale=en]
42
- * @param {boolean} [enforceTimestamp=true]
43
- * @returns Promise<boolean>
44
- */
45
- async _verify(data, fieldPk, fieldInfo, locale = "en", enforceTimestamp = true) {
46
- debug("verify", data, locale);
47
- const errors = {
48
- pkMissing: {
49
- en: `${fieldPk} is required to complete auth`,
50
- zh: `${fieldPk} 参数缺失`
51
- },
52
- tokenMissing: {
53
- en: `${fieldInfo} is required to complete auth`,
54
- zh: "JWT Token 参数缺失"
55
- },
56
- pkFormat: {
57
- en: `${fieldPk} should be either base58 or base16 format`,
58
- zh: `${fieldPk} 无法解析`
59
- },
60
- tokenInvalid: {
61
- en: "Invalid JWT token",
62
- zh: "签名无效"
63
- },
64
- timeInvalid: {
65
- en: "JWT token expired, make sure your device time in sync with network",
66
- zh: "签名中的时间戳无效,请确保设备和网络时间同步"
67
- }
68
- };
69
- const pk = data[fieldPk];
70
- const info = data[fieldInfo];
71
- if (!pk) throw new Error(errors.pkMissing[locale]);
72
- if (!info) throw new Error(errors.tokenMissing[locale]);
73
- if (!pk) throw new Error(errors.pkFormat[locale]);
74
- if (!await _arcblock_jwt.verify(info, pk)) {
75
- const isValidSig = await _arcblock_jwt.verify(info, pk, {
76
- tolerance: 0,
77
- enforceTimestamp: false
78
- });
79
- if (enforceTimestamp) {
80
- const error = isValidSig ? errors.timeInvalid[locale] : errors.tokenInvalid[locale];
81
- throw new Error(error);
82
- }
83
- }
84
- return _arcblock_jwt.decode(info);
85
- }
86
- };
87
- var base_default = BaseAuthenticator;
88
-
89
- //#endregion
90
- exports.DEFAULT_CHAIN_INFO = DEFAULT_CHAIN_INFO;
91
- exports.default = base_default;
@@ -1,23 +0,0 @@
1
- import * as Jwt from "@arcblock/jwt";
2
-
3
- //#region src/authenticator/base.d.ts
4
- declare const DEFAULT_CHAIN_INFO: {
5
- id: string;
6
- host: string;
7
- type: string;
8
- };
9
- declare class BaseAuthenticator {
10
- _validateWallet(wallet: any, canSign?: boolean): any;
11
- /**
12
- * Verify a DID auth response sent from DID Wallet
13
- *
14
- * @method
15
- * @param {object} data
16
- * @param {string} [locale=en]
17
- * @param {boolean} [enforceTimestamp=true]
18
- * @returns Promise<boolean>
19
- */
20
- _verify(data: any, fieldPk: string, fieldInfo: string, locale?: string, enforceTimestamp?: boolean): Promise<Jwt.JwtBody>;
21
- }
22
- //#endregion
23
- export { DEFAULT_CHAIN_INFO, BaseAuthenticator as default };