Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import wallet password bug OK-7755 #568

Merged
merged 6 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions packages/engine/src/dbs/indexed/indexeddb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,15 +1074,7 @@ class IndexedDBApi implements DBAPI {
account: DBAccount,
importedCredential?: PrivateKeyCredential,
): Promise<DBAccount> {
let addingImported = false;
if (walletIsImported(walletId)) {
if (typeof importedCredential === 'undefined') {
throw new OneKeyInternalError(
'Imported credential required for adding imported accounts.',
);
}
addingImported = true;
}
const addingImported = walletIsImported(walletId);

let ret: DBAccount;
return this.ready.then(
Expand Down Expand Up @@ -1180,8 +1172,15 @@ class IndexedDBApi implements DBAPI {
}
const context: OneKeyContext =
getMainContextRequest.result as OneKeyContext;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (!checkPassword(context, importedCredential!.password)) {
if (!importedCredential) {
reject(
new OneKeyInternalError(
'Imported credential required for adding imported accounts.',
),
);
return;
}
if (!checkPassword(context, importedCredential.password)) {
reject(new WrongPassword());
return;
}
Expand All @@ -1190,9 +1189,16 @@ class IndexedDBApi implements DBAPI {
credential: JSON.stringify({
privateKey:
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
importedCredential!.privateKey.toString('hex'),
importedCredential.privateKey.toString('hex'),
}),
});
if (context.verifyString === DEFAULT_VERIFY_STRING) {
context.verifyString = encrypt(
importedCredential.password,
Buffer.from(DEFAULT_VERIFY_STRING),
).toString('hex');
transaction.objectStore(CONTEXT_STORE_NAME).put(context);
}
wallet.nextAccountIds.global += 1;
walletStore.put(wallet);
transaction
Expand Down
50 changes: 26 additions & 24 deletions packages/engine/src/dbs/realms/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,30 +664,7 @@ class RealmDB implements DBAPI {
account: DBAccount,
importedCredential?: PrivateKeyCredential,
): Promise<DBAccount> {
let addingImported = false;
if (walletIsImported(walletId)) {
if (typeof importedCredential === 'undefined') {
throw new OneKeyInternalError(
'Imported credential required for adding imported accounts.',
);
}
addingImported = true;
}

try {
if (addingImported) {
const context = this.realm!.objectForPrimaryKey<ContextSchema>(
'Context',
MAIN_CONTEXT,
);
if (typeof context === 'undefined') {
return Promise.reject(new OneKeyInternalError('Context not found.'));
}
if (!checkPassword(context, importedCredential!.password)) {
return Promise.reject(new WrongPassword());
}
}

const wallet = this.realm!.objectForPrimaryKey<WalletSchema>(
'Wallet',
walletId,
Expand Down Expand Up @@ -761,12 +738,37 @@ class RealmDB implements DBAPI {
if (wallet.accounts!.size > IMPORTED_ACCOUNT_MAX_NUM) {
throw new TooManyImportedAccounts(IMPORTED_ACCOUNT_MAX_NUM);
}
const context = this.realm!.objectForPrimaryKey<ContextSchema>(
'Context',
MAIN_CONTEXT,
);
if (!context) {
return Promise.reject(
new OneKeyInternalError('Context not found.'),
);
}
if (!importedCredential) {
return Promise.reject(
new OneKeyInternalError(
'Imported credential required for adding imported accounts.',
),
);
}
if (!checkPassword(context, importedCredential.password)) {
return Promise.reject(new WrongPassword());
}
this.realm!.create('Credential', {
id: account.id,
credential: JSON.stringify({
privateKey: importedCredential!.privateKey.toString('hex'),
privateKey: importedCredential.privateKey.toString('hex'),
}),
});
if (context.verifyString === DEFAULT_VERIFY_STRING) {
context.verifyString = encrypt(
importedCredential.password,
Buffer.from(DEFAULT_VERIFY_STRING),
).toString('hex');
}
wallet.nextAccountIds!.global += 1;
break;
}
Expand Down