@@ -9,28 +9,7 @@ import {
99 TronJsonRpcProvider ,
1010} from './TronJsonRpcProvider.js' ;
1111import { TransactionRequest } from '@ethersproject/providers' ;
12-
13- /**
14- * Extract custom_rpc_header query params from a URL into a headers object.
15- * e.g. "https://api.trongrid.io?custom_rpc_header=TRON-PRO-API-KEY:abc"
16- * returns { "TRON-PRO-API-KEY": "abc" }
17- */
18- function parseCustomHeaders ( url : string ) : Record < string , string > {
19- const headers : Record < string , string > = { } ;
20- try {
21- const parsed = new URL ( url ) ;
22- for ( const [ key , value ] of parsed . searchParams ) {
23- if ( key !== 'custom_rpc_header' ) continue ;
24- const colonIdx = value . indexOf ( ':' ) ;
25- if ( colonIdx > 0 ) {
26- headers [ value . slice ( 0 , colonIdx ) ] = value . slice ( colonIdx + 1 ) ;
27- }
28- }
29- } catch {
30- // Not a valid URL, return empty headers
31- }
32- return headers ;
33- }
12+ import { stripCustomRpcHeaders } from './urlUtils.js' ;
3413
3514/** Union of possible TronWeb transaction types */
3615export type TronTransaction =
@@ -80,13 +59,11 @@ export class TronWallet extends Wallet {
8059 // TronWeb needs the base HTTP API URL — strip /jsonrpc path if present, and
8160 // fall back to public TronGrid for third-party providers that only serve JSON-RPC.
8261 // Extract custom headers before stripping path, as they may contain API keys.
83- const headers = parseCustomHeaders ( tronUrl ) ;
84- const parsed = new URL ( tronUrl ) ;
62+ const { url : cleanTronUrl , headers } = stripCustomRpcHeaders ( tronUrl ) ;
63+ const parsed = new URL ( cleanTronUrl ) ;
8564 if ( parsed . pathname . endsWith ( '/jsonrpc' ) ) {
8665 parsed . pathname = parsed . pathname . slice ( 0 , - 8 ) ;
8766 }
88- // Strip custom_rpc_header params from the base URL
89- parsed . searchParams . delete ( 'custom_rpc_header' ) ;
9067 const baseUrl = parsed . toString ( ) ;
9168 const tronWebUrl =
9269 / ^ h t t p s ? : \/ \/ ( l o c a l h o s t | 1 2 7 \. 0 \. 0 \. 1 | [ ^ / ] * t r o n g r i d ) / . test ( baseUrl )
@@ -195,14 +172,24 @@ export class TronTransactionBuilder extends TronWeb {
195172 jsonRpcUrl ?: string ,
196173 headers ?: Record < string , string > ,
197174 ) {
198- super ( { fullHost : tronWebUrl , headers } ) ;
175+ // Strip custom_rpc_header from the URL and merge with any provided headers
176+ const { url : cleanTronWebUrl , headers : parsedHeaders } =
177+ stripCustomRpcHeaders ( tronWebUrl ) ;
178+ const mergedHeaders = { ...parsedHeaders , ...headers } ;
179+ super ( { fullHost : cleanTronWebUrl , headers : mergedHeaders } ) ;
199180
200181 this . tronAddress = tronAddress ;
201182 this . setAddress ( this . tronAddress ) ;
202- // Use provided JSON-RPC URL, or derive from TronWeb URL
203- const rpcUrl =
204- jsonRpcUrl ??
205- ( tronWebUrl . endsWith ( '/jsonrpc' ) ? tronWebUrl : `${ tronWebUrl } /jsonrpc` ) ;
183+ // Use provided JSON-RPC URL, or derive from TronWeb URL.
184+ // Use URL API so /jsonrpc goes into the pathname, not after query params.
185+ let rpcUrl = jsonRpcUrl ;
186+ if ( ! rpcUrl ) {
187+ const u = new URL ( tronWebUrl ) ;
188+ if ( ! u . pathname . endsWith ( '/jsonrpc' ) ) {
189+ u . pathname = u . pathname . replace ( / \/ $ / , '' ) + '/jsonrpc' ;
190+ }
191+ rpcUrl = u . toString ( ) ;
192+ }
206193 this . provider = new TronJsonRpcProvider ( rpcUrl ) ;
207194 this . tronAddressHex = this . address . toHex ( this . tronAddress ) ;
208195 }
0 commit comments