From 01c78038cde4ce872fb1413905148fed339163d6 Mon Sep 17 00:00:00 2001 From: iGroza Date: Sat, 19 Oct 2024 00:49:37 +0700 Subject: [PATCH] feat: migrate mnemonic wallets --- package.json | 2 +- src/providers/mnemonic/provider.ts | 14 +++--------- src/providers/mnemonic/tron-provider.ts | 29 +++++++++++++++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 595ee1c..3b3e3b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@haqq/rn-wallet-providers", - "version": "0.0.7", + "version": "0.0.8", "description": "React Native providers for Haqq wallet", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/providers/mnemonic/provider.ts b/src/providers/mnemonic/provider.ts index 3143cef..c54aa90 100644 --- a/src/providers/mnemonic/provider.ts +++ b/src/providers/mnemonic/provider.ts @@ -7,11 +7,10 @@ import EncryptedStorage from 'react-native-encrypted-storage'; import {ProviderMnemonicBaseOptions} from './types'; import {ITEM_KEYS, WalletType} from '../../constants'; -import {Multichain} from '../../services/multichain'; -import {compressPublicKey, convertHdPath} from '../../utils'; +import {compressPublicKey} from '../../utils'; import {getMnemonic} from '../../utils/mnemonic/get-mnemonic'; import {ProviderBase} from '../base-provider'; -import {NETWORK_TYPE, ProviderBaseOptions, ProviderInterface} from '../types'; +import {ProviderBaseOptions, ProviderInterface} from '../types'; export class ProviderMnemonicBase extends ProviderBase @@ -119,7 +118,7 @@ export class ProviderMnemonicBase } async getAccountInfo(hdPath: string) { - let resp = {publicKey: '', address: '', tronAddress: ''}; + let resp = {publicKey: '', address: ''}; try { const share = await getMnemonic( this._options.account, @@ -131,13 +130,7 @@ export class ProviderMnemonicBase } const seed = await ProviderMnemonicBase.shareToSeed(share); - const ethPrivateKey = await derive(seed, hdPath); - const tronAddress = await Multichain.generateAddress( - NETWORK_TYPE.TRON, - convertHdPath(hdPath, NETWORK_TYPE.TRON), - await this.getMnemonicPhrase(), - ); if (!ethPrivateKey) { throw new Error('private_key_not_found'); @@ -148,7 +141,6 @@ export class ProviderMnemonicBase resp = { publicKey: compressPublicKey(account.publicKey), address: account.address, - tronAddress, }; this.emit('getPublicKeyForHDPath', true); } catch (e) { diff --git a/src/providers/mnemonic/tron-provider.ts b/src/providers/mnemonic/tron-provider.ts index 8913687..91ba052 100644 --- a/src/providers/mnemonic/tron-provider.ts +++ b/src/providers/mnemonic/tron-provider.ts @@ -1,5 +1,5 @@ /* eslint-disable no-console */ -import {derive} from '@haqq/provider-web3-utils'; +import {accountInfo, derive} from '@haqq/provider-web3-utils'; import tron from 'tronweb'; import {ProviderMnemonicBase} from './provider'; @@ -12,6 +12,7 @@ import { TransactionRequest, TypedData, } from '../types'; +import { compressPublicKey } from '../../utils'; export class ProviderMnemonicTron extends ProviderMnemonicBase @@ -42,10 +43,30 @@ export class ProviderMnemonicTron } async getAccountInfo(hdPath: string) { - const info = await super.getAccountInfo(hdPath.replace("44'", "195'")); + const share = await getMnemonic( + this._options.account, + this._options.getPassword, + ); + + if (!share) { + throw new Error('seed_not_found'); + } + + const seed = await ProviderMnemonicBase.shareToSeed(share); + const ethPrivateKey = await derive(seed, hdPath); + + if (!ethPrivateKey) { + throw new Error('private_key_not_found'); + } + + const account = await accountInfo(ethPrivateKey); + console.log('ethPrivateKey', ethPrivateKey); + console.log('account', account); + + return { - ...info, - address: tron.utils.address.fromHex(info.address), + publicKey: compressPublicKey(account.publicKey), + address: tron.utils.address.fromHex(account.address), }; }