@agentunion/fastaun 0.2.13
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/LICENSE +17 -0
- package/README.md +78 -0
- package/dist/auth.d.ts +287 -0
- package/dist/auth.js +1668 -0
- package/dist/auth.js.map +1 -0
- package/dist/client.d.ts +359 -0
- package/dist/client.js +3918 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.js +119 -0
- package/dist/config.js.map +1 -0
- package/dist/crypto.d.ts +41 -0
- package/dist/crypto.js +85 -0
- package/dist/crypto.js.map +1 -0
- package/dist/discovery.d.ts +22 -0
- package/dist/discovery.js +110 -0
- package/dist/discovery.js.map +1 -0
- package/dist/e2ee-group.d.ts +192 -0
- package/dist/e2ee-group.js +1134 -0
- package/dist/e2ee-group.js.map +1 -0
- package/dist/e2ee.d.ts +120 -0
- package/dist/e2ee.js +890 -0
- package/dist/e2ee.js.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.js +253 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +39 -0
- package/dist/events.js +82 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/keystore/aid-db.d.ts +79 -0
- package/dist/keystore/aid-db.js +621 -0
- package/dist/keystore/aid-db.js.map +1 -0
- package/dist/keystore/file.d.ts +82 -0
- package/dist/keystore/file.js +395 -0
- package/dist/keystore/file.js.map +1 -0
- package/dist/keystore/index.d.ts +88 -0
- package/dist/keystore/index.js +7 -0
- package/dist/keystore/index.js.map +1 -0
- package/dist/keystore/sqlite-backup.d.ts +40 -0
- package/dist/keystore/sqlite-backup.js +379 -0
- package/dist/keystore/sqlite-backup.js.map +1 -0
- package/dist/logger.d.ts +6 -0
- package/dist/logger.js +53 -0
- package/dist/logger.js.map +1 -0
- package/dist/namespaces/auth.d.ts +49 -0
- package/dist/namespaces/auth.js +248 -0
- package/dist/namespaces/auth.js.map +1 -0
- package/dist/namespaces/custody.d.ts +47 -0
- package/dist/namespaces/custody.js +231 -0
- package/dist/namespaces/custody.js.map +1 -0
- package/dist/secret-store/file-store.d.ts +25 -0
- package/dist/secret-store/file-store.js +124 -0
- package/dist/secret-store/file-store.js.map +1 -0
- package/dist/secret-store/index.d.ts +28 -0
- package/dist/secret-store/index.js +19 -0
- package/dist/secret-store/index.js.map +1 -0
- package/dist/seq-tracker.d.ts +29 -0
- package/dist/seq-tracker.js +221 -0
- package/dist/seq-tracker.js.map +1 -0
- package/dist/transport.d.ts +60 -0
- package/dist/transport.js +355 -0
- package/dist/transport.js.map +1 -0
- package/dist/types.d.ts +170 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth 命名空间
|
|
3
|
+
*
|
|
4
|
+
* 提供 auth.createAid / auth.authenticate 等高层方法,
|
|
5
|
+
* 内部通过 AUNClient 的 transport、auth、discovery 完成实际流程。
|
|
6
|
+
*
|
|
7
|
+
* 与 Python SDK 的 AuthNamespace 完全对齐。
|
|
8
|
+
*/
|
|
9
|
+
import { AUNError, NotFoundError, StateError, ValidationError } from '../errors.js';
|
|
10
|
+
import { isJsonObject } from '../types.js';
|
|
11
|
+
const AGENT_MD_HTTP_TIMEOUT_MS = 30_000;
|
|
12
|
+
function agentMdHttpScheme(gatewayUrl) {
|
|
13
|
+
const raw = String(gatewayUrl ?? '').trim().toLowerCase();
|
|
14
|
+
return raw.startsWith('ws://') ? 'http' : 'https';
|
|
15
|
+
}
|
|
16
|
+
function agentMdAuthority(aid, discoveryPort) {
|
|
17
|
+
const host = String(aid ?? '').trim();
|
|
18
|
+
if (!host)
|
|
19
|
+
return '';
|
|
20
|
+
if (discoveryPort && !host.includes(':')) {
|
|
21
|
+
return `${host}:${discoveryPort}`;
|
|
22
|
+
}
|
|
23
|
+
return host;
|
|
24
|
+
}
|
|
25
|
+
async function fetchWithTimeout(input, init, timeoutMs = AGENT_MD_HTTP_TIMEOUT_MS) {
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
28
|
+
try {
|
|
29
|
+
return await fetch(input, { ...init, signal: controller.signal });
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (controller.signal.aborted) {
|
|
33
|
+
throw new AUNError(`agent.md request timed out after ${timeoutMs}ms`);
|
|
34
|
+
}
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export class AuthNamespace {
|
|
42
|
+
_client;
|
|
43
|
+
constructor(client) {
|
|
44
|
+
this._client = client;
|
|
45
|
+
}
|
|
46
|
+
get _internal() {
|
|
47
|
+
return this._client;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 解析 Gateway URL。
|
|
51
|
+
* 优先使用已预置的 _gatewayUrl,否则基于 AID 自动发现。
|
|
52
|
+
*
|
|
53
|
+
* 发现流程:
|
|
54
|
+
* 发现流程:
|
|
55
|
+
* 1. 若 _gatewayUrl 已预置,直接返回
|
|
56
|
+
* 2. 开发环境:先 gateway.{issuer},再 fallback {aid}(泛域名在开发环境可能不可用)
|
|
57
|
+
* 3. 生产环境:先 {aid}(泛域名 nameservice),再 fallback gateway.{issuer}
|
|
58
|
+
*/
|
|
59
|
+
async _resolveGateway(aid) {
|
|
60
|
+
// 访问内部属性
|
|
61
|
+
const client = this._internal;
|
|
62
|
+
const gatewayUrl = client._gatewayUrl;
|
|
63
|
+
if (gatewayUrl)
|
|
64
|
+
return gatewayUrl;
|
|
65
|
+
const resolvedAid = aid ?? client._aid;
|
|
66
|
+
if (resolvedAid) {
|
|
67
|
+
const parts = resolvedAid.split('.');
|
|
68
|
+
const issuerDomain = parts.length > 1 ? parts.slice(1).join('.') : resolvedAid;
|
|
69
|
+
const configModel = client._configModel;
|
|
70
|
+
const port = configModel.discoveryPort;
|
|
71
|
+
const portSuffix = port ? `:${port}` : '';
|
|
72
|
+
const aidUrl = `https://${resolvedAid}${portSuffix}/.well-known/aun-gateway`;
|
|
73
|
+
const gatewayDomainUrl = `https://gateway.${issuerDomain}${portSuffix}/.well-known/aun-gateway`;
|
|
74
|
+
const discovery = client._discovery;
|
|
75
|
+
// 开发环境:先 gateway.{issuer}(固定域名),再 fallback {aid}(泛域名)
|
|
76
|
+
// 生产环境:先 {aid}(泛域名),再 fallback gateway.{issuer}
|
|
77
|
+
const [primaryUrl, fallbackUrl] = configModel.verifySsl
|
|
78
|
+
? [aidUrl, gatewayDomainUrl]
|
|
79
|
+
: [gatewayDomainUrl, aidUrl];
|
|
80
|
+
try {
|
|
81
|
+
return await discovery.discover(primaryUrl);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// 主路径失败,尝试 fallback
|
|
85
|
+
}
|
|
86
|
+
return await discovery.discover(fallbackUrl);
|
|
87
|
+
}
|
|
88
|
+
throw new ValidationError("unable to resolve gateway: set client._gatewayUrl or provide 'aid' for auto-discovery");
|
|
89
|
+
}
|
|
90
|
+
/** 创建新 AID */
|
|
91
|
+
async createAid(params) {
|
|
92
|
+
const aid = String(params?.aid ?? '');
|
|
93
|
+
if (!aid)
|
|
94
|
+
throw new Error("auth.create_aid requires 'aid'");
|
|
95
|
+
const client = this._internal;
|
|
96
|
+
const gatewayUrl = await this._resolveGateway(aid);
|
|
97
|
+
client._gatewayUrl = gatewayUrl;
|
|
98
|
+
const auth = client._auth;
|
|
99
|
+
const result = await auth.createAid(gatewayUrl, aid);
|
|
100
|
+
client._aid = result.aid ?? null;
|
|
101
|
+
client._identity = auth.loadIdentityOrNone(String(result.aid));
|
|
102
|
+
return {
|
|
103
|
+
aid: result.aid,
|
|
104
|
+
cert_pem: result.cert,
|
|
105
|
+
gateway: gatewayUrl,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** 认证(登录) */
|
|
109
|
+
async authenticate(params) {
|
|
110
|
+
const request = { ...(params ?? {}) };
|
|
111
|
+
const aid = request.aid;
|
|
112
|
+
const client = this._internal;
|
|
113
|
+
const gatewayUrl = await this._resolveGateway(aid);
|
|
114
|
+
client._gatewayUrl = gatewayUrl;
|
|
115
|
+
const auth = client._auth;
|
|
116
|
+
const result = await auth.authenticate(gatewayUrl, { aid });
|
|
117
|
+
client._aid = result.aid ?? null;
|
|
118
|
+
client._identity = auth.loadIdentityOrNone(String(result.aid));
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
async _resolveAgentMdUrl(aid) {
|
|
122
|
+
const resolvedAid = String(aid ?? '').trim();
|
|
123
|
+
if (!resolvedAid) {
|
|
124
|
+
throw new ValidationError('agent.md requires non-empty aid');
|
|
125
|
+
}
|
|
126
|
+
const client = this._internal;
|
|
127
|
+
let gatewayUrl = client._gatewayUrl ?? '';
|
|
128
|
+
if (!gatewayUrl) {
|
|
129
|
+
try {
|
|
130
|
+
gatewayUrl = await this._resolveGateway(resolvedAid);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
gatewayUrl = '';
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const configModel = client._configModel;
|
|
137
|
+
const discoveryPort = configModel.discoveryPort;
|
|
138
|
+
const authority = agentMdAuthority(resolvedAid, discoveryPort);
|
|
139
|
+
return `${agentMdHttpScheme(gatewayUrl)}://${authority}/agent.md`;
|
|
140
|
+
}
|
|
141
|
+
async _ensureAgentMdUploadToken(aid, gatewayUrl) {
|
|
142
|
+
const auth = this._internal._auth;
|
|
143
|
+
let identity = auth.loadIdentityOrNone(aid);
|
|
144
|
+
if (!identity) {
|
|
145
|
+
throw new StateError('no local identity found, call auth.createAid() first');
|
|
146
|
+
}
|
|
147
|
+
const cachedToken = String(identity.access_token ?? '');
|
|
148
|
+
const expiresAt = auth.getAccessTokenExpiry ? auth.getAccessTokenExpiry(identity) : null;
|
|
149
|
+
if (cachedToken && (expiresAt === null || expiresAt > Date.now() / 1000 + 30)) {
|
|
150
|
+
return cachedToken;
|
|
151
|
+
}
|
|
152
|
+
if (typeof auth.refreshCachedTokens === 'function' && identity.refresh_token) {
|
|
153
|
+
try {
|
|
154
|
+
identity = await auth.refreshCachedTokens(gatewayUrl, identity);
|
|
155
|
+
const refreshedToken = String(identity.access_token ?? '');
|
|
156
|
+
const refreshedExpiry = auth.getAccessTokenExpiry ? auth.getAccessTokenExpiry(identity) : null;
|
|
157
|
+
if (refreshedToken && (refreshedExpiry === null || refreshedExpiry > Date.now() / 1000 + 30)) {
|
|
158
|
+
return refreshedToken;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// refresh 失败时回退到完整 authenticate
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const result = await this.authenticate({ aid });
|
|
166
|
+
const token = String(result.access_token ?? '');
|
|
167
|
+
if (!token) {
|
|
168
|
+
throw new StateError('authenticate did not return access_token');
|
|
169
|
+
}
|
|
170
|
+
return token;
|
|
171
|
+
}
|
|
172
|
+
async uploadAgentMd(content) {
|
|
173
|
+
const client = this._internal;
|
|
174
|
+
const auth = client._auth;
|
|
175
|
+
const identity = auth.loadIdentityOrNone(client._aid ?? undefined);
|
|
176
|
+
if (!identity) {
|
|
177
|
+
throw new StateError('no local identity found, call auth.createAid() first');
|
|
178
|
+
}
|
|
179
|
+
const aid = String(identity.aid ?? client._aid ?? '').trim();
|
|
180
|
+
if (!aid) {
|
|
181
|
+
throw new StateError('no local identity found, call auth.createAid() first');
|
|
182
|
+
}
|
|
183
|
+
const gatewayUrl = await this._resolveGateway(aid);
|
|
184
|
+
client._gatewayUrl = gatewayUrl;
|
|
185
|
+
const token = await this._ensureAgentMdUploadToken(aid, gatewayUrl);
|
|
186
|
+
const response = await fetchWithTimeout(await this._resolveAgentMdUrl(aid), {
|
|
187
|
+
method: 'PUT',
|
|
188
|
+
headers: {
|
|
189
|
+
Authorization: `Bearer ${token}`,
|
|
190
|
+
'Content-Type': 'text/markdown; charset=utf-8',
|
|
191
|
+
},
|
|
192
|
+
body: content,
|
|
193
|
+
});
|
|
194
|
+
if (response.status === 404) {
|
|
195
|
+
throw new NotFoundError(`agent.md endpoint not found for aid: ${aid}`);
|
|
196
|
+
}
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
const message = (await response.text()).trim();
|
|
199
|
+
throw new AUNError(`upload agent.md failed: HTTP ${response.status}${message ? ` - ${message}` : ''}`);
|
|
200
|
+
}
|
|
201
|
+
const payload = await response.json();
|
|
202
|
+
if (!isJsonObject(payload)) {
|
|
203
|
+
throw new AUNError('upload agent.md returned invalid JSON payload');
|
|
204
|
+
}
|
|
205
|
+
return payload;
|
|
206
|
+
}
|
|
207
|
+
async downloadAgentMd(aid) {
|
|
208
|
+
const targetAid = String(aid ?? '').trim();
|
|
209
|
+
if (!targetAid) {
|
|
210
|
+
throw new ValidationError('downloadAgentMd requires non-empty aid');
|
|
211
|
+
}
|
|
212
|
+
const response = await fetchWithTimeout(await this._resolveAgentMdUrl(targetAid), {
|
|
213
|
+
method: 'GET',
|
|
214
|
+
headers: {
|
|
215
|
+
Accept: 'text/markdown',
|
|
216
|
+
},
|
|
217
|
+
});
|
|
218
|
+
if (response.status === 404) {
|
|
219
|
+
throw new NotFoundError(`agent.md not found for aid: ${targetAid}`);
|
|
220
|
+
}
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
const message = (await response.text()).trim();
|
|
223
|
+
throw new AUNError(`download agent.md failed: HTTP ${response.status}${message ? ` - ${message}` : ''}`);
|
|
224
|
+
}
|
|
225
|
+
return await response.text();
|
|
226
|
+
}
|
|
227
|
+
/** 下载证书 */
|
|
228
|
+
async downloadCert(params) {
|
|
229
|
+
return await this._client.call('auth.download_cert', params ?? {});
|
|
230
|
+
}
|
|
231
|
+
/** 请求签发证书 */
|
|
232
|
+
async requestCert(params) {
|
|
233
|
+
return await this._client.call('auth.request_cert', params);
|
|
234
|
+
}
|
|
235
|
+
/** 续期证书 */
|
|
236
|
+
async renewCert(params) {
|
|
237
|
+
return await this._client.call('auth.renew_cert', params ?? {});
|
|
238
|
+
}
|
|
239
|
+
/** 密钥轮换 */
|
|
240
|
+
async rekey(params) {
|
|
241
|
+
return await this._client.call('auth.rekey', params ?? {});
|
|
242
|
+
}
|
|
243
|
+
/** 获取信任根证书列表 */
|
|
244
|
+
async trustRoots(params) {
|
|
245
|
+
return await this._client.call('meta.trust_roots', params ?? {});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/namespaces/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAGpF,OAAO,EAAE,YAAY,EAAwF,MAAM,aAAa,CAAC;AAEjI,MAAM,wBAAwB,GAAG,MAAM,CAAC;AA0BxC,SAAS,iBAAiB,CAAC,UAAkB;IAC3C,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAC1D,OAAO,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;AACpD,CAAC;AAED,SAAS,gBAAgB,CAAC,GAAW,EAAE,aAAwC;IAC7E,MAAM,IAAI,GAAG,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IACtC,IAAI,CAAC,IAAI;QAAE,OAAO,EAAE,CAAC;IACrB,IAAI,aAAa,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACzC,OAAO,GAAG,IAAI,I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AAC;QACD,OAAO,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAC/B,CAAC;IAED,WAAW;IACX,KAAK,CAAC,YAAY,CAAC,MAAkB;QACnC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,oBAAoB,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,aAAa;IACb,KAAK,CAAC,WAAW,CAAC,MAAiB;QACjC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,mBAAmB,EAAE,MAAM,CAAC,CAAC;IAC9D,CAAC;IAED,WAAW;IACX,KAAK,CAAC,SAAS,CAAC,MAAkB;QAChC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,iBAAiB,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAClE,CAAC;IAED,WAAW;IACX,KAAK,CAAC,KAAK,CAAC,MAAkB;QAC5B,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IAC7D,CAAC;IAED,gBAAgB;IAChB,KAAK,CAAC,UAAU,CAAC,MAAkB;QACjC,OAAO,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,kBAAkB,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;IACnE,CAAC;CACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AUNClient } from '../client.js';
|
|
2
|
+
import { type JsonObject } from '../types.js';
|
|
3
|
+
export declare class CustodyNamespace {
|
|
4
|
+
private _client;
|
|
5
|
+
private _custodyUrl;
|
|
6
|
+
constructor(client: AUNClient);
|
|
7
|
+
private get _internal();
|
|
8
|
+
setUrl(url: string): void;
|
|
9
|
+
configureUrl(url: string): void;
|
|
10
|
+
discoverUrl(params?: {
|
|
11
|
+
aid?: string | null;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
}): Promise<string>;
|
|
14
|
+
private _resolveCustodyUrl;
|
|
15
|
+
private _getAccessToken;
|
|
16
|
+
private _post;
|
|
17
|
+
sendCode(params: {
|
|
18
|
+
phone: string;
|
|
19
|
+
aid?: string | null;
|
|
20
|
+
}): Promise<JsonObject>;
|
|
21
|
+
bindPhone(params: {
|
|
22
|
+
phone: string;
|
|
23
|
+
code: string;
|
|
24
|
+
cert: string;
|
|
25
|
+
key: string;
|
|
26
|
+
metadata?: JsonObject | null;
|
|
27
|
+
}): Promise<JsonObject>;
|
|
28
|
+
restorePhone(params: {
|
|
29
|
+
phone: string;
|
|
30
|
+
code: string;
|
|
31
|
+
aid: string;
|
|
32
|
+
}): Promise<JsonObject>;
|
|
33
|
+
createDeviceCopy(params?: {
|
|
34
|
+
aid?: string | null;
|
|
35
|
+
}): Promise<JsonObject>;
|
|
36
|
+
uploadDeviceCopyMaterials(params: {
|
|
37
|
+
transferCode: string;
|
|
38
|
+
cert: string;
|
|
39
|
+
key: string;
|
|
40
|
+
aid?: string | null;
|
|
41
|
+
metadata?: JsonObject | null;
|
|
42
|
+
}): Promise<JsonObject>;
|
|
43
|
+
claimDeviceCopy(params: {
|
|
44
|
+
aid: string;
|
|
45
|
+
transferCode: string;
|
|
46
|
+
}): Promise<JsonObject>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { AUNError, ValidationError } from '../errors.js';
|
|
2
|
+
import { isJsonObject } from '../types.js';
|
|
3
|
+
const CUSTODY_HTTP_TIMEOUT_MS = 30_000;
|
|
4
|
+
function issuerDomainFromAid(aid) {
|
|
5
|
+
const parts = String(aid || '').trim().split('.', 2);
|
|
6
|
+
return parts.length > 1 ? parts[1] : parts[0] || '';
|
|
7
|
+
}
|
|
8
|
+
function custodyWellKnownUrls(aid, discoveryPort, verifySsl) {
|
|
9
|
+
const portSuffix = discoveryPort ? `:${discoveryPort}` : '';
|
|
10
|
+
const issuerDomain = issuerDomainFromAid(aid);
|
|
11
|
+
const aidUrl = `https://${aid}${portSuffix}/.well-known/aun-custody`;
|
|
12
|
+
const fallbackUrl = `https://aid_custody.${issuerDomain}${portSuffix}/.well-known/aun-custody`;
|
|
13
|
+
const urls = verifySsl ? [aidUrl, fallbackUrl] : [fallbackUrl, aidUrl];
|
|
14
|
+
return [...new Set(urls)];
|
|
15
|
+
}
|
|
16
|
+
function extractCustodyUrl(payload) {
|
|
17
|
+
for (const key of ['custody_url', 'custodyUrl', 'url']) {
|
|
18
|
+
const value = String(payload[key] ?? '').trim();
|
|
19
|
+
if (value)
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
if (isJsonObject(payload.custody)) {
|
|
23
|
+
const value = String(payload.custody.url ?? '').trim();
|
|
24
|
+
if (value)
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
for (const key of ['custody_services', 'custodyServices', 'services']) {
|
|
28
|
+
const items = payload[key];
|
|
29
|
+
if (Array.isArray(items)) {
|
|
30
|
+
const candidates = items
|
|
31
|
+
.filter(isJsonObject)
|
|
32
|
+
.sort((a, b) => Number(a.priority ?? 999) - Number(b.priority ?? 999));
|
|
33
|
+
for (const item of candidates) {
|
|
34
|
+
const value = String(item.url ?? '').trim();
|
|
35
|
+
if (value)
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
throw new ValidationError('custody well-known missing custody url');
|
|
41
|
+
}
|
|
42
|
+
function normalizeCustodyUrl(url) {
|
|
43
|
+
const value = String(url ?? '').trim().replace(/\/+$/, '');
|
|
44
|
+
if (!value)
|
|
45
|
+
return null;
|
|
46
|
+
try {
|
|
47
|
+
const parsed = new URL(value);
|
|
48
|
+
if ((parsed.protocol !== 'http:' && parsed.protocol !== 'https:') || !parsed.hostname) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function fetchJsonWithTimeout(input, init, timeoutMs = CUSTODY_HTTP_TIMEOUT_MS) {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
60
|
+
try {
|
|
61
|
+
const response = await fetch(input, { ...init, signal: controller.signal });
|
|
62
|
+
const payload = await response.json();
|
|
63
|
+
if (response.ok) {
|
|
64
|
+
return payload;
|
|
65
|
+
}
|
|
66
|
+
const error = isJsonObject(payload) && isJsonObject(payload.error) ? payload.error : null;
|
|
67
|
+
const code = String(error?.code ?? '');
|
|
68
|
+
const message = String(error?.message ?? '');
|
|
69
|
+
throw new AUNError(message ? `custody ${code || response.status}: ${message}` : `custody HTTP ${response.status}`);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (controller.signal.aborted) {
|
|
73
|
+
throw new AUNError(`custody request timed out after ${timeoutMs}ms`);
|
|
74
|
+
}
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export class CustodyNamespace {
|
|
82
|
+
_client;
|
|
83
|
+
_custodyUrl = '';
|
|
84
|
+
constructor(client) {
|
|
85
|
+
this._client = client;
|
|
86
|
+
}
|
|
87
|
+
get _internal() {
|
|
88
|
+
return this._client;
|
|
89
|
+
}
|
|
90
|
+
setUrl(url) {
|
|
91
|
+
this._custodyUrl = String(url ?? '').trim().replace(/\/+$/, '');
|
|
92
|
+
}
|
|
93
|
+
configureUrl(url) {
|
|
94
|
+
this.setUrl(url);
|
|
95
|
+
}
|
|
96
|
+
async discoverUrl(params = {}) {
|
|
97
|
+
const aid = String(params.aid ?? this._client.aid ?? '').trim();
|
|
98
|
+
if (!aid) {
|
|
99
|
+
throw new ValidationError('custody.discoverUrl requires aid or authenticated client');
|
|
100
|
+
}
|
|
101
|
+
let lastError = null;
|
|
102
|
+
const config = this._internal._configModel;
|
|
103
|
+
const urls = custodyWellKnownUrls(aid, config.discoveryPort, config.verifySsl ?? true);
|
|
104
|
+
for (const wellKnownUrl of urls) {
|
|
105
|
+
try {
|
|
106
|
+
const payload = await fetchJsonWithTimeout(wellKnownUrl, { method: 'GET' }, params.timeout ?? 5_000);
|
|
107
|
+
if (!isJsonObject(payload)) {
|
|
108
|
+
throw new ValidationError('custody well-known returned invalid payload');
|
|
109
|
+
}
|
|
110
|
+
const custodyUrl = normalizeCustodyUrl(extractCustodyUrl(payload));
|
|
111
|
+
if (!custodyUrl) {
|
|
112
|
+
throw new ValidationError('custody well-known returned invalid custody url');
|
|
113
|
+
}
|
|
114
|
+
this._custodyUrl = custodyUrl;
|
|
115
|
+
return custodyUrl;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
lastError = error;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
throw new AUNError(`custody discovery failed for ${aid}: ${lastError instanceof Error ? lastError.message : String(lastError)}`);
|
|
122
|
+
}
|
|
123
|
+
async _resolveCustodyUrl(aid) {
|
|
124
|
+
const custodyUrl = normalizeCustodyUrl(this._custodyUrl);
|
|
125
|
+
if (custodyUrl) {
|
|
126
|
+
if (custodyUrl !== this._custodyUrl) {
|
|
127
|
+
this._custodyUrl = custodyUrl;
|
|
128
|
+
}
|
|
129
|
+
return custodyUrl;
|
|
130
|
+
}
|
|
131
|
+
return this.discoverUrl({ aid });
|
|
132
|
+
}
|
|
133
|
+
_getAccessToken() {
|
|
134
|
+
const identity = this._internal._identity;
|
|
135
|
+
if (identity) {
|
|
136
|
+
const token = String(identity.access_token ?? '').trim();
|
|
137
|
+
if (token)
|
|
138
|
+
return token;
|
|
139
|
+
}
|
|
140
|
+
throw new ValidationError('no access_token available: call auth.authenticate() first');
|
|
141
|
+
}
|
|
142
|
+
async _post(path, body, opts = {}) {
|
|
143
|
+
const headers = { 'Content-Type': 'application/json' };
|
|
144
|
+
const token = String(opts.token ?? '').trim();
|
|
145
|
+
if (token) {
|
|
146
|
+
headers.Authorization = `Bearer ${token}`;
|
|
147
|
+
}
|
|
148
|
+
const payload = await fetchJsonWithTimeout(`${await this._resolveCustodyUrl(String(body.aid ?? '') || this._client.aid)}${path}`, {
|
|
149
|
+
method: 'POST',
|
|
150
|
+
headers,
|
|
151
|
+
body: JSON.stringify(body),
|
|
152
|
+
});
|
|
153
|
+
if (!isJsonObject(payload)) {
|
|
154
|
+
throw new AUNError('custody returned invalid JSON payload');
|
|
155
|
+
}
|
|
156
|
+
return payload;
|
|
157
|
+
}
|
|
158
|
+
async sendCode(params) {
|
|
159
|
+
const phone = String(params.phone ?? '').trim();
|
|
160
|
+
const aid = String(params.aid ?? '').trim();
|
|
161
|
+
if (!phone) {
|
|
162
|
+
throw new ValidationError('custody.sendCode requires non-empty phone');
|
|
163
|
+
}
|
|
164
|
+
const body = { phone };
|
|
165
|
+
let token = null;
|
|
166
|
+
if (aid) {
|
|
167
|
+
body.aid = aid;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
token = this._getAccessToken();
|
|
171
|
+
}
|
|
172
|
+
return this._post('/custody/accounts/send-code', body, { token });
|
|
173
|
+
}
|
|
174
|
+
async bindPhone(params) {
|
|
175
|
+
const phone = String(params.phone ?? '').trim();
|
|
176
|
+
const code = String(params.code ?? '').trim();
|
|
177
|
+
const cert = String(params.cert ?? '').trim();
|
|
178
|
+
const key = String(params.key ?? '').trim();
|
|
179
|
+
if (!phone || !code || !cert || !key) {
|
|
180
|
+
throw new ValidationError('custody.bindPhone requires phone, code, cert and key');
|
|
181
|
+
}
|
|
182
|
+
const body = { phone, code, cert, key };
|
|
183
|
+
if (params.metadata && isJsonObject(params.metadata)) {
|
|
184
|
+
body.metadata = params.metadata;
|
|
185
|
+
}
|
|
186
|
+
return this._post('/custody/accounts/bind-phone', body, {
|
|
187
|
+
token: this._getAccessToken(),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
async restorePhone(params) {
|
|
191
|
+
const phone = String(params.phone ?? '').trim();
|
|
192
|
+
const code = String(params.code ?? '').trim();
|
|
193
|
+
const aid = String(params.aid ?? '').trim();
|
|
194
|
+
if (!phone || !code || !aid) {
|
|
195
|
+
throw new ValidationError('custody.restorePhone requires phone, code and aid');
|
|
196
|
+
}
|
|
197
|
+
return this._post('/custody/accounts/restore-phone', { phone, code, aid });
|
|
198
|
+
}
|
|
199
|
+
async createDeviceCopy(params = {}) {
|
|
200
|
+
const aid = String(params.aid ?? this._client.aid ?? '').trim();
|
|
201
|
+
if (!aid) {
|
|
202
|
+
throw new ValidationError('custody.createDeviceCopy requires aid or authenticated client');
|
|
203
|
+
}
|
|
204
|
+
return this._post('/custody/transfers', { aid }, { token: this._getAccessToken() });
|
|
205
|
+
}
|
|
206
|
+
async uploadDeviceCopyMaterials(params) {
|
|
207
|
+
const transferCode = String(params.transferCode ?? '').trim();
|
|
208
|
+
const aid = String(params.aid ?? this._client.aid ?? '').trim();
|
|
209
|
+
const cert = String(params.cert ?? '').trim();
|
|
210
|
+
const key = String(params.key ?? '').trim();
|
|
211
|
+
if (!transferCode || !aid || !cert || !key) {
|
|
212
|
+
throw new ValidationError('custody.uploadDeviceCopyMaterials requires transferCode, aid, cert and key');
|
|
213
|
+
}
|
|
214
|
+
const body = { aid, cert, key };
|
|
215
|
+
if (params.metadata && isJsonObject(params.metadata)) {
|
|
216
|
+
body.metadata = params.metadata;
|
|
217
|
+
}
|
|
218
|
+
return this._post(`/custody/transfers/${encodeURIComponent(transferCode)}/materials`, body, {
|
|
219
|
+
token: this._getAccessToken(),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
async claimDeviceCopy(params) {
|
|
223
|
+
const aid = String(params.aid ?? '').trim();
|
|
224
|
+
const transferCode = String(params.transferCode ?? '').trim();
|
|
225
|
+
if (!aid || !transferCode) {
|
|
226
|
+
throw new ValidationError('custody.claimDeviceCopy requires aid and transferCode');
|
|
227
|
+
}
|
|
228
|
+
return this._post('/custody/transfers/claim', { aid, transfer_code: transferCode });
|
|
229
|
+
}
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AC9B,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,MAGrB;QACC,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAC5C,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;QAC9D,IAAI,CAAC,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;YAC1B,MAAM,IAAI,eAAe,CAAC,uDAAuD,CAAC,CAAC;QACrF,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,0BAA0B,EAAE,EAAC,GAAG,EAAE,aAAa,EAAE,YAAY,EAAC,CAAC,CAAC;IACpF,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基于文件的 SecretStore(AES-256-GCM 加密)
|
|
3
|
+
*
|
|
4
|
+
* 密钥派生:
|
|
5
|
+
* - 传入 encryptionSeed → 从 seed 字符串派生
|
|
6
|
+
* - 未传 → 从 {root}/.seed 文件派生(首次自动生成)
|
|
7
|
+
*
|
|
8
|
+
* 与 Python SDK 的 FileSecretStore 完全对齐。
|
|
9
|
+
*/
|
|
10
|
+
import type { SecretStore } from './index.js';
|
|
11
|
+
import type { SecretRecord } from '../types.js';
|
|
12
|
+
export declare class FileSecretStore implements SecretStore {
|
|
13
|
+
private _root;
|
|
14
|
+
private _masterKey;
|
|
15
|
+
constructor(root: string, encryptionSeed?: string, sqliteBackup?: {
|
|
16
|
+
backupSeed(seed: Buffer): void;
|
|
17
|
+
restoreSeed(): Buffer | null;
|
|
18
|
+
});
|
|
19
|
+
protect(scope: string, name: string, plaintext: Buffer): SecretRecord;
|
|
20
|
+
reveal(scope: string, name: string, record: SecretRecord): Buffer | null;
|
|
21
|
+
/** 使用 HMAC-SHA256 从主密钥派生子密钥 */
|
|
22
|
+
private _deriveKey;
|
|
23
|
+
/** 三级恢复:文件 → SQLite → 新建,双写确保一致 */
|
|
24
|
+
private _loadOrCreateSeed;
|
|
25
|
+
}
|