From f6646f86588ffe42e16d1dfefb99f5f1930fa36b Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 23 Feb 2022 09:21:23 -0800 Subject: [PATCH] fix(instanceWallet): Refactored to use web 3 infra --- config/env/common.js | 6 ++ package.json | 1 + src/adapters/wallet.js | 58 +++++++++++-------- .../adapters/wallet.adapter.integration.js | 41 +++++++++++++ test/unit/adapters/wallet.adapter.unit.js | 10 +--- 5 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 test/integration/adapters/wallet.adapter.integration.js diff --git a/config/env/common.js b/config/env/common.js index 9ca90ec..4aa8772 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -31,6 +31,12 @@ module.exports = { ? process.env.EMAILPASS : 'emailpassword', + // PSF Web 3 community infrastructure + useFullStackCash: process.env.USE_FULLSTACKCASH ? true : false, + consumerUrl: process.env.CONSUMER_URL + ? process.env.CONSUMER_URL + : 'https://free-bch.fullstack.cash', + // FullStack.cash account information, used for automatic JWT handling. getJwtAtStartup: process.env.GET_JWT_AT_STARTUP ? true : false, authServer: process.env.AUTHSERVER diff --git a/package.json b/package.json index 9e55617..9b6149d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "test:all": "export BCH_DEX=test && nyc --reporter=text mocha --exit --timeout 15000 --recursive test/unit test/e2e/automated/", "test:unit": "export BCH_DEX=test && mocha --exit --timeout 15000 --recursive test/unit/", "test:e2e:auto": "export BCH_DEX=test && mocha --exit --timeout 15000 test/e2e/automated/", + "test:integration": "export BCH_DEX=test && mocha --exit --timeout 30000 --recursive test/integration", "test:temp": "export BCH_DEX=test && mocha --exit --timeout 15000 -g '#rate-limit' test/unit/json-rpc/", "lint": "standard --env mocha --fix", "docs": "./node_modules/.bin/apidoc -i src/ -o docs", diff --git a/src/adapters/wallet.js b/src/adapters/wallet.js index e0d3e66..227be44 100644 --- a/src/adapters/wallet.js +++ b/src/adapters/wallet.js @@ -7,6 +7,7 @@ const BchWallet = require('minimal-slp-wallet/index') // Local libraries const JsonFiles = require('./json-files') +const config = require('../../config') const WALLET_FILE = `${__dirname.toString()}/../../wallet.json` const PROOF_OF_BURN_QTY = 0.01 @@ -19,9 +20,12 @@ class WalletAdapter { this.jsonFiles = new JsonFiles() this.WALLET_FILE = WALLET_FILE this.BchWallet = BchWallet + this.config = config } // Open the wallet file, or create one if the file doesn't exist. + // Does not instance the wallet. The output of this function is expected to + // be passed to instanceWallet(). async openWallet () { try { let walletData @@ -60,6 +64,35 @@ class WalletAdapter { } } + // Create an instance of minimal-slp-wallet. Use data in the wallet.json file, + // and pass the bch-js information to the minimal-slp-wallet library. + async instanceWallet (walletData) { + try { + // TODO: throw error if wallet data is not passed in. + + const advancedConfig = {} + if (this.config.useFullStackCash) { + advancedConfig.interface = 'rest-api' + advancedConfig.restURL = this.config.apiServer + advancedConfig.apiToken = this.config.apiToken + } else { + advancedConfig.interface = 'consumer-api' + advancedConfig.restURL = this.config.consumerUrl + } + + // Instantiate minimal-slp-wallet. + this.bchWallet = new this.BchWallet(walletData.mnemonic, advancedConfig) + + // Wait for wallet to initialize. + await this.bchWallet.walletInfoPromise + + return this.bchWallet + } catch (err) { + console.error('Error in instanceWallet()') + throw err + } + } + // Increments the 'nextAddress' property in the wallet file. This property // indicates the HD index that should be used to generate a key pair for // storing funds for Offers. @@ -124,31 +157,6 @@ class WalletAdapter { } } - // Create an instance of minimal-slp-wallet. Use data in the wallet.json file, - // and pass the bch-js information to the minimal-slp-wallet library. - async instanceWallet (walletData, bchjs) { - try { - // TODO: Throw error if bch-js is not passed in. - // TODO: throw error if wallet data is not passed in. - - const advancedConfig = { - restURL: bchjs.restURL, - apiToken: bchjs.apiToken - } - - // Instantiate minimal-slp-wallet. - this.bchWallet = new this.BchWallet(walletData.mnemonic, advancedConfig) - - // Wait for wallet to initialize. - await this.bchWallet.walletInfoPromise - - return true - } catch (err) { - console.error('Error in instanceWallet()') - throw err - } - } - // Generate a cryptographic signature, required to write to the P2WDB. async generateSignature (message) { try { diff --git a/test/integration/adapters/wallet.adapter.integration.js b/test/integration/adapters/wallet.adapter.integration.js new file mode 100644 index 0000000..260c60e --- /dev/null +++ b/test/integration/adapters/wallet.adapter.integration.js @@ -0,0 +1,41 @@ +/* + Integration tests for the wallet.js adapter library. +*/ + +// Public npm libraries. +const assert = require('chai').assert + +// Local libraries. +const WalletAdapter = require('../../../src/adapters/wallet') + +describe('#wallet', () => { + let uut + + beforeEach(() => { + uut = new WalletAdapter() + }) + + describe('#instanceWallet', () => { + it('should instance the wallet using the web 3 infra by default', async () => { + const walletData = await uut.openWallet() + // console.log('walletData: ', walletData) + + const walletInstance = await uut.instanceWallet(walletData) + // console.log('walletInstance: ', walletInstance) + + assert.equal(walletInstance.ar.interface, 'consumer-api') + }) + + it('should instance using web 2 FullStack.cash infra', async () => { + const walletData = await uut.openWallet() + + // Force usage of FullStack.cash + uut.config.useFullStackCash = true + + const walletInstance = await uut.instanceWallet(walletData) + // console.log('walletInstance: ', walletInstance) + + assert.equal(walletInstance.ar.interface, 'rest-api') + }) + }) +}) diff --git a/test/unit/adapters/wallet.adapter.unit.js b/test/unit/adapters/wallet.adapter.unit.js index 4459b7a..69815f0 100644 --- a/test/unit/adapters/wallet.adapter.unit.js +++ b/test/unit/adapters/wallet.adapter.unit.js @@ -102,19 +102,15 @@ describe('#wallet', () => { // Mock dependencies uut.BchWallet = MockBchWallet - const bchjs = { - restURL: 'dummyUrl', - apiToken: 'dummyToken' - } - // Ensure we open the test file, not the production wallet file. uut.WALLET_FILE = testWalletFile const walletData = await uut.openWallet() - const result = await uut.instanceWallet(walletData.mnemonic, bchjs) + const result = await uut.instanceWallet(walletData.mnemonic) + console.log('result: ', result) - assert.equal(result, true) + assert.property(result, 'walletInfoPromise') }) it('should catch and throw an error', async () => {