fix(instanceWallet): Refactored to use web 3 infra

This commit is contained in:
Chris Troutner
2022-02-23 09:21:23 -08:00
parent aa0a8a862c
commit f6646f8658
5 changed files with 84 additions and 32 deletions
+33 -25
View File
@@ -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 {