From ab328dc20b386d0c23a87cc8004ed4259ee5ba45 Mon Sep 17 00:00:00 2001 From: iGroza Date: Fri, 18 Oct 2024 19:18:01 +0700 Subject: [PATCH] feat: provider tron init --- package.json | 2 +- src/providers/base-provider.ts | 2 +- src/providers/mnemonic/tron-provider.ts | 56 ++++++++++++++++++------- src/providers/mnemonic/types.ts | 4 ++ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index b4e18a0..595ee1c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/providers/base-provider.ts b/src/providers/base-provider.ts index d6636a3..48d7e3f 100644 --- a/src/providers/base-provider.ts +++ b/src/providers/base-provider.ts @@ -12,7 +12,7 @@ export class ProviderBase extends EventEmitter implements ProviderInterface { - protected _options: T & ProviderBaseOptions; + _options: T & ProviderBaseOptions; constructor(options: T & ProviderBaseOptions) { super(); diff --git a/src/providers/mnemonic/tron-provider.ts b/src/providers/mnemonic/tron-provider.ts index 254ce2e..8913687 100644 --- a/src/providers/mnemonic/tron-provider.ts +++ b/src/providers/mnemonic/tron-provider.ts @@ -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 { @@ -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, + options: Omit, + ): Promise { + 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 { @@ -40,36 +66,37 @@ 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) { @@ -77,7 +104,6 @@ export class ProviderMnemonicTron this.catchError(e, 'signTransaction'); } } - return resp; } diff --git a/src/providers/mnemonic/types.ts b/src/providers/mnemonic/types.ts index fe37095..df2b17d 100644 --- a/src/providers/mnemonic/types.ts +++ b/src/providers/mnemonic/types.ts @@ -2,3 +2,7 @@ export type ProviderMnemonicBaseOptions = { account: string; getPassword: () => Promise; }; + +export type ProviderMnemonicTronOptions = ProviderMnemonicBaseOptions & { + tronWebHostUrl: string; +};