|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import 'should'; |
| 4 | +import { Wallet } from '../../../../src/bitgo/wallet/wallet'; |
| 5 | + |
| 6 | +describe('Wallet - EVM Keyring Address Creation', function () { |
| 7 | + let wallet: Wallet; |
| 8 | + let mockBitGo: any; |
| 9 | + let mockBaseCoin: any; |
| 10 | + let mockWalletData: any; |
| 11 | + |
| 12 | + beforeEach(function () { |
| 13 | + mockBitGo = { |
| 14 | + post: sinon.stub(), |
| 15 | + setRequestTracer: sinon.stub(), |
| 16 | + }; |
| 17 | + |
| 18 | + mockBaseCoin = { |
| 19 | + isEVM: sinon.stub(), |
| 20 | + supportsTss: sinon.stub().returns(true), |
| 21 | + getFamily: sinon.stub().returns('eth'), |
| 22 | + isValidAddress: sinon.stub(), |
| 23 | + keychains: sinon.stub(), |
| 24 | + url: sinon.stub().returns('/test/wallet/address'), |
| 25 | + }; |
| 26 | + |
| 27 | + mockWalletData = { |
| 28 | + id: 'test-wallet-id', |
| 29 | + keys: ['user-key', 'backup-key', 'bitgo-key'], |
| 30 | + }; |
| 31 | + |
| 32 | + wallet = new Wallet(mockBitGo, mockBaseCoin, mockWalletData); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(function () { |
| 36 | + sinon.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('createAddress with EVM keyring parameters', function () { |
| 40 | + beforeEach(function () { |
| 41 | + mockBaseCoin.isEVM.returns(true); |
| 42 | + mockBaseCoin.isValidAddress.returns(true); |
| 43 | + mockBaseCoin.keychains.returns({ |
| 44 | + get: sinon.stub().resolves({ id: 'keychain-id', pub: 'public-key' }), |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should create address with evmKeyRingReferenceAddress', async function () { |
| 49 | + const mockAddressResponse = { |
| 50 | + id: '507f1f77bcf86cd799439012', |
| 51 | + address: '0x1234567890123456789012345678901234567890', |
| 52 | + }; |
| 53 | + |
| 54 | + mockBitGo.post.returns({ |
| 55 | + send: sinon.stub().returns({ |
| 56 | + result: sinon.stub().resolves(mockAddressResponse), |
| 57 | + }), |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await wallet.createAddress({ |
| 61 | + chain: 0, |
| 62 | + label: 'Test EVM Address', |
| 63 | + |
| 64 | + evmKeyRingReferenceAddress: '0x742d35Cc6634C0532925a3b8D404fddF4f780EAD', |
| 65 | + }); |
| 66 | + |
| 67 | + result.should.have.property('id', '507f1f77bcf86cd799439012'); |
| 68 | + result.should.have.property('address', '0x1234567890123456789012345678901234567890'); |
| 69 | + mockBitGo.post.should.have.been.calledOnce; |
| 70 | + }); |
| 71 | + |
| 72 | + it('should throw error if evmKeyRingReferenceAddress is not a string', async function () { |
| 73 | + try { |
| 74 | + await wallet.createAddress({ |
| 75 | + chain: 0, |
| 76 | + label: 'Test Address', |
| 77 | + evmKeyRingReferenceAddress: 123 as any, |
| 78 | + }); |
| 79 | + assert.fail('Should have thrown error'); |
| 80 | + } catch (error) { |
| 81 | + error.message.should.equal('evmKeyRingReferenceAddress has to be a string'); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw error if evmKeyRingReferenceAddress is not a valid address', async function () { |
| 86 | + mockBaseCoin.isValidAddress.returns(false); |
| 87 | + |
| 88 | + try { |
| 89 | + await wallet.createAddress({ |
| 90 | + chain: 0, |
| 91 | + label: 'Test Address', |
| 92 | + evmKeyRingReferenceAddress: 'invalid-address', |
| 93 | + }); |
| 94 | + assert.fail('Should have thrown error'); |
| 95 | + } catch (error) { |
| 96 | + error.message.should.equal('evmKeyRingReferenceAddress must be a valid address'); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it('should throw error for non-EVM chains with evmKeyRingReferenceAddress', async function () { |
| 101 | + mockBaseCoin.isEVM.returns(false); |
| 102 | + |
| 103 | + try { |
| 104 | + await wallet.createAddress({ |
| 105 | + chain: 0, |
| 106 | + label: 'Test Address', |
| 107 | + evmKeyRingReferenceAddress: '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2', |
| 108 | + }); |
| 109 | + assert.fail('Should have thrown error'); |
| 110 | + } catch (error) { |
| 111 | + error.message.should.equal('evmKeyRingReferenceAddress is only supported for EVM chains'); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('should create address without reference parameters for regular addresses', async function () { |
| 116 | + const mockAddressResponse = { |
| 117 | + id: 'regular-address-id', |
| 118 | + address: '0x9876543210987654321098765432109876543210', |
| 119 | + }; |
| 120 | + |
| 121 | + mockBitGo.post.returns({ |
| 122 | + send: sinon.stub().returns({ |
| 123 | + result: sinon.stub().resolves(mockAddressResponse), |
| 124 | + }), |
| 125 | + }); |
| 126 | + |
| 127 | + const result = await wallet.createAddress({ |
| 128 | + chain: 0, |
| 129 | + label: 'Regular Address', |
| 130 | + }); |
| 131 | + |
| 132 | + result.should.have.property('id', 'regular-address-id'); |
| 133 | + result.should.have.property('address', '0x9876543210987654321098765432109876543210'); |
| 134 | + mockBitGo.post.should.have.been.calledOnce; |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('Non-EVM chains', function () { |
| 139 | + beforeEach(function () { |
| 140 | + mockBaseCoin.isEVM.returns(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should create regular addresses for non-EVM chains', async function () { |
| 144 | + const mockAddressResponse = { |
| 145 | + id: 'btc-address-id', |
| 146 | + address: '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', |
| 147 | + }; |
| 148 | + |
| 149 | + mockBitGo.post.returns({ |
| 150 | + send: sinon.stub().returns({ |
| 151 | + result: sinon.stub().resolves(mockAddressResponse), |
| 152 | + }), |
| 153 | + }); |
| 154 | + |
| 155 | + mockBaseCoin.keychains.returns({ |
| 156 | + get: sinon.stub().resolves({ id: 'keychain-id', pub: 'public-key' }), |
| 157 | + }); |
| 158 | + |
| 159 | + const result = await wallet.createAddress({ |
| 160 | + chain: 0, |
| 161 | + label: 'BTC Address', |
| 162 | + }); |
| 163 | + |
| 164 | + result.should.have.property('id', 'btc-address-id'); |
| 165 | + result.should.have.property('address', '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'); |
| 166 | + mockBitGo.post.should.have.been.calledOnce; |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments