From beb262806564f3b0a5543935b8d38d6c19e8283b Mon Sep 17 00:00:00 2001
From: bl05b3e <naMATy-$pRBE2hbMh{x9'Tx@j?>
Date: Wed, 7 Dec 2022 18:49:48 +0100
Subject: [PATCH] [enh] Account: add SCRYPT_PARAMS const

---
 src/app/wallet/account.service.ts | 40 +++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/app/wallet/account.service.ts b/src/app/wallet/account.service.ts
index 3871f7a..19fa4f1 100644
--- a/src/app/wallet/account.service.ts
+++ b/src/app/wallet/account.service.ts
@@ -48,6 +48,36 @@ export interface LoadAccountDataOptions {
   emitEvent?: boolean;
 }
 
+export const  SCRYPT_PARAMS = {
+  SIMPLE: <Params>{
+    N: 2048,
+    r: 8,
+    p: 1
+  },
+  DEFAULT: <Params>{
+    N: 4096,
+    r: 16,
+    p: 1
+  },
+  SECURE: <Params>{
+    N: 16384,
+    r: 32,
+    p: 2
+  },
+  HARDEST: <Params>{
+    N: 65536,
+    r: 32,
+    p: 4
+  },
+  EXTREME: <Params>{
+    N: 262144,
+    r: 64,
+    p: 8
+  }
+};
+
+const ED25519_SEED_LENGTH = 32;
+
 @Injectable({providedIn: 'root'})
 export class AccountService extends StartableService {
 
@@ -545,18 +575,14 @@ export class AccountService extends StartableService {
     this._$accounts.next(this._$accounts.value.slice() /*create a copy*/);
   }
 
-  async addV1Account(data: {salt: string, password: string; meta?: AccountMeta}): Promise<Account> {
+  async addV1Account(data: {salt: string, password: string; meta?: AccountMeta, scryptParams?: Params}): Promise<Account> {
     if (!data?.salt || !data?.password) return;
 
     console.info(this._logPrefix + ' Authenticating using salt+pwd...');
     const passwordU8a = Uint8Array.from(data.password.split('').map(x => x.charCodeAt(0)));
     const saltU8a = Uint8Array.from(data.salt.split('').map(x => x.charCodeAt(0)));
-    const result = scryptEncode(passwordU8a, saltU8a, <Params>{
-      N: 4096,
-      r: 16,
-      p: 1
-    });
-    const seedHex = u8aToHex(result.password.slice(0,32));
+    const result = scryptEncode(passwordU8a, saltU8a, data.scryptParams || SCRYPT_PARAMS.DEFAULT);
+    const seedHex = u8aToHex(result.password.slice(0,ED25519_SEED_LENGTH));
 
     //console.debug('Computed seed (hex) from salt+pwd:', rawSeedString);
 
-- 
GitLab