Check Smart card public keys
The set of utility functions (guards) for determining the status of a user's hardware card. These functions read the card's public keys from the Browser local store and evaluate them to check if the card is new, already registered, or in an inconsistent "broken" state.
The primary purpose of these guards is to direct the application flow, especially during the authorization process, ensuring the user is sent to the correct screen (e.g., registration, wallet, or an error page) based on the card's state.
1. Dependencies
import { store } from @/store: The application's central Vuex store. The guards specifically accessstore.state.publicKeyCardto retrieve the card's public keys.
2. Data Structures
PublicKeyCard Interface
To ensure type safety when accessing the store, the module defines the shape of the public key object.
interface PublicKeyCard {
secp256k1?: string | null;
ed25519?: string | null;
}secp256k1: An optional 66-character hexadecimal string representing the secp256k1 public key.ed25519: An optional 64-character hexadecimal string representing the ed25519 public key.
3. Exported Functions
isCardIsNew()
- Description: Checks if the card is in a "new" state, meaning it has not yet been initialized with a seed phrase.
- Logic: It verifies if both the
secp256k1anded25519public keys consist entirely of zeros. - Returns:
boolean-trueif the card is new,falseotherwise.
isCardRegistered()
- Description: Checks if the card has been successfully registered and contains valid, non-zero public keys.
- Logic: It verifies that both public keys are valid hexadecimal strings of the correct length and are not zero-filled.
- Returns:
boolean-trueif the card is registered and valid,falseotherwise.
isCardBroken()
- Description: Detects an inconsistent or "broken" state. This typically occurs if the card initialization process was interrupted, leaving one key written but not the other.
- Logic: It returns
trueif one of the public keys is initialized (valid and non-zero) while the other is still zero-filled. - Returns:
boolean-trueif the card is in a broken state,falseotherwise.
isTagHash(url: string)
- Description: A general utility function to validate if a given string matches the format of a specific 134-character tag hash. This function is not directly related to card state but is included as a format validator.
- Parameters:
url: string: The input string to validate.
- Logic: Uses a regular expression to check if the string consists of exactly 134 hexadecimal characters (
[0-9a-fA-F]). - Returns:
boolean-trueif the string is a valid tag hash,falseotherwise.
4. Implementation Details
Performance
For efficiency, the module pre-compiles all regular expressions into const variables. This is more performant than creating new regex objects on each function call, as the compilation happens only once when the module is first loaded.
Null Safety
The functions safely handle null or undefined public key values that might come from the store. The nullish coalescing operator (?? '') is used to provide an empty string as a default, preventing runtime errors when testing against regular expressions.
5. Usage Example
The primary consumer of these guards is the Authorization.vue component, which uses them to route the user after a card has been read.
import { isCardRegistered, isCardIsNew, isCardBroken } from '@/api/checkGuards.ts';
// ... inside an async function after reading the card's public keys ...
if (isCardIsNew()) {
// The card is new. Redirect to the registration page to create a seed phrase.
router.push({ name: 'register-card' });
}
if (isCardRegistered() && !isCardIsNew()) {
// The card is fully registered. Proceed to the main dashboard.
router.push({ name: 'wallet' });
} else if (isCardBroken()) {
// The card is in an inconsistent state. Show an error to the user.
throw new Error('Card error', { cause: { errorSubtitle: "Not valid public keys." } });
}This logic ensures a robust authorization flow by handling all possible card states.
