@avacuscc/sdk 0.1.0 → 0.2.1
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 +267 -0
- package/package.json +4 -4
- package/packages/sdk/dist/index.d.mts +675 -7
- package/packages/sdk/dist/index.d.ts +675 -7
- package/packages/sdk/dist/index.js +532 -18
- package/packages/sdk/dist/index.mjs +844 -18
- package/packages/sdk/dist/{sns-D0rtlsZp.d.mts → sns-NuV2JpAA.d.mts} +24 -1
- package/packages/sdk/dist/{sns-D0rtlsZp.d.ts → sns-NuV2JpAA.d.ts} +24 -1
- package/packages/sdk/dist/sns.d.mts +1 -1
- package/packages/sdk/dist/sns.d.ts +1 -1
- package/packages/sdk/dist/sns.js +71 -14
- package/packages/sdk/dist/sns.mjs +383 -9
- package/packages/sdk/dist/chunk-T5BAFYHX.mjs +0 -342
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
// packages/sdk/src/adapters/http.ts
|
|
2
|
-
var HttpAdapter = class {
|
|
3
|
-
/**
|
|
4
|
-
* Creates a reusable HTTP transport bound to one service base URL.
|
|
5
|
-
*
|
|
6
|
-
* @param baseUrl Absolute base URL for the target service namespace.
|
|
7
|
-
* @param headers Default headers sent with every request.
|
|
8
|
-
* @param authState Shared mutable auth state so multiple adapters can reuse the same JWT.
|
|
9
|
-
*/
|
|
10
|
-
constructor(baseUrl, headers = {}, authState = {}) {
|
|
11
|
-
this.baseUrl = baseUrl;
|
|
12
|
-
this.headers = headers;
|
|
13
|
-
this.authState = authState;
|
|
14
|
-
}
|
|
15
|
-
baseUrl;
|
|
16
|
-
headers;
|
|
17
|
-
authState;
|
|
18
|
-
/**
|
|
19
|
-
* Stores the JWT access token used by authenticated requests.
|
|
20
|
-
*
|
|
21
|
-
* @param accessToken JWT returned by the authentication flow.
|
|
22
|
-
*/
|
|
23
|
-
setAccessToken(accessToken) {
|
|
24
|
-
this.authState.accessToken = accessToken;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Removes the currently stored JWT access token from shared auth state.
|
|
28
|
-
*/
|
|
29
|
-
clearAccessToken() {
|
|
30
|
-
delete this.authState.accessToken;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Returns the currently stored JWT access token, if any.
|
|
34
|
-
*/
|
|
35
|
-
getAccessToken() {
|
|
36
|
-
return this.authState.accessToken;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Sends an HTTP GET request to a path relative to this adapter base URL.
|
|
40
|
-
*
|
|
41
|
-
* @param path Relative endpoint path.
|
|
42
|
-
* @param options Request behavior such as auth requirement and extra headers.
|
|
43
|
-
*/
|
|
44
|
-
async get(path, options = {}) {
|
|
45
|
-
const response = await fetch(this.buildUrl(path), {
|
|
46
|
-
method: "GET",
|
|
47
|
-
headers: this.buildHeaders(options)
|
|
48
|
-
});
|
|
49
|
-
return this.parseResponse(response, "GET", path);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Sends an HTTP POST request with a JSON body to a path relative to this adapter base URL.
|
|
53
|
-
*
|
|
54
|
-
* @param path Relative endpoint path.
|
|
55
|
-
* @param body Serializable request payload.
|
|
56
|
-
* @param options Request behavior such as auth requirement and extra headers.
|
|
57
|
-
*/
|
|
58
|
-
async post(path, body, options = {}) {
|
|
59
|
-
const response = await fetch(this.buildUrl(path), {
|
|
60
|
-
method: "POST",
|
|
61
|
-
headers: this.buildHeaders({
|
|
62
|
-
...options,
|
|
63
|
-
headers: {
|
|
64
|
-
"Content-Type": "application/json",
|
|
65
|
-
...options.headers
|
|
66
|
-
}
|
|
67
|
-
}),
|
|
68
|
-
body: body === void 0 ? void 0 : JSON.stringify(body)
|
|
69
|
-
});
|
|
70
|
-
return this.parseResponse(response, "POST", path);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Builds the final request headers by combining default headers, per-request
|
|
74
|
-
* headers, and an authorization header when the request requires auth.
|
|
75
|
-
*/
|
|
76
|
-
buildHeaders(options) {
|
|
77
|
-
const accessToken = this.authState.accessToken;
|
|
78
|
-
if (options.auth && !accessToken) {
|
|
79
|
-
throw new Error("This request requires authentication. Call login() first.");
|
|
80
|
-
}
|
|
81
|
-
return {
|
|
82
|
-
...this.headers,
|
|
83
|
-
...options.headers,
|
|
84
|
-
...options.auth && accessToken ? {
|
|
85
|
-
Authorization: `Bearer ${accessToken}`
|
|
86
|
-
} : {}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Resolves a relative path against the adapter base URL.
|
|
91
|
-
*/
|
|
92
|
-
buildUrl(path) {
|
|
93
|
-
const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
|
|
94
|
-
return new URL(normalizedPath, this.baseUrl);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Parses a JSON response and converts non-2xx responses into descriptive errors.
|
|
98
|
-
*/
|
|
99
|
-
async parseResponse(response, method, path) {
|
|
100
|
-
const bodyText = await response.text();
|
|
101
|
-
if (!response.ok) {
|
|
102
|
-
throw new Error(
|
|
103
|
-
`HTTP ${response.status} ${response.statusText} for ${method} ${path}: ${bodyText}`
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
try {
|
|
107
|
-
return JSON.parse(bodyText);
|
|
108
|
-
} catch (error) {
|
|
109
|
-
throw new Error(
|
|
110
|
-
`Invalid JSON response for ${method} ${path}: ${bodyText}`,
|
|
111
|
-
{ cause: error }
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
// packages/sdk/src/config.ts
|
|
118
|
-
var AVACUS_ENDPOINTS = {
|
|
119
|
-
prod: "https://apis.avacus.cc/",
|
|
120
|
-
dev: "https://apis.d.avscc.unit-hosting.net/",
|
|
121
|
-
stg: "https://apis.avacus.cc/stg/"
|
|
122
|
-
};
|
|
123
|
-
function resolveAvacusBaseUrl(options) {
|
|
124
|
-
if (options.baseUrl) {
|
|
125
|
-
return options.baseUrl;
|
|
126
|
-
}
|
|
127
|
-
return AVACUS_ENDPOINTS[options.env ?? "prod"];
|
|
128
|
-
}
|
|
129
|
-
function resolveClientSettings(options = {}) {
|
|
130
|
-
return {
|
|
131
|
-
env: options.env ?? "prod",
|
|
132
|
-
baseUrl: resolveAvacusBaseUrl(options),
|
|
133
|
-
headers: options.headers
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
function isKnownAvacusBaseUrl(baseUrl) {
|
|
137
|
-
return Object.values(AVACUS_ENDPOINTS).includes(baseUrl);
|
|
138
|
-
}
|
|
139
|
-
function resolveServiceUrl(baseUrl, servicePath) {
|
|
140
|
-
const normalizedServicePath = servicePath.endsWith("/") ? servicePath : `${servicePath}/`;
|
|
141
|
-
return new URL(normalizedServicePath, baseUrl).toString();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// packages/sdk/src/services/sns.ts
|
|
145
|
-
var DEFAULT_BASE_SIGNED_MESSAGE = "Hi there from Avacus Wallet! Please sign this message to prove that you can access to secure chat. To stop hackers access to your secure chat, do not sign this message outside Avacus Wallet, and here's a unique message ID they can't guess: ";
|
|
146
|
-
var SNS_SERVICE_PATH = "1/secure-chat/";
|
|
147
|
-
var SnsService = class {
|
|
148
|
-
/**
|
|
149
|
-
* Creates the SNS service using a service-scoped HTTP adapter.
|
|
150
|
-
*
|
|
151
|
-
* @param http Adapter already configured for the SNS base path.
|
|
152
|
-
* @param options Optional SNS-specific behavior.
|
|
153
|
-
*/
|
|
154
|
-
constructor(http, options = {}) {
|
|
155
|
-
this.http = http;
|
|
156
|
-
this.options = options;
|
|
157
|
-
}
|
|
158
|
-
http;
|
|
159
|
-
options;
|
|
160
|
-
/**
|
|
161
|
-
* Requests a one-time nonce used to construct the login signature message.
|
|
162
|
-
* This endpoint is public and does not require a JWT token.
|
|
163
|
-
*
|
|
164
|
-
* @param params Wallet identity used by the backend to mint a nonce.
|
|
165
|
-
*/
|
|
166
|
-
async getNonce(params) {
|
|
167
|
-
const searchParams = new URLSearchParams({
|
|
168
|
-
wallet_address: params.walletAddress
|
|
169
|
-
});
|
|
170
|
-
return this.http.get(`v1/public/users/nonce?${searchParams.toString()}`);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Exchanges a signed message payload for an SNS auth token.
|
|
174
|
-
* This is the low-level authentication endpoint behind `login()`.
|
|
175
|
-
*
|
|
176
|
-
* @param params Payload expected by the SNS auth API.
|
|
177
|
-
*/
|
|
178
|
-
async requestAuthToken(params) {
|
|
179
|
-
return this.http.post("v1/public/users/request_auth_token", params);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Returns public user profiles for a batch of wallet addresses and/or user IDs.
|
|
183
|
-
* This endpoint is public and does not require authentication.
|
|
184
|
-
*
|
|
185
|
-
* @param params Query payload with wallet addresses and/or user IDs.
|
|
186
|
-
*/
|
|
187
|
-
async getPublicProfiles(params) {
|
|
188
|
-
return this.http.post("v1/public/users/profiles", {
|
|
189
|
-
wallets: params.wallets,
|
|
190
|
-
user_ids: params.userIds,
|
|
191
|
-
include_devices: params.includeDevices
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Creates a new SNS user account using only the inputs the application already knows:
|
|
196
|
-
* wallet address, signer callback, and optional user profile metadata.
|
|
197
|
-
* The SDK internally fetches a nonce, builds the canonical message, requests
|
|
198
|
-
* the signature, and submits the create-user payload.
|
|
199
|
-
*
|
|
200
|
-
* @param params User creation payload in SDK-friendly camelCase format.
|
|
201
|
-
*/
|
|
202
|
-
async createUser(params) {
|
|
203
|
-
const { walletAddress, signMessage } = params;
|
|
204
|
-
const { nonce } = await this.getNonce({ walletAddress });
|
|
205
|
-
const message = this.buildLoginMessage(nonce);
|
|
206
|
-
const signature = await signMessage(message);
|
|
207
|
-
return this.createUserWithSignature({
|
|
208
|
-
walletAddress,
|
|
209
|
-
message,
|
|
210
|
-
signature,
|
|
211
|
-
publicKey: params.publicKey,
|
|
212
|
-
displayName: params.displayName,
|
|
213
|
-
description: params.description,
|
|
214
|
-
device: params.device
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Low-level create-user API for callers that already have a prepared message
|
|
219
|
-
* and signature and want direct control over the exact payload.
|
|
220
|
-
*
|
|
221
|
-
* @param params Signed create-user payload.
|
|
222
|
-
*/
|
|
223
|
-
async createUserWithSignature(params) {
|
|
224
|
-
const result = await this.http.post(
|
|
225
|
-
"v1/public/users",
|
|
226
|
-
{
|
|
227
|
-
wallet_address: params.walletAddress,
|
|
228
|
-
message: params.message,
|
|
229
|
-
signature: params.signature,
|
|
230
|
-
public_key: params.publicKey,
|
|
231
|
-
display_name: params.displayName,
|
|
232
|
-
description: params.description,
|
|
233
|
-
device: params.device ? {
|
|
234
|
-
device_token: params.device.deviceToken,
|
|
235
|
-
app_name: params.device.appName,
|
|
236
|
-
app_version: params.device.appVersion,
|
|
237
|
-
platform: params.device.platform
|
|
238
|
-
} : void 0
|
|
239
|
-
}
|
|
240
|
-
);
|
|
241
|
-
const accessToken = extractAccessToken(result);
|
|
242
|
-
if (accessToken) {
|
|
243
|
-
this.http.setAccessToken(accessToken);
|
|
244
|
-
}
|
|
245
|
-
return result;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Adapts camelCase SDK parameters to the snake_case payload expected by the backend.
|
|
249
|
-
*
|
|
250
|
-
* @param params Wallet address, signed message, and signature returned by the signer.
|
|
251
|
-
*/
|
|
252
|
-
async authenticate(params) {
|
|
253
|
-
return this.requestAuthToken({
|
|
254
|
-
wallet_address: params.walletAddress,
|
|
255
|
-
message: params.message,
|
|
256
|
-
signature: params.signature
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Executes the full SNS login flow:
|
|
261
|
-
* 1. fetch nonce
|
|
262
|
-
* 2. build login message
|
|
263
|
-
* 3. sign message via injected signer callback
|
|
264
|
-
* 4. exchange signature for JWT
|
|
265
|
-
* 5. persist token in shared HTTP auth state
|
|
266
|
-
*
|
|
267
|
-
* @param params Wallet address and async signing callback.
|
|
268
|
-
*/
|
|
269
|
-
async login(params) {
|
|
270
|
-
const { walletAddress, signMessage } = params;
|
|
271
|
-
const { nonce } = await this.getNonce({ walletAddress });
|
|
272
|
-
const message = this.buildLoginMessage(nonce);
|
|
273
|
-
const signature = await signMessage(message);
|
|
274
|
-
const result = await this.authenticate({
|
|
275
|
-
walletAddress,
|
|
276
|
-
message,
|
|
277
|
-
signature
|
|
278
|
-
});
|
|
279
|
-
const accessToken = extractAccessToken(result);
|
|
280
|
-
if (accessToken) {
|
|
281
|
-
this.http.setAccessToken(accessToken);
|
|
282
|
-
}
|
|
283
|
-
return result;
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* Builds the exact message string that the wallet must sign during login.
|
|
287
|
-
*/
|
|
288
|
-
buildLoginMessage(nonce) {
|
|
289
|
-
return `${this.options.baseSignedMessage ?? DEFAULT_BASE_SIGNED_MESSAGE}${nonce}`;
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
function extractAccessToken(result) {
|
|
293
|
-
const candidateKeys = ["access_token", "accessToken", "token", "jwt"];
|
|
294
|
-
for (const key of candidateKeys) {
|
|
295
|
-
const value = result[key];
|
|
296
|
-
if (typeof value === "string" && value.length > 0) {
|
|
297
|
-
return value;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return void 0;
|
|
301
|
-
}
|
|
302
|
-
async function loginWithSns(sns, params) {
|
|
303
|
-
return sns.login(params);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// packages/sdk/src/sns.ts
|
|
307
|
-
var SnsAuthClient = class extends SnsService {
|
|
308
|
-
settings;
|
|
309
|
-
usingCustomBaseUrl;
|
|
310
|
-
constructor(options = {}) {
|
|
311
|
-
const settings = resolveClientSettings(options);
|
|
312
|
-
const http = new HttpAdapter(
|
|
313
|
-
resolveServiceUrl(settings.baseUrl, "1/secure-chat/"),
|
|
314
|
-
settings.headers
|
|
315
|
-
);
|
|
316
|
-
super(http, {
|
|
317
|
-
baseSignedMessage: options.baseSignedMessage
|
|
318
|
-
});
|
|
319
|
-
this.settings = settings;
|
|
320
|
-
this.usingCustomBaseUrl = !isKnownAvacusBaseUrl(settings.baseUrl);
|
|
321
|
-
}
|
|
322
|
-
getSettings() {
|
|
323
|
-
return { ...this.settings };
|
|
324
|
-
}
|
|
325
|
-
isUsingCustomBaseUrl() {
|
|
326
|
-
return this.usingCustomBaseUrl;
|
|
327
|
-
}
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
export {
|
|
331
|
-
AVACUS_ENDPOINTS,
|
|
332
|
-
resolveAvacusBaseUrl,
|
|
333
|
-
resolveClientSettings,
|
|
334
|
-
isKnownAvacusBaseUrl,
|
|
335
|
-
resolveServiceUrl,
|
|
336
|
-
HttpAdapter,
|
|
337
|
-
DEFAULT_BASE_SIGNED_MESSAGE,
|
|
338
|
-
SNS_SERVICE_PATH,
|
|
339
|
-
SnsService,
|
|
340
|
-
loginWithSns,
|
|
341
|
-
SnsAuthClient
|
|
342
|
-
};
|