@clawdvault/sdk 0.1.3 → 0.2.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.mjs CHANGED
@@ -114,8 +114,17 @@ async function signAndSerialize(transaction, signer) {
114
114
  return Buffer.from(signedTx.serialize()).toString("base64");
115
115
  }
116
116
  }
117
- async function createAuthSignature(signer, payload) {
118
- const message = JSON.stringify(payload);
117
+ async function createAuthSignature(signer, payload, action) {
118
+ const authAction = action || payload.action || "session";
119
+ const timestamp = Math.floor(Date.now() / 1e3);
120
+ const window2 = Math.floor(timestamp / 300) * 300;
121
+ let signData;
122
+ if (authAction === "session") {
123
+ signData = { action: "create_session" };
124
+ } else {
125
+ signData = { ...payload };
126
+ }
127
+ const message = `ClawdVault:${authAction}:${window2}:${JSON.stringify(signData)}`;
119
128
  const messageBytes = new TextEncoder().encode(message);
120
129
  const signatureBytes = await signer.signMessage(messageBytes);
121
130
  return {
@@ -162,7 +171,7 @@ var ClawdVaultClient = class {
162
171
  return this.signer?.publicKey.toBase58() ?? null;
163
172
  }
164
173
  async request(method, path, options = {}) {
165
- const { params, body, auth, formData } = options;
174
+ const { params, body, auth, formData, action } = options;
166
175
  let url = `${this.baseUrl}${path}`;
167
176
  if (params) {
168
177
  const searchParams = new URLSearchParams();
@@ -179,9 +188,12 @@ var ClawdVaultClient = class {
179
188
  if (this.sessionToken) {
180
189
  headers["Authorization"] = `Bearer ${this.sessionToken}`;
181
190
  } else if (this.signer && body) {
182
- const { signature, wallet } = await createAuthSignature(this.signer, body);
191
+ const { signature, wallet } = await createAuthSignature(this.signer, body, action);
183
192
  headers["X-Wallet"] = wallet;
184
193
  headers["X-Signature"] = signature;
194
+ if (action) {
195
+ headers["X-Action"] = action;
196
+ }
185
197
  }
186
198
  }
187
199
  let requestBody;
@@ -339,7 +351,8 @@ var ClawdVaultClient = class {
339
351
  throw new Error("Signer required for sellPercent");
340
352
  }
341
353
  const wallet = this.signer.publicKey.toBase58();
342
- const { balance } = await this.getBalance(wallet, mint);
354
+ const balanceResponse = await this.getBalance(wallet, mint);
355
+ const balance = balanceResponse.balance ?? 0;
343
356
  if (balance <= 0) {
344
357
  throw new Error("No tokens to sell");
345
358
  }
@@ -496,7 +509,7 @@ var ClawdVaultClient = class {
496
509
  * Send chat message
497
510
  */
498
511
  async sendChat(params) {
499
- return this.request("POST", "/chat", { body: params, auth: true });
512
+ return this.request("POST", "/chat", { body: params, auth: true, action: "chat" });
500
513
  }
501
514
  /**
502
515
  * Add reaction
@@ -504,7 +517,8 @@ var ClawdVaultClient = class {
504
517
  async addReaction(messageId, emoji) {
505
518
  await this.request("POST", "/reactions", {
506
519
  body: { messageId, emoji },
507
- auth: true
520
+ auth: true,
521
+ action: "react"
508
522
  });
509
523
  }
510
524
  /**
@@ -513,7 +527,8 @@ var ClawdVaultClient = class {
513
527
  async removeReaction(messageId, emoji) {
514
528
  await this.request("DELETE", "/reactions", {
515
529
  params: { messageId, emoji },
516
- auth: true
530
+ auth: true,
531
+ action: "unreact"
517
532
  });
518
533
  }
519
534
  // ============ User / Auth ============
@@ -527,13 +542,13 @@ var ClawdVaultClient = class {
527
542
  * Update profile
528
543
  */
529
544
  async updateProfile(params) {
530
- await this.request("POST", "/profile", { body: params, auth: true });
545
+ await this.request("POST", "/profile", { body: params, auth: true, action: "profile" });
531
546
  }
532
547
  /**
533
548
  * Create session token
534
549
  */
535
550
  async createSession() {
536
- return this.request("POST", "/auth/session", { body: {}, auth: true });
551
+ return this.request("POST", "/auth/session", { body: {}, auth: true, action: "session" });
537
552
  }
538
553
  /**
539
554
  * Validate session token
@@ -577,285 +592,20 @@ function createClient(config) {
577
592
  return new ClawdVaultClient(config);
578
593
  }
579
594
 
580
- // src/streaming.ts
581
- var getEventSource = () => {
582
- if (typeof EventSource !== "undefined") {
583
- return EventSource;
584
- }
585
- return __require("eventsource");
586
- };
587
- var StreamConnection = class {
588
- constructor(url, options = {}) {
589
- this.eventSource = null;
590
- this.reconnectAttempts = 0;
591
- this.reconnectTimeout = null;
592
- this.listeners = /* @__PURE__ */ new Map();
593
- this.onConnectCallbacks = /* @__PURE__ */ new Set();
594
- this.onDisconnectCallbacks = /* @__PURE__ */ new Set();
595
- this.onErrorCallbacks = /* @__PURE__ */ new Set();
596
- this.isConnected = false;
597
- this.isManuallyDisconnected = false;
598
- this.url = url;
599
- this.options = {
600
- autoReconnect: options.autoReconnect ?? true,
601
- reconnectDelay: options.reconnectDelay ?? 3e3,
602
- maxReconnectAttempts: options.maxReconnectAttempts ?? 10
603
- };
604
- }
605
- /**
606
- * Connect to the stream
607
- */
608
- connect() {
609
- if (this.eventSource) {
610
- return;
611
- }
612
- this.isManuallyDisconnected = false;
613
- const EventSourceImpl = getEventSource();
614
- this.eventSource = new EventSourceImpl(this.url);
615
- this.eventSource.onopen = () => {
616
- this.isConnected = true;
617
- this.reconnectAttempts = 0;
618
- this.onConnectCallbacks.forEach((cb) => cb());
619
- };
620
- this.eventSource.onerror = (event) => {
621
- const wasConnected = this.isConnected;
622
- this.isConnected = false;
623
- if (wasConnected) {
624
- this.onDisconnectCallbacks.forEach((cb) => cb());
625
- }
626
- this.onErrorCallbacks.forEach((cb) => cb(new Error("Stream connection error")));
627
- if (this.options.autoReconnect && !this.isManuallyDisconnected) {
628
- this.scheduleReconnect();
629
- }
630
- };
631
- this.eventSource.onmessage = (event) => {
632
- this.handleEvent("message", event.data);
633
- };
634
- for (const eventType of this.listeners.keys()) {
635
- this.eventSource.addEventListener(eventType, (event) => {
636
- this.handleEvent(eventType, event.data);
637
- });
638
- }
639
- }
640
- handleEvent(eventType, data) {
641
- const callbacks = this.listeners.get(eventType);
642
- if (!callbacks) return;
643
- try {
644
- const parsed = JSON.parse(data);
645
- callbacks.forEach((cb) => cb(parsed));
646
- } catch {
647
- }
648
- }
649
- scheduleReconnect() {
650
- if (this.reconnectTimeout) {
651
- clearTimeout(this.reconnectTimeout);
652
- }
653
- if (this.reconnectAttempts >= this.options.maxReconnectAttempts) {
654
- this.onErrorCallbacks.forEach(
655
- (cb) => cb(new Error(`Max reconnect attempts (${this.options.maxReconnectAttempts}) reached`))
656
- );
657
- return;
658
- }
659
- this.reconnectAttempts++;
660
- const delay = this.options.reconnectDelay * Math.pow(1.5, this.reconnectAttempts - 1);
661
- this.reconnectTimeout = setTimeout(() => {
662
- this.eventSource?.close();
663
- this.eventSource = null;
664
- this.connect();
665
- }, delay);
666
- }
667
- /**
668
- * Disconnect from the stream
669
- */
670
- disconnect() {
671
- this.isManuallyDisconnected = true;
672
- if (this.reconnectTimeout) {
673
- clearTimeout(this.reconnectTimeout);
674
- this.reconnectTimeout = null;
675
- }
676
- if (this.eventSource) {
677
- this.eventSource.close();
678
- this.eventSource = null;
679
- }
680
- if (this.isConnected) {
681
- this.isConnected = false;
682
- this.onDisconnectCallbacks.forEach((cb) => cb());
683
- }
684
- }
685
- /**
686
- * Subscribe to an event type
687
- */
688
- on(eventType, callback) {
689
- if (!this.listeners.has(eventType)) {
690
- this.listeners.set(eventType, /* @__PURE__ */ new Set());
691
- if (this.eventSource) {
692
- this.eventSource.addEventListener(eventType, (event) => {
693
- this.handleEvent(eventType, event.data);
694
- });
695
- }
696
- }
697
- this.listeners.get(eventType).add(callback);
698
- return () => {
699
- const callbacks = this.listeners.get(eventType);
700
- if (callbacks) {
701
- callbacks.delete(callback);
702
- }
703
- };
704
- }
705
- /**
706
- * One-time event listener
707
- */
708
- once(eventType, callback) {
709
- const unsubscribe = this.on(eventType, (data) => {
710
- unsubscribe();
711
- callback(data);
712
- });
713
- }
714
- /**
715
- * Register connection callback
716
- */
717
- onConnect(callback) {
718
- this.onConnectCallbacks.add(callback);
719
- return () => this.onConnectCallbacks.delete(callback);
720
- }
721
- /**
722
- * Register disconnection callback
723
- */
724
- onDisconnect(callback) {
725
- this.onDisconnectCallbacks.add(callback);
726
- return () => this.onDisconnectCallbacks.delete(callback);
727
- }
728
- /**
729
- * Register error callback
730
- */
731
- onError(callback) {
732
- this.onErrorCallbacks.add(callback);
733
- return () => this.onErrorCallbacks.delete(callback);
734
- }
735
- /**
736
- * Check if connected
737
- */
738
- get connected() {
739
- return this.isConnected;
740
- }
741
- };
742
- var ClawdVaultStreaming = class {
743
- constructor(baseUrl = "https://clawdvault.com/api", options = {}) {
744
- this.connections = /* @__PURE__ */ new Map();
745
- this.baseUrl = baseUrl.replace(/\/$/, "");
746
- this.options = options;
747
- }
748
- /**
749
- * Stream trades for a token
750
- */
751
- streamTrades(mint) {
752
- const key = `trades:${mint}`;
753
- if (!this.connections.has(key)) {
754
- const conn = new StreamConnection(
755
- `${this.baseUrl}/stream/trades?mint=${mint}`,
756
- this.options
757
- );
758
- this.connections.set(key, conn);
759
- }
760
- return this.connections.get(key);
761
- }
762
- /**
763
- * Stream token updates (price, market cap)
764
- */
765
- streamToken(mint) {
766
- const key = `token:${mint}`;
767
- if (!this.connections.has(key)) {
768
- const conn = new StreamConnection(
769
- `${this.baseUrl}/stream/token?mint=${mint}`,
770
- this.options
771
- );
772
- this.connections.set(key, conn);
773
- }
774
- return this.connections.get(key);
775
- }
776
- /**
777
- * Stream chat messages
778
- */
779
- streamChat(mint) {
780
- const key = `chat:${mint}`;
781
- if (!this.connections.has(key)) {
782
- const conn = new StreamConnection(
783
- `${this.baseUrl}/stream/chat?mint=${mint}`,
784
- this.options
785
- );
786
- this.connections.set(key, conn);
787
- }
788
- return this.connections.get(key);
789
- }
790
- /**
791
- * Convenience: Subscribe to trades with callback
792
- */
793
- onTrades(mint, callback) {
794
- const conn = this.streamTrades(mint);
795
- const unsubscribe = conn.on("trade", callback);
796
- conn.connect();
797
- return { unsubscribe, connection: conn };
798
- }
799
- /**
800
- * Convenience: Subscribe to token price updates
801
- */
802
- onPrice(mint, callback) {
803
- const conn = this.streamToken(mint);
804
- const unsubscribe = conn.on("update", callback);
805
- conn.connect();
806
- return { unsubscribe, connection: conn };
807
- }
808
- /**
809
- * Convenience: Subscribe to chat messages
810
- */
811
- onChat(mint, callback) {
812
- const conn = this.streamChat(mint);
813
- const unsubscribe = conn.on("message", callback);
814
- conn.connect();
815
- return { unsubscribe, connection: conn };
816
- }
817
- /**
818
- * Disconnect all streams
819
- */
820
- disconnectAll() {
821
- for (const conn of this.connections.values()) {
822
- conn.disconnect();
823
- }
824
- this.connections.clear();
825
- }
826
- /**
827
- * Disconnect a specific stream
828
- */
829
- disconnect(type, mint) {
830
- const key = `${type}:${mint}`;
831
- const conn = this.connections.get(key);
832
- if (conn) {
833
- conn.disconnect();
834
- this.connections.delete(key);
835
- }
836
- }
837
- };
838
- function createStreaming(baseUrl, options) {
839
- return new ClawdVaultStreaming(baseUrl, options);
840
- }
841
-
842
595
  // src/index.ts
843
596
  import { PublicKey as PublicKey2, Keypair as Keypair2 } from "@solana/web3.js";
844
597
  var PROGRAM_ID = "GUyF2TVe32Cid4iGVt2F6wPYDhLSVmTUZBj2974outYM";
845
598
  var DEFAULT_BASE_URL = "https://clawdvault.com/api";
846
599
  export {
847
600
  ClawdVaultClient,
848
- ClawdVaultStreaming,
849
601
  DEFAULT_BASE_URL,
850
602
  Keypair2 as Keypair,
851
603
  KeypairSigner,
852
604
  PROGRAM_ID,
853
605
  PhantomSigner,
854
606
  PublicKey2 as PublicKey,
855
- StreamConnection,
856
607
  createAuthSignature,
857
608
  createClient,
858
- createStreaming,
859
609
  signAndSerialize,
860
610
  verifySignature
861
611
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawdvault/sdk",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "TypeScript SDK for ClawdVault - Solana token launchpad",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -19,16 +19,19 @@
19
19
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",
20
20
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
21
21
  "test": "vitest run",
22
- "clean": "rm -rf dist"
22
+ "clean": "rm -rf dist",
23
+ "generate-types": "openapi-typescript https://clawdvault.com/api/openapi -o src/generated/api.ts",
24
+ "generate-types:local": "openapi-typescript ../../clawdvault/app/public/openapi.yaml -o src/generated/api.ts"
23
25
  },
24
26
  "dependencies": {
25
- "@solana/web3.js": "^1.91.0",
26
27
  "@solana/spl-token": "^0.4.0",
28
+ "@solana/web3.js": "^1.91.0",
27
29
  "bs58": "^5.0.0",
28
- "tweetnacl": "^1.0.3",
29
- "eventsource": "^2.0.2"
30
+ "eventsource": "^2.0.2",
31
+ "tweetnacl": "^1.0.3"
30
32
  },
31
33
  "devDependencies": {
34
+ "openapi-typescript": "^7.10.1",
32
35
  "tsup": "^8.0.1",
33
36
  "typescript": "^5.3.3",
34
37
  "vitest": "^1.2.0"