@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.
@@ -822,7 +822,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
822
822
  super(...arguments);
823
823
  this.type = "tronlink" /* TRONLINK */;
824
824
  this.chainType = ChainType.TRON;
825
- this.name = "TronLink";
825
+ this.name = "TronWeb";
826
826
  this.icon = "https://www.tronlink.org/static/logoIcon.svg";
827
827
  /**
828
828
  * 轮询检测账户变化(备用方案)
@@ -866,16 +866,57 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
866
866
  await this.ensureAvailable();
867
867
  try {
868
868
  this.setState("connecting" /* CONNECTING */);
869
+ const w = window;
869
870
  const tronWeb = this.getTronWeb();
870
- const result = await tronWeb.request({
871
- method: "tron_requestAccounts"
872
- });
873
- if (!result || !result.code || result.code !== 200) {
874
- throw new ConnectionRejectedError(this.type);
871
+ if (tronWeb.ready) {
872
+ await tronWeb.ready;
873
+ }
874
+ let address = tronWeb.defaultAddress?.base58;
875
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
876
+ try {
877
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
878
+ } catch (e) {
879
+ }
880
+ }
881
+ if (!address) {
882
+ for (let i = 0; i < 20; i++) {
883
+ await new Promise((resolve) => setTimeout(resolve, 100));
884
+ address = tronWeb.defaultAddress?.base58;
885
+ if (address) break;
886
+ }
887
+ }
888
+ if (!address) {
889
+ if (w.tronLink && typeof w.tronLink.request === "function") {
890
+ try {
891
+ const result = await w.tronLink.request({
892
+ method: "tron_requestAccounts"
893
+ });
894
+ if (!result || result.code !== 200) {
895
+ throw new ConnectionRejectedError(this.type);
896
+ }
897
+ } catch (error) {
898
+ if (error.code === 4001 || error.message?.includes("User rejected") || error.message?.includes("rejected")) {
899
+ throw new ConnectionRejectedError(this.type);
900
+ }
901
+ }
902
+ }
903
+ address = tronWeb.defaultAddress?.base58;
904
+ if (!address && tronWeb.defaultAddress?.hex && tronWeb.address && typeof tronWeb.address.fromHex === "function") {
905
+ try {
906
+ address = tronWeb.address.fromHex(tronWeb.defaultAddress.hex);
907
+ } catch (e) {
908
+ }
909
+ }
910
+ if (!address) {
911
+ for (let i = 0; i < 20; i++) {
912
+ await new Promise((resolve) => setTimeout(resolve, 100));
913
+ address = tronWeb.defaultAddress?.base58;
914
+ if (address) break;
915
+ }
916
+ }
875
917
  }
876
- const address = tronWeb.defaultAddress?.base58;
877
918
  if (!address) {
878
- throw new Error("Failed to get Tron address");
919
+ throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
879
920
  }
880
921
  const tronChainId = chainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
881
922
  const account = {
@@ -901,9 +942,9 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
901
942
  /**
902
943
  * 签名消息
903
944
  *
904
- * Note: TronLink supports two signing methods:
905
- * - trx.sign(): Signs a transaction object
906
- * - trx.signMessageV2(): Signs a plain text message (what we use here)
945
+ * Note: TronWeb 兼容钱包支持两种签名方法:
946
+ * - trx.sign(): 签名交易对象
947
+ * - trx.signMessageV2(): 签名纯文本消息(我们使用此方法)
907
948
  */
908
949
  async signMessage(message) {
909
950
  this.ensureConnected();
@@ -951,17 +992,41 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
951
992
  }
952
993
  /**
953
994
  * 读取合约
995
+ * 参考 webserver 的实现,使用 TronWeb 合约实例的标准 call() 方法
954
996
  */
955
997
  async readContract(params) {
956
998
  this.ensureConnected();
957
999
  try {
958
1000
  const tronWeb = this.getTronWeb();
959
- const contract = await tronWeb.contract(params.abi, params.address);
960
- const result = await contract[params.functionName](...params.args || []).call();
961
- return result;
1001
+ if (!this.currentAccount) {
1002
+ throw new Error("No account connected");
1003
+ }
1004
+ try {
1005
+ const contract = await tronWeb.contract(params.abi, params.address);
1006
+ const method = contract[params.functionName];
1007
+ if (!method || typeof method !== "function") {
1008
+ throw new Error(`Function ${params.functionName} not found in contract ABI`);
1009
+ }
1010
+ const result = await method(...params.args || []).call();
1011
+ return result;
1012
+ } catch (method1Error) {
1013
+ console.warn("\u26A0\uFE0F [\u65B9\u6CD51] TronWeb\u6807\u51C6\u65B9\u6CD5\u5931\u8D25\uFF0C\u5C1D\u8BD5\u65B9\u6CD52:", method1Error.message);
1014
+ try {
1015
+ const contract2 = await tronWeb.contract().at(params.address);
1016
+ const method2 = contract2[params.functionName];
1017
+ if (!method2 || typeof method2 !== "function") {
1018
+ throw new Error(`Function ${params.functionName} not found in contract`);
1019
+ }
1020
+ const result = await method2(...params.args || []).call();
1021
+ return result;
1022
+ } catch (method2Error) {
1023
+ console.error("\u26A0\uFE0F [\u65B9\u6CD52] \u4E5F\u5931\u8D25:", method2Error.message);
1024
+ throw method1Error;
1025
+ }
1026
+ }
962
1027
  } catch (error) {
963
1028
  console.error("Read contract error:", error);
964
- throw new Error(`Failed to read contract: ${error.message}`);
1029
+ throw new Error(`Failed to read contract: ${error.message || "Unknown error"}`);
965
1030
  }
966
1031
  }
967
1032
  /**
@@ -1006,26 +1071,38 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1006
1071
  }));
1007
1072
  console.log("[TronLink] Transaction options:", options);
1008
1073
  console.log("[TronLink] Parameters:", parameter);
1009
- const transaction = await tronWeb.transactionBuilder.triggerSmartContract(
1074
+ const functionSelector = params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")";
1075
+ console.log("[TronLink] Function selector:", functionSelector);
1076
+ console.log("[TronLink] Transaction options:", options);
1077
+ console.log("[TronLink] Parameters:", parameter);
1078
+ const tx = await tronWeb.transactionBuilder.triggerSmartContract(
1010
1079
  params.address,
1011
- params.functionName + "(" + functionAbi.inputs.map((i) => i.type).join(",") + ")",
1080
+ functionSelector,
1012
1081
  options,
1013
1082
  parameter,
1014
1083
  this.currentAccount.nativeAddress
1015
1084
  );
1016
- console.log("[TronLink] Transaction built:", transaction);
1017
- if (!transaction || !transaction.transaction) {
1085
+ console.log("[TronLink] Transaction built:", tx);
1086
+ if (!tx || !tx.transaction) {
1018
1087
  throw new Error("Failed to build transaction");
1019
1088
  }
1020
- const signedTx = await tronWeb.trx.sign(transaction.transaction);
1089
+ console.log("[TronLink] Requesting user signature...");
1090
+ const signedTx = await tronWeb.trx.sign(tx.transaction);
1091
+ console.log("[TronLink] Transaction signed:", signedTx);
1092
+ const txID = signedTx.txID;
1093
+ console.log("[TronLink] Transaction hash (txID):", txID);
1094
+ console.log("[TronLink] Broadcasting transaction...");
1021
1095
  const broadcast = await tronWeb.trx.sendRawTransaction(signedTx);
1022
1096
  console.log("[TronLink] Broadcast result:", broadcast);
1023
- if (!broadcast.result) {
1024
- throw new Error(broadcast.message || "Transaction broadcast failed");
1097
+ if (broadcast && broadcast.result === true) {
1098
+ return txID || broadcast.txid || "";
1099
+ } else {
1100
+ if (txID) {
1101
+ console.warn("[TronLink] Broadcast returned false but txID exists:", txID);
1102
+ return txID;
1103
+ }
1104
+ throw new Error(broadcast?.message || "Transaction broadcast failed");
1025
1105
  }
1026
- const txHash = broadcast.txid || broadcast.transaction?.txID;
1027
- console.log("[TronLink] Transaction hash:", txHash);
1028
- return txHash || "";
1029
1106
  } catch (error) {
1030
1107
  console.error("Write contract error:", error);
1031
1108
  if (error.message?.includes("User rejected") || error.message?.includes("Confirmation declined")) {
@@ -1078,7 +1155,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1078
1155
  return this.getTronWeb();
1079
1156
  }
1080
1157
  /**
1081
- * 获取浏览器中的 TronWeb
1158
+ * 获取浏览器中的 TronWeb 实例
1159
+ * 支持所有 TronWeb 兼容的钱包,包括:
1160
+ * - TronLink (window.tronLink.tronWeb 或 window.tronWeb)
1161
+ * - TokenPocket (window.tronWeb)
1162
+ * - 其他提供 window.tronWeb 接口的钱包
1082
1163
  */
1083
1164
  getBrowserProvider() {
1084
1165
  if (typeof window === "undefined") {
@@ -1093,7 +1174,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1093
1174
  getTronWeb() {
1094
1175
  const provider = this.getBrowserProvider();
1095
1176
  if (!provider) {
1096
- throw new Error("TronWeb not found");
1177
+ 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");
1097
1178
  }
1098
1179
  return provider;
1099
1180
  }
@@ -1115,9 +1196,11 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1115
1196
  w.tronLink.on("disconnect", this.handleDisconnect);
1116
1197
  } else if (w.tronWeb && w.tronWeb.eventServer) {
1117
1198
  this.startPolling();
1199
+ } else {
1200
+ this.startPolling();
1118
1201
  }
1119
1202
  } catch (error) {
1120
- console.warn("TronLink event listener setup failed:", error);
1203
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u8BBE\u7F6E\u5931\u8D25\uFF0C\u4F7F\u7528\u8F6E\u8BE2\u65B9\u5F0F:", error);
1121
1204
  this.startPolling();
1122
1205
  }
1123
1206
  }
@@ -1133,7 +1216,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
1133
1216
  w.tronLink.off("disconnect", this.handleDisconnect);
1134
1217
  }
1135
1218
  } catch (error) {
1136
- console.warn("TronLink event listener removal failed:", error);
1219
+ console.warn("TronWeb \u94B1\u5305\u4E8B\u4EF6\u76D1\u542C\u79FB\u9664\u5931\u8D25:", error);
1137
1220
  }
1138
1221
  this.stopPolling();
1139
1222
  }