@dainprotocol/service-sdk 1.2.2 → 1.2.4
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/dist/client/client-auth.d.ts +35 -62
- package/dist/client/client-auth.js +91 -188
- package/dist/client/client-auth.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Client Authentication for DAIN Services
|
|
3
|
+
*
|
|
4
|
+
* Supports two authentication modes:
|
|
5
|
+
* 1. JWT - For end users (browser-side), NO keypairs/orgId/agentId
|
|
6
|
+
* 2. API Key - For server-to-server auth (Node.js server-side only)
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* JWT Authentication Config (End Users - Browser)
|
|
3
10
|
*/
|
|
4
11
|
export interface DainClientAuthConfigJWT {
|
|
5
12
|
/** JWT access token from DAIN ID OAuth */
|
|
@@ -12,39 +19,29 @@ export interface DainClientAuthConfigJWT {
|
|
|
12
19
|
webhookUrl?: string;
|
|
13
20
|
}
|
|
14
21
|
/**
|
|
15
|
-
*
|
|
22
|
+
* API Key Authentication Config (Server-to-Server - Node.js only)
|
|
16
23
|
*/
|
|
17
|
-
export interface
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
/** Agent ID */
|
|
21
|
-
agentId?: string;
|
|
22
|
-
/** Organization ID */
|
|
23
|
-
orgId?: string;
|
|
24
|
-
/** API Key (alternative to privateKeyBase58/agentId/orgId) */
|
|
25
|
-
apiKey?: string;
|
|
24
|
+
export interface DainClientAuthConfigAPIKey {
|
|
25
|
+
/** API Key for server-side authentication */
|
|
26
|
+
apiKey: string;
|
|
26
27
|
/** Smart Account PDA on Solana (optional) */
|
|
27
28
|
smartAccountPDA?: string;
|
|
28
29
|
/** Webhook URL for async operations (optional) */
|
|
29
30
|
webhookUrl?: string;
|
|
30
31
|
}
|
|
32
|
+
export type DainClientAuthConfig = DainClientAuthConfigJWT | DainClientAuthConfigAPIKey;
|
|
31
33
|
/**
|
|
32
|
-
*
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* Unified DainClientAuth class
|
|
34
|
+
* DainClientAuth - JWT-only authentication for users
|
|
35
|
+
*
|
|
36
|
+
* This class is for CLIENT-SIDE use only (e.g., butterfly-web).
|
|
37
|
+
* Users authenticate with JWT tokens - NO keypairs, NO orgId, NO agentId.
|
|
37
38
|
*
|
|
38
|
-
*
|
|
39
|
+
* For SERVICE-SIDE authentication with keypairs, use the service SDK's built-in auth.
|
|
39
40
|
*/
|
|
40
41
|
export declare class DainClientAuth {
|
|
41
|
-
private authType;
|
|
42
42
|
private jwt?;
|
|
43
|
+
private apiKey?;
|
|
43
44
|
private smartAccountId?;
|
|
44
|
-
private privateKey?;
|
|
45
|
-
private agentId?;
|
|
46
|
-
private orgId?;
|
|
47
|
-
private publicKey?;
|
|
48
45
|
private smartAccountPDA?;
|
|
49
46
|
private webhookUrl?;
|
|
50
47
|
constructor(config: DainClientAuthConfig);
|
|
@@ -54,65 +51,36 @@ export declare class DainClientAuth {
|
|
|
54
51
|
*/
|
|
55
52
|
private decodeJWTPayload;
|
|
56
53
|
/**
|
|
57
|
-
*
|
|
58
|
-
* @private
|
|
59
|
-
*/
|
|
60
|
-
private parseApiKey;
|
|
61
|
-
/**
|
|
62
|
-
* Sign request
|
|
63
|
-
* - JWT mode: returns empty (no signing needed)
|
|
64
|
-
* - Keypair mode: signs with Ed25519
|
|
54
|
+
* Sign request - NOT NEEDED for JWT (returns empty for compatibility)
|
|
65
55
|
*/
|
|
66
|
-
signRequest(
|
|
56
|
+
signRequest(_method: string, _path: string, _body: string): Promise<{
|
|
67
57
|
signature: string;
|
|
68
58
|
timestamp: string;
|
|
69
59
|
}>;
|
|
70
60
|
/**
|
|
71
61
|
* Get headers for HTTP requests
|
|
72
|
-
* - JWT mode: returns Authorization Bearer header
|
|
73
|
-
* - Keypair mode: returns signature headers
|
|
74
|
-
*/
|
|
75
|
-
getHeaders(signature: string, timestamp: string): Record<string, string>;
|
|
76
|
-
/**
|
|
77
|
-
* Sign a message (keypair mode only)
|
|
78
62
|
*/
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Verify a message signature (static method)
|
|
82
|
-
*/
|
|
83
|
-
static verifyMessage(message: string, signature: string, publicKey: Uint8Array): boolean;
|
|
84
|
-
/**
|
|
85
|
-
* Verify SSE event signature (keypair mode only)
|
|
86
|
-
*/
|
|
87
|
-
verifyEventSignature(data: string, signature: string, timestamp: string, publicKeyBase58: string): boolean;
|
|
63
|
+
getHeaders(_signature: string, _timestamp: string): Record<string, string>;
|
|
88
64
|
/**
|
|
89
65
|
* Check if using JWT authentication
|
|
90
66
|
*/
|
|
91
67
|
isJWT(): boolean;
|
|
92
68
|
/**
|
|
93
|
-
*
|
|
69
|
+
* Check if using API Key authentication
|
|
94
70
|
*/
|
|
95
|
-
|
|
71
|
+
isAPIKey(): boolean;
|
|
96
72
|
/**
|
|
97
|
-
* Get
|
|
73
|
+
* Get JWT token
|
|
98
74
|
*/
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Get agent ID (keypair mode only)
|
|
102
|
-
*/
|
|
103
|
-
getAgentId(): string | undefined;
|
|
104
|
-
/**
|
|
105
|
-
* Get org ID (keypair mode only)
|
|
106
|
-
*/
|
|
107
|
-
getOrgId(): string | undefined;
|
|
75
|
+
getJWT(): string | undefined;
|
|
108
76
|
/**
|
|
109
|
-
* Get
|
|
77
|
+
* Get API Key
|
|
110
78
|
*/
|
|
111
|
-
|
|
79
|
+
getAPIKey(): string | undefined;
|
|
112
80
|
/**
|
|
113
|
-
* Get
|
|
81
|
+
* Get smart account ID
|
|
114
82
|
*/
|
|
115
|
-
|
|
83
|
+
getSmartAccountId(): string | undefined;
|
|
116
84
|
/**
|
|
117
85
|
* Get smart account PDA
|
|
118
86
|
*/
|
|
@@ -121,6 +89,11 @@ export declare class DainClientAuth {
|
|
|
121
89
|
* Get webhook URL
|
|
122
90
|
*/
|
|
123
91
|
getWebhookUrl(): string | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* Verify SSE event signature from service
|
|
94
|
+
* (Services sign their event responses, users verify them)
|
|
95
|
+
*/
|
|
96
|
+
verifyEventSignature(data: string, signature: string, timestamp: string, publicKeyBase58: string): boolean;
|
|
124
97
|
/**
|
|
125
98
|
* Serialize auth config
|
|
126
99
|
*/
|
|
@@ -1,51 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DainClientAuth = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
2
|
//File: src/client/client-auth.ts
|
|
6
3
|
/**
|
|
7
|
-
*
|
|
4
|
+
* Client Authentication for DAIN Services
|
|
8
5
|
*
|
|
9
6
|
* Supports two authentication modes:
|
|
10
|
-
* 1. JWT
|
|
11
|
-
* 2.
|
|
12
|
-
*/
|
|
13
|
-
const ed25519_1 = require("@noble/curves/ed25519");
|
|
14
|
-
const sha256_1 = require("@noble/hashes/sha256");
|
|
15
|
-
const utils_1 = require("@noble/hashes/utils");
|
|
16
|
-
const bs58_1 = tslib_1.__importDefault(require("bs58"));
|
|
17
|
-
/**
|
|
18
|
-
* Type guard to check if config is JWT
|
|
7
|
+
* 1. JWT - For end users (browser-side), NO keypairs/orgId/agentId
|
|
8
|
+
* 2. API Key - For server-to-server auth (Node.js server-side only)
|
|
19
9
|
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.DainClientAuth = void 0;
|
|
20
12
|
function isJWTConfig(config) {
|
|
21
|
-
return 'jwt' in config
|
|
13
|
+
return 'jwt' in config;
|
|
14
|
+
}
|
|
15
|
+
function isAPIKeyConfig(config) {
|
|
16
|
+
return 'apiKey' in config;
|
|
22
17
|
}
|
|
23
18
|
/**
|
|
24
|
-
*
|
|
19
|
+
* DainClientAuth - JWT-only authentication for users
|
|
20
|
+
*
|
|
21
|
+
* This class is for CLIENT-SIDE use only (e.g., butterfly-web).
|
|
22
|
+
* Users authenticate with JWT tokens - NO keypairs, NO orgId, NO agentId.
|
|
25
23
|
*
|
|
26
|
-
*
|
|
24
|
+
* For SERVICE-SIDE authentication with keypairs, use the service SDK's built-in auth.
|
|
27
25
|
*/
|
|
28
26
|
class DainClientAuth {
|
|
29
|
-
// Auth type
|
|
30
|
-
authType;
|
|
31
|
-
// JWT fields (users only)
|
|
32
27
|
jwt;
|
|
28
|
+
apiKey;
|
|
33
29
|
smartAccountId;
|
|
34
|
-
// Keypair fields (services/agents only)
|
|
35
|
-
privateKey;
|
|
36
|
-
agentId;
|
|
37
|
-
orgId;
|
|
38
|
-
publicKey;
|
|
39
|
-
// Shared fields
|
|
40
30
|
smartAccountPDA;
|
|
41
31
|
webhookUrl;
|
|
42
32
|
constructor(config) {
|
|
43
33
|
if (isJWTConfig(config)) {
|
|
44
|
-
// JWT Authentication (Users)
|
|
45
|
-
this.authType = 'jwt';
|
|
46
|
-
if (!config.jwt) {
|
|
47
|
-
throw new Error('JWT token is required for user authentication');
|
|
48
|
-
}
|
|
34
|
+
// JWT Authentication (End Users)
|
|
49
35
|
this.jwt = config.jwt;
|
|
50
36
|
// Extract smartAccountId from config or decode from JWT
|
|
51
37
|
if (config.smartAccountId) {
|
|
@@ -58,28 +44,15 @@ class DainClientAuth {
|
|
|
58
44
|
this.smartAccountPDA = config.smartAccountPDA;
|
|
59
45
|
this.webhookUrl = config.webhookUrl;
|
|
60
46
|
}
|
|
61
|
-
else {
|
|
62
|
-
//
|
|
63
|
-
this.
|
|
64
|
-
if (config.apiKey) {
|
|
65
|
-
const { privateKey, agentId, orgId, publicKey } = this.parseApiKey(config.apiKey);
|
|
66
|
-
this.privateKey = privateKey;
|
|
67
|
-
this.agentId = agentId;
|
|
68
|
-
this.orgId = orgId;
|
|
69
|
-
this.publicKey = publicKey;
|
|
70
|
-
}
|
|
71
|
-
else if (config.privateKeyBase58 && config.agentId && config.orgId) {
|
|
72
|
-
this.privateKey = bs58_1.default.decode(config.privateKeyBase58);
|
|
73
|
-
this.agentId = config.agentId.replace('agent_', '');
|
|
74
|
-
this.orgId = config.orgId.replace('org_', '');
|
|
75
|
-
this.publicKey = ed25519_1.ed25519.getPublicKey(this.privateKey);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
throw new Error('Invalid configuration. Provide either jwt (for users) or apiKey/privateKeyBase58+agentId+orgId (for services/agents).');
|
|
79
|
-
}
|
|
47
|
+
else if (isAPIKeyConfig(config)) {
|
|
48
|
+
// API Key Authentication (Server-to-Server)
|
|
49
|
+
this.apiKey = config.apiKey;
|
|
80
50
|
this.smartAccountPDA = config.smartAccountPDA;
|
|
81
51
|
this.webhookUrl = config.webhookUrl;
|
|
82
52
|
}
|
|
53
|
+
else {
|
|
54
|
+
throw new Error('Invalid authentication config - must provide either jwt or apiKey');
|
|
55
|
+
}
|
|
83
56
|
}
|
|
84
57
|
/**
|
|
85
58
|
* Decode JWT payload (without verification)
|
|
@@ -94,143 +67,62 @@ class DainClientAuth {
|
|
|
94
67
|
return JSON.parse(payload);
|
|
95
68
|
}
|
|
96
69
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @private
|
|
70
|
+
* Sign request - NOT NEEDED for JWT (returns empty for compatibility)
|
|
99
71
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (parts.length !== 5 || parts[0] !== 'sk' || parts[1] !== 'agent') {
|
|
103
|
-
throw new Error('Invalid API key format');
|
|
104
|
-
}
|
|
105
|
-
const orgId = parts[2].replace('org_', '');
|
|
106
|
-
const agentId = parts[3].replace('agent_', '');
|
|
107
|
-
const privateKeyBase58 = parts[4];
|
|
108
|
-
const privateKey = bs58_1.default.decode(privateKeyBase58).slice(0, 32);
|
|
109
|
-
const publicKey = bs58_1.default.decode(privateKeyBase58).slice(32);
|
|
110
|
-
return { privateKey, agentId, orgId, publicKey };
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Sign request
|
|
114
|
-
* - JWT mode: returns empty (no signing needed)
|
|
115
|
-
* - Keypair mode: signs with Ed25519
|
|
116
|
-
*/
|
|
117
|
-
async signRequest(method, path, body) {
|
|
118
|
-
if (this.authType === 'jwt') {
|
|
119
|
-
// JWT doesn't need request signing
|
|
120
|
-
return { signature: '', timestamp: '' };
|
|
121
|
-
}
|
|
122
|
-
// Keypair signing for services/agents
|
|
123
|
-
const timestamp = Date.now().toString();
|
|
124
|
-
const message = `${method}:${path}:${timestamp}:${body}`;
|
|
125
|
-
const messageHash = (0, sha256_1.sha256)(message);
|
|
126
|
-
const signature = ed25519_1.ed25519.sign(messageHash, this.privateKey);
|
|
127
|
-
return { signature: (0, utils_1.bytesToHex)(signature), timestamp };
|
|
72
|
+
async signRequest(_method, _path, _body) {
|
|
73
|
+
return { signature: '', timestamp: '' };
|
|
128
74
|
}
|
|
129
75
|
/**
|
|
130
76
|
* Get headers for HTTP requests
|
|
131
|
-
* - JWT mode: returns Authorization Bearer header
|
|
132
|
-
* - Keypair mode: returns signature headers
|
|
133
77
|
*/
|
|
134
|
-
getHeaders(
|
|
135
|
-
const
|
|
136
|
-
if (this.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (this.webhookUrl) {
|
|
140
|
-
baseHeaders["X-DAIN-WEBHOOK-URL"] = this.webhookUrl;
|
|
78
|
+
getHeaders(_signature, _timestamp) {
|
|
79
|
+
const headers = {};
|
|
80
|
+
if (this.jwt) {
|
|
81
|
+
// JWT Authentication
|
|
82
|
+
headers["Authorization"] = `Bearer ${this.jwt}`;
|
|
141
83
|
}
|
|
142
|
-
if (this.
|
|
143
|
-
//
|
|
144
|
-
|
|
145
|
-
...baseHeaders,
|
|
146
|
-
"Authorization": `Bearer ${this.jwt}`,
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
// Keypair authentication headers
|
|
150
|
-
return {
|
|
151
|
-
...baseHeaders,
|
|
152
|
-
"X-DAIN-SIGNATURE": signature,
|
|
153
|
-
"X-DAIN-TIMESTAMP": timestamp,
|
|
154
|
-
"X-DAIN-AGENT-ID": this.agentId,
|
|
155
|
-
"X-DAIN-ORG-ID": this.orgId,
|
|
156
|
-
"X-DAIN-ADDRESS": bs58_1.default.encode(this.publicKey),
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Sign a message (keypair mode only)
|
|
161
|
-
*/
|
|
162
|
-
signMessage(message) {
|
|
163
|
-
if (this.authType === 'jwt') {
|
|
164
|
-
throw new Error('signMessage not available in JWT mode');
|
|
84
|
+
else if (this.apiKey) {
|
|
85
|
+
// API Key Authentication
|
|
86
|
+
headers["X-DAIN-API-KEY"] = this.apiKey;
|
|
165
87
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return (0, utils_1.bytesToHex)(signature);
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Verify a message signature (static method)
|
|
172
|
-
*/
|
|
173
|
-
static verifyMessage(message, signature, publicKey) {
|
|
174
|
-
const messageHash = (0, sha256_1.sha256)(message);
|
|
175
|
-
return ed25519_1.ed25519.verify(signature, messageHash, publicKey);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Verify SSE event signature (keypair mode only)
|
|
179
|
-
*/
|
|
180
|
-
verifyEventSignature(data, signature, timestamp, publicKeyBase58) {
|
|
181
|
-
try {
|
|
182
|
-
const message = `${data}:${timestamp}`;
|
|
183
|
-
const messageHash = (0, sha256_1.sha256)(message);
|
|
184
|
-
const publicKey = bs58_1.default.decode(publicKeyBase58);
|
|
185
|
-
return ed25519_1.ed25519.verify(signature, messageHash, publicKey);
|
|
88
|
+
if (this.smartAccountPDA) {
|
|
89
|
+
headers["X-DAIN-SMART-ACCOUNT-PDA"] = this.smartAccountPDA;
|
|
186
90
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
return false;
|
|
91
|
+
if (this.webhookUrl) {
|
|
92
|
+
headers["X-DAIN-WEBHOOK-URL"] = this.webhookUrl;
|
|
190
93
|
}
|
|
94
|
+
return headers;
|
|
191
95
|
}
|
|
192
96
|
// ===== Getter Methods =====
|
|
193
97
|
/**
|
|
194
98
|
* Check if using JWT authentication
|
|
195
99
|
*/
|
|
196
100
|
isJWT() {
|
|
197
|
-
return this.
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Get JWT token (JWT mode only)
|
|
201
|
-
*/
|
|
202
|
-
getJWT() {
|
|
203
|
-
return this.jwt;
|
|
101
|
+
return !!this.jwt;
|
|
204
102
|
}
|
|
205
103
|
/**
|
|
206
|
-
*
|
|
104
|
+
* Check if using API Key authentication
|
|
207
105
|
*/
|
|
208
|
-
|
|
209
|
-
return this.
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Get agent ID (keypair mode only)
|
|
213
|
-
*/
|
|
214
|
-
getAgentId() {
|
|
215
|
-
return this.agentId;
|
|
106
|
+
isAPIKey() {
|
|
107
|
+
return !!this.apiKey;
|
|
216
108
|
}
|
|
217
109
|
/**
|
|
218
|
-
* Get
|
|
110
|
+
* Get JWT token
|
|
219
111
|
*/
|
|
220
|
-
|
|
221
|
-
return this.
|
|
112
|
+
getJWT() {
|
|
113
|
+
return this.jwt;
|
|
222
114
|
}
|
|
223
115
|
/**
|
|
224
|
-
* Get
|
|
116
|
+
* Get API Key
|
|
225
117
|
*/
|
|
226
|
-
|
|
227
|
-
return this.
|
|
118
|
+
getAPIKey() {
|
|
119
|
+
return this.apiKey;
|
|
228
120
|
}
|
|
229
121
|
/**
|
|
230
|
-
* Get
|
|
122
|
+
* Get smart account ID
|
|
231
123
|
*/
|
|
232
|
-
|
|
233
|
-
return this.
|
|
124
|
+
getSmartAccountId() {
|
|
125
|
+
return this.smartAccountId;
|
|
234
126
|
}
|
|
235
127
|
/**
|
|
236
128
|
* Get smart account PDA
|
|
@@ -244,12 +136,33 @@ class DainClientAuth {
|
|
|
244
136
|
getWebhookUrl() {
|
|
245
137
|
return this.webhookUrl;
|
|
246
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Verify SSE event signature from service
|
|
141
|
+
* (Services sign their event responses, users verify them)
|
|
142
|
+
*/
|
|
143
|
+
verifyEventSignature(data, signature, timestamp, publicKeyBase58) {
|
|
144
|
+
try {
|
|
145
|
+
// This is for verifying SERVICE signatures, not user signatures
|
|
146
|
+
// Import crypto libs only when needed
|
|
147
|
+
const { ed25519 } = require("@noble/curves/ed25519");
|
|
148
|
+
const { sha256 } = require("@noble/hashes/sha256");
|
|
149
|
+
const bs58 = require("bs58");
|
|
150
|
+
const message = `${data}:${timestamp}`;
|
|
151
|
+
const messageHash = sha256(message);
|
|
152
|
+
const publicKey = bs58.decode(publicKeyBase58);
|
|
153
|
+
return ed25519.verify(signature, messageHash, publicKey);
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
console.error('Error verifying event signature:', error);
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
247
160
|
// ===== Serialization =====
|
|
248
161
|
/**
|
|
249
162
|
* Serialize auth config
|
|
250
163
|
*/
|
|
251
164
|
serialize() {
|
|
252
|
-
if (this.
|
|
165
|
+
if (this.jwt) {
|
|
253
166
|
const data = {
|
|
254
167
|
authMethod: 'jwt',
|
|
255
168
|
jwt: this.jwt,
|
|
@@ -259,51 +172,41 @@ class DainClientAuth {
|
|
|
259
172
|
};
|
|
260
173
|
return Buffer.from(JSON.stringify(data)).toString('base64');
|
|
261
174
|
}
|
|
262
|
-
else {
|
|
175
|
+
else if (this.apiKey) {
|
|
263
176
|
const data = {
|
|
264
|
-
authMethod: '
|
|
265
|
-
|
|
266
|
-
agentId: this.agentId,
|
|
267
|
-
orgId: this.orgId,
|
|
268
|
-
publicKey: Array.from(this.publicKey),
|
|
177
|
+
authMethod: 'apiKey',
|
|
178
|
+
apiKey: this.apiKey,
|
|
269
179
|
smartAccountPDA: this.smartAccountPDA,
|
|
270
180
|
webhookUrl: this.webhookUrl
|
|
271
181
|
};
|
|
272
|
-
return
|
|
182
|
+
return Buffer.from(JSON.stringify(data)).toString('base64');
|
|
273
183
|
}
|
|
184
|
+
throw new Error('No authentication method available');
|
|
274
185
|
}
|
|
275
186
|
/**
|
|
276
187
|
* Deserialize auth config
|
|
277
188
|
*/
|
|
278
189
|
static deserialize(serialized) {
|
|
279
190
|
try {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
webhookUrl: data.webhookUrl
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
catch {
|
|
293
|
-
// Not JWT format, try keypair
|
|
191
|
+
const data = JSON.parse(Buffer.from(serialized, 'base64').toString());
|
|
192
|
+
if (data.authMethod === 'jwt') {
|
|
193
|
+
return new DainClientAuth({
|
|
194
|
+
jwt: data.jwt,
|
|
195
|
+
smartAccountId: data.smartAccountId,
|
|
196
|
+
smartAccountPDA: data.smartAccountPDA,
|
|
197
|
+
webhookUrl: data.webhookUrl
|
|
198
|
+
});
|
|
294
199
|
}
|
|
295
|
-
|
|
296
|
-
const data = JSON.parse(Buffer.from(bs58_1.default.decode(serialized)).toString());
|
|
297
|
-
if (data.authMethod === 'keypair') {
|
|
200
|
+
else if (data.authMethod === 'apiKey') {
|
|
298
201
|
return new DainClientAuth({
|
|
299
|
-
|
|
300
|
-
agentId: data.agentId,
|
|
301
|
-
orgId: data.orgId,
|
|
202
|
+
apiKey: data.apiKey,
|
|
302
203
|
smartAccountPDA: data.smartAccountPDA,
|
|
303
204
|
webhookUrl: data.webhookUrl
|
|
304
205
|
});
|
|
305
206
|
}
|
|
306
|
-
|
|
207
|
+
else {
|
|
208
|
+
throw new Error('Invalid auth method - must be jwt or apiKey');
|
|
209
|
+
}
|
|
307
210
|
}
|
|
308
211
|
catch (error) {
|
|
309
212
|
throw new Error('Failed to deserialize DainClientAuth: ' + error.message);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-auth.js","sourceRoot":"","sources":["../../src/client/client-auth.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client-auth.js","sourceRoot":"","sources":["../../src/client/client-auth.ts"],"names":[],"mappings":";AAAA,iCAAiC;AACjC;;;;;;GAMG;;;AAwCH,SAAS,WAAW,CAAC,MAA4B;IAC/C,OAAO,KAAK,IAAI,MAAM,CAAC;AACzB,CAAC;AAED,SAAS,cAAc,CAAC,MAA4B;IAClD,OAAO,QAAQ,IAAI,MAAM,CAAC;AAC5B,CAAC;AAED;;;;;;;GAOG;AACH,MAAa,cAAc;IACjB,GAAG,CAAU;IACb,MAAM,CAAU;IAChB,cAAc,CAAU;IACxB,eAAe,CAAU;IACzB,UAAU,CAAU;IAE5B,YAAY,MAA4B;QACtC,IAAI,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;YACxB,iCAAiC;YACjC,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;YAEtB,wDAAwD;YACxD,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;gBAC1B,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;YAC9C,CAAC;iBAAM,CAAC;gBACN,MAAM,OAAO,GAAG,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBAClD,IAAI,CAAC,cAAc,GAAG,OAAO,CAAC,gBAAgB,IAAI,OAAO,CAAC,GAAG,CAAC;YAChE,CAAC;YAED,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;YAC9C,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QACtC,CAAC;aAAM,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAClC,4CAA4C;YAC5C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YAC5B,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;YAC9C,IAA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