@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.
@@ -829,7 +829,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
829
829
  super(...arguments);
830
830
  this.type = "tronlink" /* TRONLINK */;
831
831
  this.chainType = ChainType.TRON;
832
- this.name = "TronLink";
832
+ this.name = "TronWeb";
833
833
  this.icon = "https://www.tronlink.org/static/logoIcon.svg";
834
834
  /**
835
835
  * 轮询检测账户变化(备用方案)
@@ -873,16 +873,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
873
873
  await this.ensureAvailable();
874
874
  try {
875
875
  this.setState("connecting" /* CONNECTING */);
876
+ const w = window;
876
877
  const tronWeb = this.getTronWeb();
877
- const result = await tronWeb.request({
878
- method: "tron_requestAccounts"
879
- });
880
- if (!result || !result.code || result.code !== 200) {
881
- throw new ConnectionRejectedError(this.type);
878
+ if (tronWeb.ready) {
879
+ await tronWeb.ready;
880
+ }
881
+ let address = tronWeb.defaultAddress?.base58;
882
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
883
+ try {
884
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
885
+ } catch (e) {
886
+ }
887
+ }
888
+ if (!address) {
889
+ for (let i = 0; i < 20; i++) {
890
+ await new Promise((resolve) => setTimeout(resolve, 100));
891
+ address = tronWeb.defaultAddress?.base58;
892
+ if (address) break;
893
+ }
894
+ }
895
+ if (!address) {
896
+ if (w.tronLink && typeof w.tronLink.request === "function") {
897
+ try {
898
+ const result = await w.tronLink.request({
899
+ method: "tron_requestAccounts"
900
+ });
901
+ if (!result || result.code !== 200) {
902
+ throw new ConnectionRejectedError(this.type);
903
+ }
904
+ } catch (error) {
905
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
906
+ throw new ConnectionRejectedError(this.type);
907
+ }
908
+ }
909
+ }
910
+ address = tronWeb.defaultAddress?.base58;
911
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
912
+ try {
913
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
914
+ } catch (e) {
915
+ }
916
+ }
917
+ if (!address) {
918
+ for (let i = 0; i < 20; i++) {
919
+ await new Promise((resolve) => setTimeout(resolve, 100));
920
+ address = tronWeb.defaultAddress?.base58;
921
+ if (address) break;
922
+ }
923
+ }
882
924
  }
883
- const address = tronWeb.defaultAddress?.base58;
884
925
  if (!address) {
885
- throw new Error("Failed to get Tron address");
926
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
886
927
  }
887
928
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
888
929
  const account = {
@@ -908,9 +949,9 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
908
949
  /**
909
950
  * 签名消息
910
951
  *
911
- * Note: TronLink supports two signing methods:
912
- * - trx.sign(): Signs a transaction object
913
- * - trx.signMessageV2(): Signs a plain text message (what we use here)
952
+ * Note: TronWeb 兼容钱包支持两种签名方法:
953
+ * - trx.sign(): 签名交易对象
954
+ * - trx.signMessageV2(): 签名纯文本消息(我们使用此方法)
914
955
  */
915
956
  async signMessage(message) {
916
957
  this.ensureConnected();
@@ -958,17 +999,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
958
999
  }
959
1000
  /**
960
1001
  * 读取合约
1002
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
961
1003
  */
962
1004
  async readContract(params) {
963
1005
  this.ensureConnected();
964
1006
  try {
965
1007
  const tronWeb = this.getTronWeb();
966
- const contract = await tronWeb.contract(params.abi, params.address);
967
- const result = await contract[params.functionName](...params.args || []).call();
968
- return result;
1008
+ if (!this.currentAccount) {
1009
+ throw new Error("No account connected");
1010
+ }
1011
+ try {
1012
+ const contract = await tronWeb.contract(params.abi, params.address);
1013
+ const method = contract[params.functionName];
1014
+ if (!method || typeof method !== "function") {
1015
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1016
+ }
1017
+ const result = await method(...params.args || []).call();
1018
+ return result;
1019
+ } catch (method1Error) {
1020
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1021
+ try {
1022
+ const contract2 = await tronWeb.contract().at(params.address);
1023
+ const method2 = contract2[params.functionName];
1024
+ if (!method2 || typeof method2 !== "function") {
1025
+ throw new Error(`Function ${params.functionName} not found in contract`);
1026
+ }
1027
+ const result = await method2(...params.args || []).call();
1028
+ return result;
1029
+ } catch (method2Error) {
1030
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1031
+ throw method1Error;
1032
+ }
1033
+ }
969
1034
  } catch (error) {
970
1035
  console.error("Read contract error:", error);
971
- throw new Error(`Failed to read contract: ${error.message}`);
1036
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
972
1037
  }
973
1038
  }
974
1039
  /**
@@ -1013,26 +1078,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1013
1078
  }));
1014
1079
  console.log("[TronLink] Transaction options:", options);
1015
1080
  console.log("[TronLink] Parameters:", parameter);
1016
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1081
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1082
+ console.log("[TronLink] Function selector:", functionSelector);
1083
+ console.log("[TronLink] Transaction options:", options);
1084
+ console.log("[TronLink] Parameters:", parameter);
1085
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1017
1086
  params.address,
1018
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1087
+ functionSelector,
1019
1088
  options,
1020
1089
  parameter,
1021
1090
  this.currentAccount.nativeAddress
1022
1091
  );
1023
- console.log("[TronLink] Transaction built:", transaction);
1024
- if (!transaction || !transaction.transaction) {
1092
+ console.log("[TronLink] Transaction built:", tx);
1093
+ if (!tx || !tx.transaction) {
1025
1094
  throw new Error("Failed to build transaction");
1026
1095
  }
1027
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1096
+ console.log("[TronLink] Requesting user signature...");
1097
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1098
+ console.log("[TronLink] Transaction signed:", signedTx);
1099
+ const txID = signedTx.txID;
1100
+ console.log("[TronLink] Transaction hash (txID):", txID);
1101
+ console.log("[TronLink] Broadcasting transaction...");
1028
1102
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1029
1103
  console.log("[TronLink] Broadcast result:", broadcast);
1030
- if (!broadcast.result) {
1031
- throw new Error(broadcast.message || "Transaction broadcast failed");
1104
+ if (broadcast && broadcast.result === true) {
1105
+ return txID || broadcast.txid || "";
1106
+ } else {
1107
+ if (txID) {
1108
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1109
+ return txID;
1110
+ }
1111
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1032
1112
  }
1033
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1034
- console.log("[TronLink] Transaction hash:", txHash);
1035
- return txHash || "";
1036
1113
  } catch (error) {
1037
1114
  console.error("Write contract error:", error);
1038
1115
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {
@@ -1085,7 +1162,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1085
1162
  return this.getTronWeb();
1086
1163
  }
1087
1164
  /**
1088
- * 获取浏览器中的 TronWeb
1165
+ * 获取浏览器中的 TronWeb 实例
1166
+ * 支持所有 TronWeb 兼容的钱包,包括:
1167
+ * - TronLink (window.tronLink.tronWeb 或 window.tronWeb)
1168
+ * - TokenPocket (window.tronWeb)
1169
+ * - 其他提供 window.tronWeb 接口的钱包
1089
1170
  */
1090
1171
  getBrowserProvider() {
1091
1172
  if (typeof window === "undefined") {
@@ -1100,7 +1181,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1100
1181
  getTronWeb() {
1101
1182
  const provider = this.getBrowserProvider();
1102
1183
  if (!provider) {
1103
- throw new Error("TronWeb not found");
1184
+ 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");
1104
1185
  }
1105
1186
  return provider;
1106
1187
  }
@@ -1122,9 +1203,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1122
1203
  w.tronLink.on("disconnect", this.handleDisconnect);
1123
1204
  } else if (w.tronWeb && w.tronWeb.eventServer) {
1124
1205
  this.startPolling();
1206
+ } else {
1207
+ this.startPolling();
1125
1208
  }
1126
1209
  } catch (error) {
1127
- console.warn("TronLink event listener setup failed:", error);
1210
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u8BBE\u7F6E\u5931\u8D25\uFF0C\u4F7F\u7528\u8F6E\u8BE2\u65B9\u5F0F:", error);
1128
1211
  this.startPolling();
1129
1212
  }
1130
1213
  }
@@ -1140,7 +1223,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1140
1223
  w.tronLink.off("disconnect", this.handleDisconnect);
1141
1224
  }
1142
1225
  } catch (error) {
1143
- console.warn("TronLink event listener removal failed:", error);
1226
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u79FB\u9664\u5931\u8D25:", error);
1144
1227
  }
1145
1228
  this.stopPolling();
1146
1229
  }