refactor(instancing): Refactored instancing of minimal-slp-wallet

This commit is contained in:
Daniel Gonzalez
2021-12-21 11:46:51 -04:00
parent 977c86a375
commit 2c413343c9
6 changed files with 130 additions and 41 deletions
+14 -10
View File
@@ -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 (
<Row>
<Col sm={12}>
@@ -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()
}
}
@@ -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)
+39 -12
View File
@@ -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 = ''
+38 -12
View File
@@ -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({
+6 -1
View File
@@ -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}
/>
<ImportWallet
@@ -42,6 +45,7 @@ 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
+30 -6
View File
@@ -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)
}