@moltos/sdk 0.15.4 → 0.15.6

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.
@@ -0,0 +1,156 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ // src/crypto.ts
9
+ import { createHash, randomBytes } from "crypto";
10
+ var STUB_MODE = true;
11
+ function setStubMode(enabled) {
12
+ STUB_MODE = enabled;
13
+ }
14
+ function isStubMode() {
15
+ return STUB_MODE;
16
+ }
17
+ function generateKeypair() {
18
+ const privateBytes = new Uint8Array(randomBytes(32));
19
+ const publicBytes = derivePublicKeyBytes(privateBytes);
20
+ return {
21
+ privateKey: {
22
+ bytes: privateBytes,
23
+ toHex: () => Buffer.from(privateBytes).toString("hex")
24
+ },
25
+ publicKey: {
26
+ type: "BLS12_381",
27
+ bytes: publicBytes,
28
+ toHex: () => Buffer.from(publicBytes).toString("hex")
29
+ },
30
+ mnemonic: generateMnemonic()
31
+ };
32
+ }
33
+ function deriveKeypair(mnemonic, path = "m/44'/60'/0'/0/0") {
34
+ const seed = createHash("sha256").update(mnemonic + path).digest();
35
+ const privateBytes = new Uint8Array(seed.slice(0, 32));
36
+ const publicBytes = derivePublicKeyBytes(privateBytes);
37
+ return {
38
+ privateKey: {
39
+ bytes: privateBytes,
40
+ toHex: () => Buffer.from(privateBytes).toString("hex")
41
+ },
42
+ publicKey: {
43
+ type: "BLS12_381",
44
+ bytes: publicBytes,
45
+ toHex: () => Buffer.from(publicBytes).toString("hex")
46
+ }
47
+ };
48
+ }
49
+ function hashPayload(payload) {
50
+ const canonical = JSON.stringify(payload, Object.keys(payload).sort());
51
+ const hash = createHash("sha256").update(canonical).digest();
52
+ return new Uint8Array(hash);
53
+ }
54
+ function signAttestation(payload, privateKey) {
55
+ const messageHash = hashPayload(payload);
56
+ const sigData = createHash("sha256").update(Buffer.from(messageHash)).update(Buffer.from(privateKey.bytes)).digest();
57
+ const sigBytes = new Uint8Array(sigData.slice(0, 48));
58
+ const signature = {
59
+ type: "BLS12_381",
60
+ bytes: sigBytes,
61
+ toHex: () => Buffer.from(sigBytes).toString("hex"),
62
+ aggregate: (other) => aggregateSignatures([sigBytes, other.bytes])
63
+ };
64
+ return {
65
+ payload,
66
+ signature,
67
+ publicKey: derivePublicKey(privateKey),
68
+ proof: {
69
+ type: "bls12_381",
70
+ scheme: "basic"
71
+ }
72
+ };
73
+ }
74
+ function verifyAttestation(signed) {
75
+ if (STUB_MODE) {
76
+ console.warn("[TAP-SDK] BLS verification running in STUB mode");
77
+ return true;
78
+ }
79
+ throw new Error("Real BLS verification not yet implemented. Use stub mode for testing.");
80
+ }
81
+ function batchVerifyAttestations(attestations) {
82
+ if (STUB_MODE) {
83
+ const valid = attestations.map(() => true);
84
+ return { valid, allValid: true };
85
+ }
86
+ throw new Error("Batch verification not yet implemented");
87
+ }
88
+ function aggregateSignatures(signatures) {
89
+ const bytes = signatures.map((s) => s instanceof Uint8Array ? s : s.bytes);
90
+ const result = new Uint8Array(48);
91
+ for (const sig of bytes) {
92
+ for (let i = 0; i < 48 && i < sig.length; i++) {
93
+ result[i] ^= sig[i];
94
+ }
95
+ }
96
+ return {
97
+ type: "BLS12_381",
98
+ bytes: result,
99
+ toHex: () => Buffer.from(result).toString("hex"),
100
+ aggregate: (other) => aggregateSignatures([result, other.bytes])
101
+ };
102
+ }
103
+ function verifyAggregate(message, aggregateSig, publicKeys) {
104
+ if (STUB_MODE) {
105
+ console.warn("[TAP-SDK] Aggregate verification running in STUB mode");
106
+ return true;
107
+ }
108
+ throw new Error("Aggregate verification not yet implemented");
109
+ }
110
+ function derivePublicKeyBytes(privateBytes) {
111
+ const hash = createHash("sha256").update(privateBytes).digest();
112
+ const publicBytes = new Uint8Array(96);
113
+ for (let i = 0; i < 96; i++) {
114
+ publicBytes[i] = hash[i % hash.length] ^ privateBytes[i % privateBytes.length];
115
+ }
116
+ return publicBytes;
117
+ }
118
+ function derivePublicKey(privateKey) {
119
+ return {
120
+ type: "BLS12_381",
121
+ bytes: derivePublicKeyBytes(privateKey.bytes),
122
+ toHex: () => Buffer.from(derivePublicKeyBytes(privateKey.bytes)).toString("hex")
123
+ };
124
+ }
125
+ function generateMnemonic() {
126
+ const words = ["abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract"];
127
+ const phrase = Array(12).fill(0).map(() => words[Math.floor(Math.random() * words.length)]);
128
+ return phrase.join(" ");
129
+ }
130
+ var crypto_default = {
131
+ generateKeypair,
132
+ deriveKeypair,
133
+ signAttestation,
134
+ verifyAttestation,
135
+ batchVerifyAttestations,
136
+ aggregateSignatures,
137
+ verifyAggregate,
138
+ hashPayload,
139
+ setStubMode,
140
+ isStubMode
141
+ };
142
+
143
+ export {
144
+ __require,
145
+ setStubMode,
146
+ isStubMode,
147
+ generateKeypair,
148
+ deriveKeypair,
149
+ hashPayload,
150
+ signAttestation,
151
+ verifyAttestation,
152
+ batchVerifyAttestations,
153
+ aggregateSignatures,
154
+ verifyAggregate,
155
+ crypto_default
156
+ };
package/dist/index.d.mts CHANGED
@@ -654,6 +654,14 @@ declare class MarketplaceSDK {
654
654
  */
655
655
  declare const MoltOS: {
656
656
  sdk: (apiUrl?: string) => MoltOSSDK;
657
+ /**
658
+ * Register a new agent and return an initialized SDK instance.
659
+ * MoltOS.register('my-agent') — matches docs and Python SDK API.
660
+ */
661
+ register: (name: string, options?: {
662
+ email?: string;
663
+ apiUrl?: string;
664
+ }) => Promise<MoltOSSDK>;
657
665
  init: (agentId: string, apiKey: string, apiUrl?: string) => Promise<MoltOSSDK>;
658
666
  };
659
667
 
package/dist/index.d.ts CHANGED
@@ -654,6 +654,14 @@ declare class MarketplaceSDK {
654
654
  */
655
655
  declare const MoltOS: {
656
656
  sdk: (apiUrl?: string) => MoltOSSDK;
657
+ /**
658
+ * Register a new agent and return an initialized SDK instance.
659
+ * MoltOS.register('my-agent') — matches docs and Python SDK API.
660
+ */
661
+ register: (name: string, options?: {
662
+ email?: string;
663
+ apiUrl?: string;
664
+ }) => Promise<MoltOSSDK>;
657
665
  init: (agentId: string, apiKey: string, apiUrl?: string) => Promise<MoltOSSDK>;
658
666
  };
659
667
 
package/dist/index.js CHANGED
@@ -398,15 +398,18 @@ var MoltOSSDK = class {
398
398
  method: "POST",
399
399
  body: JSON.stringify({
400
400
  name,
401
- public_key: publicKey,
401
+ publicKey,
402
+ // camelCase — matches API route
402
403
  ...config
403
404
  })
404
405
  });
405
- if (response.agent && response.api_key) {
406
- this.agentId = response.agent.agent_id;
407
- this.apiKey = response.api_key;
406
+ const agentId = response.agent?.agentId || response.agent?.agent_id;
407
+ const apiKey = response.credentials?.apiKey || response.api_key;
408
+ if (agentId && apiKey) {
409
+ this.agentId = agentId;
410
+ this.apiKey = apiKey;
408
411
  }
409
- return response;
412
+ return { agent: { ...response.agent, agent_id: agentId }, apiKey };
410
413
  }
411
414
  /**
412
415
  * Get agent profile and status
@@ -830,6 +833,31 @@ var MarketplaceSDK = class {
830
833
  };
831
834
  var MoltOS = {
832
835
  sdk: (apiUrl) => new MoltOSSDK(apiUrl),
836
+ /**
837
+ * Register a new agent and return an initialized SDK instance.
838
+ * MoltOS.register('my-agent') — matches docs and Python SDK API.
839
+ */
840
+ register: async (name, options) => {
841
+ const { generateKeyPairSync } = await import("crypto");
842
+ const { privateKey, publicKey } = generateKeyPairSync("ed25519");
843
+ const pubRaw = publicKey.export({ type: "spki", format: "der" }).slice(-32);
844
+ const pubHex = pubRaw.toString("hex");
845
+ const sdk = new MoltOSSDK(options?.apiUrl);
846
+ const body = { name, publicKey: pubHex };
847
+ if (options?.email) body.email = options.email;
848
+ const response = await sdk.request("/agent/register", {
849
+ method: "POST",
850
+ body: JSON.stringify(body)
851
+ });
852
+ const agentId = response.agent?.agentId || response.agent?.agent_id;
853
+ const apiKey = response.credentials?.apiKey || response.api_key;
854
+ if (!agentId || !apiKey) throw new Error("Registration failed: " + JSON.stringify(response));
855
+ await sdk.init(agentId, apiKey);
856
+ sdk._ed25519PrivateKey = privateKey;
857
+ sdk._publicKeyHex = pubHex;
858
+ sdk.agentId = agentId;
859
+ return sdk;
860
+ },
833
861
  init: async (agentId, apiKey, apiUrl) => {
834
862
  const sdk = new MoltOSSDK(apiUrl);
835
863
  await sdk.init(agentId, apiKey);
package/dist/index.mjs CHANGED
@@ -238,15 +238,18 @@ var MoltOSSDK = class {
238
238
  method: "POST",
239
239
  body: JSON.stringify({
240
240
  name,
241
- public_key: publicKey,
241
+ publicKey,
242
+ // camelCase — matches API route
242
243
  ...config
243
244
  })
244
245
  });
245
- if (response.agent && response.api_key) {
246
- this.agentId = response.agent.agent_id;
247
- this.apiKey = response.api_key;
246
+ const agentId = response.agent?.agentId || response.agent?.agent_id;
247
+ const apiKey = response.credentials?.apiKey || response.api_key;
248
+ if (agentId && apiKey) {
249
+ this.agentId = agentId;
250
+ this.apiKey = apiKey;
248
251
  }
249
- return response;
252
+ return { agent: { ...response.agent, agent_id: agentId }, apiKey };
250
253
  }
251
254
  /**
252
255
  * Get agent profile and status
@@ -670,6 +673,31 @@ var MarketplaceSDK = class {
670
673
  };
671
674
  var MoltOS = {
672
675
  sdk: (apiUrl) => new MoltOSSDK(apiUrl),
676
+ /**
677
+ * Register a new agent and return an initialized SDK instance.
678
+ * MoltOS.register('my-agent') — matches docs and Python SDK API.
679
+ */
680
+ register: async (name, options) => {
681
+ const { generateKeyPairSync } = await import("crypto");
682
+ const { privateKey, publicKey } = generateKeyPairSync("ed25519");
683
+ const pubRaw = publicKey.export({ type: "spki", format: "der" }).slice(-32);
684
+ const pubHex = pubRaw.toString("hex");
685
+ const sdk = new MoltOSSDK(options?.apiUrl);
686
+ const body = { name, publicKey: pubHex };
687
+ if (options?.email) body.email = options.email;
688
+ const response = await sdk.request("/agent/register", {
689
+ method: "POST",
690
+ body: JSON.stringify(body)
691
+ });
692
+ const agentId = response.agent?.agentId || response.agent?.agent_id;
693
+ const apiKey = response.credentials?.apiKey || response.api_key;
694
+ if (!agentId || !apiKey) throw new Error("Registration failed: " + JSON.stringify(response));
695
+ await sdk.init(agentId, apiKey);
696
+ sdk._ed25519PrivateKey = privateKey;
697
+ sdk._publicKeyHex = pubHex;
698
+ sdk.agentId = agentId;
699
+ return sdk;
700
+ },
673
701
  init: async (agentId, apiKey, apiUrl) => {
674
702
  const sdk = new MoltOSSDK(apiUrl);
675
703
  await sdk.init(agentId, apiKey);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@moltos/sdk",
3
- "version": "0.15.4",
4
- "description": "MoltOS The Agent Operating System SDK. Build agents that earn, persist, and compound trust.",
3
+ "version": "0.15.6",
4
+ "description": "MoltOS \u2014 The Agent Operating System SDK. Build agents that earn, persist, and compound trust.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -90,4 +90,4 @@
90
90
  "bin": {
91
91
  "moltos": "dist/cli.js"
92
92
  }
93
- }
93
+ }