mirror of
https://github.com/Permissionless-Software-Foundation/bch-dex.git
synced 2026-09-22 17:12:03 -07:00
fix(instanceWallet): Refactored to use web 3 infra
This commit is contained in:
Vendored
+6
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
+33
-25
@@ -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 {
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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 () => {
|
||||
|
||||
Reference in New Issue
Block a user