|
| 1 | +import 'should'; |
| 2 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 3 | +import { TestBitGo, TestBitGoAPI } from '@bitgo/sdk-test'; |
| 4 | +import { EvmCoin } from '../../src/evmCoin'; |
| 5 | +import { coins } from '@bitgo/statics'; |
| 6 | + |
| 7 | +describe('EvmCoin', function () { |
| 8 | + let bitgo: TestBitGoAPI; |
| 9 | + let evmCoin: EvmCoin; |
| 10 | + |
| 11 | + before(function () { |
| 12 | + bitgo = TestBitGo.decorate(BitGoAPI, { env: 'test' }); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('isValidAddress', function () { |
| 16 | + beforeEach(function () { |
| 17 | + // Create EvmCoin instance for testing |
| 18 | + const staticsCoin = coins.get('hbarevm'); |
| 19 | + evmCoin = new (EvmCoin as any)(bitgo, staticsCoin); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should validate standard Ethereum addresses (backward compatible)', function () { |
| 23 | + const validAddresses = [ |
| 24 | + '0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed', |
| 25 | + '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', |
| 26 | + '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', |
| 27 | + '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb', |
| 28 | + '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb1', |
| 29 | + ]; |
| 30 | + |
| 31 | + validAddresses.forEach((address) => { |
| 32 | + evmCoin.isValidAddress(address).should.be.true(`${address} should be valid`); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should validate addresses without 0x prefix (backward compatible)', function () { |
| 37 | + const validAddresses = ['5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed', 'fB6916095ca1df60bB79Ce92cE3Ea74c37c5d359']; |
| 38 | + |
| 39 | + validAddresses.forEach((address) => { |
| 40 | + evmCoin.isValidAddress(address).should.be.true(`${address} should be valid`); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should reject invalid Ethereum addresses (backward compatible)', function () { |
| 45 | + const invalidAddresses = [ |
| 46 | + '0xInvalidAddress', |
| 47 | + '0x123', |
| 48 | + 'not-an-address', |
| 49 | + '', |
| 50 | + '0x', |
| 51 | + '0xzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', |
| 52 | + ]; |
| 53 | + |
| 54 | + invalidAddresses.forEach((address) => { |
| 55 | + evmCoin.isValidAddress(address).should.be.false(`${address} should be invalid`); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should work with first parameter only (backward compatible)', function () { |
| 60 | + const address = '0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed'; |
| 61 | + evmCoin.isValidAddress(address).should.be.true(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('with optional isAlternateAddress parameter for HBAREVM', function () { |
| 65 | + it('should validate Hedera account IDs when isAlternateAddress is true', function () { |
| 66 | + const validHederaAccountIds = ['0.0.123456', '0.0.1234567890', '0.0.1']; |
| 67 | + |
| 68 | + validHederaAccountIds.forEach((accountId) => { |
| 69 | + evmCoin.isValidAddress(accountId, true).should.be.true(`${accountId} should be valid`); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should reject invalid Hedera account IDs when isAlternateAddress is true', function () { |
| 74 | + const invalidHederaAccountIds = ['0.0', '0.0.abc', '0.0.-123', 'not-an-account-id']; |
| 75 | + |
| 76 | + invalidHederaAccountIds.forEach((accountId) => { |
| 77 | + evmCoin.isValidAddress(accountId, true).should.be.false(`${accountId} should be invalid`); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should still validate Ethereum addresses when isAlternateAddress is false', function () { |
| 82 | + const ethAddress = '0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed'; |
| 83 | + evmCoin.isValidAddress(ethAddress, false).should.be.true(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should validate Ethereum addresses when isAlternateAddress is undefined (default)', function () { |
| 87 | + const ethAddress = '0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed'; |
| 88 | + evmCoin.isValidAddress(ethAddress, undefined).should.be.true(); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('for non-HBAREVM coins', function () { |
| 93 | + beforeEach(function () { |
| 94 | + // Create a non-HBAREVM coin instance |
| 95 | + const staticsCoin = coins.get('polygon'); |
| 96 | + evmCoin = new (EvmCoin as any)(bitgo, staticsCoin); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should validate standard Ethereum addresses regardless of isAlternateAddress flag', function () { |
| 100 | + const ethAddress = '0x5aAeb6053f3E94C9b9A09f33669435E7Ef1BeAed'; |
| 101 | + evmCoin.isValidAddress(ethAddress).should.be.true(); |
| 102 | + evmCoin.isValidAddress(ethAddress, false).should.be.true(); |
| 103 | + evmCoin.isValidAddress(ethAddress, true).should.be.true(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should not validate Hedera account IDs even with isAlternateAddress=true', function () { |
| 107 | + const hederaAccountId = '0.0.123456'; |
| 108 | + evmCoin.isValidAddress(hederaAccountId, true).should.be.false(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments