From 2c413343c97ac290a40ce24dee75894004325394 Mon Sep 17 00:00:00 2001 From: Daniel Gonzalez Date: Tue, 21 Dec 2021 11:46:51 -0400 Subject: [PATCH] refactor(instancing): Refactored instancing of minimal-slp-wallet --- src/components/admin-lte/configure/jwt.js | 24 +++++---- src/components/admin-lte/configure/servers.js | 3 ++ src/components/admin-lte/wallet/create.js | 51 ++++++++++++++----- src/components/admin-lte/wallet/import.js | 50 +++++++++++++----- src/components/admin-lte/wallet/index.js | 7 ++- src/redux/createStore.js | 36 ++++++++++--- 6 files changed, 130 insertions(+), 41 deletions(-) diff --git a/src/components/admin-lte/configure/jwt.js b/src/components/admin-lte/configure/jwt.js index 0776ebf..262a70c 100644 --- a/src/components/admin-lte/configure/jwt.js +++ b/src/components/admin-lte/configure/jwt.js @@ -13,7 +13,7 @@ const { Text } = Inputs let _this class JsonWebTokens extends React.Component { - constructor (props) { + constructor(props) { super(props) _this = this @@ -26,7 +26,7 @@ class JsonWebTokens extends React.Component { _this.BchWallet = BchWallet } - render () { + render() { return ( @@ -70,7 +70,7 @@ class JsonWebTokens extends React.Component { ) } - setJwt () { + setJwt() { const { JWT } = _this.props.walletInfo if (JWT) { const jwtElem = document.getElementById('jwt') @@ -78,32 +78,36 @@ class JsonWebTokens extends React.Component { } } - handleUpdate (event) { + handleUpdate(event) { const value = event.target.value _this.setState({ [event.target.name]: value }) } - async handleUpdateJWT () { + async handleUpdateJWT() { try { const { mnemonic, selectedServer } = _this.props.walletInfo + const _interface = _this.props.walletInfo.interface + + const apiToken = _this.state.JWT // Update instance with JWT if (mnemonic) { - const bchjsOptions = { apiToken: apiToken } + let bchjsOptions = { apiToken: apiToken } if (selectedServer) { bchjsOptions.restURL = selectedServer } - // console.log('bchjs options : ', bchjsOptions) + bchjsOptions.interface = _interface + //console.log('bchjs options : ', bchjsOptions) const bchWalletLib = new _this.BchWallet(mnemonic, bchjsOptions) // Update bchjs instances of minimal-slp-wallet libraries bchWalletLib.tokens.sendBch.bchjs = new bchWalletLib.BCHJS(bchjsOptions) bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions) - + _this.props.setBchWallet(bchWalletLib) } @@ -120,7 +124,7 @@ class JsonWebTokens extends React.Component { } // Reset form and component state - resetValues () { + resetValues() { _this.setState({ JWT: '', errMsg: '' @@ -129,7 +133,7 @@ class JsonWebTokens extends React.Component { jwtElem.value = '' } - componentDidMount () { + componentDidMount() { _this.setJwt() } } diff --git a/src/components/admin-lte/configure/servers.js b/src/components/admin-lte/configure/servers.js index 18850ad..172fcd2 100644 --- a/src/components/admin-lte/configure/servers.js +++ b/src/components/admin-lte/configure/servers.js @@ -215,6 +215,7 @@ class Servers extends React.Component { const { mnemonic, JWT } = _this.props.walletInfo const apiToken = JWT const restURL = _this.state.selectedServer + const _interface = _this.props.walletInfo.interface // Update instance with the selected url if (mnemonic) { @@ -224,6 +225,8 @@ class Servers extends React.Component { } bchjsOptions.restURL = restURL + bchjsOptions.interface = _interface + //console.log('bchjs options : ', bchjsOptions) const bchWalletLib = new _this.BchWallet(mnemonic, bchjsOptions) diff --git a/src/components/admin-lte/wallet/create.js b/src/components/admin-lte/wallet/create.js index 1b7f9f7..1c1eb06 100644 --- a/src/components/admin-lte/wallet/create.js +++ b/src/components/admin-lte/wallet/create.js @@ -79,18 +79,8 @@ class NewWallet extends React.Component { _this.setState({ inFetch: true }) - const apiToken = currentWallet.JWT - const restURL = currentWallet.selectedServer - const bchjsOptions = {} - - if (apiToken || restURL) { - if (apiToken) { - bchjsOptions.apiToken = apiToken - } - if (restURL) { - bchjsOptions.restURL = restURL - } - } + + const bchjsOptions = _this.getBchjsOptions() const bchWalletLib = new _this.BchWallet(null, bchjsOptions) @@ -102,6 +92,7 @@ class NewWallet extends React.Component { const walletInfo = bchWalletLib.walletInfo walletInfo.from = 'created' + walletInfo.interface = bchjsOptions.interface Object.assign(currentWallet, walletInfo) @@ -132,7 +123,43 @@ class NewWallet extends React.Component { _this.handleError(error) } } + getBchjsOptions (){ + try { + const currentWallet = _this.props.walletInfo + // force interface from props + if(_this.props.interface){ + currentWallet.interface = _this.props.interface + } + const _interface = currentWallet.interface || 'consumer-api' + + const jwtToken = currentWallet.JWT + const restURL = currentWallet.selectedServer + const bchjsOptions = {} + + if(_interface === 'consumer-api'){ + bchjsOptions.interface = _interface + return bchjsOptions + } + + if (jwtToken) { + bchjsOptions.apiToken = jwtToken + + } + + if (restURL) { + bchjsOptions.restURL = restURL + } + + if (_interface === 'rest-api') { + bchjsOptions.interface = _interface + } + + return bchjsOptions + } catch (error) { + console.warn(error) + } + } handleError (error) { // console.error(error) let errMsg = '' diff --git a/src/components/admin-lte/wallet/import.js b/src/components/admin-lte/wallet/import.js index d06bfdd..9000090 100644 --- a/src/components/admin-lte/wallet/import.js +++ b/src/components/admin-lte/wallet/import.js @@ -113,19 +113,8 @@ class ImportWallet extends React.Component { inFetch: true }) - const apiToken = currentWallet.JWT - const restURL = currentWallet.selectedServer + const bchjsOptions = _this.getBchjsOptions() - const bchjsOptions = {} - - if (apiToken || restURL) { - if (apiToken) { - bchjsOptions.apiToken = apiToken - } - if (restURL) { - bchjsOptions.restURL = restURL - } - } const bchWalletLib = new _this.BchWallet( _this.state.mnemonic, @@ -140,6 +129,7 @@ class ImportWallet extends React.Component { const walletInfo = bchWalletLib.walletInfo walletInfo.from = 'imported' + walletInfo.interface = bchjsOptions.interface Object.assign(currentWallet, walletInfo) @@ -172,7 +162,43 @@ class ImportWallet extends React.Component { _this.handleError(error) } } + getBchjsOptions (){ + try { + const currentWallet = _this.props.walletInfo + // force interface from props + if(_this.props.interface){ + currentWallet.interface = _this.props.interface + } + const _interface = currentWallet.interface || 'consumer-api' + + const jwtToken = currentWallet.JWT + const restURL = currentWallet.selectedServer + const bchjsOptions = {} + + if(_interface === 'consumer-api'){ + bchjsOptions.interface = _interface + return bchjsOptions + } + + if (jwtToken) { + bchjsOptions.apiToken = jwtToken + + } + + if (restURL) { + bchjsOptions.restURL = restURL + } + + if (_interface === 'rest-api') { + bchjsOptions.interface = _interface + } + + return bchjsOptions + } catch (error) { + console.warn(error) + } + } // Reset form and component state resetValues () { _this.setState({ diff --git a/src/components/admin-lte/wallet/index.js b/src/components/admin-lte/wallet/index.js index 1e138e0..38bddf4 100644 --- a/src/components/admin-lte/wallet/index.js +++ b/src/components/admin-lte/wallet/index.js @@ -7,6 +7,7 @@ import ImportWallet from './import' import NewWallet from './create' import InfoWallets from './info' import WalletInfo from './wallet-info' +import { interfaces } from 'mocha' let _this class Wallet extends React.Component { @@ -35,6 +36,8 @@ class Wallet extends React.Component { setWalletInfo={_this.props.setWalletInfo} setBchWallet={_this.props.setBchWallet} walletInfo={_this.props.walletInfo} + interface={_this.props.interface} + /> )} @@ -56,7 +60,8 @@ Wallet.propTypes = { setWalletInfo: PropTypes.func.isRequired, walletInfo: PropTypes.object.isRequired, updateBalance: PropTypes.func.isRequired, - setBchWallet: PropTypes.func.isRequired + setBchWallet: PropTypes.func.isRequired, + interface :PropTypes.string } export default Wallet diff --git a/src/redux/createStore.js b/src/redux/createStore.js index aab00e0..eaaf2dd 100644 --- a/src/redux/createStore.js +++ b/src/redux/createStore.js @@ -76,29 +76,53 @@ const instanceWallet = () => { try { if (!localStorageInfo.mnemonic) return null + const bchjsOptions = getBchjsOptions() + + const bchWalletLib = new BchWallet(localStorageInfo.mnemonic, bchjsOptions) + + // Update bchjs instances of minimal-slp-wallet libraries + bchWalletLib.tokens.sendBch.bchjs = new bchWalletLib.BCHJS(bchjsOptions) + bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions) + + return bchWalletLib + } catch (error) { + console.warn(error) + } +} + +const getBchjsOptions = ()=> { + try { + const _interface = localStorageInfo.interface || 'consumer-api' + const jwtToken = localStorageInfo.JWT const restURL = localStorageInfo.selectedServer const bchjsOptions = {} + + if(_interface === 'consumer-api'){ + bchjsOptions.interface = _interface + return bchjsOptions + } if (jwtToken) { bchjsOptions.apiToken = jwtToken + } + if (restURL) { bchjsOptions.restURL = restURL } + if (_interface === 'rest-api') { + bchjsOptions.interface = _interface + } + // Assign the tx fee based on environment variable const FEE = process.env.FEE ? process.env.FEE : 1 bchjsOptions.fee = FEE console.log(`Using ${bchjsOptions.fee} sats per byte for tx fees.`) - const bchWalletLib = new BchWallet(localStorageInfo.mnemonic, bchjsOptions) - // Update bchjs instances of minimal-slp-wallet libraries - bchWalletLib.tokens.sendBch.bchjs = new bchWalletLib.BCHJS(bchjsOptions) - bchWalletLib.tokens.utxos.bchjs = new bchWalletLib.BCHJS(bchjsOptions) - - return bchWalletLib + return bchjsOptions } catch (error) { console.warn(error) }