@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.mjs CHANGED
@@ -913,7 +913,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
913
913
  super(...arguments);
914
914
  this.type = "tronlink" /* TRONLINK */;
915
915
  this.chainType = ChainType.TRON;
916
- this.name = "TronLink";
916
+ this.name = "TronWeb";
917
917
  this.icon = "https://www.tronlink.org/static/logoIcon.svg";
918
918
  /**
919
919
  * 轮询检测账户变化(备用方案)
@@ -957,16 +957,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
957
957
  await this.ensureAvailable();
958
958
  try {
959
959
  this.setState("connecting" /* CONNECTING */);
960
+ const w = window;
960
961
  const tronWeb = this.getTronWeb();
961
- const result = await tronWeb.request({
962
- method: "tron_requestAccounts"
963
- });
964
- if (!result || !result.code || result.code !== 200) {
965
- throw new ConnectionRejectedError(this.type);
962
+ if (tronWeb.ready) {
963
+ await tronWeb.ready;
964
+ }
965
+ let address = tronWeb.defaultAddress?.base58;
966
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
967
+ try {
968
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
969
+ } catch (e) {
970
+ }
971
+ }
972
+ if (!address) {
973
+ for (let i = 0; i < 20; i++) {
974
+ await new Promise((resolve) => setTimeout(resolve, 100));
975
+ address = tronWeb.defaultAddress?.base58;
976
+ if (address) break;
977
+ }
978
+ }
979
+ if (!address) {
980
+ if (w.tronLink && typeof w.tronLink.request === "function") {
981
+ try {
982
+ const result = await w.tronLink.request({
983
+ method: "tron_requestAccounts"
984
+ });
985
+ if (!result || result.code !== 200) {
986
+ throw new ConnectionRejectedError(this.type);
987
+ }
988
+ } catch (error) {
989
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
990
+ throw new ConnectionRejectedError(this.type);
991
+ }
992
+ }
993
+ }
994
+ address = tronWeb.defaultAddress?.base58;
995
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
996
+ try {
997
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
998
+ } catch (e) {
999
+ }
1000
+ }
1001
+ if (!address) {
1002
+ for (let i = 0; i < 20; i++) {
1003
+ await new Promise((resolve) => setTimeout(resolve, 100));
1004
+ address = tronWeb.defaultAddress?.base58;
1005
+ if (address) break;
1006
+ }
1007
+ }
966
1008
  }
967
- const address = tronWeb.defaultAddress?.base58;
968
1009
  if (!address) {
969
- throw new Error("Failed to get Tron address");
1010
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
970
1011
  }
971
1012
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
972
1013
  const account = {
@@ -992,9 +1033,9 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
992
1033
  /**
993
1034
  * 签名消息
994
1035
  *
995
- * Note: TronLink supports two signing methods:
996
- * - trx.sign(): Signs a transaction object
997
- * - trx.signMessageV2(): Signs a plain text message (what we use here)
1036
+ * Note: TronWeb 兼容钱包支持两种签名方法:
1037
+ * - trx.sign(): 签名交易对象
1038
+ * - trx.signMessageV2(): 签名纯文本消息(我们使用此方法)
998
1039
  */
999
1040
  async signMessage(message) {
1000
1041
  this.ensureConnected();
@@ -1042,17 +1083,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1042
1083
  }
1043
1084
  /**
1044
1085
  * 读取合约
1086
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
1045
1087
  */
1046
1088
  async readContract(params) {
1047
1089
  this.ensureConnected();
1048
1090
  try {
1049
1091
  const tronWeb = this.getTronWeb();
1050
- const contract = await tronWeb.contract(params.abi, params.address);
1051
- const result = await contract[params.functionName](...params.args || []).call();
1052
- return result;
1092
+ if (!this.currentAccount) {
1093
+ throw new Error("No account connected");
1094
+ }
1095
+ try {
1096
+ const contract = await tronWeb.contract(params.abi, params.address);
1097
+ const method = contract[params.functionName];
1098
+ if (!method || typeof method !== "function") {
1099
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1100
+ }
1101
+ const result = await method(...params.args || []).call();
1102
+ return result;
1103
+ } catch (method1Error) {
1104
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1105
+ try {
1106
+ const contract2 = await tronWeb.contract().at(params.address);
1107
+ const method2 = contract2[params.functionName];
1108
+ if (!method2 || typeof method2 !== "function") {
1109
+ throw new Error(`Function ${params.functionName} not found in contract`);
1110
+ }
1111
+ const result = await method2(...params.args || []).call();
1112
+ return result;
1113
+ } catch (method2Error) {
1114
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1115
+ throw method1Error;
1116
+ }
1117
+ }
1053
1118
  } catch (error) {
1054
1119
  console.error("Read contract error:", error);
1055
- throw new Error(`Failed to read contract: ${error.message}`);
1120
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
1056
1121
  }
1057
1122
  }
1058
1123
  /**
@@ -1097,26 +1162,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1097
1162
  }));
1098
1163
  console.log("[TronLink] Transaction options:", options);
1099
1164
  console.log("[TronLink] Parameters:", parameter);
1100
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1165
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1166
+ console.log("[TronLink] Function selector:", functionSelector);
1167
+ console.log("[TronLink] Transaction options:", options);
1168
+ console.log("[TronLink] Parameters:", parameter);
1169
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1101
1170
  params.address,
1102
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1171
+ functionSelector,
1103
1172
  options,
1104
1173
  parameter,
1105
1174
  this.currentAccount.nativeAddress
1106
1175
  );
1107
- console.log("[TronLink] Transaction built:", transaction);
1108
- if (!transaction || !transaction.transaction) {
1176
+ console.log("[TronLink] Transaction built:", tx);
1177
+ if (!tx || !tx.transaction) {
1109
1178
  throw new Error("Failed to build transaction");
1110
1179
  }
1111
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1180
+ console.log("[TronLink] Requesting user signature...");
1181
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1182
+ console.log("[TronLink] Transaction signed:", signedTx);
1183
+ const txID = signedTx.txID;
1184
+ console.log("[TronLink] Transaction hash (txID):", txID);
1185
+ console.log("[TronLink] Broadcasting transaction...");
1112
1186
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1113
1187
  console.log("[TronLink] Broadcast result:", broadcast);
1114
- if (!broadcast.result) {
1115
- throw new Error(broadcast.message || "Transaction broadcast failed");
1188
+ if (broadcast && broadcast.result === true) {
1189
+ return txID || broadcast.txid || "";
1190
+ } else {
1191
+ if (txID) {
1192
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1193
+ return txID;
1194
+ }
1195
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1116
1196
  }
1117
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1118
- console.log("[TronLink] Transaction hash:", txHash);
1119
- return txHash || "";
1120
1197
  } catch (error) {
1121
1198
  console.error("Write contract error:", error);
1122
1199
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {
@@ -1169,7 +1246,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1169
1246
  return this.getTronWeb();
1170
1247
  }
1171
1248
  /**
1172
- * 获取浏览器中的 TronWeb
1249
+ * 获取浏览器中的 TronWeb 实例
1250
+ * 支持所有 TronWeb 兼容的钱包,包括:
1251
+ * - TronLink (window.tronLink.tronWeb 或 window.tronWeb)
1252
+ * - TokenPocket (window.tronWeb)
1253
+ * - 其他提供 window.tronWeb 接口的钱包
1173
1254
  */
1174
1255
  getBrowserProvider() {
1175
1256
  if (typeof window === "undefined") {
@@ -1184,7 +1265,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1184
1265
  getTronWeb() {
1185
1266
  const provider = this.getBrowserProvider();
1186
1267
  if (!provider) {
1187
- throw new Error("TronWeb not found");
1268
+ 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");
1188
1269
  }
1189
1270
  return provider;
1190
1271
  }
@@ -1206,9 +1287,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1206
1287
  w.tronLink.on("disconnect", this.handleDisconnect);
1207
1288
  } else if (w.tronWeb && w.tronWeb.eventServer) {
1208
1289
  this.startPolling();
1290
+ } else {
1291
+ this.startPolling();
1209
1292
  }
1210
1293
  } catch (error) {
1211
- console.warn("TronLink event listener setup failed:", error);
1294
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u8BBE\u7F6E\u5931\u8D25\uFF0C\u4F7F\u7528\u8F6E\u8BE2\u65B9\u5F0F:", error);
1212
1295
  this.startPolling();
1213
1296
  }
1214
1297
  }
@@ -1224,7 +1307,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1224
1307
  w.tronLink.off("disconnect", this.handleDisconnect);
1225
1308
  }
1226
1309
  } catch (error) {
1227
- console.warn("TronLink event listener removal failed:", error);
1310
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u79FB\u9664\u5931\u8D25:", error);
1228
1311
  }
1229
1312
  this.stopPolling();
1230
1313
  }
@@ -2232,11 +2315,11 @@ var SUPPORTED_WALLETS = {
2232
2315
  },
2233
2316
  ["tronlink" /* TRONLINK */]: {
2234
2317
  type: "tronlink" /* TRONLINK */,
2235
- name: "TronLink",
2318
+ name: "TronWeb",
2236
2319
  chainType: ChainType.TRON,
2237
2320
  icon: "https://www.tronlink.org/static/logoIcon.svg",
2238
2321
  downloadUrl: "https://www.tronlink.org/",
2239
- description: "The official Tron wallet"
2322
+ description: "TronWeb \u517C\u5BB9\u94B1\u5305\uFF08\u652F\u6301 TronLink\u3001TokenPocket \u7B49\uFF09"
2240
2323
  },
2241
2324
  ["walletconnect-tron" /* WALLETCONNECT_TRON */]: {
2242
2325
  type: "walletconnect-tron" /* WALLETCONNECT_TRON */,
@@ -2336,7 +2419,9 @@ var WalletDetector = class {
2336
2419
  return !!w.ethereum;
2337
2420
  }
2338
2421
  /**
2339
- * 检测 TronLink
2422
+ * 检测 TronWeb 兼容钱包
2423
+ * 支持所有提供 window.tronWeb 或 window.tronLink 接口的钱包
2424
+ * 包括但不限于:TronLink、TokenPocket 等
2340
2425
  */
2341
2426
  isTronLinkAvailable() {
2342
2427
  const w = window;