@moltdm/client 1.0.1 → 1.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/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +35 -0
- package/dist/index.mjs +35 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -197,6 +197,18 @@ declare class MoltDMClient {
|
|
|
197
197
|
private encrypt;
|
|
198
198
|
decrypt(ciphertext: string, key: Uint8Array): Promise<string>;
|
|
199
199
|
private ensureInitialized;
|
|
200
|
+
/**
|
|
201
|
+
* Sign a message using Ed25519
|
|
202
|
+
*/
|
|
203
|
+
private signMessage;
|
|
204
|
+
/**
|
|
205
|
+
* Create the message to sign for a request
|
|
206
|
+
* Format: timestamp:method:path:bodyHash
|
|
207
|
+
*/
|
|
208
|
+
private createSignedMessage;
|
|
209
|
+
/**
|
|
210
|
+
* Make an authenticated fetch request with Ed25519 signature
|
|
211
|
+
*/
|
|
200
212
|
private fetch;
|
|
201
213
|
}
|
|
202
214
|
|
package/dist/index.d.ts
CHANGED
|
@@ -197,6 +197,18 @@ declare class MoltDMClient {
|
|
|
197
197
|
private encrypt;
|
|
198
198
|
decrypt(ciphertext: string, key: Uint8Array): Promise<string>;
|
|
199
199
|
private ensureInitialized;
|
|
200
|
+
/**
|
|
201
|
+
* Sign a message using Ed25519
|
|
202
|
+
*/
|
|
203
|
+
private signMessage;
|
|
204
|
+
/**
|
|
205
|
+
* Create the message to sign for a request
|
|
206
|
+
* Format: timestamp:method:path:bodyHash
|
|
207
|
+
*/
|
|
208
|
+
private createSignedMessage;
|
|
209
|
+
/**
|
|
210
|
+
* Make an authenticated fetch request with Ed25519 signature
|
|
211
|
+
*/
|
|
200
212
|
private fetch;
|
|
201
213
|
}
|
|
202
214
|
|
package/dist/index.js
CHANGED
|
@@ -507,12 +507,47 @@ var MoltDMClient = class {
|
|
|
507
507
|
throw new Error("Not initialized. Call initialize() first.");
|
|
508
508
|
}
|
|
509
509
|
}
|
|
510
|
+
/**
|
|
511
|
+
* Sign a message using Ed25519
|
|
512
|
+
*/
|
|
513
|
+
async signMessage(message) {
|
|
514
|
+
const privateKeyBytes = fromBase64(this.identity.privateKey);
|
|
515
|
+
const signature = await ed.signAsync(
|
|
516
|
+
new TextEncoder().encode(message),
|
|
517
|
+
privateKeyBytes
|
|
518
|
+
);
|
|
519
|
+
return toBase64(signature);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Create the message to sign for a request
|
|
523
|
+
* Format: timestamp:method:path:bodyHash
|
|
524
|
+
*/
|
|
525
|
+
async createSignedMessage(timestamp, method, path2, body) {
|
|
526
|
+
let bodyHash = "";
|
|
527
|
+
if (body) {
|
|
528
|
+
const bodyBytes = new TextEncoder().encode(body);
|
|
529
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", bodyBytes);
|
|
530
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
531
|
+
bodyHash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
532
|
+
}
|
|
533
|
+
return `${timestamp}:${method}:${path2}:${bodyHash}`;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Make an authenticated fetch request with Ed25519 signature
|
|
537
|
+
*/
|
|
510
538
|
async fetch(path2, options = {}) {
|
|
539
|
+
const method = options.method || "GET";
|
|
540
|
+
const body = options.body;
|
|
541
|
+
const timestamp = Date.now().toString();
|
|
542
|
+
const message = await this.createSignedMessage(timestamp, method, path2, body);
|
|
543
|
+
const signature = await this.signMessage(message);
|
|
511
544
|
const response = await fetch(`${this.relayUrl}${path2}`, {
|
|
512
545
|
...options,
|
|
513
546
|
headers: {
|
|
514
547
|
"Content-Type": "application/json",
|
|
515
548
|
"X-Moltbot-Id": this.identity.moltbotId,
|
|
549
|
+
"X-Timestamp": timestamp,
|
|
550
|
+
"X-Signature": signature,
|
|
516
551
|
...options.headers
|
|
517
552
|
}
|
|
518
553
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -472,12 +472,47 @@ var MoltDMClient = class {
|
|
|
472
472
|
throw new Error("Not initialized. Call initialize() first.");
|
|
473
473
|
}
|
|
474
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Sign a message using Ed25519
|
|
477
|
+
*/
|
|
478
|
+
async signMessage(message) {
|
|
479
|
+
const privateKeyBytes = fromBase64(this.identity.privateKey);
|
|
480
|
+
const signature = await ed.signAsync(
|
|
481
|
+
new TextEncoder().encode(message),
|
|
482
|
+
privateKeyBytes
|
|
483
|
+
);
|
|
484
|
+
return toBase64(signature);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Create the message to sign for a request
|
|
488
|
+
* Format: timestamp:method:path:bodyHash
|
|
489
|
+
*/
|
|
490
|
+
async createSignedMessage(timestamp, method, path2, body) {
|
|
491
|
+
let bodyHash = "";
|
|
492
|
+
if (body) {
|
|
493
|
+
const bodyBytes = new TextEncoder().encode(body);
|
|
494
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", bodyBytes);
|
|
495
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
496
|
+
bodyHash = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
497
|
+
}
|
|
498
|
+
return `${timestamp}:${method}:${path2}:${bodyHash}`;
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Make an authenticated fetch request with Ed25519 signature
|
|
502
|
+
*/
|
|
475
503
|
async fetch(path2, options = {}) {
|
|
504
|
+
const method = options.method || "GET";
|
|
505
|
+
const body = options.body;
|
|
506
|
+
const timestamp = Date.now().toString();
|
|
507
|
+
const message = await this.createSignedMessage(timestamp, method, path2, body);
|
|
508
|
+
const signature = await this.signMessage(message);
|
|
476
509
|
const response = await fetch(`${this.relayUrl}${path2}`, {
|
|
477
510
|
...options,
|
|
478
511
|
headers: {
|
|
479
512
|
"Content-Type": "application/json",
|
|
480
513
|
"X-Moltbot-Id": this.identity.moltbotId,
|
|
514
|
+
"X-Timestamp": timestamp,
|
|
515
|
+
"X-Signature": signature,
|
|
481
516
|
...options.headers
|
|
482
517
|
}
|
|
483
518
|
});
|