-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle duplicated wallet name (#10459)
* fix: handle duplicated wallet name * fix: enable * chore: add test * fix: handle duplicated wallet name * fix: add test * fix: add test * fix: naming * fix: add test * fix: add test * fix: add test * fix: add test * fix: add test * chore: rename * fix: no disconect when own proof change * fix: naming * fix: persona setting link * chore: reply code review
- Loading branch information
1 parent
852632a
commit 8ab78c9
Showing
16 changed files
with
96 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/dashboard/src/pages/CreateMaskWallet/Recovery/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/shared-base/src/helpers/handleDuplicatedWalletName.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/web3-shared/base/src/helpers/generateNewWalletName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { max } from 'lodash-es' | ||
import type { Wallet } from '@masknet/shared-base' | ||
|
||
export function generateNewWalletName(wallets: Wallet[], index = 0, preIndex?: number) { | ||
const indexList = wallets.filter((x) => !x.owner).map((x) => x.name.split('Wallet ')[1]) | ||
const maxIndex = max(indexList.filter((x) => x && !Number.isNaN(x)).map(Number)) ?? 0 | ||
const preIndexNotExists = !indexList.some((x) => Number(x) === preIndex) | ||
const finalIndex = preIndex && (preIndex > maxIndex || preIndexNotExists) ? preIndex : maxIndex + index + 1 | ||
return `Wallet ${finalIndex}` | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/web3-shared/base/src/helpers/generateUniqueWalletName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isUndefined, max } from 'lodash-es' | ||
import type { Wallet } from '@masknet/shared-base' | ||
|
||
export function generateUniqueWalletName(wallets: Wallet[], name: string) { | ||
const num = name.match(/\((\d+)\)/)?.[1] | ||
const _name = num ? name.split(` (${num})`)?.[0] : name | ||
|
||
const maxIndex = | ||
max([ | ||
...wallets | ||
.filter((x) => !x.owner) | ||
.map((x) => x.name.split(`${_name} `)[1]?.match(/\((\d+)\)/)?.[1]) | ||
.filter((x) => x && !Number.isNaN(x)) | ||
.map(Number), | ||
...(num && !Number.isNaN(num) ? [Number(num) - 1] : []), | ||
]) ?? (wallets.some((wallet) => wallet.name === _name) ? 0 : undefined) | ||
|
||
if (isUndefined(maxIndex)) return name | ||
|
||
return `${_name} (${maxIndex + 1})` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/web3-shared/base/tests/helpers/generateNewWalletName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import type { Wallet } from '@masknet/shared-base' | ||
import { generateNewWalletName } from '../../src/helpers/generateNewWalletName.js' | ||
|
||
describe('generateNewWalletName util test', () => { | ||
test.each([ | ||
{ wallets: [{ name: 'Wallet 1' }] as Wallet[], index: 0, expected: 'Wallet 2' }, | ||
{ wallets: [{ name: 'Wallet 3' }] as Wallet[], index: 0, expected: 'Wallet 4' }, | ||
{ wallets: [{ name: 'Wallet 2' }, { name: 'Wallet 3' }] as Wallet[], index: 1, expected: 'Wallet 5' }, | ||
{ wallets: [{ name: 'Wallet 3' }] as Wallet[], index: 0, preIndex: 1, expected: 'Wallet 1' }, | ||
{ wallets: [{ name: 'Wallet 5' }] as Wallet[], index: 1, preIndex: 10, expected: 'Wallet 10' }, | ||
])('.format($wallets $index $preIndex)', ({ wallets, index, preIndex, expected }) => { | ||
expect(generateNewWalletName(wallets, index, preIndex)).toBe(expected) | ||
}) | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/web3-shared/base/tests/helpers/generateUniqueWalletName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import type { Wallet } from '@masknet/shared-base' | ||
import { generateUniqueWalletName } from '../../src/helpers/generateUniqueWalletName.js' | ||
|
||
describe('generateUniqueWalletName util test', () => { | ||
test.each([ | ||
{ wallets: [{ name: 'a' }] as Wallet[], name: 'a', expected: 'a (1)' }, | ||
{ wallets: [{ name: 'b' }] as Wallet[], name: 'a', expected: 'a' }, | ||
{ wallets: [{ name: 'b' }] as Wallet[], name: 'a', expected: 'a' }, | ||
{ wallets: [{ name: 'a (1)' }, { name: 'a (5)' }] as Wallet[], name: 'a', expected: 'a (6)' }, | ||
{ wallets: [{ name: 'a (3)' }] as Wallet[], name: 'a (1)', expected: 'a (4)' }, | ||
{ wallets: [{ name: 'a (3)' }] as Wallet[], name: 'a (11)', expected: 'a (11)' }, | ||
])('.format($wallets $name)', ({ wallets, name, expected }) => { | ||
expect(generateUniqueWalletName(wallets, name)).toBe(expected) | ||
}) | ||
}) |