1
+ /* eslint-disable no-console */
1
2
import { derive } from '@haqq/provider-web3-utils' ;
2
3
import tron from 'tronweb' ;
3
4
4
5
import { ProviderMnemonicBase } from './provider' ;
6
+ import { ProviderMnemonicTronOptions } from './types' ;
5
7
6
8
import { getMnemonic } from '../../utils/mnemonic/get-mnemonic' ;
7
9
import {
@@ -15,6 +17,30 @@ export class ProviderMnemonicTron
15
17
extends ProviderMnemonicBase
16
18
implements ProviderInterface
17
19
{
20
+ private _tronWebHostUrl : string ;
21
+ constructor ( options : ProviderMnemonicTronOptions ) {
22
+ super ( options ) ;
23
+ this . _tronWebHostUrl = options . tronWebHostUrl ;
24
+ }
25
+
26
+ static async initialize (
27
+ mnemonic : string | null ,
28
+ getPassword : ( ) => Promise < string > ,
29
+ options : Omit < ProviderMnemonicTronOptions , 'getPassword' > ,
30
+ ) : Promise < ProviderMnemonicTron > {
31
+ const base = await ProviderMnemonicBase . initialize (
32
+ mnemonic ,
33
+ getPassword ,
34
+ options ,
35
+ ) ;
36
+
37
+ return new ProviderMnemonicTron ( {
38
+ ...options ,
39
+ getPassword,
40
+ account : base . _options . account ,
41
+ } ) ;
42
+ }
43
+
18
44
async getAccountInfo ( hdPath : string ) {
19
45
const info = await super . getAccountInfo ( hdPath . replace ( "44'" , "195'" ) ) ;
20
46
return {
@@ -40,44 +66,44 @@ export class ProviderMnemonicTron
40
66
41
67
const seed = await ProviderMnemonicBase . shareToSeed ( share ) ;
42
68
43
- const privateKey = await derive ( seed , hdPath ) ;
69
+ const privateKey = ( await derive ( seed , hdPath ) ) . replace ( / ^ 0 x / , '' ) ;
44
70
45
71
if ( ! privateKey ) {
46
72
throw new Error ( 'private_key_not_found' ) ;
47
73
}
48
74
49
75
const tronWeb = new tron . TronWeb ( {
50
- fullHost : 'https://api.trongrid.io' ,
51
- privateKey : privateKey ,
76
+ fullHost : this . _tronWebHostUrl ,
77
+ privateKey,
52
78
} ) ;
53
79
54
80
// Convert Ethereum-style transaction to Tron transaction
55
81
const tronTransaction = {
56
- to_address : tronWeb . address . toHex ( transaction . to ) ,
57
- owner_address : tronWeb . address . toHex ( transaction . from ) ,
58
- amount : tronWeb . toSun ( Number ( transaction . value ) ) ,
82
+ to_address : tron . utils . address . isAddress ( transaction . to )
83
+ ? transaction . to
84
+ : tron . utils . address . fromHex ( transaction . to ) ,
85
+ owner_address : tron . utils . crypto . pkToAddress ( privateKey ) ,
86
+ amount : tron . TronWeb . toSun (
87
+ Number ( transaction . value ) ,
88
+ ) as unknown as number ,
59
89
} ;
60
90
61
- // Create an unsigned transaction
62
- const unsignedTxn = await tronWeb . transactionBuilder . sendTrx (
91
+ // Get the signature
92
+ const tx = await tronWeb . transactionBuilder . sendTrx (
63
93
tronTransaction . to_address ,
64
- Number ( transaction . value ) ,
94
+ tronTransaction . amount ,
65
95
tronTransaction . owner_address ,
66
96
) ;
67
97
68
- // Sign the transaction
69
- const signedTxn = await tronWeb . trx . sign ( unsignedTxn , privateKey ) ;
70
-
71
- // Get the signature
72
- resp = signedTxn . signature [ 0 ] ;
98
+ const signedTx = await tronWeb . trx . signTransaction ( tx ) ;
99
+ resp = signedTx . signature [ 0 ] ;
73
100
74
101
this . emit ( 'signTransaction' , true ) ;
75
102
} catch ( e ) {
76
103
if ( e instanceof Error ) {
77
104
this . catchError ( e , 'signTransaction' ) ;
78
105
}
79
106
}
80
-
81
107
return resp ;
82
108
}
83
109
0 commit comments