@enclave-hq/wallet-sdk 1.1.6 → 1.2.1

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 CHANGED
@@ -327,7 +327,7 @@ declare class MetaMaskAdapter extends BrowserWalletAdapter {
327
327
  declare class TronLinkAdapter extends BrowserWalletAdapter {
328
328
  readonly type = WalletType.TRONLINK;
329
329
  readonly chainType = ChainType.TRON;
330
- readonly name = "TronLink";
330
+ readonly name = "TronWeb";
331
331
  readonly icon = "https://www.tronlink.org/static/logoIcon.svg";
332
332
  private static readonly TRON_MAINNET_CHAIN_ID;
333
333
  connect(chainId?: number): Promise<Account>;
package/dist/index.d.ts CHANGED
@@ -327,7 +327,7 @@ declare class MetaMaskAdapter extends BrowserWalletAdapter {
327
327
  declare class TronLinkAdapter extends BrowserWalletAdapter {
328
328
  readonly type = WalletType.TRONLINK;
329
329
  readonly chainType = ChainType.TRON;
330
- readonly name = "TronLink";
330
+ readonly name = "TronWeb";
331
331
  readonly icon = "https://www.tronlink.org/static/logoIcon.svg";
332
332
  private static readonly TRON_MAINNET_CHAIN_ID;
333
333
  connect(chainId?: number): Promise<Account>;
package/dist/index.js CHANGED
@@ -921,7 +921,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
921
921
  super(...arguments);
922
922
  this.type = "tronlink" /* TRONLINK */;
923
923
  this.chainType = ChainType.TRON;
924
- this.name = "TronLink";
924
+ this.name = "TronWeb";
925
925
  this.icon = "https://www.tronlink.org/static/logoIcon.svg";
926
926
  /**
927
927
  * 轮询检测账户变化(备用方案)
@@ -965,16 +965,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
965
965
  await this.ensureAvailable();
966
966
  try {
967
967
  this.setState("connecting" /* CONNECTING */);
968
+ const w = window;
968
969
  const tronWeb = this.getTronWeb();
969
- const result = await tronWeb.request({
970
- method: "tron_requestAccounts"
971
- });
972
- if (!result || !result.code || result.code !== 200) {
973
- throw new ConnectionRejectedError(this.type);
970
+ if (tronWeb.ready) {
971
+ await tronWeb.ready;
972
+ }
973
+ let address = tronWeb.defaultAddress?.base58;
974
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
975
+ try {
976
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
977
+ } catch (e) {
978
+ }
979
+ }
980
+ if (!address) {
981
+ for (let i = 0; i < 20; i++) {
982
+ await new Promise((resolve) => setTimeout(resolve, 100));
983
+ address = tronWeb.defaultAddress?.base58;
984
+ if (address) break;
985
+ }
986
+ }
987
+ if (!address) {
988
+ if (w.tronLink && typeof w.tronLink.request === "function") {
989
+ try {
990
+ const result = await w.tronLink.request({
991
+ method: "tron_requestAccounts"
992
+ });
993
+ if (!result || result.code !== 200) {
994
+ throw new ConnectionRejectedError(this.type);
995
+ }
996
+ } catch (error) {
997
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
998
+ throw new ConnectionRejectedError(this.type);
999
+ }
1000
+ }
1001
+ }
1002
+ address = tronWeb.defaultAddress?.base58;
1003
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
1004
+ try {
1005
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
1006
+ } catch (e) {
1007
+ }
1008
+ }
1009
+ if (!address) {
1010
+ for (let i = 0; i < 20; i++) {
1011
+ await new Promise((resolve) => setTimeout(resolve, 100));
1012
+ address = tronWeb.defaultAddress?.base58;
1013
+ if (address) break;
1014
+ }
1015
+ }
974
1016
  }
975
- const address = tronWeb.defaultAddress?.base58;
976
1017
  if (!address) {
977
- throw new Error("Failed to get Tron address");
1018
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
978
1019
  }
979
1020
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
980
1021
  const account = {
@@ -1000,9 +1041,9 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1000
1041
  /**
1001
1042
  * 签名消息
1002
1043
  *
1003
- * Note: TronLink supports two signing methods:
1004
- * - trx.sign(): Signs a transaction object
1005
- * - trx.signMessageV2(): Signs a plain text message (what we use here)
1044
+ * Note: TronWeb 兼容钱包支持两种签名方法:
1045
+ * - trx.sign(): 签名交易对象
1046
+ * - trx.signMessageV2(): 签名纯文本消息(我们使用此方法)
1006
1047
  */
1007
1048
  async signMessage(message) {
1008
1049
  this.ensureConnected();
@@ -1050,17 +1091,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1050
1091
  }
1051
1092
  /**
1052
1093
  * 读取合约
1094
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
1053
1095
  */
1054
1096
  async readContract(params) {
1055
1097
  this.ensureConnected();
1056
1098
  try {
1057
1099
  const tronWeb = this.getTronWeb();
1058
- const contract = await tronWeb.contract(params.abi, params.address);
1059
- const result = await contract[params.functionName](...params.args || []).call();
1060
- return result;
1100
+ if (!this.currentAccount) {
1101
+ throw new Error("No account connected");
1102
+ }
1103
+ try {
1104
+ const contract = await tronWeb.contract(params.abi, params.address);
1105
+ const method = contract[params.functionName];
1106
+ if (!method || typeof method !== "function") {
1107
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1108
+ }
1109
+ const result = await method(...params.args || []).call();
1110
+ return result;
1111
+ } catch (method1Error) {
1112
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1113
+ try {
1114
+ const contract2 = await tronWeb.contract().at(params.address);
1115
+ const method2 = contract2[params.functionName];
1116
+ if (!method2 || typeof method2 !== "function") {
1117
+ throw new Error(`Function ${params.functionName} not found in contract`);
1118
+ }
1119
+ const result = await method2(...params.args || []).call();
1120
+ return result;
1121
+ } catch (method2Error) {
1122
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1123
+ throw method1Error;
1124
+ }
1125
+ }
1061
1126
  } catch (error) {
1062
1127
  console.error("Read contract error:", error);
1063
- throw new Error(`Failed to read contract: ${error.message}`);
1128
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
1064
1129
  }
1065
1130
  }
1066
1131
  /**
@@ -1105,26 +1170,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1105
1170
  }));
1106
1171
  console.log("[TronLink] Transaction options:", options);
1107
1172
  console.log("[TronLink] Parameters:", parameter);
1108
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1173
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1174
+ console.log("[TronLink] Function selector:", functionSelector);
1175
+ console.log("[TronLink] Transaction options:", options);
1176
+ console.log("[TronLink] Parameters:", parameter);
1177
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1109
1178
  params.address,
1110
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1179
+ functionSelector,
1111
1180
  options,
1112
1181
  parameter,
1113
1182
  this.currentAccount.nativeAddress
1114
1183
  );
1115
- console.log("[TronLink] Transaction built:", transaction);
1116
- if (!transaction || !transaction.transaction) {
1184
+ console.log("[TronLink] Transaction built:", tx);
1185
+ if (!tx || !tx.transaction) {
1117
1186
  throw new Error("Failed to build transaction");
1118
1187
  }
1119
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1188
+ console.log("[TronLink] Requesting user signature...");
1189
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1190
+ console.log("[TronLink] Transaction signed:", signedTx);
1191
+ const txID = signedTx.txID;
1192
+ console.log("[TronLink] Transaction hash (txID):", txID);
1193
+ console.log("[TronLink] Broadcasting transaction...");
1120
1194
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1121
1195
  console.log("[TronLink] Broadcast result:", broadcast);
1122
- if (!broadcast.result) {
1123
- throw new Error(broadcast.message || "Transaction broadcast failed");
1196
+ if (broadcast && broadcast.result === true) {
1197
+ return txID || broadcast.txid || "";
1198
+ } else {
1199
+ if (txID) {
1200
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1201
+ return txID;
1202
+ }
1203
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1124
1204
  }
1125
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1126
- console.log("[TronLink] Transaction hash:", txHash);
1127
- return txHash || "";
1128
1205
  } catch (error) {
1129
1206
  console.error("Write contract error:", error);
1130
1207
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {
@@ -1177,7 +1254,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1177
1254
  return this.getTronWeb();
1178
1255
  }
1179
1256
  /**
1180
- * 获取浏览器中的 TronWeb
1257
+ * 获取浏览器中的 TronWeb 实例
1258
+ * 支持所有 TronWeb 兼容的钱包,包括:
1259
+ * - TronLink (window.tronLink.tronWeb 或 window.tronWeb)
1260
+ * - TokenPocket (window.tronWeb)
1261
+ * - 其他提供 window.tronWeb 接口的钱包
1181
1262
  */
1182
1263
  getBrowserProvider() {
1183
1264
  if (typeof window === "undefined") {
@@ -1192,7 +1273,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1192
1273
  getTronWeb() {
1193
1274
  const provider = this.getBrowserProvider();
1194
1275
  if (!provider) {
1195
- throw new Error("TronWeb not found");
1276
+ throw new Error("\u672A\u68C0\u6D4B\u5230 TronWeb \u517C\u5BB9\u7684\u94B1\u5305\u3002\u8BF7\u5B89\u88C5 TronLink \u6216\u5176\u4ED6 TronWeb \u517C\u5BB9\u7684\u94B1\u5305\u3002");
1196
1277
  }
1197
1278
  return provider;
1198
1279
  }
@@ -1214,9 +1295,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1214
1295
  w.tronLink.on("disconnect", this.handleDisconnect);
1215
1296
  } else if (w.tronWeb && w.tronWeb.eventServer) {
1216
1297
  this.startPolling();
1298
+ } else {
1299
+ this.startPolling();
1217
1300
  }
1218
1301
  } catch (error) {
1219
- console.warn("TronLink event listener setup failed:", error);
1302
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u8BBE\u7F6E\u5931\u8D25\uFF0C\u4F7F\u7528\u8F6E\u8BE2\u65B9\u5F0F:", error);
1220
1303
  this.startPolling();
1221
1304
  }
1222
1305
  }
@@ -1232,7 +1315,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1232
1315
  w.tronLink.off("disconnect", this.handleDisconnect);
1233
1316
  }
1234
1317
  } catch (error) {
1235
- console.warn("TronLink event listener removal failed:", error);
1318
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u79FB\u9664\u5931\u8D25:", error);
1236
1319
  }
1237
1320
  this.stopPolling();
1238
1321
  }
@@ -2240,11 +2323,11 @@ var SUPPORTED_WALLETS = {
2240
2323
  },
2241
2324
  ["tronlink" /* TRONLINK */]: {
2242
2325
  type: "tronlink" /* TRONLINK */,
2243
- name: "TronLink",
2326
+ name: "TronWeb",
2244
2327
  chainType: ChainType.TRON,
2245
2328
  icon: "https://www.tronlink.org/static/logoIcon.svg",
2246
2329
  downloadUrl: "https://www.tronlink.org/",
2247
- description: "The official Tron wallet"
2330
+ description: "TronWeb \u517C\u5BB9\u94B1\u5305\uFF08\u652F\u6301 TronLink\u3001TokenPocket \u7B49\uFF09"
2248
2331
  },
2249
2332
  ["walletconnect-tron" /* WALLETCONNECT_TRON */]: {
2250
2333
  type: "walletconnect-tron" /* WALLETCONNECT_TRON */,
@@ -2344,7 +2427,9 @@ var WalletDetector = class {
2344
2427
  return !!w.ethereum;
2345
2428
  }
2346
2429
  /**
2347
- * 检测 TronLink
2430
+ * 检测 TronWeb 兼容钱包
2431
+ * 支持所有提供 window.tronWeb 或 window.tronLink 接口的钱包
2432
+ * 包括但不限于:TronLink、TokenPocket 等
2348
2433
  */
2349
2434
  isTronLinkAvailable() {
2350
2435
  const w = window;