Skip to content

Commit

Permalink
feat: migrate mnemonic wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
iGroza committed Oct 18, 2024
1 parent ab328dc commit 01c7803
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 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.7",
"version": "0.0.8",
"description": "React Native providers for Haqq wallet",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 3 additions & 11 deletions src/providers/mnemonic/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderMnemonicBaseOptions>
Expand Down Expand Up @@ -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,
Expand All @@ -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');
Expand All @@ -148,7 +141,6 @@ export class ProviderMnemonicBase
resp = {
publicKey: compressPublicKey(account.publicKey),
address: account.address,
tronAddress,
};
this.emit('getPublicKeyForHDPath', true);
} catch (e) {
Expand Down
29 changes: 25 additions & 4 deletions src/providers/mnemonic/tron-provider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,6 +12,7 @@ import {
TransactionRequest,
TypedData,
} from '../types';
import { compressPublicKey } from '../../utils';

export class ProviderMnemonicTron
extends ProviderMnemonicBase
Expand Down Expand Up @@ -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),
};
}

Expand Down

0 comments on commit 01c7803

Please sign in to comment.