Skip to content

Commit

Permalink
Merge pull request #29 from leapwallet/fix/encryption-function
Browse files Browse the repository at this point in the history
Fix/encryption function
  • Loading branch information
baryon2 authored Sep 17, 2024
2 parents 94dc1bd + 90455b0 commit 909896a
Show file tree
Hide file tree
Showing 7 changed files with 801 additions and 401 deletions.
4 changes: 2 additions & 2 deletions 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.2",
"version": "0.2.5-beta.4",
"description": "A javascript library for crypto key management",
"scripts": {
"test": "jest",
Expand Down Expand Up @@ -72,7 +72,7 @@
"@typescript-eslint/eslint-plugin": "5.9.1",
"@typescript-eslint/parser": "5.9.1",
"eslint": "8.6.0",
"jest": "28.1.2",
"jest": "29.7.0",
"prettier": "2.5.1",
"ts-jest": "28.0.5",
"typedoc": "0.22.17",
Expand Down
6 changes: 4 additions & 2 deletions src/encryption-utils/encryption-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//import CryptoJS from 'crypto-js';
import { cbc } from '@noble/ciphers/aes';
import { pbkdf2 } from '@noble/hashes/pbkdf2';
import { Input } from '@noble/hashes/utils';
import { randomBytes } from '@noble/ciphers/webcrypto';
import { sha1 } from '@noble/hashes/sha1';
import { base64, hex } from '@scure/base';
Expand Down Expand Up @@ -45,8 +46,9 @@ const new_iterations = 10_000;
// }).toString(CryptoJS.enc.Utf8);
// };

export const encrypt = (msg: string, pass: string, iterations?: number) => {
export const encrypt = (msg: string, pass: Input, iterations?: number) => {
const salt = randomBytes(128 / 8);

const key = pbkdf2(sha1, pass, salt, { c: iterations ?? new_iterations, dkLen: keySize / 8 });
const iv = randomBytes(128 / 8);
const stream = cbc(key, iv);
Expand All @@ -58,7 +60,7 @@ export const encrypt = (msg: string, pass: string, iterations?: number) => {
return saltString + ivString + encryptedString;
};

export const decrypt = (transitmessage: string, pass: string, iterations?: number): string => {
export const decrypt = (transitmessage: string, pass: Input, iterations?: number): string => {
const salt = hex.decode(transitmessage.substring(0, 32));
const iv = hex.decode(transitmessage.substring(32, 64));
const encrypted = base64.decode(transitmessage.substring(64));
Expand Down
3 changes: 1 addition & 2 deletions src/key/eth-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class EthWallet {
private walletType: 'mnemonic' | 'pvtKey',
private options: WalletOptions,
private provider?: Provider,
) {}
) { }

/**
* Generates a wallet from a mnemonic. Returns an EthWallet object.
Expand Down Expand Up @@ -127,7 +127,6 @@ export class EthWallet {
throw new Error(`Address ${signerAddress} not found in wallet`);
}
const { ethWallet } = account;

return ethWallet.signTransaction(transaction);
}

Expand Down
11 changes: 11 additions & 0 deletions src/key/wallet-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import { Wallet } from './wallet';
import * as base64js from 'base64-js';
import { bip39Token } from '../crypto/bip39/bip39-token';


/***
* Generate a wallet from a mnemonic
* @param mnemonic
* @param options {
* hdPath: string,
* addressPrefix: string,
* ethWallet: boolean, - if true, it generates an ethereum wallet regardless of cointype
* pubKeyBech32Address: boolean - if true, it generates a bech32 address from public key instead of ethereum address.
* }
*/
export function generateWalletFromMnemonic(
mnemonic: string,
{
Expand Down
17 changes: 9 additions & 8 deletions src/keychain/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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';
import { Input } from '@noble/ciphers/utils';

export const KEYCHAIN = 'keystore';
export const ENCRYPTED_KEYCHAIN = 'encrypted-keystore';
Expand Down Expand Up @@ -56,7 +57,7 @@ export class KeyChain {

public static async createNewWalletAccount<T extends string>(
name: string,
password: string,
password: Input,
colorIndex: number,
chainInfos: ChainInfo[],
): Promise<Key<T>> {
Expand Down Expand Up @@ -101,7 +102,7 @@ export class KeyChain {

public static async importNewWallet<T extends string>(
privateKey: string,
password: string,
password: Input,
chainInfos: ChainInfo[],
addressIndex?: number,
name?: string,
Expand All @@ -112,9 +113,9 @@ export class KeyChain {
const wallet =
chainInfo.coinType === '60'
? EthWallet.generateWalletFromPvtKey(privateKey, {
paths: [getHDPath('60', '0')],
addressPrefix: chainInfo.addressPrefix,
})
paths: [getHDPath('60', '0')],
addressPrefix: chainInfo.addressPrefix,
})
: await PvtKeyWallet.generateWallet(privateKey, chainInfo.addressPrefix);
const [account] = await wallet.getAccounts();
if (account) {
Expand Down Expand Up @@ -206,7 +207,7 @@ export class KeyChain {

public static async getSigner<T extends string>(
walletId: string,
password: string,
password: Input,
{
addressPrefix,
coinType,
Expand Down Expand Up @@ -252,7 +253,7 @@ export class KeyChain {
storage.set(KEYCHAIN, keychain);
}

public static async encrypt<T extends string>(password: string) {
public static async encrypt<T extends string>(password: Input) {
const storage = Container.get(storageToken);
const keychain = (await storage.get(KEYCHAIN)) as unknown as Keystore<T>;
const activeWallet = (await storage.get(ACTIVE_WALLET)) as unknown as Key<T>;
Expand All @@ -271,7 +272,7 @@ export class KeyChain {
}
}

public static async decrypt(password: string) {
public static async decrypt(password: Input) {
const storage = Container.get(storageToken);
const encryptedKeychain = (await storage.get(ENCRYPTED_KEYCHAIN)) as unknown as string;
const encryptedActiveWallet = (await storage.get(ENCRYPTED_ACTIVE_WALLET)) as unknown as string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/keychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Keystore<T extends string> = Record<string, Key<T>>;
export type CreateWalletParams = {
name: string;
mnemonic: string;
password: string;
password: string | Uint8Array;
addressIndex: number;
colorIndex: number;
chainInfos: ChainInfo[];
Expand Down
Loading

0 comments on commit 909896a

Please sign in to comment.