@avacuscc/sdk 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 +216 -0
- package/package.json +37 -0
- package/packages/sdk/dist/chunk-T5BAFYHX.mjs +342 -0
- package/packages/sdk/dist/index.d.mts +53 -0
- package/packages/sdk/dist/index.d.ts +53 -0
- package/packages/sdk/dist/index.js +460 -0
- package/packages/sdk/dist/index.mjs +104 -0
- package/packages/sdk/dist/sns-D0rtlsZp.d.mts +267 -0
- package/packages/sdk/dist/sns-D0rtlsZp.d.ts +267 -0
- package/packages/sdk/dist/sns.d.mts +1 -0
- package/packages/sdk/dist/sns.d.ts +1 -0
- package/packages/sdk/dist/sns.js +367 -0
- package/packages/sdk/dist/sns.mjs +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# @avacuscc/sdk
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for Avacus services.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- `AvacusClient` as the main entrypoint
|
|
8
|
+
- SNS authentication flow in `client.sns`
|
|
9
|
+
- no lock-in to `ethers`
|
|
10
|
+
- login API based on capability: `walletAddress + signMessage`
|
|
11
|
+
- production endpoint by default
|
|
12
|
+
cp examples/basic-typescript/.env.example examples/basic-typescript/.env
|
|
13
|
+
- optional `env: 'dev' | 'stg' | 'prod'` without exposing raw URLs to SDK consumers
|
|
14
|
+
pnpm install
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Build
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm build
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Create client
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { AvacusClient } from '@avacuscc/sdk';
|
|
29
|
+
|
|
30
|
+
const client = new AvacusClient();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
By default, the SDK uses the production endpoint.
|
|
34
|
+
|
|
35
|
+
You can switch environment without knowing the raw URL:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
const client = new AvacusClient({ env: 'dev' });
|
|
39
|
+
const stgClient = new AvacusClient({ env: 'stg' });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
If you pass `baseUrl`, it takes priority over `env`:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const client = new AvacusClient({
|
|
46
|
+
env: 'prod',
|
|
47
|
+
baseUrl: 'https://your-custom-host/',
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or install just the SNS-focused client via the subpath entrypoint:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { SnsAuthClient } from '@avacuscc/sdk/sns';
|
|
55
|
+
|
|
56
|
+
const sns = new SnsAuthClient();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Or target a non-production environment:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
const sns = new SnsAuthClient({ env: 'dev' });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### SNS login
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { AvacusClient } from '@avacuscc/sdk';
|
|
69
|
+
|
|
70
|
+
const client = new AvacusClient();
|
|
71
|
+
|
|
72
|
+
const result = await client.sns.login({
|
|
73
|
+
walletAddress,
|
|
74
|
+
signMessage,
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### SNS create user
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { AvacusClient } from '@avacuscc/sdk';
|
|
82
|
+
import { Wallet } from 'ethers';
|
|
83
|
+
|
|
84
|
+
const wallet = Wallet.createRandom();
|
|
85
|
+
|
|
86
|
+
const client = new AvacusClient({ env: 'dev' });
|
|
87
|
+
|
|
88
|
+
const result = await client.sns.createUser({
|
|
89
|
+
walletAddress: wallet.address,
|
|
90
|
+
signMessage: (message) => wallet.signMessage(message),
|
|
91
|
+
displayName: 'sdk-user',
|
|
92
|
+
description: 'Created from SDK',
|
|
93
|
+
device: {
|
|
94
|
+
deviceToken: 'device-token',
|
|
95
|
+
appName: 'Avacus Wallet',
|
|
96
|
+
appVersion: '1.0.0',
|
|
97
|
+
platform: 'ios',
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
The SDK handles this flow internally:
|
|
103
|
+
|
|
104
|
+
1. `getNonce(walletAddress)`
|
|
105
|
+
2. build signed message from `DEFAULT_BASE_SIGNED_MESSAGE + nonce`
|
|
106
|
+
3. `signMessage(message)`
|
|
107
|
+
4. `createUserWithSignature({ walletAddress, message, signature, ...profileFields })`
|
|
108
|
+
|
|
109
|
+
If the create-user response contains a JWT token, the SDK stores it automatically
|
|
110
|
+
so subsequent authenticated service calls can reuse it.
|
|
111
|
+
|
|
112
|
+
### SNS public profiles
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const profiles = await client.sns.getPublicProfiles({
|
|
116
|
+
wallets: [walletAddress],
|
|
117
|
+
includeDevices: true,
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### SNS-only import
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
import { SnsAuthClient } from '@avacuscc/sdk/sns';
|
|
125
|
+
|
|
126
|
+
const sns = new SnsAuthClient();
|
|
127
|
+
|
|
128
|
+
const result = await sns.login({
|
|
129
|
+
walletAddress,
|
|
130
|
+
signMessage,
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The SDK handles this flow internally:
|
|
135
|
+
|
|
136
|
+
1. `getNonce(walletAddress)`
|
|
137
|
+
2. build signed message from `DEFAULT_BASE_SIGNED_MESSAGE + nonce`
|
|
138
|
+
3. `signMessage(message)`
|
|
139
|
+
4. `requestAuthToken({ wallet_address, message, signature })`
|
|
140
|
+
|
|
141
|
+
### Ethers example
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
await client.sns.login({
|
|
145
|
+
walletAddress: await signer.getAddress(),
|
|
146
|
+
signMessage: (message) => signer.signMessage(message),
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Viem example
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
await client.sns.login({
|
|
154
|
+
walletAddress: account.address,
|
|
155
|
+
signMessage: (message) =>
|
|
156
|
+
walletClient.signMessage({
|
|
157
|
+
account,
|
|
158
|
+
message,
|
|
159
|
+
}),
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Custom signer / KMS example
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
await client.sns.login({
|
|
167
|
+
walletAddress: kmsWallet.address,
|
|
168
|
+
signMessage: async (message) => kmsSigner.signMessage(message),
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Low-level SNS APIs
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const { nonce } = await client.sns.getNonce({ walletAddress });
|
|
176
|
+
|
|
177
|
+
const result = await client.sns.requestAuthToken({
|
|
178
|
+
wallet_address: walletAddress,
|
|
179
|
+
message,
|
|
180
|
+
signature,
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Examples
|
|
185
|
+
|
|
186
|
+
See `examples/README.md`.
|
|
187
|
+
|
|
188
|
+
### Install from package registry
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
yarn add @avacuscc/sdk
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Quick commands from the repository root:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
cp examples/basic-typescript/.env.example examples/basic-typescript/.env
|
|
198
|
+
pnpm build
|
|
199
|
+
pnpm --dir examples/basic-typescript install
|
|
200
|
+
pnpm --dir examples/basic-typescript start
|
|
201
|
+
pnpm --dir examples/basic-typescript create-user
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The example uses production by default. Set `AVACUS_BASE_URL` only if you want to override the endpoint.
|
|
205
|
+
|
|
206
|
+
## Project structure
|
|
207
|
+
|
|
208
|
+
- `packages/sdk/src/config.ts` — environment-based endpoint resolution
|
|
209
|
+
- `packages/sdk/src/core/client.ts` — main SDK client
|
|
210
|
+
- `packages/sdk/src/services/sns.ts` — SNS auth service
|
|
211
|
+
- `packages/sdk/src/adapters/http.ts` — shared HTTP transport
|
|
212
|
+
|
|
213
|
+
## Notes
|
|
214
|
+
|
|
215
|
+
- build output is generated into `packages/sdk/dist`
|
|
216
|
+
- public exports come from `packages/sdk/src/index.ts`
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avacuscc/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "SDK for interacting with services in the Wakumo ecosystem",
|
|
5
|
+
"main": "./packages/sdk/dist/index.js",
|
|
6
|
+
"module": "./packages/sdk/dist/index.mjs",
|
|
7
|
+
"types": "./packages/sdk/dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./packages/sdk/dist/index.d.ts",
|
|
11
|
+
"import": "./packages/sdk/dist/index.mjs",
|
|
12
|
+
"require": "./packages/sdk/dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./sns": {
|
|
15
|
+
"types": "./packages/sdk/dist/sns.d.ts",
|
|
16
|
+
"import": "./packages/sdk/dist/sns.mjs",
|
|
17
|
+
"require": "./packages/sdk/dist/sns.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"packages/sdk/dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup packages/sdk/src/index.ts packages/sdk/src/sns.ts --format cjs,esm --dts --clean --out-dir packages/sdk/dist",
|
|
26
|
+
"dev": "tsup packages/sdk/src/index.ts packages/sdk/src/sns.ts --format cjs,esm --dts --watch --out-dir packages/sdk/dist",
|
|
27
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [],
|
|
30
|
+
"author": "longhoang@wakumo.vn",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"packageManager": "pnpm@10.33.0",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"tsup": "^8.5.1",
|
|
35
|
+
"typescript": "^6.0.2"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,342 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { H as HttpAdapter, S as SnsService, B as BaseClientOptions } from './sns-D0rtlsZp.mjs';
|
|
2
|
+
export { A as AVACUS_ENDPOINTS, a as AuthState, b as AvacusEnvironment, D as DEFAULT_BASE_SIGNED_MESSAGE, R as RequestOptions, c as ResolvedClientSettings, d as SNS_SERVICE_PATH, e as SignMessageFn, f as SnsAuthClient, g as SnsAuthClientOptions, h as SnsAuthTokenPayload, i as SnsAuthenticateParams, j as SnsAvatar, k as SnsCreateUserDeviceParams, l as SnsCreateUserParams, m as SnsCreateUserPayload, n as SnsCreateUserResult, o as SnsCreateUserWithSignatureParams, p as SnsDevice, q as SnsGetNonceParams, r as SnsGetProfilesParams, s as SnsLoginParams, t as SnsLoginResult, u as SnsNonceResponse, v as SnsProfile, w as SnsServiceOptions, x as isKnownAvacusBaseUrl, y as loginWithSns, z as resolveAvacusBaseUrl, C as resolveClientSettings, E as resolveServiceUrl } from './sns-D0rtlsZp.mjs';
|
|
3
|
+
|
|
4
|
+
declare class BalancerService {
|
|
5
|
+
private readonly http;
|
|
6
|
+
constructor(http: HttpAdapter);
|
|
7
|
+
getPools(): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base path for gas-account endpoints. The client resolves this path against the
|
|
12
|
+
* selected environment host before creating the service adapter.
|
|
13
|
+
*/
|
|
14
|
+
declare const GAS_ACCOUNT_SERVICE_PATH = "1/gas-account/";
|
|
15
|
+
declare class GasAccountService {
|
|
16
|
+
private readonly http;
|
|
17
|
+
/**
|
|
18
|
+
* Creates the gas-account service using a service-scoped HTTP adapter.
|
|
19
|
+
*
|
|
20
|
+
* @param http Adapter already configured for the gas-account base path.
|
|
21
|
+
*/
|
|
22
|
+
constructor(http: HttpAdapter);
|
|
23
|
+
/**
|
|
24
|
+
* Returns the deposit vault address mapping keyed by chain ID.
|
|
25
|
+
* Requires a JWT token from a successful SNS login.
|
|
26
|
+
*/
|
|
27
|
+
getDepositVaults(): Promise<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the authenticated gas-account service status payload.
|
|
30
|
+
* Requires a JWT token from a successful SNS login.
|
|
31
|
+
*/
|
|
32
|
+
getStatus(): Promise<unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface AvacusClientOptions extends BaseClientOptions {
|
|
36
|
+
}
|
|
37
|
+
declare class AvacusClient {
|
|
38
|
+
readonly http: HttpAdapter;
|
|
39
|
+
readonly gasAccountHttp: HttpAdapter;
|
|
40
|
+
readonly gasAccount: GasAccountService;
|
|
41
|
+
readonly sns: SnsService;
|
|
42
|
+
readonly balancer: BalancerService;
|
|
43
|
+
/**
|
|
44
|
+
* Creates the root SDK client and wires all service adapters with a shared
|
|
45
|
+
* auth state so a token obtained from `sns.login()` is reused automatically by
|
|
46
|
+
* other authenticated services.
|
|
47
|
+
*
|
|
48
|
+
* @param options Environment selection, optional host override, and default headers.
|
|
49
|
+
*/
|
|
50
|
+
constructor(options?: AvacusClientOptions);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { AvacusClient, type AvacusClientOptions, BalancerService, BaseClientOptions, GAS_ACCOUNT_SERVICE_PATH, GasAccountService, HttpAdapter, SnsService };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { H as HttpAdapter, S as SnsService, B as BaseClientOptions } from './sns-D0rtlsZp.js';
|
|
2
|
+
export { A as AVACUS_ENDPOINTS, a as AuthState, b as AvacusEnvironment, D as DEFAULT_BASE_SIGNED_MESSAGE, R as RequestOptions, c as ResolvedClientSettings, d as SNS_SERVICE_PATH, e as SignMessageFn, f as SnsAuthClient, g as SnsAuthClientOptions, h as SnsAuthTokenPayload, i as SnsAuthenticateParams, j as SnsAvatar, k as SnsCreateUserDeviceParams, l as SnsCreateUserParams, m as SnsCreateUserPayload, n as SnsCreateUserResult, o as SnsCreateUserWithSignatureParams, p as SnsDevice, q as SnsGetNonceParams, r as SnsGetProfilesParams, s as SnsLoginParams, t as SnsLoginResult, u as SnsNonceResponse, v as SnsProfile, w as SnsServiceOptions, x as isKnownAvacusBaseUrl, y as loginWithSns, z as resolveAvacusBaseUrl, C as resolveClientSettings, E as resolveServiceUrl } from './sns-D0rtlsZp.js';
|
|
3
|
+
|
|
4
|
+
declare class BalancerService {
|
|
5
|
+
private readonly http;
|
|
6
|
+
constructor(http: HttpAdapter);
|
|
7
|
+
getPools(): Promise<unknown>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base path for gas-account endpoints. The client resolves this path against the
|
|
12
|
+
* selected environment host before creating the service adapter.
|
|
13
|
+
*/
|
|
14
|
+
declare const GAS_ACCOUNT_SERVICE_PATH = "1/gas-account/";
|
|
15
|
+
declare class GasAccountService {
|
|
16
|
+
private readonly http;
|
|
17
|
+
/**
|
|
18
|
+
* Creates the gas-account service using a service-scoped HTTP adapter.
|
|
19
|
+
*
|
|
20
|
+
* @param http Adapter already configured for the gas-account base path.
|
|
21
|
+
*/
|
|
22
|
+
constructor(http: HttpAdapter);
|
|
23
|
+
/**
|
|
24
|
+
* Returns the deposit vault address mapping keyed by chain ID.
|
|
25
|
+
* Requires a JWT token from a successful SNS login.
|
|
26
|
+
*/
|
|
27
|
+
getDepositVaults(): Promise<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the authenticated gas-account service status payload.
|
|
30
|
+
* Requires a JWT token from a successful SNS login.
|
|
31
|
+
*/
|
|
32
|
+
getStatus(): Promise<unknown>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface AvacusClientOptions extends BaseClientOptions {
|
|
36
|
+
}
|
|
37
|
+
declare class AvacusClient {
|
|
38
|
+
readonly http: HttpAdapter;
|
|
39
|
+
readonly gasAccountHttp: HttpAdapter;
|
|
40
|
+
readonly gasAccount: GasAccountService;
|
|
41
|
+
readonly sns: SnsService;
|
|
42
|
+
readonly balancer: BalancerService;
|
|
43
|
+
/**
|
|
44
|
+
* Creates the root SDK client and wires all service adapters with a shared
|
|
45
|
+
* auth state so a token obtained from `sns.login()` is reused automatically by
|
|
46
|
+
* other authenticated services.
|
|
47
|
+
*
|
|
48
|
+
* @param options Environment selection, optional host override, and default headers.
|
|
49
|
+
*/
|
|
50
|
+
constructor(options?: AvacusClientOptions);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export { AvacusClient, type AvacusClientOptions, BalancerService, BaseClientOptions, GAS_ACCOUNT_SERVICE_PATH, GasAccountService, HttpAdapter, SnsService };
|