hm-tracking-sdk 0.1.6 → 0.1.9

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/README.md CHANGED
@@ -26,6 +26,8 @@
26
26
  const options: SDKInitOptions = {
27
27
  // 可选:不传则使用内置默认地址
28
28
  baseURL: "https://your-api.example.com",
29
+ // 可选:回调校验用,SDK方会提供
30
+ appId: "app-xxxxxxx",
29
31
 
30
32
  // 可选:本地存储 Key 前缀,避免项目间冲突
31
33
  storageKeyPrefix: "your_app_prefix",
@@ -34,12 +36,13 @@
34
36
  customUser: {
35
37
  user: {
36
38
  id: 123456789,
37
- firstName: "Test",
38
- lastName: "User",
39
+ first_name: "Test",
40
+ last_name: "User",
39
41
  username: "test_user",
40
- languageCode: "zh-hans",
42
+ language_code: "zh-hans",
43
+ photo_url: "https://t.me/i/userpic/320/example.svg",
41
44
  },
42
- authDate: new Date().toISOString(),
45
+ auth_date: Math.floor(Date.now() / 1000),
43
46
  hash: "your_hash_value",
44
47
  },
45
48
  };
@@ -55,12 +58,14 @@
55
58
  export interface SDKInitOptions {
56
59
  /** 自定义接口地址(可选)。不传则使用默认正式环境 */
57
60
  baseURL?: string;
58
- appId?: string; // 可选:回调校验用
61
+ appId?: string; // 可选:回调校验用,SDK方提供
59
62
  notifyUrl?: string; // 可选:SDK 初始化鉴权成功后的服务端回调地址
60
63
  /** 本地存储 Key 前缀,避免冲突 */
61
64
  storageKeyPrefix?: string;
62
65
  /** 自定义用户信息(可选) */
63
66
  customUser?: TelegramUserInfo;
67
+ /** 可选:TON Connect manifest 文件地址,规则请看5.1.0;未传则使用默认测试清单 */
68
+ tonConnectManifestUrl?: string;
64
69
  }
65
70
  ```
66
71
 
@@ -68,7 +73,7 @@
68
73
  - 说明:后端服务地址;不传则使用 SDK 内置默认地址。
69
74
  - 类型:`string`
70
75
  - appId (可选)
71
- - 说明:服务器之间用来鉴权
76
+ - 说明:服务器之间用来鉴权,SDK方提供
72
77
  - 类型:`string`
73
78
  - notifyUrl (可选)
74
79
  - 说明:SDK方服务器回调参数
@@ -79,6 +84,9 @@
79
84
  - customUser(可选)
80
85
  - 说明:自定义用户数据,仅在非 Telegram 环境可用;若不传,SDK 会自动使用“匿名浏览器用户”(基于 Cookie 生成并持久化随机 userId),从而可以直接完成鉴权与后续请求。
81
86
  - 类型:`TelegramUserInfo`
87
+ - tonConnectManifestUrl(可选)
88
+ - 说明:TON Connect manifest 文件地址。若不传,SDK 使用默认测试清单:`https://miniapp.spinfi.me/tonconnect-manifest.json`。当小程序部署后需要改为对应链接,具体见`5.1.0`说明
89
+ - 类型:`string`
82
90
  - 返回值:`Promise<void>`。
83
91
 
84
92
  ### 2.1 初始化成功通知调用说明(服务端回调)
@@ -88,7 +96,23 @@
88
96
  - JSON 示例:
89
97
 
90
98
  ```json
91
- {"id":"7506466780","first_name":"hong","last_name":"tang","nickname":"hongtang","username":"tanghong1992","language_code":"zh","allows_write_to_pm":1,"auth_date":"2025-04-08T02:08:53.000Z","hash":"a9d2d7c1b066bebb73c170006dc474061d7601183b388b037936aa7cb6620acf","chat_instance":"-2254244616056093259","is_premium_user":0,"photo_url":"https://t.me/i/userpic/320/uLzA4KyVzKwd0vi6Uvupe6U2mdnVEQOIbUZbFTQff6B3UhjRpQObL9I8Nq81G0ln.svg"}
99
+ {
100
+ "user": {
101
+ "id": 7506466780,
102
+ "first_name": "first",
103
+ "last_name": "name",
104
+ "username": "nickname1992",
105
+ "language_code": "zh",
106
+ "allows_write_to_pm": true,
107
+ "is_premium": false,
108
+ "photo_url": "https://t.me/i/userpic/320/uLzA4KyVzKwd0vi6Uvupe6U2mdnVEQOIbUZbFTQff6B3UhjRpQObL9I8Nq81G0ln.svg"
109
+ },
110
+ "auth_date": 1735792133,
111
+ "hash": "a9d2d7c1b066bebb73c170006dc474061d7601183b388b037936aa7cb6620acf",
112
+ "chat_instance": "-2254244616056098321",
113
+ "chat_type": "sender",
114
+ "start_param": "..."
115
+ }
92
116
  ```
93
117
 
94
118
  - 若为加密字符串:请与服务端约定解密方式及签名校验流程。
@@ -138,14 +162,21 @@
138
162
 
139
163
  ```ts
140
164
  export interface TelegramUserInfo {
141
- id?: number;
142
- first_name?: string;
143
- last_name?: string;
144
- username?: string;
145
- language_code?: string;
146
- allows_write_to_pm?: boolean;
165
+ user: {
166
+ id: number;
167
+ first_name?: string;
168
+ last_name?: string;
169
+ username?: string;
170
+ photo_url?: string;
171
+ language_code?: string;
172
+ allows_write_to_pm?: boolean;
173
+ is_premium?: boolean;
174
+ };
147
175
  auth_date?: number;
148
176
  hash?: string;
177
+ start_param?: string;
178
+ chat_type?: string;
179
+ chat_instance?: string;
149
180
  [key: string]: unknown;
150
181
  }
151
182
  ```
@@ -201,6 +232,46 @@
201
232
 
202
233
  ### 5.1 TON钱包相关API
203
234
 
235
+ #### 5.1.0 配置 tonconnect-manifest
236
+
237
+ - 作用
238
+ - TON Connect 要求每个 dApp(小程序) 提供 `tonconnect-manifest.json`。钱包在连接前会读取该文件以展示 dApp 名称、图标,并校验来源。
239
+ - 托管与访问要求
240
+ - 将该 JSON 文件托管在公网可访问的 HTTPS 地址(建议:`https://your-domain.com/tonconnect-manifest.json`,放在站点根路径)。
241
+ - 文件及其中引用的资源(如图标)须可在任意来源公开访问,避免 CORS 限制。
242
+ - 必须能通过 GET 直接获取,无需鉴权或特殊请求头。
243
+ - `url` 字段请避免末尾斜杠,例如使用 `https://mydapp.com` 而非 `https://mydapp.com/`。
244
+ - 最小示例
245
+
246
+ ```json
247
+ {
248
+ "url": "https://your-domain.com",
249
+ "name": "Your DApp Name",
250
+ "iconUrl": "https://your-domain.com/icons/tonconnect-icon.png"
251
+ }
252
+ ```
253
+
254
+ - 字段要求
255
+ - `url`(必填):应用基础 URL,点击钱包内图标时用于打开应用;避免末尾斜杠。
256
+ - `name`(必填):应用名称。
257
+ - `iconUrl`(必填):应用图标 URL,仅支持 PNG 或 ICO,不支持 SVG;建议 180x180 PNG。
258
+ - `termsOfUseUrl`(可选):条款页 URL。
259
+ - `privacyPolicyUrl`(可选):隐私政策 URL。
260
+ - SDK 行为与配置
261
+ - 默认使用测试清单:`https://miniapp.spinfi.me/tonconnect-manifest.json`。
262
+ - 生产环境建议通过初始化参数传入:
263
+
264
+ ```ts
265
+ const sdk = new HmTrackingSDK({
266
+ tonConnectManifestUrl: "https://your-domain.com/tonconnect-manifest.json",
267
+ // 其他可选项...
268
+ });
269
+ ```
270
+
271
+ - 自检
272
+ - 在浏览器访问你的 manifest URL,确认返回 200 且 JSON 内容正确。
273
+ - 打开连接钱包弹窗,应显示你在 manifest 中配置的名称与图标。
274
+
204
275
  #### 5.1.1 钱包是否链接
205
276
 
206
277
  - 接口: isWalletConnected()
@@ -4374,18 +4374,19 @@ function getTelegramUserUnsafe() {
4374
4374
  return !r.user || !r.user.id ? null : {
4375
4375
  user: {
4376
4376
  id: r.user.id,
4377
- firstName: r.user.first_name || "",
4378
- lastName: r.user.last_name || "",
4377
+ first_name: r.user.first_name || "",
4378
+ last_name: r.user.last_name || "",
4379
4379
  username: r.user.username,
4380
- languageCode: r.user.language_code,
4381
- isPremium: r.user.is_premium || !1,
4382
- allowsWriteToPm: r.user.allows_write_to_pm || !1
4380
+ photo_url: r.user.photo_url,
4381
+ language_code: r.user.language_code,
4382
+ allows_write_to_pm: r.user.allows_write_to_pm || !1,
4383
+ is_premium: r.user.is_premium || !1
4383
4384
  },
4384
- authDate: (/* @__PURE__ */ new Date()).toISOString(),
4385
+ auth_date: r.auth_date,
4385
4386
  hash: r.hash || "",
4386
- startParam: r.start_param,
4387
- chatType: r.chat_type,
4388
- chatInstance: r.chat_instance
4387
+ start_param: r.start_param,
4388
+ chat_type: r.chat_type,
4389
+ chat_instance: r.chat_instance
4389
4390
  };
4390
4391
  } catch {
4391
4392
  return null;
@@ -4425,36 +4426,35 @@ function getAnonymousBrowserUser(r = "tg_tracking_uid") {
4425
4426
  return {
4426
4427
  user: {
4427
4428
  id: d,
4428
- firstName: "Guest",
4429
- lastName: "",
4429
+ first_name: "Guest",
4430
+ last_name: "",
4430
4431
  username: `guest_${f.slice(-6)}`,
4431
- languageCode: m.toLowerCase(),
4432
- isPremium: !1,
4433
- allowsWriteToPm: !1
4432
+ language_code: m.toLowerCase(),
4433
+ allows_write_to_pm: !1
4434
4434
  },
4435
- authDate: (/* @__PURE__ */ new Date()).toISOString(),
4435
+ auth_date: Math.floor(Date.now() / 1e3),
4436
4436
  hash: "anonymous_cookie_user",
4437
- startParam: "browser_guest",
4438
- chatType: "sender",
4439
- chatInstance: ""
4437
+ start_param: "browser_guest",
4438
+ chat_type: "sender",
4439
+ chat_instance: ""
4440
4440
  };
4441
4441
  }
4442
4442
  function getDefaultMockUser() {
4443
4443
  return {
4444
4444
  user: {
4445
4445
  id: 2077220643,
4446
- firstName: "Mock",
4447
- lastName: "User",
4446
+ first_name: "Mock",
4447
+ last_name: "User",
4448
4448
  username: "mock_user_demo",
4449
- languageCode: "zh-hans",
4450
- isPremium: !0,
4451
- allowsWriteToPm: !0
4449
+ language_code: "zh-hans",
4450
+ allows_write_to_pm: !0,
4451
+ is_premium: !0
4452
4452
  },
4453
- authDate: (/* @__PURE__ */ new Date()).toISOString(),
4453
+ auth_date: Math.floor(Date.now() / 1e3),
4454
4454
  hash: "mock_hash_for_demo_purpose",
4455
- startParam: "demo_start_param",
4456
- chatType: "sender",
4457
- chatInstance: "-245769840322609948"
4455
+ start_param: "demo_start_param",
4456
+ chat_type: "sender",
4457
+ chat_instance: "-245769840322609948"
4458
4458
  };
4459
4459
  }
4460
4460
  var naclUtil$1 = { exports: {} }, naclUtil = naclUtil$1.exports, hasRequiredNaclUtil;
@@ -35925,14 +35925,16 @@ class TonConnectUI {
35925
35925
  }
35926
35926
  }
35927
35927
  const DEFAULT_MANIFEST_URL = "https://miniapp.spinfi.me/tonconnect-manifest.json";
35928
- let tonConnectUIInstance = null;
35928
+ let tonConnectUIInstance = null, currentManifestUrl = null;
35929
35929
  class DefaultTonWalletProvider {
35930
- constructor(f) {
35931
- if (this.bindWalletCallback = f, !tonConnectUIInstance)
35930
+ constructor(f, d) {
35931
+ this.bindWalletCallback = d;
35932
+ const m = f || DEFAULT_MANIFEST_URL;
35933
+ if (!tonConnectUIInstance || currentManifestUrl !== m)
35932
35934
  try {
35933
35935
  tonConnectUIInstance = new TonConnectUI({
35934
- manifestUrl: DEFAULT_MANIFEST_URL
35935
- });
35936
+ manifestUrl: m
35937
+ }), currentManifestUrl = m;
35936
35938
  } catch {
35937
35939
  throw new Error("创建TonConnectUI实例失败");
35938
35940
  }
@@ -36029,7 +36031,10 @@ class HmTrackingSDK {
36029
36031
  onAuthError: () => this.clearAuth(),
36030
36032
  baseURL: m,
36031
36033
  getAppId: () => this.options && this.options.appId || null
36032
- }), this.api = new APIService(this.http), this.hmAdManager = new HmAdManager({ autoInjectScript: !0 }), this.adMonitor = new AdMonitor((v) => this.api.adMonitor(v)), this.tonWallet = new DefaultTonWalletProvider(this.bindWallet.bind(this));
36034
+ }), this.api = new APIService(this.http), this.hmAdManager = new HmAdManager({ autoInjectScript: !0 }), this.adMonitor = new AdMonitor((v) => this.api.adMonitor(v)), this.tonWallet = new DefaultTonWalletProvider(
36035
+ d.tonConnectManifestUrl,
36036
+ this.bindWallet.bind(this)
36037
+ );
36033
36038
  }
36034
36039
  async init() {
36035
36040
  const f = isTelegramEnv();