|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { createNonceTerms } from '../../src/caveats/nonce'; |
| 4 | + |
| 5 | +describe('createNonceTerms', () => { |
| 6 | + const EXPECTED_BYTE_LENGTH = 32; // 32 bytes for nonce |
| 7 | + |
| 8 | + it('creates valid terms for simple nonce', () => { |
| 9 | + const nonce = '0x1234567890abcdef'; |
| 10 | + const result = createNonceTerms({ nonce }); |
| 11 | + |
| 12 | + expect(result).toStrictEqual( |
| 13 | + '0x0000000000000000000000000000000000000000000000001234567890abcdef', |
| 14 | + ); |
| 15 | + }); |
| 16 | + |
| 17 | + it('creates valid terms for zero nonce', () => { |
| 18 | + const nonce = '0x0'; |
| 19 | + const result = createNonceTerms({ nonce }); |
| 20 | + |
| 21 | + expect(result).toStrictEqual( |
| 22 | + '0x0000000000000000000000000000000000000000000000000000000000000000', |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + it('creates valid terms for minimal nonce', () => { |
| 27 | + const nonce = '0x1'; |
| 28 | + const result = createNonceTerms({ nonce }); |
| 29 | + |
| 30 | + expect(result).toStrictEqual( |
| 31 | + '0x0000000000000000000000000000000000000000000000000000000000000001', |
| 32 | + ); |
| 33 | + }); |
| 34 | + |
| 35 | + it('creates valid terms for full 32-byte nonce', () => { |
| 36 | + const nonce = |
| 37 | + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; |
| 38 | + const result = createNonceTerms({ nonce }); |
| 39 | + |
| 40 | + expect(result).toStrictEqual(nonce); |
| 41 | + }); |
| 42 | + |
| 43 | + it('creates valid terms for uppercase hex nonce', () => { |
| 44 | + const nonce = '0x1234567890ABCDEF'; |
| 45 | + const result = createNonceTerms({ nonce }); |
| 46 | + |
| 47 | + expect(result).toStrictEqual( |
| 48 | + '0x0000000000000000000000000000000000000000000000001234567890ABCDEF', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('creates valid terms for mixed case hex nonce', () => { |
| 53 | + const nonce = '0x1234567890AbCdEf'; |
| 54 | + const result = createNonceTerms({ nonce }); |
| 55 | + |
| 56 | + expect(result).toStrictEqual( |
| 57 | + '0x0000000000000000000000000000000000000000000000001234567890AbCdEf', |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it('pads shorter hex values with leading zeros', () => { |
| 62 | + const nonce = '0xff'; |
| 63 | + const result = createNonceTerms({ nonce }); |
| 64 | + |
| 65 | + expect(result).toStrictEqual( |
| 66 | + '0x00000000000000000000000000000000000000000000000000000000000000ff', |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it('throws an error for empty nonce', () => { |
| 71 | + const nonce = '0x'; |
| 72 | + |
| 73 | + expect(() => createNonceTerms({ nonce })).toThrow( |
| 74 | + 'Invalid nonce: must be a non-empty hex string', |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('throws an error for undefined nonce', () => { |
| 79 | + expect(() => createNonceTerms({ nonce: undefined as any })).toThrow( |
| 80 | + 'Invalid nonce: must be a non-empty hex string', |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('throws an error for null nonce', () => { |
| 85 | + expect(() => createNonceTerms({ nonce: null as any })).toThrow( |
| 86 | + 'Invalid nonce: must be a non-empty hex string', |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('throws an error for nonce without 0x prefix', () => { |
| 91 | + const nonce = '1234567890abcdef' as any; |
| 92 | + |
| 93 | + expect(() => createNonceTerms({ nonce })).toThrow( |
| 94 | + 'Invalid nonce: must be a valid hex string', |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('throws an error for invalid hex characters', () => { |
| 99 | + const nonce = '0x1234567890abcdefg' as any; |
| 100 | + |
| 101 | + expect(() => createNonceTerms({ nonce })).toThrow( |
| 102 | + 'Invalid nonce: must be a valid hex string', |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it('throws an error for non-string nonce', () => { |
| 107 | + const nonce = 123456 as any; |
| 108 | + |
| 109 | + expect(() => createNonceTerms({ nonce })).toThrow( |
| 110 | + 'Invalid nonce: must be a valid hex string', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('throws an error for nonce longer than 32 bytes', () => { |
| 115 | + // 33 bytes (66 hex chars + 0x prefix = 68 chars total, which exceeds 66) |
| 116 | + const nonce = |
| 117 | + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12' as any; |
| 118 | + |
| 119 | + expect(() => createNonceTerms({ nonce })).toThrow( |
| 120 | + 'Invalid nonce: must be 32 bytes or less in length', |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('accepts nonce with exactly 32 bytes', () => { |
| 125 | + // 32 bytes (64 hex chars + 0x prefix = 66 chars total) |
| 126 | + const nonce = |
| 127 | + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; |
| 128 | + const result = createNonceTerms({ nonce }); |
| 129 | + |
| 130 | + expect(result).toStrictEqual(nonce); |
| 131 | + }); |
| 132 | + |
| 133 | + it('throws an error for string that looks like hex but has odd length', () => { |
| 134 | + const nonce = '0x123' as any; |
| 135 | + // This should still work as we pad it |
| 136 | + const result = createNonceTerms({ nonce }); |
| 137 | + |
| 138 | + expect(result).toStrictEqual( |
| 139 | + '0x0000000000000000000000000000000000000000000000000000000000000123', |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + // Tests for bytes return type |
| 144 | + describe('bytes return type', () => { |
| 145 | + it('returns Uint8Array when bytes encoding is specified', () => { |
| 146 | + const nonce = '0x1234567890abcdef'; |
| 147 | + const result = createNonceTerms({ nonce }, { out: 'bytes' }); |
| 148 | + |
| 149 | + expect(result).toBeInstanceOf(Uint8Array); |
| 150 | + expect(result).toHaveLength(EXPECTED_BYTE_LENGTH); |
| 151 | + }); |
| 152 | + |
| 153 | + it('returns Uint8Array for minimal nonce with bytes encoding', () => { |
| 154 | + const nonce = '0x1'; |
| 155 | + const result = createNonceTerms({ nonce }, { out: 'bytes' }); |
| 156 | + |
| 157 | + expect(result).toBeInstanceOf(Uint8Array); |
| 158 | + expect(result).toHaveLength(EXPECTED_BYTE_LENGTH); |
| 159 | + // Should be 31 zeros followed by 1 |
| 160 | + const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0); |
| 161 | + expectedBytes[EXPECTED_BYTE_LENGTH - 1] = 1; |
| 162 | + expect(Array.from(result)).toEqual(expectedBytes); |
| 163 | + }); |
| 164 | + |
| 165 | + it('returns Uint8Array for zero nonce with bytes encoding', () => { |
| 166 | + const nonce = '0x0'; |
| 167 | + const result = createNonceTerms({ nonce }, { out: 'bytes' }); |
| 168 | + |
| 169 | + expect(result).toBeInstanceOf(Uint8Array); |
| 170 | + expect(result).toHaveLength(EXPECTED_BYTE_LENGTH); |
| 171 | + // Should be all zeros |
| 172 | + const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0); |
| 173 | + expect(Array.from(result)).toEqual(expectedBytes); |
| 174 | + }); |
| 175 | + |
| 176 | + it('returns Uint8Array for full nonce with bytes encoding', () => { |
| 177 | + const nonce = |
| 178 | + '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'; |
| 179 | + const result = createNonceTerms({ nonce }, { out: 'bytes' }); |
| 180 | + |
| 181 | + expect(result).toBeInstanceOf(Uint8Array); |
| 182 | + expect(result).toHaveLength(EXPECTED_BYTE_LENGTH); |
| 183 | + // Convert expected hex to bytes for comparison |
| 184 | + const expectedBytes = [ |
| 185 | + 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, |
| 186 | + 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, |
| 187 | + 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, |
| 188 | + ]; |
| 189 | + expect(Array.from(result)).toEqual(expectedBytes); |
| 190 | + }); |
| 191 | + |
| 192 | + it('returns Uint8Array for padded hex values with bytes encoding', () => { |
| 193 | + const nonce = '0xff'; |
| 194 | + const result = createNonceTerms({ nonce }, { out: 'bytes' }); |
| 195 | + |
| 196 | + expect(result).toBeInstanceOf(Uint8Array); |
| 197 | + expect(result).toHaveLength(EXPECTED_BYTE_LENGTH); |
| 198 | + // Should be 31 zeros followed by 0xff |
| 199 | + const expectedBytes = new Array(EXPECTED_BYTE_LENGTH).fill(0); |
| 200 | + expectedBytes[EXPECTED_BYTE_LENGTH - 1] = 0xff; |
| 201 | + expect(Array.from(result)).toEqual(expectedBytes); |
| 202 | + }); |
| 203 | + }); |
| 204 | +}); |
0 commit comments