|
| 1 | +import { isHexString } from '@metamask/utils'; |
| 2 | +import type { BytesLike } from '@metamask/utils'; |
| 3 | + |
| 4 | +import { |
| 5 | + bytesLikeToHex, |
| 6 | + defaultOptions, |
| 7 | + prepareResult, |
| 8 | + type EncodingOptions, |
| 9 | + type ResultValue, |
| 10 | +} from '../returns'; |
| 11 | +import type { Hex } from '../types'; |
| 12 | + |
| 13 | +// char length of 32 byte hex string (including 0x prefix) |
| 14 | +const MAX_NONCE_STRING_LENGTH = 66; |
| 15 | + |
| 16 | +/** |
| 17 | + * Terms for configuring a Nonce caveat. |
| 18 | + */ |
| 19 | +export type NonceTerms = { |
| 20 | + /** The nonce as BytesLike (0x-prefixed hex string or Uint8Array) to allow bulk revocation of delegations. */ |
| 21 | + nonce: BytesLike; |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates terms for a Nonce caveat that uses a nonce value for bulk revocation of delegations. |
| 26 | + * |
| 27 | + * @param terms - The terms for the Nonce caveat. |
| 28 | + * @param encodingOptions - The encoding options for the result. |
| 29 | + * @returns The terms as a 32-byte hex string. |
| 30 | + * @throws Error if the nonce is invalid. |
| 31 | + */ |
| 32 | +export function createNonceTerms( |
| 33 | + terms: NonceTerms, |
| 34 | + encodingOptions?: EncodingOptions<'hex'>, |
| 35 | +): Hex; |
| 36 | +export function createNonceTerms( |
| 37 | + terms: NonceTerms, |
| 38 | + encodingOptions: EncodingOptions<'bytes'>, |
| 39 | +): Uint8Array; |
| 40 | +/** |
| 41 | + * Creates terms for a Nonce caveat that uses a nonce value for bulk revocation of delegations. |
| 42 | + * |
| 43 | + * @param terms - The terms for the Nonce caveat. |
| 44 | + * @param encodingOptions - The encoding options for the result. |
| 45 | + * @returns The terms as a 32-byte padded value in the specified encoding format. |
| 46 | + * @throws Error if the nonce is invalid or empty. |
| 47 | + */ |
| 48 | +export function createNonceTerms( |
| 49 | + terms: NonceTerms, |
| 50 | + encodingOptions: EncodingOptions<ResultValue> = defaultOptions, |
| 51 | +): Hex | Uint8Array { |
| 52 | + const { nonce } = terms; |
| 53 | + |
| 54 | + // Handle zero-length Uint8Array specifically |
| 55 | + if (nonce instanceof Uint8Array && nonce.length === 0) { |
| 56 | + throw new Error('Invalid nonce: Uint8Array must not be empty'); |
| 57 | + } |
| 58 | + |
| 59 | + // Validate that strings have 0x prefix (as required by BytesLike) |
| 60 | + if (typeof nonce === 'string' && !nonce.startsWith('0x')) { |
| 61 | + throw new Error('Invalid nonce: string must have 0x prefix'); |
| 62 | + } |
| 63 | + |
| 64 | + // Convert to hex string for consistent processing |
| 65 | + const hexNonce = bytesLikeToHex(nonce); |
| 66 | + |
| 67 | + // Check for empty hex string (0x) first - more specific error |
| 68 | + if (hexNonce === '0x') { |
| 69 | + throw new Error('Invalid nonce: must not be empty'); |
| 70 | + } |
| 71 | + |
| 72 | + if (!isHexString(hexNonce)) { |
| 73 | + throw new Error('Invalid nonce: must be a valid BytesLike value'); |
| 74 | + } |
| 75 | + |
| 76 | + if (hexNonce.length > MAX_NONCE_STRING_LENGTH) { |
| 77 | + throw new Error('Invalid nonce: must be 32 bytes or less in length'); |
| 78 | + } |
| 79 | + |
| 80 | + // Remove '0x' prefix for padding, then add it back |
| 81 | + const nonceWithoutPrefix = hexNonce.slice(2); |
| 82 | + const paddedNonce = nonceWithoutPrefix.padStart(64, '0'); // 64 hex chars = 32 bytes |
| 83 | + const hexValue = `0x${paddedNonce}`; |
| 84 | + |
| 85 | + return prepareResult(hexValue, encodingOptions); |
| 86 | +} |
0 commit comments