Skip to content

Commit

Permalink
feat(compose_txn): add validation rules for voting key, selection key…
Browse files Browse the repository at this point in the history
… & state proof key fields
  • Loading branch information
No-Cash-7970 committed Dec 5, 2024
1 parent c2616c5 commit caeff33
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
44 changes: 40 additions & 4 deletions src/app/lib/txn-data/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import {
MAX_ASSET_TOTAL,
MAX_DECIMAL_PLACES,
MIN_TX_FEE,
SELECTION_KEY_LENGTH,
STATE_PROOF_KEY_LENGTH,
UNIT_NAME_MAX_LENGTH,
URL_MAX_LENGTH
URL_MAX_LENGTH,
VOTE_KEY_LENGTH
} from './constants';
import type {
BoxRefAtomGroup,
Expand Down Expand Up @@ -431,13 +434,46 @@ export const apbx = splitAtom(apbxListAtom);
*/

/** Key Registration - Voting key */
export const votekey = atomWithValidate<string>('', { validate: v => v });
export const votekey = atomWithValidate<string>('', { validate: v => {
let validationSchema = YupString();

// Check the length only when the field value is nonempty
if (v) validationSchema = validationSchema.length(VOTE_KEY_LENGTH);

validationSchema.matches(base64RegExp, {
excludeEmptyString: true,
message: (): ValidationMessage => ({key: 'fields.base64.error'})
}).validateSync(v);
return v;
}});

/** Key Registration - Selection key */
export const selkey = atomWithValidate<string>('', { validate: v => v });
export const selkey = atomWithValidate<string>('', { validate: v => {
let validationSchema = YupString();

// Check the length only when the field value is nonempty
if (v) validationSchema = validationSchema.length(SELECTION_KEY_LENGTH);

validationSchema.matches(base64RegExp, {
excludeEmptyString: true,
message: (): ValidationMessage => ({key: 'fields.base64.error'})
}).validateSync(v);
return v;
}});

/** Key Registration - State proof key */
export const sprfkey = atomWithValidate<string>('', { validate: v => v });
export const sprfkey = atomWithValidate<string>('', { validate: v => {
let validationSchema = YupString();

// Check the length only when the field value is nonempty
if (v) validationSchema = validationSchema.length(STATE_PROOF_KEY_LENGTH);

validationSchema.matches(base64RegExp, {
excludeEmptyString: true,
message: (): ValidationMessage => ({key: 'fields.base64.error'})
}).validateSync(v);
return v;
}});

/** Key Registration - First voting round */
export const votefst = atomWithValidate<number|undefined>(undefined, {
Expand Down
30 changes: 26 additions & 4 deletions src/app/lib/txn-data/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

/** Number of characters in a valid account address */
export const ADDRESS_LENGTH = 58;
/** Maximum length of a lease in bytes (or characters if only using ASCII characters) */
/** Length of a lease in bytes (or characters if only using ASCII characters) */
export const LEASE_LENGTH = 32;
/** Maximum length of a lease when encoded in base64
/** Length of a lease when encoded in base64
*
* Equation to find length of base64 string for a given number of bytes:
* `b64_length = (num_bytes + 2) / 3 * 4`
Expand Down Expand Up @@ -39,9 +39,9 @@ export const UNIT_NAME_MAX_LENGTH = 8;
export const ASSET_NAME_MAX_LENGTH = 32;
/** Maximum length of an asset's URL in bytes (or characters if only using ASCII characters) */
export const URL_MAX_LENGTH = 96;
/** Allowed length of an asset's metadata hash. No more and no less (fewer), unless empty. */
/** Length of an asset's metadata hash. No more and no less (fewer), unless empty. */
export const METADATA_HASH_LENGTH = 32;
/** Maximum length of a metadata hash when encoded in base64
/** Length of a metadata hash when encoded in base64
*
* Equation to find length of base64 string for a given number of bytes:
* `b64_length = (num_bytes + 2) / 3 * 4`
Expand All @@ -53,6 +53,28 @@ export const MAX_DECIMAL_PLACES = 19;
/** Maximum number for total */
export const MAX_ASSET_TOTAL = BigInt(2**64) - BigInt(1);

/** Length of a voter key, which is encoded in base64
*
* Equation to find length of base64 string for a given number of bytes:
* `b64_length = (num_bytes + 2) / 3 * 4`
* (From: https://stackoverflow.com/a/60067262)
*/
export const VOTE_KEY_LENGTH = 44; // (32 + 2) / 3 * 4
/** Length of a selection key, which is encoded in base64
*
* Equation to find length of base64 string for a given number of bytes:
* `b64_length = (num_bytes + 2) / 3 * 4`
* (From: https://stackoverflow.com/a/60067262)
*/
export const SELECTION_KEY_LENGTH = 44; // (32 + 2) / 3 * 4
/** Length of a state proof key, which is encoded in base64
*
* Equation to find length of base64 string for a given number of bytes:
* `b64_length = (num_bytes + 2) / 3 * 4`
* (From: https://stackoverflow.com/a/60067262)
*/
export const STATE_PROOF_KEY_LENGTH = 88; // (64 + 2) / 3 * 4

// Sources:
// https://developer.algorand.org/docs/get-details/parameter_tables/
// https://developer.algorand.org/docs/get-details/dapps/smart-contracts/apps/#resource-availability
Expand Down

0 comments on commit caeff33

Please sign in to comment.