github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/test/common/hex-strings.spec.ts (about) 1 import { BigNumber } from '@ethersproject/bignumber' 2 3 /* Imports: Internal */ 4 import { expect } from '../setup' 5 import { 6 toRpcHexString, 7 remove0x, 8 add0x, 9 fromHexString, 10 toHexString, 11 padHexString, 12 encodeHex, 13 hexStringEquals, 14 bytes32ify, 15 } from '../../src' 16 17 describe('remove0x', () => { 18 it('should return undefined', () => { 19 expect(remove0x(undefined)).to.deep.equal(undefined) 20 }) 21 22 it('should return without a 0x', () => { 23 const cases = [ 24 { input: '0x', output: '' }, 25 { 26 input: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', 27 output: '1f9840a85d5af5bf1d1762f925bdaddc4201f984', 28 }, 29 { input: 'a', output: 'a' }, 30 ] 31 for (const test of cases) { 32 expect(remove0x(test.input)).to.deep.equal(test.output) 33 } 34 }) 35 }) 36 37 describe('add0x', () => { 38 it('should return undefined', () => { 39 expect(add0x(undefined)).to.deep.equal(undefined) 40 }) 41 42 it('should return with a 0x', () => { 43 const cases = [ 44 { input: '0x', output: '0x' }, 45 { 46 input: '1f9840a85d5af5bf1d1762f925bdaddc4201f984', 47 output: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', 48 }, 49 { input: '', output: '0x' }, 50 ] 51 for (const test of cases) { 52 expect(add0x(test.input)).to.deep.equal(test.output) 53 } 54 }) 55 }) 56 57 describe('toHexString', () => { 58 it('should throw an error when input is null', () => { 59 expect(() => { 60 toHexString(null) 61 }).to.throw( 62 'The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received null' 63 ) 64 }) 65 66 it('should return with a hex string', () => { 67 const cases = [ 68 { input: 0, output: '0x00' }, 69 { input: 48, output: '0x30' }, 70 { 71 input: '0', 72 output: '0x30', 73 }, 74 { input: '', output: '0x' }, 75 ] 76 for (const test of cases) { 77 expect(toHexString(test.input)).to.deep.equal(test.output) 78 } 79 }) 80 }) 81 82 describe('fromHexString', () => { 83 it('should return a buffer from a hex string', () => { 84 const cases = [ 85 { input: '0x', output: Buffer.from('', 'hex') }, 86 { 87 input: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', 88 output: Buffer.from('1f9840a85d5af5bf1d1762f925bdaddc4201f984', 'hex'), 89 }, 90 { input: '', output: Buffer.from('', 'hex') }, 91 { 92 input: Buffer.from('1f9840a85d5af5bf1d1762f925bdaddc4201f984'), 93 output: Buffer.from('1f9840a85d5af5bf1d1762f925bdaddc4201f984'), 94 }, 95 ] 96 97 for (const test of cases) { 98 expect(fromHexString(test.input)).to.deep.equal(test.output) 99 } 100 }) 101 }) 102 103 describe('padHexString', () => { 104 it('should return return input string if length is 2 + length * 2', () => { 105 expect(padHexString('abcd', 1)).to.deep.equal('abcd') 106 expect(padHexString('abcdefgh', 3).length).to.deep.equal(8) 107 }) 108 109 it('should return a string padded with 0x and zeros', () => { 110 expect(padHexString('0xabcd', 3)).to.deep.equal('0x00abcd') 111 }) 112 }) 113 114 describe('toRpcHexString', () => { 115 it('should parse 0', () => { 116 expect(toRpcHexString(0)).to.deep.equal('0x0') 117 expect(toRpcHexString(BigNumber.from(0))).to.deep.equal('0x0') 118 }) 119 120 it('should parse non 0', () => { 121 const cases = [ 122 { input: 2, output: '0x2' }, 123 { input: BigNumber.from(2), output: '0x2' }, 124 { input: 100, output: '0x64' }, 125 { input: BigNumber.from(100), output: '0x64' }, 126 { input: 300, output: '0x12c' }, 127 { input: BigNumber.from(300), output: '0x12c' }, 128 ] 129 for (const test of cases) { 130 expect(toRpcHexString(test.input)).to.deep.equal(test.output) 131 } 132 }) 133 }) 134 135 describe('encodeHex', () => { 136 it('should throw an error when val is invalid', () => { 137 expect(() => { 138 encodeHex(null, 0) 139 }).to.throw('invalid BigNumber value') 140 141 expect(() => { 142 encodeHex(10.5, 0) 143 }).to.throw('fault="underflow", operation="BigNumber.from", value=10.5') 144 145 expect(() => { 146 encodeHex('10.5', 0) 147 }).to.throw('invalid BigNumber string') 148 }) 149 150 it('should return a hex string of val with length len', () => { 151 const cases = [ 152 { 153 input: { 154 val: 0, 155 len: 0, 156 }, 157 output: '00', 158 }, 159 { 160 input: { 161 val: 0, 162 len: 4, 163 }, 164 output: '0000', 165 }, 166 { 167 input: { 168 val: 1, 169 len: 0, 170 }, 171 output: '01', 172 }, 173 { 174 input: { 175 val: 1, 176 len: 10, 177 }, 178 output: '0000000001', 179 }, 180 { 181 input: { 182 val: 100, 183 len: 4, 184 }, 185 output: '0064', 186 }, 187 { 188 input: { 189 val: '100', 190 len: 0, 191 }, 192 output: '64', 193 }, 194 ] 195 for (const test of cases) { 196 expect(encodeHex(test.input.val, test.input.len)).to.deep.equal( 197 test.output 198 ) 199 } 200 }) 201 }) 202 203 describe('hexStringEquals', () => { 204 it('should throw an error when input is not a hex string', () => { 205 expect(() => { 206 hexStringEquals('', '') 207 }).to.throw('input is not a hex string: ') 208 209 expect(() => { 210 hexStringEquals('0xx', '0x1') 211 }).to.throw('input is not a hex string: 0xx') 212 213 expect(() => { 214 hexStringEquals('0x1', '2') 215 }).to.throw('input is not a hex string: 2') 216 217 expect(() => { 218 hexStringEquals('-0x1', '0x1') 219 }).to.throw('input is not a hex string: -0x1') 220 }) 221 222 it('should return the hex strings equality', () => { 223 const cases = [ 224 { 225 input: { 226 stringA: '0x', 227 stringB: '0x', 228 }, 229 output: true, 230 }, 231 { 232 input: { 233 stringA: '0x1', 234 stringB: '0x1', 235 }, 236 output: true, 237 }, 238 { 239 input: { 240 stringA: '0x064', 241 stringB: '0x064', 242 }, 243 output: true, 244 }, 245 { 246 input: { 247 stringA: '0x', 248 stringB: '0x0', 249 }, 250 output: false, 251 }, 252 { 253 input: { 254 stringA: '0x0', 255 stringB: '0x1', 256 }, 257 output: false, 258 }, 259 { 260 input: { 261 stringA: '0x64', 262 stringB: '0x064', 263 }, 264 output: false, 265 }, 266 ] 267 for (const test of cases) { 268 expect( 269 hexStringEquals(test.input.stringA, test.input.stringB) 270 ).to.deep.equal(test.output) 271 } 272 }) 273 }) 274 275 describe('bytes32ify', () => { 276 it('should throw an error when input is invalid', () => { 277 expect(() => { 278 bytes32ify(-1) 279 }).to.throw('invalid hex string') 280 }) 281 282 it('should return a zero padded, 32 bytes hex string', () => { 283 const cases = [ 284 { 285 input: 0, 286 output: 287 '0x0000000000000000000000000000000000000000000000000000000000000000', 288 }, 289 { 290 input: BigNumber.from(0), 291 output: 292 '0x0000000000000000000000000000000000000000000000000000000000000000', 293 }, 294 { 295 input: 2, 296 output: 297 '0x0000000000000000000000000000000000000000000000000000000000000002', 298 }, 299 { 300 input: BigNumber.from(2), 301 output: 302 '0x0000000000000000000000000000000000000000000000000000000000000002', 303 }, 304 { 305 input: 100, 306 output: 307 '0x0000000000000000000000000000000000000000000000000000000000000064', 308 }, 309 ] 310 for (const test of cases) { 311 expect(bytes32ify(test.input)).to.deep.equal(test.output) 312 } 313 }) 314 })