decane-connect-kit 1.0.0 → 1.0.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.cjs +90 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +90 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1056,7 +1056,14 @@ async function getSolanaAddress(wallet) {
|
|
|
1056
1056
|
}
|
|
1057
1057
|
|
|
1058
1058
|
// src/tron/adapter.ts
|
|
1059
|
+
function getTronWeb() {
|
|
1060
|
+
if (typeof window === "undefined") return void 0;
|
|
1061
|
+
const win = window;
|
|
1062
|
+
return win.tronLink?.tronWeb ?? win.tronWeb;
|
|
1063
|
+
}
|
|
1059
1064
|
async function getTronAddress(provider) {
|
|
1065
|
+
const addr = getTronWeb()?.defaultAddress?.base58;
|
|
1066
|
+
if (addr) return addr;
|
|
1060
1067
|
try {
|
|
1061
1068
|
const result = await provider.request({ method: "tron_requestAccounts" });
|
|
1062
1069
|
const accounts = result;
|
|
@@ -1075,11 +1082,80 @@ async function tronRequest(provider, method, payload, account) {
|
|
|
1075
1082
|
method: "tron_signMessage",
|
|
1076
1083
|
params: { message: payload }
|
|
1077
1084
|
});
|
|
1078
|
-
case "wallet_sendTransaction":
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1085
|
+
case "wallet_sendTransaction": {
|
|
1086
|
+
const tronWeb = getTronWeb();
|
|
1087
|
+
const p = payload;
|
|
1088
|
+
const toAddress = p.to;
|
|
1089
|
+
const sunAmount = typeof p.value === "string" ? parseInt(p.value, 10) : p.value ?? 0;
|
|
1090
|
+
let unsignedTx;
|
|
1091
|
+
if (tronWeb?.transactionBuilder?.sendTrx) {
|
|
1092
|
+
if (p.data && p.data !== "0x") {
|
|
1093
|
+
const res = await tronWeb.transactionBuilder.triggerSmartContract(
|
|
1094
|
+
toAddress,
|
|
1095
|
+
"transfer(address,uint256)",
|
|
1096
|
+
{ callValue: 0, feeLimit: 1e8 },
|
|
1097
|
+
[],
|
|
1098
|
+
account
|
|
1099
|
+
);
|
|
1100
|
+
unsignedTx = res.transaction;
|
|
1101
|
+
} else {
|
|
1102
|
+
unsignedTx = await tronWeb.transactionBuilder.sendTrx(toAddress, sunAmount, account);
|
|
1103
|
+
}
|
|
1104
|
+
} else {
|
|
1105
|
+
const host = tronWeb?.fullNode?.host ?? "https://api.trongrid.io";
|
|
1106
|
+
const endpoint = p.data && p.data !== "0x" ? `${host}/wallet/triggersmartcontract` : `${host}/wallet/createtransaction`;
|
|
1107
|
+
const body = p.data && p.data !== "0x" ? {
|
|
1108
|
+
owner_address: account,
|
|
1109
|
+
contract_address: toAddress,
|
|
1110
|
+
function_selector: "transfer(address,uint256)",
|
|
1111
|
+
fee_limit: 1e8,
|
|
1112
|
+
call_value: 0,
|
|
1113
|
+
visible: true
|
|
1114
|
+
} : {
|
|
1115
|
+
owner_address: account,
|
|
1116
|
+
to_address: toAddress,
|
|
1117
|
+
amount: sunAmount,
|
|
1118
|
+
visible: true
|
|
1119
|
+
};
|
|
1120
|
+
const res = await fetch(endpoint, {
|
|
1121
|
+
method: "POST",
|
|
1122
|
+
headers: { "Content-Type": "application/json" },
|
|
1123
|
+
body: JSON.stringify(body)
|
|
1124
|
+
});
|
|
1125
|
+
if (!res.ok) {
|
|
1126
|
+
throw new DecaneError(
|
|
1127
|
+
5022 /* PROVIDER_ERROR */,
|
|
1128
|
+
`Failed to build Tron transaction: ${res.statusText}`
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
const json = await res.json();
|
|
1132
|
+
unsignedTx = json.transaction ?? json;
|
|
1133
|
+
}
|
|
1134
|
+
let signedTx;
|
|
1135
|
+
if (tronWeb?.trx?.sign) {
|
|
1136
|
+
signedTx = await tronWeb.trx.sign(unsignedTx);
|
|
1137
|
+
} else {
|
|
1138
|
+
signedTx = await provider.request({
|
|
1139
|
+
method: "tron_signTransaction",
|
|
1140
|
+
params: unsignedTx
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
let txid;
|
|
1144
|
+
if (tronWeb?.trx?.sendRawTransaction) {
|
|
1145
|
+
const result = await tronWeb.trx.sendRawTransaction(signedTx);
|
|
1146
|
+
txid = result?.txid;
|
|
1147
|
+
} else {
|
|
1148
|
+
const result = await provider.request({
|
|
1149
|
+
method: "tron_sendRawTransaction",
|
|
1150
|
+
params: signedTx
|
|
1151
|
+
});
|
|
1152
|
+
txid = result?.txid;
|
|
1153
|
+
}
|
|
1154
|
+
if (!txid) {
|
|
1155
|
+
throw new DecaneError(5022 /* PROVIDER_ERROR */, "Tron transaction broadcast failed");
|
|
1156
|
+
}
|
|
1157
|
+
return txid;
|
|
1158
|
+
}
|
|
1083
1159
|
default:
|
|
1084
1160
|
throw new DecaneError(
|
|
1085
1161
|
5030 /* METHOD_NOT_SUPPORTED */,
|
|
@@ -2383,6 +2459,11 @@ function useDecaneContext() {
|
|
|
2383
2459
|
|
|
2384
2460
|
// src/ui/hooks.ts
|
|
2385
2461
|
import { useState as useState3, useEffect as useEffect2, useCallback as useCallback3 } from "react";
|
|
2462
|
+
function toDecaneError(err) {
|
|
2463
|
+
if (err instanceof DecaneError) return err;
|
|
2464
|
+
const msg = err instanceof Error ? err.message : err?.message != null ? String(err.message) : String(err);
|
|
2465
|
+
return new DecaneError(5022 /* PROVIDER_ERROR */, msg);
|
|
2466
|
+
}
|
|
2386
2467
|
function useDecane() {
|
|
2387
2468
|
const { provider, theme, setTheme } = useDecaneContext();
|
|
2388
2469
|
const [, forceUpdate] = useState3(0);
|
|
@@ -2460,7 +2541,7 @@ function useChainSwitch() {
|
|
|
2460
2541
|
});
|
|
2461
2542
|
forceUpdate((n) => n + 1);
|
|
2462
2543
|
} catch (err) {
|
|
2463
|
-
const e =
|
|
2544
|
+
const e = toDecaneError(err);
|
|
2464
2545
|
setError(e);
|
|
2465
2546
|
throw err;
|
|
2466
2547
|
} finally {
|
|
@@ -2511,7 +2592,7 @@ function useSignMessage() {
|
|
|
2511
2592
|
setSignature(sig);
|
|
2512
2593
|
return sig;
|
|
2513
2594
|
} catch (err) {
|
|
2514
|
-
const e =
|
|
2595
|
+
const e = toDecaneError(err);
|
|
2515
2596
|
setError(e);
|
|
2516
2597
|
throw err;
|
|
2517
2598
|
} finally {
|
|
@@ -2549,7 +2630,7 @@ function useSendTransaction() {
|
|
|
2549
2630
|
setTxHash(hash);
|
|
2550
2631
|
return hash;
|
|
2551
2632
|
} catch (err) {
|
|
2552
|
-
const e =
|
|
2633
|
+
const e = toDecaneError(err);
|
|
2553
2634
|
setError(e);
|
|
2554
2635
|
throw err;
|
|
2555
2636
|
} finally {
|
|
@@ -2586,7 +2667,7 @@ function useSignTypedData() {
|
|
|
2586
2667
|
setSignature(sig);
|
|
2587
2668
|
return sig;
|
|
2588
2669
|
} catch (err) {
|
|
2589
|
-
const e =
|
|
2670
|
+
const e = toDecaneError(err);
|
|
2590
2671
|
setError(e);
|
|
2591
2672
|
throw err;
|
|
2592
2673
|
} finally {
|