|
| 1 | +/*! |
| 2 | + * Copyright (c) 2024 Digital Bazaar, Inc. All rights reserved. |
| 3 | + */ |
| 4 | +import { |
| 5 | + DEFAULT_DERIVATION_PATH, |
| 6 | + generateMnemonic, |
| 7 | + walletFromMnemonic, |
| 8 | +} from '../../lib/ethereum/index.js'; |
| 9 | + |
| 10 | +describe('Ethereum Module', function() { |
| 11 | + describe('generateMnemonic()', function() { |
| 12 | + it('should generate a 12-word mnemonic by default', function() { |
| 13 | + const mnemonic = generateMnemonic(); |
| 14 | + |
| 15 | + should.exist(mnemonic); |
| 16 | + mnemonic.should.be.a('string'); |
| 17 | + |
| 18 | + const words = mnemonic.trim().split(/\s+/); |
| 19 | + words.length.should.equal(12); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should generate a 24-word mnemonic with strength=256', function() { |
| 23 | + const mnemonic = generateMnemonic({strength: 256}); |
| 24 | + |
| 25 | + should.exist(mnemonic); |
| 26 | + const words = mnemonic.trim().split(/\s+/); |
| 27 | + words.length.should.equal(24); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should generate unique mnemonics each time', function() { |
| 31 | + const mnemonic1 = generateMnemonic(); |
| 32 | + const mnemonic2 = generateMnemonic(); |
| 33 | + |
| 34 | + mnemonic1.should.not.equal(mnemonic2); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('walletFromMnemonic()', function() { |
| 39 | + const testMnemonic = |
| 40 | + 'abandon abandon abandon abandon abandon abandon ' + |
| 41 | + 'abandon abandon abandon abandon abandon about'; |
| 42 | + |
| 43 | + it('should derive a wallet from a valid mnemonic', function() { |
| 44 | + const wallet = walletFromMnemonic({mnemonic: testMnemonic}); |
| 45 | + |
| 46 | + should.exist(wallet); |
| 47 | + should.exist(wallet.address); |
| 48 | + should.exist(wallet.privateKey); |
| 49 | + should.exist(wallet.publicKey); |
| 50 | + should.exist(wallet.derivationPath); |
| 51 | + should.exist(wallet.mnemonic); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return a valid Ethereum address (0x + 40 hex chars)', |
| 55 | + function() { |
| 56 | + const wallet = walletFromMnemonic({mnemonic: testMnemonic}); |
| 57 | + wallet.address.should.match(/^0x[a-fA-F0-9]{40}$/); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return a valid private key (0x + 64 hex chars)', function() { |
| 61 | + const wallet = walletFromMnemonic({mnemonic: testMnemonic}); |
| 62 | + wallet.privateKey.should.match(/^0x[a-fA-F0-9]{64}$/); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should use default derivation path m/49\'/60\'/0\'/0/0', function() { |
| 66 | + const wallet = walletFromMnemonic({mnemonic: testMnemonic}); |
| 67 | + |
| 68 | + wallet.derivationPath.should.equal(DEFAULT_DERIVATION_PATH); |
| 69 | + wallet.derivationPath.should.equal('m/49\'/60\'/0\'/0/0'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should derive the same wallet from the same mnemonic', function() { |
| 73 | + const wallet1 = walletFromMnemonic({mnemonic: testMnemonic}); |
| 74 | + const wallet2 = walletFromMnemonic({mnemonic: testMnemonic}); |
| 75 | + |
| 76 | + wallet1.address.should.equal(wallet2.address); |
| 77 | + wallet1.privateKey.should.equal(wallet2.privateKey); |
| 78 | + wallet1.publicKey.should.equal(wallet2.publicKey); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should derive different wallets from different mnemonics', function() { |
| 82 | + const mnemonic1 = generateMnemonic(); |
| 83 | + const mnemonic2 = generateMnemonic(); |
| 84 | + |
| 85 | + const wallet1 = walletFromMnemonic({mnemonic: mnemonic1}); |
| 86 | + const wallet2 = walletFromMnemonic({mnemonic: mnemonic2}); |
| 87 | + |
| 88 | + wallet1.address.should.not.equal(wallet2.address); |
| 89 | + wallet1.privateKey.should.not.equal(wallet2.privateKey); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should support custom derivation path', function() { |
| 93 | + const customPath = 'm/44\'/60\'/0\'/0/0'; |
| 94 | + const wallet = walletFromMnemonic({ |
| 95 | + mnemonic: testMnemonic, |
| 96 | + path: customPath |
| 97 | + }); |
| 98 | + |
| 99 | + wallet.derivationPath.should.equal(customPath); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should derive different addresses for different paths', function() { |
| 103 | + const path1 = 'm/49\'/60\'/0\'/0/0'; |
| 104 | + const path2 = 'm/49\'/60\'/0\'/0/1'; |
| 105 | + |
| 106 | + const wallet1 = walletFromMnemonic({mnemonic: testMnemonic, path: path1}); |
| 107 | + const wallet2 = walletFromMnemonic({mnemonic: testMnemonic, path: path2}); |
| 108 | + |
| 109 | + wallet1.address.should.not.equal(wallet2.address); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should throw error for invalid mnemonic', function() { |
| 113 | + let error; |
| 114 | + try { |
| 115 | + walletFromMnemonic({mnemonic: 'invalid mnemonic phrase'}); |
| 116 | + } catch(e) { |
| 117 | + error = e; |
| 118 | + } |
| 119 | + |
| 120 | + should.exist(error); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should include the mnemonic in the returned wallet', function() { |
| 124 | + const wallet = walletFromMnemonic({mnemonic: testMnemonic}); |
| 125 | + wallet.mnemonic.should.equal(testMnemonic); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('Integration: generateMnemonic + walletFromMnemonic', function() { |
| 130 | + it('should create a complete wallet from generated mnemonic', function() { |
| 131 | + // Generate mnemonic |
| 132 | + const mnemonic = generateMnemonic(); |
| 133 | + |
| 134 | + // Derive wallet |
| 135 | + const wallet = walletFromMnemonic({mnemonic}); |
| 136 | + |
| 137 | + // Verify all fields exist |
| 138 | + should.exist(wallet.address); |
| 139 | + should.exist(wallet.privateKey); |
| 140 | + should.exist(wallet.publicKey); |
| 141 | + wallet.derivationPath.should.equal('m/49\'/60\'/0\'/0/0'); |
| 142 | + wallet.mnemonic.should.equal(mnemonic); |
| 143 | + |
| 144 | + // Verify formats |
| 145 | + wallet.address.should.match(/^0x[a-fA-F0-9]{40}$/); |
| 146 | + wallet.privateKey.should.match(/^0x[a-fA-F0-9]{64}$/); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should allow wallet recovery from mnemonic', function() { |
| 150 | + // Simulate: User creates wallet |
| 151 | + const mnemonic = generateMnemonic(); |
| 152 | + const originalWallet = walletFromMnemonic({mnemonic}); |
| 153 | + |
| 154 | + // Simulate: User loses device, recovers with mnemonic |
| 155 | + const recoveredWallet = walletFromMnemonic({mnemonic}); |
| 156 | + |
| 157 | + // Same mnemonic = same wallet |
| 158 | + recoveredWallet.address.should.equal(originalWallet.address); |
| 159 | + recoveredWallet.privateKey.should.equal(originalWallet.privateKey); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments