mirror of
https://github.com/fullstack-cash/fullstack-gatsby-theme-bch-wallet.git
synced 2026-09-21 16:52:06 -07:00
feat(wallet): Re-initialize minimal-slp-wallet
This commit is contained in:
Generated
+9587
-40788
File diff suppressed because it is too large
Load Diff
@@ -37,6 +37,7 @@
|
||||
"gatsby-transformer-sharp": "3.7.1",
|
||||
"https-browserify": "^1.0.0",
|
||||
"ipfs-coord": "^6.6.4",
|
||||
"jsonrpc-lite": "^2.2.0",
|
||||
"path-browserify": "^1.0.1",
|
||||
"process": "^0.11.10",
|
||||
"prop-types": "^15.7.2",
|
||||
|
||||
@@ -6,7 +6,9 @@ import IPFSTabs from './ipfs-tabs'
|
||||
import CommandRouter from '../lib/commands'
|
||||
|
||||
import './ipfs.css'
|
||||
// const BchWallet = typeof window !== 'undefined' ? window.SlpWallet : null
|
||||
|
||||
const WalletService = require('../lib/wallet-service')
|
||||
const BchWallet = typeof window !== 'undefined' ? window.SlpWallet : null
|
||||
|
||||
const { Checkbox } = Inputs
|
||||
|
||||
@@ -30,6 +32,9 @@ class IPFS extends React.Component {
|
||||
}
|
||||
}
|
||||
_this = this
|
||||
_this.BchWallet = BchWallet
|
||||
_this.WalletService = WalletService
|
||||
|
||||
this.initIPFSControl()
|
||||
}
|
||||
|
||||
@@ -290,6 +295,7 @@ class IPFS extends React.Component {
|
||||
}
|
||||
}
|
||||
|
||||
// pollForServices callback
|
||||
onAppStatus (msg) {
|
||||
try {
|
||||
let output
|
||||
@@ -301,12 +307,77 @@ class IPFS extends React.Component {
|
||||
_this.setState({
|
||||
appStatusOutput: output
|
||||
})
|
||||
|
||||
// Updates the bchWallet instance so it works
|
||||
// under the ipfs services
|
||||
_this.reInitialize()
|
||||
} catch (error) {
|
||||
console.warn(error)
|
||||
// Don't throw an error as this is a top-level handler.
|
||||
}
|
||||
}
|
||||
|
||||
// Updates the bchWallet instance so it works
|
||||
// under the ipfs services
|
||||
async reInitialize () {
|
||||
try {
|
||||
const currentWallet = _this.props.walletInfo
|
||||
const { JWT, selectedServer, mnemonic } = currentWallet
|
||||
|
||||
const apiToken = JWT
|
||||
const restURL = selectedServer
|
||||
|
||||
const bchjsOptions = {}
|
||||
|
||||
if (apiToken || restURL) {
|
||||
if (apiToken) {
|
||||
bchjsOptions.apiToken = apiToken
|
||||
}
|
||||
if (restURL) {
|
||||
bchjsOptions.restURL = restURL
|
||||
}
|
||||
}
|
||||
bchjsOptions.interface = 'json-rpc'
|
||||
|
||||
const walletService = new _this.WalletService({
|
||||
ipfsControl: this.ipfsControl
|
||||
})
|
||||
bchjsOptions.jsonRpcWalletService = walletService
|
||||
|
||||
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)
|
||||
|
||||
await bchWalletLib.walletInfoPromise // Wait for wallet to be created.
|
||||
|
||||
const walletInfo = bchWalletLib.walletInfo
|
||||
|
||||
Object.assign(currentWallet, walletInfo)
|
||||
|
||||
const myBalance = await bchWalletLib.getBalance()
|
||||
|
||||
const bchjs = bchWalletLib.bchjs
|
||||
let currentRate
|
||||
|
||||
if (bchjs.restURL.includes('abc.fullstack')) {
|
||||
currentRate = (await bchjs.Price.getBchaUsd()) * 100
|
||||
} else {
|
||||
// BCHN price.
|
||||
currentRate = (await bchjs.Price.getUsd()) * 100
|
||||
}
|
||||
|
||||
// Update redux state
|
||||
_this.props.setWalletInfo(currentWallet)
|
||||
_this.props.updateBalance({ myBalance, currentRate })
|
||||
_this.props.setBchWallet(bchWalletLib)
|
||||
} catch (error) {
|
||||
_this.onStatusLog('Error on re-initialize')
|
||||
_this.onStatusLog(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
sleep (ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
This library interacts with the ipfs-bch-wallet-service via the JSON RPC
|
||||
over IPFS.
|
||||
*/
|
||||
const { v4: uid } = require('uuid')
|
||||
const jsonrpc = require('jsonrpc-lite')
|
||||
|
||||
// Public npm libraries.
|
||||
const axios = require('axios')
|
||||
// const Conf = require('conf')
|
||||
|
||||
class WalletService {
|
||||
constructor (localConfig = {}) {
|
||||
// Encapsulate dependencies
|
||||
this.axios = axios
|
||||
// this.conf = new Conf()
|
||||
// this.ipfsControl = localConfig.ipfsControl
|
||||
this.uid = uid
|
||||
this.jsonrpc = jsonrpc
|
||||
this.ipfsControl = localConfig.ipfsControl
|
||||
// A queue for holding RPC data that has arrived.
|
||||
this.rpcDataQueue = []
|
||||
}
|
||||
|
||||
checkServiceId () {
|
||||
try {
|
||||
// this.conf = new Conf()
|
||||
|
||||
const serviceId = this.ipfsControl.selectedServiceProvider
|
||||
console.log(`serviceId : ${serviceId}`)
|
||||
if (!serviceId) {
|
||||
throw new Error('Wallet service ID does not exist')
|
||||
}
|
||||
|
||||
return serviceId
|
||||
} catch (error) {
|
||||
console.error('Error in checkServiceId()')
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// Get up to 20 addresses.
|
||||
async getBalances (addrs) {
|
||||
try {
|
||||
// Input validation.
|
||||
if (!addrs || !Array.isArray(addrs)) {
|
||||
throw new Error(
|
||||
'addrs input to getBalance() must be an array, of up to 20 addresses.'
|
||||
)
|
||||
}
|
||||
|
||||
const serviceId = this.checkServiceId()
|
||||
// console.log(`serviceId: ${serviceId}`)
|
||||
|
||||
const rpcId = this.uid()
|
||||
const rpcData = {
|
||||
endpoint: 'balance',
|
||||
addresses: addrs
|
||||
}
|
||||
// Generate a JSON RPC command.
|
||||
const cmd = this.jsonrpc.request(rpcId, 'bch', rpcData)
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
/// ///
|
||||
/* const result = await this.axios.post(LOCAL_REST_API, {
|
||||
sendTo: serviceId,
|
||||
rpcData: {
|
||||
endpoint: 'balance',
|
||||
addresses: addrs
|
||||
}
|
||||
}) */
|
||||
// console.log(`result.data: ${JSON.stringify(result.data, null, 2)}`)
|
||||
|
||||
// If there is a timeout or other network failure.
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
console.error('Error in getBalance()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Get hydrated UTXOs for an address
|
||||
async getUtxos (addr) {
|
||||
try {
|
||||
// Input validation
|
||||
if (!addr || typeof addr !== 'string') {
|
||||
throw new Error('getUtxos() input address must be a string.')
|
||||
}
|
||||
|
||||
const serviceId = this.checkServiceId()
|
||||
// console.log(`serviceId: ${serviceId}`)
|
||||
|
||||
const rpcId = this.uid()
|
||||
const rpcData = {
|
||||
endpoint: 'utxos',
|
||||
address: addr
|
||||
}
|
||||
// Generate a JSON RPC command.
|
||||
const cmd = this.jsonrpc.request(rpcId, 'bch', rpcData)
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
console.log('cmdStr', cmdStr)
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
console.error('Error in getUtxos()', err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
// Broadcast a transaction to the network.
|
||||
async sendTx (hex) {
|
||||
try {
|
||||
// Input validation
|
||||
if (!hex || typeof hex !== 'string') {
|
||||
throw new Error('sendTx() input hex must be a string.')
|
||||
}
|
||||
|
||||
const serviceId = this.checkServiceId()
|
||||
// console.log(`serviceId: ${serviceId}`)
|
||||
const rpcId = this.uid()
|
||||
const rpcData = {
|
||||
endpoint: 'broadcast',
|
||||
hex
|
||||
}
|
||||
// Generate a JSON RPC command.
|
||||
const cmd = this.jsonrpc.request(rpcId, 'bch', rpcData)
|
||||
const cmdStr = JSON.stringify(cmd)
|
||||
const thisNode = this.ipfsControl.ipfsCoord.thisNode
|
||||
|
||||
const result = await this.ipfsControl.ipfsCoord.useCases.peer.sendPrivateMessage(
|
||||
serviceId,
|
||||
cmdStr,
|
||||
thisNode
|
||||
)
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
console.error('Error in sendTx()')
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WalletService
|
||||
Reference in New Issue
Block a user