Skip to content

Commit

Permalink
Merge pull request #27 from leapwallet/update/encryption-library
Browse files Browse the repository at this point in the history
Speed up wallet creation
  • Loading branch information
baryon2 authored Jun 13, 2024
2 parents 1f54e15 + 2377a2f commit 3a23590
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@leapwallet/leap-keychain",
"version": "0.2.5-beta.0",
"version": "0.2.5-beta.1",
"description": "A javascript library for crypto key management",
"scripts": {
"test": "jest",
Expand Down
10 changes: 10 additions & 0 deletions src/keychain/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { v4 as uuidv4 } from 'uuid';
import { correctMnemonic } from '../utils/correct-mnemonic';
import { ChainInfo, CreateWalletParams, Key, Keystore, WALLETTYPE } from '../types/keychain';
import { compressedPublicKey, generateWalletFromMnemonic, generateWalletsFromMnemonic } from '../key/wallet-utils';
import { convertAddress } from '../utils/bech32-address-converter';

export const KEYCHAIN = 'keystore';
export const ENCRYPTED_KEYCHAIN = 'encrypted-keystore';
Expand Down Expand Up @@ -293,7 +294,15 @@ export class KeyChain {
const chainsData = chainInfos;
const addresses: Record<string, string> = {};
const pubKeys: Record<string, string> = {};
const coinTypeKeys: Record<string, { address: string; pubkey: string }> = {};

for (const chainInfo of chainsData) {
const coinTypeKey = coinTypeKeys[chainInfo.coinType];
if (coinTypeKey) {
addresses[chainInfo.key] = convertAddress(coinTypeKey.address, chainInfo.addressPrefix);
pubKeys[chainInfo.key] = coinTypeKey.pubkey;
continue;
}
const wallet = generateWalletFromMnemonic(mnemonic, {
hdPath: getHDPath(chainInfo.coinType, addressIndex.toString()),
addressPrefix: chainInfo.addressPrefix,
Expand All @@ -302,6 +311,7 @@ export class KeyChain {

const [account] = wallet.getAccounts();
if (account?.address && account?.pubkey) {
coinTypeKeys[chainInfo.coinType] = { address: account.address, pubkey: compressedPublicKey(account.pubkey) };
addresses[chainInfo.key] = account.address;
pubKeys[chainInfo.key] = compressedPublicKey(account.pubkey);
}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/bech32-address-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { bech32 } from 'bech32';

export function convertAddress(address: string, prefix: string) {
const { words } = bech32.decode(address);
return bech32.encode(prefix, words);
}

0 comments on commit 3a23590

Please sign in to comment.