@rango-dev/signer-tron 0.43.1-next.0 → 0.44.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # [0.44.0](https://github.com/rango-exchange/rango-client/compare/signer-tron@0.43.0...signer-tron@0.44.0) (2026-10-17)
2
+
3
+
4
+ ### Features
5
+
6
+ * **approval:** migrate EVM and Tron approval to the prerequisites flow ([23f3bf4](https://github.com/rango-exchange/rango-client/commit/23f3bf4bb871c5b367214c3b82c5786dd9748adc))
1
7
  # [0.43.0](https://github.com/rango-exchange/rango-client/compare/signer-tron@0.42.0...signer-tron@0.43.0) (2026-09-16)
2
8
 
3
9
 
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- var d=Object.defineProperty;var t=(a,r)=>d(a,"name",{value:r,configurable:!0});import{SignerError as n,SignerErrorCode as s}from"rango-types";var i=class a{static{t(this,"DefaultTronSigner")}provider;constructor(r){this.provider=r}static buildTx(r){let e={};return r.txID&&(e={...e,txID:r.txID}),r.visible!==void 0&&(e={...e,visible:r.visible}),r.__payload__&&(e={...e,__payload__:r.__payload__}),r.raw_data&&(e={...e,raw_data:r.raw_data}),r.raw_data_hex&&(e={...e,raw_data_hex:r.raw_data_hex}),e}async signMessage(r){try{return await this.provider.tronWeb.trx.signMessageV2(r)}catch(e){throw new n(s.SIGN_TX_ERROR,void 0,e)}}async signAndSendTx(r){try{let e=a.buildTx(r),o=await this.provider.tronWeb.trx.sign(e);return{hash:(await this.provider.tronWeb.trx.sendRawTransaction(o))?.transaction?.txID}}catch(e){throw new n(s.SEND_TX_ERROR,void 0,e)}}};export{i as DefaultTronSigner};
1
+ var _=Object.defineProperty;var o=(e,n)=>_(e,"name",{value:n,configurable:!0});import{SignerError as i,SignerErrorCode as a}from"rango-types";function h(e){if(!e)return;let n=e.startsWith("0x")?e.slice(2):e;if(n.length%2!==0||!/^[0-9a-f]+$/i.test(n))return e;let r=n.match(/.{2}/g)?.map(t=>parseInt(t,16))??[];return new TextDecoder().decode(new Uint8Array(r))}o(h,"decodeBroadcastMessage");var d=class e{static{o(this,"DefaultTronSigner")}provider;constructor(n){this.provider=n}static buildTx(n){let r={};return n.txID&&(r={...r,txID:n.txID}),n.visible!==void 0&&(r={...r,visible:n.visible}),n.__payload__&&(r={...r,__payload__:n.__payload__}),n.raw_data&&(r={...r,raw_data:n.raw_data}),n.raw_data_hex&&(r={...r,raw_data_hex:n.raw_data_hex}),r}async signMessage(n){try{return await this.provider.tronWeb.trx.signMessageV2(n)}catch(r){throw new i(a.SIGN_TX_ERROR,void 0,r)}}async signAndSendTx(n){let r,t;try{let s=e.buildTx(n);t=await this.provider.tronWeb.trx.sign(s),r=await this.provider.tronWeb.trx.sendRawTransaction(t)}catch(s){throw new i(a.SEND_TX_ERROR,void 0,s)}let c=r?.Error??(r?.code?h(r.message)??`The Tron node refused the transaction (${r.code}).`:void 0);if(c)throw new i(a.SEND_TX_ERROR,c);let g=r?.txid??r?.transaction?.txID??t?.txID;if(!g)throw new i(a.SEND_TX_ERROR,"Tron transaction was broadcast without a transaction hash.");return{hash:g}}};export{d as DefaultTronSigner};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/signer.ts"],
4
- "sourcesContent": ["import type { GenericSigner, TronTransaction } from 'rango-types';\n\nimport { SignerError, SignerErrorCode } from 'rango-types';\n\n/*\n * TODO - replace with real type\n * tslint:disable-next-line: no-any\n */\ntype TronExternalProvider = any;\n\nexport class DefaultTronSigner implements GenericSigner<TronTransaction> {\n private provider: TronExternalProvider;\n\n constructor(provider: TronExternalProvider) {\n this.provider = provider;\n }\n\n static buildTx(tronTx: TronTransaction) {\n let tx = {};\n if (!!tronTx.txID) {\n tx = { ...tx, txID: tronTx.txID };\n }\n if (tronTx.visible !== undefined) {\n tx = { ...tx, visible: tronTx.visible };\n }\n if (!!tronTx.__payload__) {\n tx = { ...tx, __payload__: tronTx.__payload__ };\n }\n if (!!tronTx.raw_data) {\n tx = { ...tx, raw_data: tronTx.raw_data };\n }\n if (!!tronTx.raw_data_hex) {\n tx = { ...tx, raw_data_hex: tronTx.raw_data_hex };\n }\n return tx;\n }\n async signMessage(msg: string): Promise<string> {\n try {\n return await this.provider.tronWeb.trx.signMessageV2(msg);\n } catch (error) {\n throw new SignerError(SignerErrorCode.SIGN_TX_ERROR, undefined, error);\n }\n }\n\n async signAndSendTx(tx: TronTransaction): Promise<{ hash: string }> {\n try {\n const transaction = DefaultTronSigner.buildTx(tx);\n const signedTxn = await this.provider.tronWeb.trx.sign(transaction);\n const receipt = await this.provider.tronWeb.trx.sendRawTransaction(\n signedTxn\n );\n const hash = receipt?.transaction?.txID;\n return { hash };\n } catch (error) {\n throw new SignerError(SignerErrorCode.SEND_TX_ERROR, undefined, error);\n }\n }\n}\n"],
5
- "mappings": "+EAEA,OAAS,eAAAA,EAAa,mBAAAC,MAAuB,cAQtC,IAAMC,EAAN,MAAMC,CAA4D,CAVzE,MAUyE,CAAAC,EAAA,0BAC/D,SAER,YAAYC,EAAgC,CAC1C,KAAK,SAAWA,CAClB,CAEA,OAAO,QAAQC,EAAyB,CACtC,IAAIC,EAAK,CAAC,EACV,OAAMD,EAAO,OACXC,EAAK,CAAE,GAAGA,EAAI,KAAMD,EAAO,IAAK,GAE9BA,EAAO,UAAY,SACrBC,EAAK,CAAE,GAAGA,EAAI,QAASD,EAAO,OAAQ,GAElCA,EAAO,cACXC,EAAK,CAAE,GAAGA,EAAI,YAAaD,EAAO,WAAY,GAE1CA,EAAO,WACXC,EAAK,CAAE,GAAGA,EAAI,SAAUD,EAAO,QAAS,GAEpCA,EAAO,eACXC,EAAK,CAAE,GAAGA,EAAI,aAAcD,EAAO,YAAa,GAE3CC,CACT,CACA,MAAM,YAAYC,EAA8B,CAC9C,GAAI,CACF,OAAO,MAAM,KAAK,SAAS,QAAQ,IAAI,cAAcA,CAAG,CAC1D,OAASC,EAAO,CACd,MAAM,IAAIC,EAAYC,EAAgB,cAAe,OAAWF,CAAK,CACvE,CACF,CAEA,MAAM,cAAcF,EAAgD,CAClE,GAAI,CACF,IAAMK,EAAcT,EAAkB,QAAQI,CAAE,EAC1CM,EAAY,MAAM,KAAK,SAAS,QAAQ,IAAI,KAAKD,CAAW,EAKlE,MAAO,CAAE,MAJO,MAAM,KAAK,SAAS,QAAQ,IAAI,mBAC9CC,CACF,IACsB,aAAa,IACrB,CAChB,OAASJ,EAAO,CACd,MAAM,IAAIC,EAAYC,EAAgB,cAAe,OAAWF,CAAK,CACvE,CACF,CACF",
6
- "names": ["SignerError", "SignerErrorCode", "DefaultTronSigner", "_DefaultTronSigner", "__name", "provider", "tronTx", "tx", "msg", "error", "SignerError", "SignerErrorCode", "transaction", "signedTxn"]
4
+ "sourcesContent": ["import type { GenericSigner, TronTransaction } from 'rango-types';\n\nimport { SignerError, SignerErrorCode } from 'rango-types';\n\n/** A transaction as signed by TronWeb, carrying the id it was signed under. */\ntype SignedTronTransaction = { txID?: string };\n\n/**\n * The broadcast response. Wallet-injected TronWeb instances differ in where\n * they expose the transaction id, so both known shapes are optional here.\n *\n * A node that rejects the broadcast answers with `result: false` plus a `code`\n * and a hex-encoded `message` - `sendRawTransaction` resolves in that case\n * rather than throwing.\n */\ntype BroadcastReceipt = {\n result?: boolean;\n code?: string;\n message?: string;\n /**\n * A node-side failure, reported separately from `code` and capitalised as the\n * node sends it, e.g. `\"class java.lang.NullPointerException : null\"`.\n */\n Error?: string;\n txid?: string;\n transaction?: { txID?: string };\n};\n\n/** Tron hex-encodes the rejection reason; returned as-is when it is not hex. */\nfunction decodeBroadcastMessage(message?: string): string | undefined {\n if (!message) {\n return undefined;\n }\n\n const hex = message.startsWith('0x') ? message.slice(2) : message;\n if (hex.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(hex)) {\n return message;\n }\n\n const bytes = hex.match(/.{2}/g)?.map((byte) => parseInt(byte, 16)) ?? [];\n\n return new TextDecoder().decode(new Uint8Array(bytes));\n}\n\n/**\n * The TronWeb surface this signer calls. `tronweb` is not a dependency of this\n * package (the wallet injects its own instance), so only the methods used here\n * are typed rather than pulling in the package's declarations.\n */\ntype TronExternalProvider = {\n tronWeb: {\n trx: {\n signMessageV2: (message: string) => Promise<string>;\n sign: (transaction: object) => Promise<SignedTronTransaction>;\n sendRawTransaction: (\n signedTransaction: SignedTronTransaction\n ) => Promise<BroadcastReceipt>;\n };\n };\n};\n\nexport class DefaultTronSigner implements GenericSigner<TronTransaction> {\n private provider: TronExternalProvider;\n\n constructor(provider: TronExternalProvider) {\n this.provider = provider;\n }\n\n static buildTx(tronTx: TronTransaction) {\n let tx = {};\n if (!!tronTx.txID) {\n tx = { ...tx, txID: tronTx.txID };\n }\n if (tronTx.visible !== undefined) {\n tx = { ...tx, visible: tronTx.visible };\n }\n if (!!tronTx.__payload__) {\n tx = { ...tx, __payload__: tronTx.__payload__ };\n }\n if (!!tronTx.raw_data) {\n tx = { ...tx, raw_data: tronTx.raw_data };\n }\n if (!!tronTx.raw_data_hex) {\n tx = { ...tx, raw_data_hex: tronTx.raw_data_hex };\n }\n return tx;\n }\n async signMessage(msg: string): Promise<string> {\n try {\n return await this.provider.tronWeb.trx.signMessageV2(msg);\n } catch (error) {\n throw new SignerError(SignerErrorCode.SIGN_TX_ERROR, undefined, error);\n }\n }\n\n async signAndSendTx(tx: TronTransaction): Promise<{ hash: string }> {\n let receipt: BroadcastReceipt | undefined;\n let signedTxn: SignedTronTransaction | undefined;\n\n try {\n const transaction = DefaultTronSigner.buildTx(tx);\n signedTxn = await this.provider.tronWeb.trx.sign(transaction);\n receipt = await this.provider.tronWeb.trx.sendRawTransaction(signedTxn);\n } catch (error) {\n throw new SignerError(SignerErrorCode.SEND_TX_ERROR, undefined, error);\n }\n\n /*\n * A refused broadcast resolves instead of throwing, and still carries a\n * `txid` - the id is derived from the transaction's own bytes, so it exists\n * whether or not the network accepted it. Taking that id at face value is\n * what left approvals polling a transaction no node ever had, reporting\n * \"waiting for approval\" until the user gave up.\n *\n * Refusals arrive in more than one shape and never set `result` to `false`\n * - it is simply absent - so success cannot be inferred from it. What they\n * do have in common is that one of the failure fields is populated:\n * `code` with a hex `message` (`TRANSACTION_EXPIRATION_ERROR`), or a\n * capitalised `Error` carrying a node-side exception.\n */\n const failure =\n receipt?.Error ??\n (receipt?.code\n ? decodeBroadcastMessage(receipt.message) ??\n `The Tron node refused the transaction (${receipt.code}).`\n : undefined);\n\n if (failure) {\n throw new SignerError(SignerErrorCode.SEND_TX_ERROR, failure);\n }\n\n /*\n * Instances differ in where they put the id, and it is derived from the\n * transaction itself, so the signed transaction's own `txID` is a valid\n * last resort now that the broadcast is known to have been accepted.\n */\n const hash = receipt?.txid ?? receipt?.transaction?.txID ?? signedTxn?.txID;\n\n if (!hash) {\n throw new SignerError(\n SignerErrorCode.SEND_TX_ERROR,\n 'Tron transaction was broadcast without a transaction hash.'\n );\n }\n\n return { hash };\n }\n}\n"],
5
+ "mappings": "+EAEA,OAAS,eAAAA,EAAa,mBAAAC,MAAuB,cA2B7C,SAASC,EAAuBC,EAAsC,CACpE,GAAI,CAACA,EACH,OAGF,IAAMC,EAAMD,EAAQ,WAAW,IAAI,EAAIA,EAAQ,MAAM,CAAC,EAAIA,EAC1D,GAAIC,EAAI,OAAS,IAAM,GAAK,CAAC,eAAe,KAAKA,CAAG,EAClD,OAAOD,EAGT,IAAME,EAAQD,EAAI,MAAM,OAAO,GAAG,IAAKE,GAAS,SAASA,EAAM,EAAE,CAAC,GAAK,CAAC,EAExE,OAAO,IAAI,YAAY,EAAE,OAAO,IAAI,WAAWD,CAAK,CAAC,CACvD,CAbSE,EAAAL,EAAA,0BAgCF,IAAMM,EAAN,MAAMC,CAA4D,CA7DzE,MA6DyE,CAAAF,EAAA,0BAC/D,SAER,YAAYG,EAAgC,CAC1C,KAAK,SAAWA,CAClB,CAEA,OAAO,QAAQC,EAAyB,C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", "__name", "DefaultTronSigner", "_DefaultTronSigner", "provider", "tronTx", "tx", "msg", "error", "SignerError", "SignerErrorCode", "receipt", "signedTxn", "transaction", "failure", "hash"]
7
7
  }
package/dist/signer.d.ts CHANGED
@@ -1,5 +1,44 @@
1
1
  import type { GenericSigner, TronTransaction } from 'rango-types';
2
- type TronExternalProvider = any;
2
+ /** A transaction as signed by TronWeb, carrying the id it was signed under. */
3
+ type SignedTronTransaction = {
4
+ txID?: string;
5
+ };
6
+ /**
7
+ * The broadcast response. Wallet-injected TronWeb instances differ in where
8
+ * they expose the transaction id, so both known shapes are optional here.
9
+ *
10
+ * A node that rejects the broadcast answers with `result: false` plus a `code`
11
+ * and a hex-encoded `message` - `sendRawTransaction` resolves in that case
12
+ * rather than throwing.
13
+ */
14
+ type BroadcastReceipt = {
15
+ result?: boolean;
16
+ code?: string;
17
+ message?: string;
18
+ /**
19
+ * A node-side failure, reported separately from `code` and capitalised as the
20
+ * node sends it, e.g. `"class java.lang.NullPointerException : null"`.
21
+ */
22
+ Error?: string;
23
+ txid?: string;
24
+ transaction?: {
25
+ txID?: string;
26
+ };
27
+ };
28
+ /**
29
+ * The TronWeb surface this signer calls. `tronweb` is not a dependency of this
30
+ * package (the wallet injects its own instance), so only the methods used here
31
+ * are typed rather than pulling in the package's declarations.
32
+ */
33
+ type TronExternalProvider = {
34
+ tronWeb: {
35
+ trx: {
36
+ signMessageV2: (message: string) => Promise<string>;
37
+ sign: (transaction: object) => Promise<SignedTronTransaction>;
38
+ sendRawTransaction: (signedTransaction: SignedTronTransaction) => Promise<BroadcastReceipt>;
39
+ };
40
+ };
41
+ };
3
42
  export declare class DefaultTronSigner implements GenericSigner<TronTransaction> {
4
43
  private provider;
5
44
  constructor(provider: TronExternalProvider);
@@ -1 +1 @@
1
- {"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAA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BAAgB,CAAC,CAAC;SAChC,CAAC;KACH,CAAC;CACH,CAAC;AAEF,qBAAa,iBAAkB,YAAW,aAAa,CAAC,eAAe,CAAC;IACtE,OAAO,CAAC,QAAQ,CAAuB;gBAE3B,QAAQ,EAAE,oBAAoB;IAI1C,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,eAAe;IAmBhC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQzC,aAAa,CAAC,EAAE,EAAE,eAAe,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CAoDpE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/signer-tron",
3
- "version": "0.43.1-next.0",
3
+ "version": "0.44.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -22,7 +22,7 @@
22
22
  "lint": "eslint \"**/*.{ts,tsx}\""
23
23
  },
24
24
  "dependencies": {
25
- "rango-types": "^0.5.0"
25
+ "rango-types": "^0.6.0"
26
26
  },
27
27
  "publishConfig": {
28
28
  "access": "public"
package/src/signer.ts CHANGED
@@ -2,11 +2,62 @@ import type { GenericSigner, TronTransaction } from 'rango-types';
2
2
 
3
3
  import { SignerError, SignerErrorCode } from 'rango-types';
4
4
 
5
- /*
6
- * TODO - replace with real type
7
- * tslint:disable-next-line: no-any
5
+ /** A transaction as signed by TronWeb, carrying the id it was signed under. */
6
+ type SignedTronTransaction = { txID?: string };
7
+
8
+ /**
9
+ * The broadcast response. Wallet-injected TronWeb instances differ in where
10
+ * they expose the transaction id, so both known shapes are optional here.
11
+ *
12
+ * A node that rejects the broadcast answers with `result: false` plus a `code`
13
+ * and a hex-encoded `message` - `sendRawTransaction` resolves in that case
14
+ * rather than throwing.
15
+ */
16
+ type BroadcastReceipt = {
17
+ result?: boolean;
18
+ code?: string;
19
+ message?: string;
20
+ /**
21
+ * A node-side failure, reported separately from `code` and capitalised as the
22
+ * node sends it, e.g. `"class java.lang.NullPointerException : null"`.
23
+ */
24
+ Error?: string;
25
+ txid?: string;
26
+ transaction?: { txID?: string };
27
+ };
28
+
29
+ /** Tron hex-encodes the rejection reason; returned as-is when it is not hex. */
30
+ function decodeBroadcastMessage(message?: string): string | undefined {
31
+ if (!message) {
32
+ return undefined;
33
+ }
34
+
35
+ const hex = message.startsWith('0x') ? message.slice(2) : message;
36
+ if (hex.length % 2 !== 0 || !/^[0-9a-f]+$/i.test(hex)) {
37
+ return message;
38
+ }
39
+
40
+ const bytes = hex.match(/.{2}/g)?.map((byte) => parseInt(byte, 16)) ?? [];
41
+
42
+ return new TextDecoder().decode(new Uint8Array(bytes));
43
+ }
44
+
45
+ /**
46
+ * The TronWeb surface this signer calls. `tronweb` is not a dependency of this
47
+ * package (the wallet injects its own instance), so only the methods used here
48
+ * are typed rather than pulling in the package's declarations.
8
49
  */
9
- type TronExternalProvider = any;
50
+ type TronExternalProvider = {
51
+ tronWeb: {
52
+ trx: {
53
+ signMessageV2: (message: string) => Promise<string>;
54
+ sign: (transaction: object) => Promise<SignedTronTransaction>;
55
+ sendRawTransaction: (
56
+ signedTransaction: SignedTronTransaction
57
+ ) => Promise<BroadcastReceipt>;
58
+ };
59
+ };
60
+ };
10
61
 
11
62
  export class DefaultTronSigner implements GenericSigner<TronTransaction> {
12
63
  private provider: TronExternalProvider;
@@ -43,16 +94,55 @@ export class DefaultTronSigner implements GenericSigner<TronTransaction> {
43
94
  }
44
95
 
45
96
  async signAndSendTx(tx: TronTransaction): Promise<{ hash: string }> {
97
+ let receipt: BroadcastReceipt | undefined;
98
+ let signedTxn: SignedTronTransaction | undefined;
99
+
46
100
  try {
47
101
  const transaction = DefaultTronSigner.buildTx(tx);
48
- const signedTxn = await this.provider.tronWeb.trx.sign(transaction);
49
- const receipt = await this.provider.tronWeb.trx.sendRawTransaction(
50
- signedTxn
51
- );
52
- const hash = receipt?.transaction?.txID;
53
- return { hash };
102
+ signedTxn = await this.provider.tronWeb.trx.sign(transaction);
103
+ receipt = await this.provider.tronWeb.trx.sendRawTransaction(signedTxn);
54
104
  } catch (error) {
55
105
  throw new SignerError(SignerErrorCode.SEND_TX_ERROR, undefined, error);
56
106
  }
107
+
108
+ /*
109
+ * A refused broadcast resolves instead of throwing, and still carries a
110
+ * `txid` - the id is derived from the transaction's own bytes, so it exists
111
+ * whether or not the network accepted it. Taking that id at face value is
112
+ * what left approvals polling a transaction no node ever had, reporting
113
+ * "waiting for approval" until the user gave up.
114
+ *
115
+ * Refusals arrive in more than one shape and never set `result` to `false`
116
+ * - it is simply absent - so success cannot be inferred from it. What they
117
+ * do have in common is that one of the failure fields is populated:
118
+ * `code` with a hex `message` (`TRANSACTION_EXPIRATION_ERROR`), or a
119
+ * capitalised `Error` carrying a node-side exception.
120
+ */
121
+ const failure =
122
+ receipt?.Error ??
123
+ (receipt?.code
124
+ ? decodeBroadcastMessage(receipt.message) ??
125
+ `The Tron node refused the transaction (${receipt.code}).`
126
+ : undefined);
127
+
128
+ if (failure) {
129
+ throw new SignerError(SignerErrorCode.SEND_TX_ERROR, failure);
130
+ }
131
+
132
+ /*
133
+ * Instances differ in where they put the id, and it is derived from the
134
+ * transaction itself, so the signed transaction's own `txID` is a valid
135
+ * last resort now that the broadcast is known to have been accepted.
136
+ */
137
+ const hash = receipt?.txid ?? receipt?.transaction?.txID ?? signedTxn?.txID;
138
+
139
+ if (!hash) {
140
+ throw new SignerError(
141
+ SignerErrorCode.SEND_TX_ERROR,
142
+ 'Tron transaction was broadcast without a transaction hash.'
143
+ );
144
+ }
145
+
146
+ return { hash };
57
147
  }
58
148
  }