-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
web-wallet: Add address validation (Transfer flow)
Resolves #1377
- Loading branch information
1 parent
b790655
commit 9eb65d5
Showing
8 changed files
with
106 additions
and
12 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
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
45 changes: 45 additions & 0 deletions
45
web-wallet/src/lib/dusk/string/__tests__/validateAddress.spec.js
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,45 @@ | ||
import { validateAddress } from ".."; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
describe("validateAddress", () => { | ||
const validAddresses = [ | ||
"47jNTgAhzn9KCKF3msCfvKg3k1P1QpPCLZ3HG3AoNp87sQ5WNS3QyjckYHWeuXqW7uvLmbKgejpP8Xkcip89vnMM", | ||
"4xwKPC9UMvketmoNkDvyaJufcTZWmNn8giB8xWTf3Qk8nFkRW81nTVwSdGPcbomzHThPuoXsdFzrzwiMar6BEfdw", | ||
"5kB6VBePF8eFhFVjLwM1xrEL6yGBm1uDsoWyRjdqDQ2nNz8nECAsRh3MZiM6uEo6WmukqyKzzCK9B5rcPTnjZQgt", | ||
"4LaS4bWzFQtvxZ7frUaXbfm3xsbnHHYwNkGnLqqpmWPYQeSfbAPy7N4Md8gk5gHn9f4wxNSNyFJuyxcnXPSWTRMd", | ||
"gMxrVEH5aW7XuQiXN2Pm2YRLHyCNmokmBb1VzjcmcQg7gzmxstPnozdt7SvvMKLP71BadPsa5jmoWFc2WzWDYPo" | ||
]; | ||
|
||
const invalidAddresses = [ | ||
// Invalid Base58 | ||
"InvalidKey12345", | ||
|
||
// Too short | ||
"4LaS4bWzFQtvxZ7frUaXbfm3xsbnHHYwNkGnLqqpmWPY", | ||
|
||
// Too long | ||
"5kB6VBePF8eFhFVjLwM1xrEL6yGBm1uDsoWyRjdqDQ2nNz8nECAsRh3MZiM6uEo6WmukqyKzzCK9B5rcPTnjZQgtXXXXXXXX", | ||
|
||
// Empty string | ||
"", | ||
|
||
// Contains an invalid character | ||
"47jNTgAhzn9KCKF3msCfvKg3k1P1QpPCLZ3HG3AoNp87sQ5WNS3QyjckYHWeuXqW7uvLmbKgejpP8Xkcip89vnM!" | ||
]; | ||
|
||
it("passes when supplied with a valid address", () => { | ||
for (const address of validAddresses) { | ||
const result = validateAddress(address); | ||
|
||
expect(result.isValid).toBe(true); | ||
} | ||
}); | ||
|
||
it("fails when supplied with an invalid address", () => { | ||
for (const address of invalidAddresses) { | ||
const result = validateAddress(address); | ||
|
||
expect(result.isValid).toBe(false); | ||
} | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Validates a Dusk address, with feedback on failure or success. | ||
* @param {String} address The public spent key to validate. | ||
* @returns {{isValid: boolean, reason: string}} An object with two keys: | ||
* - `isValid` {Boolean} - true if the address is valid, false if invalid. | ||
* - `reason` {String} - describes why the address is invalid or confirms if it is valid. | ||
*/ | ||
export default function validateAddress (address) { | ||
const regex = /[^\w]/g; | ||
|
||
if (address.length < 87 || address.length > 88) { | ||
return { isValid: false, reason: "Invalid length. Addresses must be 87 or 88 characters long." }; | ||
} | ||
|
||
if (address.search(regex) !== -1) { | ||
return { isValid: false, reason: "Invalid character set. Address contains forbidden characters." }; | ||
} | ||
|
||
return { isValid: true, reason: "Valid address." }; | ||
} |