Merge pull request #389 from shocknet/feature/request-channel
request channel
This commit is contained in:
commit
7e5c43fb42
3 changed files with 115 additions and 5 deletions
|
|
@ -36,6 +36,7 @@ const LV2 = require('../utils/lightningServices/v2')
|
||||||
const GunWriteRPC = require('../services/gunDB/rpc')
|
const GunWriteRPC = require('../services/gunDB/rpc')
|
||||||
const Key = require('../services/gunDB/contact-api/key')
|
const Key = require('../services/gunDB/contact-api/key')
|
||||||
const { startedStream, endStream } = require('../services/streams')
|
const { startedStream, endStream } = require('../services/streams')
|
||||||
|
const channelRequest = require('../utils/lightningServices/channelRequests')
|
||||||
|
|
||||||
const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10
|
const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10
|
||||||
const SESSION_ID = uuid()
|
const SESSION_ID = uuid()
|
||||||
|
|
@ -619,7 +620,7 @@ module.exports = async (
|
||||||
// If we're connected to lnd, unlock the wallet using the password supplied
|
// If we're connected to lnd, unlock the wallet using the password supplied
|
||||||
// and generate an auth token if that operation was successful.
|
// and generate an auth token if that operation was successful.
|
||||||
if (health.LNDStatus.success && walletInitialized) {
|
if (health.LNDStatus.success && walletInitialized) {
|
||||||
const { alias, password } = req.body
|
const { alias, password, invite } = req.body
|
||||||
|
|
||||||
await recreateLnServices()
|
await recreateLnServices()
|
||||||
|
|
||||||
|
|
@ -798,6 +799,7 @@ module.exports = async (
|
||||||
|
|
||||||
onNewChannelBackup()
|
onNewChannelBackup()
|
||||||
|
|
||||||
|
channelRequest(invite)
|
||||||
res.json({
|
res.json({
|
||||||
authorization: token,
|
authorization: token,
|
||||||
user: {
|
user: {
|
||||||
|
|
@ -840,7 +842,7 @@ module.exports = async (
|
||||||
app.post('/api/lnd/wallet', async (req, res) => {
|
app.post('/api/lnd/wallet', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { walletUnlocker } = LightningServices.services
|
const { walletUnlocker } = LightningServices.services
|
||||||
const { password, alias } = req.body
|
const { password, alias, invite } = req.body
|
||||||
const healthResponse = await checkHealth()
|
const healthResponse = await checkHealth()
|
||||||
if (!alias) {
|
if (!alias) {
|
||||||
return res.status(400).json({
|
return res.status(400).json({
|
||||||
|
|
@ -958,6 +960,7 @@ module.exports = async (
|
||||||
await LightningServices.init()
|
await LightningServices.init()
|
||||||
|
|
||||||
const token = await auth.generateToken()
|
const token = await auth.generateToken()
|
||||||
|
channelRequest(invite)
|
||||||
return res.json({
|
return res.json({
|
||||||
mnemonicPhrase,
|
mnemonicPhrase,
|
||||||
authorization: token,
|
authorization: token,
|
||||||
|
|
@ -1005,7 +1008,7 @@ module.exports = async (
|
||||||
|
|
||||||
app.post('/api/lnd/wallet/existing', async (req, res) => {
|
app.post('/api/lnd/wallet/existing', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { password, alias } = req.body
|
const { password, alias, invite } = req.body
|
||||||
const healthResponse = await checkHealth()
|
const healthResponse = await checkHealth()
|
||||||
const exists = await walletExists()
|
const exists = await walletExists()
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
|
|
@ -1062,7 +1065,7 @@ module.exports = async (
|
||||||
|
|
||||||
// Generate Access Token
|
// Generate Access Token
|
||||||
const token = await auth.generateToken()
|
const token = await auth.generateToken()
|
||||||
|
channelRequest(invite)
|
||||||
res.json({
|
res.json({
|
||||||
authorization: token,
|
authorization: token,
|
||||||
user: {
|
user: {
|
||||||
|
|
|
||||||
68
utils/lightningServices/channelRequests.js
Normal file
68
utils/lightningServices/channelRequests.js
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
const logger = require('winston')
|
||||||
|
const fetch = require('node-fetch')
|
||||||
|
const Storage = require('node-persist')
|
||||||
|
const { listPeers, connectPeer,getInfo } = require('./v2')
|
||||||
|
|
||||||
|
const handlerBaseUrl = "https://channels.shock.network"
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} inviteFromAuth
|
||||||
|
*/
|
||||||
|
module.exports = async (inviteFromAuth) => {
|
||||||
|
/**
|
||||||
|
* @type string | undefined
|
||||||
|
*/
|
||||||
|
const invite = inviteFromAuth || process.env.HOSTING_INVITE
|
||||||
|
if(!invite) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
await Storage.getItem('processedInvites')
|
||||||
|
} catch(e) {
|
||||||
|
await Storage.setItem('processedInvites',[])
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
/**
|
||||||
|
* @type string[]
|
||||||
|
*/
|
||||||
|
const invites = await Storage.getItem('processedInvites')
|
||||||
|
if(invites.includes(invite)){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const me = await getInfo()
|
||||||
|
const {identity_pubkey} = me
|
||||||
|
//@ts-expect-error
|
||||||
|
const connectReq = await fetch(`${handlerBaseUrl}/connect`)
|
||||||
|
if(connectReq.status !== 200 ){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const connJson = await connectReq.json()
|
||||||
|
const [uri] = connJson.uris
|
||||||
|
const [pub,host] = uri.split("@")
|
||||||
|
const peers = await listPeers()
|
||||||
|
if(peers.findIndex(peer => peer.pub_key === pub) === -1){
|
||||||
|
await connectPeer(pub,host)
|
||||||
|
}
|
||||||
|
|
||||||
|
//@ts-expect-error
|
||||||
|
const res = await fetch(`${handlerBaseUrl}/channel`,{
|
||||||
|
method:'POST',
|
||||||
|
body:JSON.stringify({
|
||||||
|
userPubKey:identity_pubkey,
|
||||||
|
invite,
|
||||||
|
lndTo:pub,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
invites.push(invite)
|
||||||
|
await Storage.setItem('processedInvites',invites)
|
||||||
|
|
||||||
|
} catch(e){
|
||||||
|
logger.error("error sending invite to channels handler")
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -631,6 +631,43 @@ const subscribeTransactions = (dataCb, errorCb) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getInfo = () =>
|
||||||
|
Common.makePromise((res, rej) => {
|
||||||
|
const { lightning } = lightningServices.getServices()
|
||||||
|
|
||||||
|
lightning.getInfo({}, (err, resp) => {
|
||||||
|
if (err) {
|
||||||
|
rej(new Error(err.message))
|
||||||
|
} else {
|
||||||
|
// Needs cast because typescript refuses to assign Record<string, any>
|
||||||
|
// to an actual object :shrugs
|
||||||
|
res(resp)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {string} pubkey
|
||||||
|
* @param {string} host
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const connectPeer = (pubkey, host) =>
|
||||||
|
Common.makePromise((res, rej) => {
|
||||||
|
const { lightning } = lightningServices.getServices()
|
||||||
|
const connectRequest = {
|
||||||
|
addr: { pubkey, host },
|
||||||
|
perm: true
|
||||||
|
}
|
||||||
|
lightning.connectPeer(connectRequest, (err, resp) => {
|
||||||
|
if (err) {
|
||||||
|
rej(new Error(err.message))
|
||||||
|
} else {
|
||||||
|
// Needs cast because typescript refuses to assign Record<string, any>
|
||||||
|
// to an actual object :shrugs
|
||||||
|
res(resp)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
module.exports = {
|
module.exports = {
|
||||||
sendPaymentV2Keysend,
|
sendPaymentV2Keysend,
|
||||||
sendPaymentV2Invoice,
|
sendPaymentV2Invoice,
|
||||||
|
|
@ -644,5 +681,7 @@ module.exports = {
|
||||||
pendingChannels,
|
pendingChannels,
|
||||||
addInvoice,
|
addInvoice,
|
||||||
subscribeInvoices,
|
subscribeInvoices,
|
||||||
subscribeTransactions
|
subscribeTransactions,
|
||||||
|
getInfo,
|
||||||
|
connectPeer
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue