Skip to content

Commit

Permalink
feat: provider tron init
Browse files Browse the repository at this point in the history
  • Loading branch information
iGroza committed Oct 18, 2024
1 parent 8b5d09e commit ab328dc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haqq/rn-wallet-providers",
"version": "0.0.6",
"version": "0.0.7",
"description": "React Native providers for Haqq wallet",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/providers/base-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ProviderBase<T extends object>
extends EventEmitter
implements ProviderInterface
{
protected _options: T & ProviderBaseOptions;
_options: T & ProviderBaseOptions;

constructor(options: T & ProviderBaseOptions) {
super();
Expand Down
56 changes: 41 additions & 15 deletions src/providers/mnemonic/tron-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-console */
import {derive} from '@haqq/provider-web3-utils';
import tron from 'tronweb';

import {ProviderMnemonicBase} from './provider';
import {ProviderMnemonicTronOptions} from './types';

import {getMnemonic} from '../../utils/mnemonic/get-mnemonic';
import {
Expand All @@ -15,6 +17,30 @@ export class ProviderMnemonicTron
extends ProviderMnemonicBase
implements ProviderInterface
{
private _tronWebHostUrl: string;
constructor(options: ProviderMnemonicTronOptions) {
super(options);
this._tronWebHostUrl = options.tronWebHostUrl;
}

static async initialize(
mnemonic: string | null,
getPassword: () => Promise<string>,
options: Omit<ProviderMnemonicTronOptions, 'getPassword'>,
): Promise<ProviderMnemonicTron> {
const base = await ProviderMnemonicBase.initialize(
mnemonic,
getPassword,
options,
);

return new ProviderMnemonicTron({
...options,
getPassword,
account: base._options.account,
});
}

async getAccountInfo(hdPath: string) {
const info = await super.getAccountInfo(hdPath.replace("44'", "195'"));
return {
Expand All @@ -40,44 +66,44 @@ export class ProviderMnemonicTron

const seed = await ProviderMnemonicBase.shareToSeed(share);

const privateKey = await derive(seed, hdPath);
const privateKey = (await derive(seed, hdPath)).replace(/^0x/, '');

if (!privateKey) {
throw new Error('private_key_not_found');
}

const tronWeb = new tron.TronWeb({
fullHost: 'https://api.trongrid.io',
privateKey: privateKey,
fullHost: this._tronWebHostUrl,
privateKey,
});

// Convert Ethereum-style transaction to Tron transaction
const tronTransaction = {
to_address: tronWeb.address.toHex(transaction.to),
owner_address: tronWeb.address.toHex(transaction.from),
amount: tronWeb.toSun(Number(transaction.value)),
to_address: tron.utils.address.isAddress(transaction.to)
? transaction.to
: tron.utils.address.fromHex(transaction.to),
owner_address: tron.utils.crypto.pkToAddress(privateKey),
amount: tron.TronWeb.toSun(
Number(transaction.value),
) as unknown as number,
};

// Create an unsigned transaction
const unsignedTxn = await tronWeb.transactionBuilder.sendTrx(
// Get the signature
const tx = await tronWeb.transactionBuilder.sendTrx(
tronTransaction.to_address,
Number(transaction.value),
tronTransaction.amount,
tronTransaction.owner_address,
);

// Sign the transaction
const signedTxn = await tronWeb.trx.sign(unsignedTxn, privateKey);

// Get the signature
resp = signedTxn.signature[0];
const signedTx = await tronWeb.trx.signTransaction(tx);
resp = signedTx.signature[0];

this.emit('signTransaction', true);
} catch (e) {
if (e instanceof Error) {
this.catchError(e, 'signTransaction');
}
}

return resp;
}

Expand Down
4 changes: 4 additions & 0 deletions src/providers/mnemonic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export type ProviderMnemonicBaseOptions = {
account: string;
getPassword: () => Promise<string>;
};

export type ProviderMnemonicTronOptions = ProviderMnemonicBaseOptions & {
tronWebHostUrl: string;
};

0 comments on commit ab328dc

Please sign in to comment.