|
| 1 | +import {accountInfo, derive} from '@haqq/provider-web3-utils'; |
| 2 | +import tron from 'tronweb'; |
| 3 | + |
| 4 | +import {ProviderSSSBase} from './provider'; |
| 5 | +import {ProviderSSSTronOptions} from './types'; |
| 6 | + |
| 7 | +import {compressPublicKey, getSeed} from '../../utils'; |
| 8 | +import { |
| 9 | + BytesLike, |
| 10 | + ProviderInterface, |
| 11 | + TransactionRequest, |
| 12 | + TypedData, |
| 13 | +} from '../types'; |
| 14 | + |
| 15 | +export class ProviderSSSTron |
| 16 | + extends ProviderSSSBase |
| 17 | + implements ProviderInterface |
| 18 | +{ |
| 19 | + private _tronWebHostUrl: string; |
| 20 | + constructor(options: ProviderSSSTronOptions) { |
| 21 | + super(options); |
| 22 | + this._tronWebHostUrl = options.tronWebHostUrl; |
| 23 | + } |
| 24 | + |
| 25 | + async getAccountInfo(hdPath: string) { |
| 26 | + let resp = {publicKey: '', address: ''}; |
| 27 | + try { |
| 28 | + const {seed} = await getSeed( |
| 29 | + this._options.account, |
| 30 | + this._options.storage, |
| 31 | + this._options.getPassword, |
| 32 | + ); |
| 33 | + |
| 34 | + if (!seed) { |
| 35 | + throw new Error('seed_not_found'); |
| 36 | + } |
| 37 | + |
| 38 | + const privateKey = await derive(seed, hdPath); |
| 39 | + |
| 40 | + if (!privateKey) { |
| 41 | + throw new Error('private_key_not_found'); |
| 42 | + } |
| 43 | + |
| 44 | + const account = await accountInfo(privateKey); |
| 45 | + |
| 46 | + resp = { |
| 47 | + publicKey: compressPublicKey(account.publicKey), |
| 48 | + address: tron.utils.address.fromHex(account.address), |
| 49 | + }; |
| 50 | + this.emit('getPublicKeyForHDPath', true); |
| 51 | + } catch (e) { |
| 52 | + if (e instanceof Error) { |
| 53 | + this.catchError(e, 'getPublicKeyForHDPath'); |
| 54 | + } |
| 55 | + } |
| 56 | + return resp; |
| 57 | + } |
| 58 | + |
| 59 | + async signTransaction( |
| 60 | + hdPath: string, |
| 61 | + transaction: TransactionRequest, |
| 62 | + ): Promise<string> { |
| 63 | + let resp = ''; |
| 64 | + try { |
| 65 | + const {seed} = await getSeed( |
| 66 | + this._options.account, |
| 67 | + this._options.storage, |
| 68 | + this._options.getPassword, |
| 69 | + ); |
| 70 | + |
| 71 | + if (!seed) { |
| 72 | + throw new Error('seed_not_found'); |
| 73 | + } |
| 74 | + |
| 75 | + const privateKey = (await derive(seed, hdPath)).replace(/^0x/, ''); |
| 76 | + |
| 77 | + if (!privateKey) { |
| 78 | + throw new Error('private_key_not_found'); |
| 79 | + } |
| 80 | + |
| 81 | + const tronWeb = new tron.TronWeb({ |
| 82 | + fullHost: this._tronWebHostUrl, |
| 83 | + privateKey, |
| 84 | + }); |
| 85 | + |
| 86 | + // Convert Ethereum-style transaction to Tron transaction |
| 87 | + const tronTransaction = { |
| 88 | + to_address: tron.utils.address.isAddress(transaction.to) |
| 89 | + ? transaction.to |
| 90 | + : tron.utils.address.fromHex(transaction.to), |
| 91 | + owner_address: tron.utils.crypto.pkToAddress(privateKey), |
| 92 | + amount: tron.TronWeb.toSun( |
| 93 | + Number(transaction.value), |
| 94 | + ) as unknown as number, |
| 95 | + }; |
| 96 | + |
| 97 | + // Get the signature |
| 98 | + const tx = await tronWeb.transactionBuilder.sendTrx( |
| 99 | + tronTransaction.to_address, |
| 100 | + tronTransaction.amount, |
| 101 | + tronTransaction.owner_address, |
| 102 | + ); |
| 103 | + |
| 104 | + const signedTx = await tronWeb.trx.signTransaction(tx); |
| 105 | + resp = signedTx.signature[0]; |
| 106 | + |
| 107 | + this.emit('signTransaction', true); |
| 108 | + } catch (e) { |
| 109 | + if (e instanceof Error) { |
| 110 | + this.catchError(e, 'signTransaction'); |
| 111 | + } |
| 112 | + } |
| 113 | + return resp; |
| 114 | + } |
| 115 | + |
| 116 | + async signPersonalMessage( |
| 117 | + _hdPath: string, |
| 118 | + _message: BytesLike | string, |
| 119 | + ): Promise<string> { |
| 120 | + throw new Error(' signPersonalMessage not implemented'); |
| 121 | + } |
| 122 | + |
| 123 | + async signTypedData(_hdPath: string, _typedData: TypedData): Promise<string> { |
| 124 | + try { |
| 125 | + throw new Error( |
| 126 | + "Tron blockchain doesn't support signTypedData medthods.", |
| 127 | + ); |
| 128 | + } catch (e) { |
| 129 | + this.catchError(e, 'signTypedData'); |
| 130 | + } finally { |
| 131 | + return ''; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments