Skip to content

Commit ab328dc

Browse files
author
iGroza
committed
feat: provider tron init
1 parent 8b5d09e commit ab328dc

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@haqq/rn-wallet-providers",
3-
"version": "0.0.6",
3+
"version": "0.0.7",
44
"description": "React Native providers for Haqq wallet",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/providers/base-provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class ProviderBase<T extends object>
1212
extends EventEmitter
1313
implements ProviderInterface
1414
{
15-
protected _options: T & ProviderBaseOptions;
15+
_options: T & ProviderBaseOptions;
1616

1717
constructor(options: T & ProviderBaseOptions) {
1818
super();

src/providers/mnemonic/tron-provider.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
/* eslint-disable no-console */
12
import {derive} from '@haqq/provider-web3-utils';
23
import tron from 'tronweb';
34

45
import {ProviderMnemonicBase} from './provider';
6+
import {ProviderMnemonicTronOptions} from './types';
57

68
import {getMnemonic} from '../../utils/mnemonic/get-mnemonic';
79
import {
@@ -15,6 +17,30 @@ export class ProviderMnemonicTron
1517
extends ProviderMnemonicBase
1618
implements ProviderInterface
1719
{
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+
1844
async getAccountInfo(hdPath: string) {
1945
const info = await super.getAccountInfo(hdPath.replace("44'", "195'"));
2046
return {
@@ -40,44 +66,44 @@ export class ProviderMnemonicTron
4066

4167
const seed = await ProviderMnemonicBase.shareToSeed(share);
4268

43-
const privateKey = await derive(seed, hdPath);
69+
const privateKey = (await derive(seed, hdPath)).replace(/^0x/, '');
4470

4571
if (!privateKey) {
4672
throw new Error('private_key_not_found');
4773
}
4874

4975
const tronWeb = new tron.TronWeb({
50-
fullHost: 'https://api.trongrid.io',
51-
privateKey: privateKey,
76+
fullHost: this._tronWebHostUrl,
77+
privateKey,
5278
});
5379

5480
// Convert Ethereum-style transaction to Tron transaction
5581
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,
5989
};
6090

61-
// Create an unsigned transaction
62-
const unsignedTxn = await tronWeb.transactionBuilder.sendTrx(
91+
// Get the signature
92+
const tx = await tronWeb.transactionBuilder.sendTrx(
6393
tronTransaction.to_address,
64-
Number(transaction.value),
94+
tronTransaction.amount,
6595
tronTransaction.owner_address,
6696
);
6797

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];
73100

74101
this.emit('signTransaction', true);
75102
} catch (e) {
76103
if (e instanceof Error) {
77104
this.catchError(e, 'signTransaction');
78105
}
79106
}
80-
81107
return resp;
82108
}
83109

src/providers/mnemonic/types.ts

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ export type ProviderMnemonicBaseOptions = {
22
account: string;
33
getPassword: () => Promise<string>;
44
};
5+
6+
export type ProviderMnemonicTronOptions = ProviderMnemonicBaseOptions & {
7+
tronWebHostUrl: string;
8+
};

0 commit comments

Comments
 (0)