From ec4445e591704f1521a9d60e2d4e1d07d5f78102 Mon Sep 17 00:00:00 2001 From: emjshrx Date: Sat, 10 Feb 2024 13:32:59 +0530 Subject: [PATCH] test: add tests for new methods --- test/wallet-db.spec.ts | 10 ++++++++++ test/wallet.spec.ts | 44 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/test/wallet-db.spec.ts b/test/wallet-db.spec.ts index 64c098d..b2b15d2 100644 --- a/test/wallet-db.spec.ts +++ b/test/wallet-db.spec.ts @@ -24,6 +24,16 @@ describe('Wallet DB', () => { expect(await walletDB.getVersion()).toBe(1); }); + it('should set and retrieve encryptedPrivateKey and encryptedChainCode', async () => { + const samplePrivateKey = 'samplePrivateKey'; + const sampleChainCode = 'sampleChainCode'; + await walletDB.setMasterKey(samplePrivateKey, sampleChainCode); + const { encryptedPrivateKey, encryptedChainCode } = + await walletDB.getMasterKey(); + expect(encryptedPrivateKey).toStrictEqual(samplePrivateKey); + expect(encryptedChainCode).toStrictEqual(sampleChainCode); + }); + afterAll(async () => { await walletDB.close(); fs.rmSync('./test/wallet-db', { recursive: true, force: true }); diff --git a/test/wallet.spec.ts b/test/wallet.spec.ts index 62f333a..baf6f46 100644 --- a/test/wallet.spec.ts +++ b/test/wallet.spec.ts @@ -32,9 +32,10 @@ describe('Wallet', () => { }); it('should initialise the wallet', async () => { - await wallet.init( - 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', - ); + await wallet.init({ + mnemonic: + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', + }); }); it('should derive first receive address', async () => { @@ -132,3 +133,40 @@ describe('Wallet', () => { fs.rmSync('./test/wallet', { recursive: true, force: true }); }); }); + +describe('Wallet - DB', () => { + let wallet: Wallet; + + beforeAll(async () => { + const walletDB = new WalletDB({ + location: './test/wallet', + }); + + wallet = new Wallet({ + db: walletDB, + networkClient: new EsploraClient({ + protocol: 'http', + host: '127.0.0.1:8094', + network: 'regtest', + }), + }); + await wallet.init({ + mnemonic: + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', + password: 'samplePassword', + }); + }); + + it('should write masterKey with new password', async () => { + await wallet.setPassword('newPassword'); + await wallet.close(); + await wallet.init({ password: 'newPassword' }); + const address = await wallet.deriveReceiveAddress(); + expect(address).toBe('bcrt1qcr8te4kr609gcawutmrza0j4xv80jy8zeqchgx'); + }); + + afterAll(async () => { + await wallet.close(); + fs.rmSync('./test/wallet', { recursive: true, force: true }); + }); +});