Merge pull request #389 from shocknet/feature/request-channel

request channel
This commit is contained in:
CapDog 2021-06-03 17:44:47 -04:00 committed by GitHub
commit 7e5c43fb42
3 changed files with 115 additions and 5 deletions

View file

@ -36,6 +36,7 @@ const LV2 = require('../utils/lightningServices/v2')
const GunWriteRPC = require('../services/gunDB/rpc')
const Key = require('../services/gunDB/contact-api/key')
const { startedStream, endStream } = require('../services/streams')
const channelRequest = require('../utils/lightningServices/channelRequests')
const DEFAULT_MAX_NUM_ROUTES_TO_QUERY = 10
const SESSION_ID = uuid()
@ -619,7 +620,7 @@ module.exports = async (
// If we're connected to lnd, unlock the wallet using the password supplied
// and generate an auth token if that operation was successful.
if (health.LNDStatus.success && walletInitialized) {
const { alias, password } = req.body
const { alias, password, invite } = req.body
await recreateLnServices()
@ -798,6 +799,7 @@ module.exports = async (
onNewChannelBackup()
channelRequest(invite)
res.json({
authorization: token,
user: {
@ -840,7 +842,7 @@ module.exports = async (
app.post('/api/lnd/wallet', async (req, res) => {
try {
const { walletUnlocker } = LightningServices.services
const { password, alias } = req.body
const { password, alias, invite } = req.body
const healthResponse = await checkHealth()
if (!alias) {
return res.status(400).json({
@ -958,6 +960,7 @@ module.exports = async (
await LightningServices.init()
const token = await auth.generateToken()
channelRequest(invite)
return res.json({
mnemonicPhrase,
authorization: token,
@ -1005,7 +1008,7 @@ module.exports = async (
app.post('/api/lnd/wallet/existing', async (req, res) => {
try {
const { password, alias } = req.body
const { password, alias, invite } = req.body
const healthResponse = await checkHealth()
const exists = await walletExists()
if (!exists) {
@ -1062,7 +1065,7 @@ module.exports = async (
// Generate Access Token
const token = await auth.generateToken()
channelRequest(invite)
res.json({
authorization: token,
user: {

View 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)
}
}

View file

@ -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 = {
sendPaymentV2Keysend,
sendPaymentV2Invoice,
@ -644,5 +681,7 @@ module.exports = {
pendingChannels,
addInvoice,
subscribeInvoices,
subscribeTransactions
subscribeTransactions,
getInfo,
connectPeer
}