@phantom/utils 1.0.0-beta.3 → 1.0.0-beta.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/index.d.ts CHANGED
@@ -15,4 +15,11 @@ declare function randomUUID(): string;
15
15
  */
16
16
  declare function randomString(length: number): string;
17
17
 
18
- export { randomString, randomUUID };
18
+ /**
19
+ * Secure time service that fetches server time from Phantom's time API
20
+ * instead of relying on local machine time which can be manipulated
21
+ */
22
+ declare const getSecureTimestamp: () => Promise<number>;
23
+ declare const getSecureTimestampSync: () => number;
24
+
25
+ export { getSecureTimestamp, getSecureTimestampSync, randomString, randomUUID };
package/dist/index.js CHANGED
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ getSecureTimestamp: () => getSecureTimestamp,
24
+ getSecureTimestampSync: () => getSecureTimestampSync,
23
25
  randomString: () => randomString,
24
26
  randomUUID: () => randomUUID
25
27
  });
@@ -44,8 +46,77 @@ function randomString(length) {
44
46
  }
45
47
  return result;
46
48
  }
49
+
50
+ // src/time.ts
51
+ var TimeService = class {
52
+ constructor() {
53
+ this.cache = null;
54
+ this.CACHE_DURATION = 3e4;
55
+ // 30 seconds cache
56
+ this.TIME_API_URL = "https://time.phantom.app/utc";
57
+ }
58
+ static getInstance() {
59
+ if (!TimeService.instance) {
60
+ TimeService.instance = new TimeService();
61
+ }
62
+ return TimeService.instance;
63
+ }
64
+ /**
65
+ * Get current timestamp from Phantom's secure time API
66
+ * Includes basic caching to reduce API calls
67
+ */
68
+ async now() {
69
+ const now = Date.now();
70
+ if (this.cache && now - this.cache.fetchedAt < this.CACHE_DURATION) {
71
+ const elapsed = now - this.cache.fetchedAt;
72
+ return this.cache.timestamp + elapsed;
73
+ }
74
+ try {
75
+ const response = await fetch(this.TIME_API_URL);
76
+ if (!response.ok) {
77
+ throw new Error(`Time API responded with status: ${response.status}`);
78
+ }
79
+ const timestampText = await response.text();
80
+ const timestamp = parseInt(timestampText, 10);
81
+ if (isNaN(timestamp)) {
82
+ throw new Error(`Invalid timestamp received: ${timestampText}`);
83
+ }
84
+ this.cache = {
85
+ timestamp,
86
+ fetchedAt: now
87
+ };
88
+ return timestamp;
89
+ } catch (error) {
90
+ return Date.now();
91
+ }
92
+ }
93
+ /**
94
+ * Synchronous version that uses cached time if available,
95
+ * otherwise falls back to Date.now()
96
+ */
97
+ nowSync() {
98
+ if (this.cache) {
99
+ const elapsed = Date.now() - this.cache.fetchedAt;
100
+ if (elapsed < this.CACHE_DURATION) {
101
+ return this.cache.timestamp + elapsed;
102
+ }
103
+ }
104
+ return Date.now();
105
+ }
106
+ /**
107
+ * Clear the cache (useful for testing)
108
+ */
109
+ clearCache() {
110
+ this.cache = null;
111
+ }
112
+ };
113
+ var timeService = TimeService.getInstance();
114
+ var getSecureTimestamp = () => timeService.now();
115
+ var getSecureTimestampSync = () => timeService.nowSync();
47
116
  // Annotate the CommonJS export names for ESM import in node:
48
117
  0 && (module.exports = {
118
+ getSecureTimestamp,
119
+ getSecureTimestampSync,
49
120
  randomString,
50
121
  randomUUID
51
122
  });
package/dist/index.mjs CHANGED
@@ -17,7 +17,76 @@ function randomString(length) {
17
17
  }
18
18
  return result;
19
19
  }
20
+
21
+ // src/time.ts
22
+ var TimeService = class {
23
+ constructor() {
24
+ this.cache = null;
25
+ this.CACHE_DURATION = 3e4;
26
+ // 30 seconds cache
27
+ this.TIME_API_URL = "https://time.phantom.app/utc";
28
+ }
29
+ static getInstance() {
30
+ if (!TimeService.instance) {
31
+ TimeService.instance = new TimeService();
32
+ }
33
+ return TimeService.instance;
34
+ }
35
+ /**
36
+ * Get current timestamp from Phantom's secure time API
37
+ * Includes basic caching to reduce API calls
38
+ */
39
+ async now() {
40
+ const now = Date.now();
41
+ if (this.cache && now - this.cache.fetchedAt < this.CACHE_DURATION) {
42
+ const elapsed = now - this.cache.fetchedAt;
43
+ return this.cache.timestamp + elapsed;
44
+ }
45
+ try {
46
+ const response = await fetch(this.TIME_API_URL);
47
+ if (!response.ok) {
48
+ throw new Error(`Time API responded with status: ${response.status}`);
49
+ }
50
+ const timestampText = await response.text();
51
+ const timestamp = parseInt(timestampText, 10);
52
+ if (isNaN(timestamp)) {
53
+ throw new Error(`Invalid timestamp received: ${timestampText}`);
54
+ }
55
+ this.cache = {
56
+ timestamp,
57
+ fetchedAt: now
58
+ };
59
+ return timestamp;
60
+ } catch (error) {
61
+ return Date.now();
62
+ }
63
+ }
64
+ /**
65
+ * Synchronous version that uses cached time if available,
66
+ * otherwise falls back to Date.now()
67
+ */
68
+ nowSync() {
69
+ if (this.cache) {
70
+ const elapsed = Date.now() - this.cache.fetchedAt;
71
+ if (elapsed < this.CACHE_DURATION) {
72
+ return this.cache.timestamp + elapsed;
73
+ }
74
+ }
75
+ return Date.now();
76
+ }
77
+ /**
78
+ * Clear the cache (useful for testing)
79
+ */
80
+ clearCache() {
81
+ this.cache = null;
82
+ }
83
+ };
84
+ var timeService = TimeService.getInstance();
85
+ var getSecureTimestamp = () => timeService.now();
86
+ var getSecureTimestampSync = () => timeService.nowSync();
20
87
  export {
88
+ getSecureTimestamp,
89
+ getSecureTimestampSync,
21
90
  randomString,
22
91
  randomUUID
23
92
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phantom/utils",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0-beta.4",
4
4
  "description": "Utility functions for Phantom Wallet SDK",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",