|
1 | 1 | import { describe, test, expect, vi, afterEach } from 'vitest';
|
2 | 2 | import * as crossFetch from 'cross-fetch';
|
3 |
| -import { validate, getScores, getVp } from './utils'; |
| 3 | +import { |
| 4 | + validate, |
| 5 | + validateSchema, |
| 6 | + getScores, |
| 7 | + getVp, |
| 8 | + getFormattedAddress |
| 9 | +} from './utils'; |
4 | 10 |
|
5 | 11 | vi.mock('cross-fetch', async () => {
|
6 | 12 | const actual = await vi.importActual('cross-fetch');
|
@@ -492,4 +498,117 @@ describe('utils', () => {
|
492 | 498 | });
|
493 | 499 | });
|
494 | 500 | });
|
| 501 | + |
| 502 | + describe('getFormattedAddress', () => { |
| 503 | + test('returns a checksummed EVM address', () => { |
| 504 | + const address = '0x91fd2c8d24767db4ece7069aa27832ffaf8590f3'; |
| 505 | + expect(getFormattedAddress(address, 'evm')).toEqual( |
| 506 | + '0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3' |
| 507 | + ); |
| 508 | + }); |
| 509 | + |
| 510 | + test('returns a padded and lowercased starknet address', () => { |
| 511 | + const address = |
| 512 | + '0x2a0a8f3b6097e7a6bd7649deb30715323072a159c0e6b71b689bd245c146cc0'; |
| 513 | + expect(getFormattedAddress(address, 'starknet')).toEqual( |
| 514 | + '0x02a0a8f3b6097e7a6bd7649deb30715323072a159c0e6b71b689bd245c146cc0' |
| 515 | + ); |
| 516 | + }); |
| 517 | + |
| 518 | + test('returns an EVM address as starknet address', () => { |
| 519 | + const address = '0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3'; |
| 520 | + expect(getFormattedAddress(address, 'starknet')).toEqual( |
| 521 | + '0x00000000000000000000000091fd2c8d24767db4ece7069aa27832ffaf8590f3' |
| 522 | + ); |
| 523 | + }); |
| 524 | + |
| 525 | + test('throws an error when the address is not a starknet address', () => { |
| 526 | + const address = 'hello'; |
| 527 | + expect(() => getFormattedAddress(address, 'starknet')).toThrow(); |
| 528 | + }); |
| 529 | + |
| 530 | + test('throws an error when the address is not an EVM address', () => { |
| 531 | + const address = |
| 532 | + '0x2a0a8f3b6097e7a6bd7649deb30715323072a159c0e6b71b689bd245c146cc0'; |
| 533 | + expect(() => getFormattedAddress(address, 'evm')).toThrow(); |
| 534 | + }); |
| 535 | + }); |
| 536 | + |
| 537 | + describe('address validation', () => { |
| 538 | + const evmSchema = { |
| 539 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 540 | + $ref: '#/definitions/Envelope', |
| 541 | + definitions: { |
| 542 | + Envelope: { |
| 543 | + type: 'object', |
| 544 | + properties: { |
| 545 | + address: { |
| 546 | + type: 'string', |
| 547 | + format: 'address' |
| 548 | + } |
| 549 | + } |
| 550 | + } |
| 551 | + } |
| 552 | + }; |
| 553 | + |
| 554 | + const starknetSchema = { |
| 555 | + $schema: 'http://json-schema.org/draft-07/schema#', |
| 556 | + $ref: '#/definitions/Envelope', |
| 557 | + definitions: { |
| 558 | + Envelope: { |
| 559 | + type: 'object', |
| 560 | + properties: { |
| 561 | + address: { |
| 562 | + type: 'string', |
| 563 | + format: 'starknetAddress' |
| 564 | + } |
| 565 | + } |
| 566 | + } |
| 567 | + } |
| 568 | + }; |
| 569 | + |
| 570 | + test('should return true on checksummed EVM address', async () => { |
| 571 | + const result = validateSchema(evmSchema, { |
| 572 | + address: '0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3' |
| 573 | + }); |
| 574 | + expect(result).toBe(true); |
| 575 | + }); |
| 576 | + |
| 577 | + test('should return an error on lowercase EVM address', async () => { |
| 578 | + const result = validateSchema(evmSchema, { |
| 579 | + address: '0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3'.toLowerCase() |
| 580 | + }); |
| 581 | + expect(result).not.toBe(true); |
| 582 | + }); |
| 583 | + |
| 584 | + test('should return an error on empty address', async () => { |
| 585 | + const result = validateSchema(evmSchema, { |
| 586 | + address: '' |
| 587 | + }); |
| 588 | + expect(result).not.toBe(true); |
| 589 | + }); |
| 590 | + |
| 591 | + test('should return true on lowercase padded starknet address', async () => { |
| 592 | + const result = validateSchema(starknetSchema, { |
| 593 | + address: |
| 594 | + '0x02a0a8f3b6097e7a6bd7649deb30715323072a159c0e6b71b689bd245c146cc0' |
| 595 | + }); |
| 596 | + expect(result).toBe(true); |
| 597 | + }); |
| 598 | + |
| 599 | + test('should return an error on non-padded starknet address', async () => { |
| 600 | + const result = validateSchema(starknetSchema, { |
| 601 | + address: |
| 602 | + '0x2a0a8f3b6097e7a6bd7649deb30715323072a159c0e6b71b689bd245c146cc0' |
| 603 | + }); |
| 604 | + expect(result).not.toBe(true); |
| 605 | + }); |
| 606 | + |
| 607 | + test('should return an error on empty starknet address', async () => { |
| 608 | + const result = validateSchema(starknetSchema, { |
| 609 | + address: '' |
| 610 | + }); |
| 611 | + expect(result).not.toBe(true); |
| 612 | + }); |
| 613 | + }); |
495 | 614 | });
|
0 commit comments