Browse Source

exec loopbot in child process

dependabot/npm_and_yarn/ini-1.3.7
Evan Feenstra 4 years ago
parent
commit
bd59a916bc
  1. 173
      dist/src/builtin/loop.js
  2. 2
      dist/src/builtin/loop.js.map
  3. 204
      src/builtin/loop.ts

173
dist/src/builtin/loop.js

@ -11,14 +11,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const Sphinx = require("sphinx-bot"); const Sphinx = require("sphinx-bot");
const api_1 = require("../controllers/api"); const api_1 = require("../controllers/api");
const node_fetch_1 = require("node-fetch"); // import fetch from 'node-fetch'
const path = require("path"); const path = require("path");
const models_1 = require("../models"); const models_1 = require("../models");
const constants_1 = require("../constants"); const constants_1 = require("../constants");
const child_process_1 = require("child_process");
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env];
var validate = require('bitcoin-address-validation'); var validate = require('bitcoin-address-validation');
const msg_types = Sphinx.MSG_TYPE; const msg_types = Sphinx.MSG_TYPE;
let initted = false; let initted = false;
const baseurl = 'https://localhost:8080'; // const baseurl = 'https://localhost:8080'
function init() { function init() {
if (initted) if (initted)
return; return;
@ -56,12 +59,53 @@ function init() {
message.channel.send({ embed }); message.channel.send({ embed });
return; return;
} }
// try {
// const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt)
// console.log("=> LOOP QUOTE RES", j)
// if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
// return
// }
// let chan
// const bot = await getBot(message.channel.id)
// if (bot && bot.meta) chan = bot.meta
// if (!chan) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('No channel set')
// message.channel.send({ embed })
// return
// }
// const j2 = await doRequest(baseurl + '/v1/loop/out', {
// method: 'POST',
// body: JSON.stringify({
// amt: amt,
// dest: addy,
// outgoing_chan_set: [chan],
// max_swap_fee: j.swap_fee_sat,
// max_prepay_amt: j.prepay_amt_sat
// }),
// })
// console.log("=> LOOP RESPONSE", j2)
// if (j2 && j2.error) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('Error: ' + j2.error)
// message.channel.send({ embed })
// return
// }
// // if (!(j2 && j2.server_message)) {
// // return
// // }
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setTitle('Payment was sent!')
// // .setDescription('Success!')
// message.channel.send({ embed })
// return
// } catch (e) {
// console.log('LoopBot error', e)
// }
try { try {
const j = yield doRequest(baseurl + '/v1/loop/out/quote/' + amt);
console.log("=> LOOP QUOTE RES", j);
if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
return;
}
let chan; let chan;
const bot = yield getBot(message.channel.id); const bot = yield getBot(message.channel.id);
if (bot && bot.meta) if (bot && bot.meta)
@ -73,33 +117,27 @@ function init() {
message.channel.send({ embed }); message.channel.send({ embed });
return; return;
} }
const j2 = yield doRequest(baseurl + '/v1/loop/out', { const cmd = `loop --tlscertpath=${config.tls_location} --rpcserver=localhost:10009 out --channel=${chan} --amt=${amt} --fast --addr=${addy}`;
method: 'POST', console.log("=> LOOPBOT cmd:", cmd);
body: JSON.stringify({ let childProcess = child_process_1.spawn(cmd);
amt: amt, childProcess.stdout.on('data', function (data) {
dest: addy, const stdout = data.toString();
outgoing_chan_set: [chan], console.log("LOOPBOT stdout:", stdout);
max_swap_fee: j.swap_fee_sat, if (stdout) {
max_prepay_amt: j.prepay_amt_sat console.log('=> LOOPBOT stdout', stdout);
}), if (stdout.startsWith('CONTINUE SWAP?')) {
childProcess.stdin.write('y\n');
}
if (stdout.startsWith('Swap initiated')) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!');
// .setDescription('Success!')
message.channel.send({ embed });
return;
}
}
}); });
console.log("=> LOOP RESPONSE", j2);
if (j2 && j2.error) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Error: ' + j2.error);
message.channel.send({ embed });
return;
}
// if (!(j2 && j2.server_message)) {
// return
// }
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!');
// .setDescription('Success!')
message.channel.send({ embed });
return;
} }
catch (e) { catch (e) {
console.log('LoopBot error', e); console.log('LoopBot error', e);
@ -146,16 +184,20 @@ function init() {
exports.init = init; exports.init = init;
function getBot(tribeUUID) { function getBot(tribeUUID) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const chat = yield models_1.models.Chat.findOne({ where: { const chat = yield models_1.models.Chat.findOne({
where: {
uuid: tribeUUID uuid: tribeUUID
} }); }
});
if (!chat) if (!chat)
return; return;
const chatBot = yield models_1.models.ChatBot.findOne({ where: { const chatBot = yield models_1.models.ChatBot.findOne({
where: {
chatId: chat.id, chatId: chat.id,
botPrefix: '/loopout', botPrefix: '/loopout',
botType: constants_1.default.bot_types.builtin botType: constants_1.default.bot_types.builtin
} }); }
});
return chatBot; return chatBot;
}); });
} }
@ -167,34 +209,33 @@ function validateAmount(amtString) {
const ok = amt > 0; const ok = amt > 0;
return ok; return ok;
} }
const fs = require('fs'); // const fs = require('fs')
const https = require("https"); // const https = require("https");
// const homedir = require('os').homedir(); // const homedir = require('os').homedir();
const agent = new https.Agent({ // const agent = new https.Agent({
rejectUnauthorized: false // rejectUnauthorized: false
}); // })
const env = process.env.NODE_ENV || 'development'; // async function doRequest(theurl: string, params?: Object) {
const config = require(path.join(__dirname, '../../config/app.json'))[env]; // const ps = params || {}
function doRequest(theurl, params) { // try {
return __awaiter(this, void 0, void 0, function* () { // const macLocation = config.loop_macaroon_location
const ps = params || {}; // if (!macLocation) {
try { // throw new Error('no macaroon')
const macLocation = config.loop_macaroon_location; // }
if (!macLocation) { // var macaroonString = fs.readFileSync(macLocation);
throw new Error('no macaroon'); // var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
} // const theParams = {
var macaroonString = fs.readFileSync(macLocation); // agent,
var mac = Buffer.from(macaroonString, 'utf8').toString('hex'); // headers: {
const theParams = Object.assign({ agent, headers: { // 'Grpc-Metadata-macaroon': mac
'Grpc-Metadata-macaroon': mac // },
} }, ps); // ...ps
const r = yield node_fetch_1.default(theurl, theParams); // }
const j = yield r.json(); // const r = await fetch(theurl, theParams)
return j; // const j = await r.json()
} // return j
catch (e) { // } catch (e) {
throw e; // throw e
} // }
}); // }
}
//# sourceMappingURL=loop.js.map //# sourceMappingURL=loop.js.map

2
dist/src/builtin/loop.js.map

File diff suppressed because one or more lines are too long

204
src/builtin/loop.ts

@ -1,15 +1,20 @@
import * as Sphinx from 'sphinx-bot' import * as Sphinx from 'sphinx-bot'
import { finalAction } from '../controllers/api' import { finalAction } from '../controllers/api'
import fetch from 'node-fetch' // import fetch from 'node-fetch'
import * as path from 'path' import * as path from 'path'
import { models } from '../models' import { models } from '../models'
import constants from '../constants' import constants from '../constants'
import { spawn } from 'child_process'
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env]
var validate = require('bitcoin-address-validation'); var validate = require('bitcoin-address-validation');
const msg_types = Sphinx.MSG_TYPE const msg_types = Sphinx.MSG_TYPE
let initted = false let initted = false
const baseurl = 'https://localhost:8080' // const baseurl = 'https://localhost:8080'
export function init() { export function init() {
if (initted) return if (initted) return
@ -42,20 +47,62 @@ export function init() {
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
if (messageAmount < parseInt(amt)){ if (messageAmount < parseInt(amt)) {
const embed = new Sphinx.MessageEmbed() const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot') .setAuthor('LoopBot')
.setDescription('Incorrect amount') .setDescription('Incorrect amount')
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
// try {
// const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt)
// console.log("=> LOOP QUOTE RES", j)
// if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
// return
// }
// let chan
// const bot = await getBot(message.channel.id)
// if (bot && bot.meta) chan = bot.meta
// if (!chan) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('No channel set')
// message.channel.send({ embed })
// return
// }
// const j2 = await doRequest(baseurl + '/v1/loop/out', {
// method: 'POST',
// body: JSON.stringify({
// amt: amt,
// dest: addy,
// outgoing_chan_set: [chan],
// max_swap_fee: j.swap_fee_sat,
// max_prepay_amt: j.prepay_amt_sat
// }),
// })
// console.log("=> LOOP RESPONSE", j2)
// if (j2 && j2.error) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('Error: ' + j2.error)
// message.channel.send({ embed })
// return
// }
// // if (!(j2 && j2.server_message)) {
// // return
// // }
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setTitle('Payment was sent!')
// // .setDescription('Success!')
// message.channel.send({ embed })
// return
// } catch (e) {
// console.log('LoopBot error', e)
// }
try { try {
const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt)
console.log("=> LOOP QUOTE RES", j)
if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
return
}
let chan let chan
const bot = await getBot(message.channel.id) const bot = await getBot(message.channel.id)
if (bot && bot.meta) chan = bot.meta if (bot && bot.meta) chan = bot.meta
@ -66,34 +113,27 @@ export function init() {
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
const cmd = `loop --tlscertpath=${config.tls_location} --rpcserver=localhost:10009 out --channel=${chan} --amt=${amt} --fast --addr=${addy}`
const j2 = await doRequest(baseurl + '/v1/loop/out', { console.log("=> LOOPBOT cmd:",cmd)
method: 'POST', let childProcess = spawn(cmd)
body: JSON.stringify({ childProcess.stdout.on('data', function (data) {
amt: amt, const stdout = data.toString()
dest: addy, console.log("LOOPBOT stdout:",stdout)
outgoing_chan_set: [chan], if(stdout){
max_swap_fee: j.swap_fee_sat, console.log('=> LOOPBOT stdout',stdout)
max_prepay_amt: j.prepay_amt_sat if(stdout.startsWith('CONTINUE SWAP?')) {
}), childProcess.stdin.write('y\n')
}
if(stdout.startsWith('Swap initiated')) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!')
// .setDescription('Success!')
message.channel.send({ embed })
return
}
}
}) })
console.log("=> LOOP RESPONSE", j2)
if (j2 && j2.error) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Error: ' + j2.error)
message.channel.send({ embed })
return
}
// if (!(j2 && j2.server_message)) {
// return
// }
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!')
// .setDescription('Success!')
message.channel.send({ embed })
return
} catch (e) { } catch (e) {
console.log('LoopBot error', e) console.log('LoopBot error', e)
} }
@ -103,15 +143,15 @@ export function init() {
const isAdmin = message.member.roles.find(role => role.name === 'Admin') const isAdmin = message.member.roles.find(role => role.name === 'Admin')
if(isAdmin && cmd.startsWith('setchan=')) { if (isAdmin && cmd.startsWith('setchan=')) {
const bot = await getBot(message.channel.id) const bot = await getBot(message.channel.id)
const arr = cmd.split('=') const arr = cmd.split('=')
if(bot && arr.length>1) { if (bot && arr.length > 1) {
const chan = arr[1] const chan = arr[1]
await bot.update({meta: chan}) await bot.update({ meta: chan })
const embed = new Sphinx.MessageEmbed() const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot') .setAuthor('LoopBot')
.setDescription('Channel updated to '+chan) .setDescription('Channel updated to ' + chan)
.setThumbnail(botSVG) .setThumbnail(botSVG)
message.channel.send({ embed }) message.channel.send({ embed })
return return
@ -140,16 +180,20 @@ export function init() {
}) })
} }
async function getBot(tribeUUID:string){ async function getBot(tribeUUID: string) {
const chat = await models.Chat.findOne({ where:{ const chat = await models.Chat.findOne({
uuid: tribeUUID where: {
}}) uuid: tribeUUID
}
})
if (!chat) return if (!chat) return
const chatBot = await models.ChatBot.findOne({where: { const chatBot = await models.ChatBot.findOne({
chatId: chat.id, where: {
botPrefix: '/loopout', chatId: chat.id,
botType: constants.bot_types.builtin botPrefix: '/loopout',
}}) botType: constants.bot_types.builtin
}
})
return chatBot return chatBot
} }
@ -163,36 +207,34 @@ function validateAmount(amtString: string) {
return ok return ok
} }
const fs = require('fs') // const fs = require('fs')
const https = require("https"); // const https = require("https");
// const homedir = require('os').homedir(); // const homedir = require('os').homedir();
const agent = new https.Agent({ // const agent = new https.Agent({
rejectUnauthorized: false // rejectUnauthorized: false
}) // })
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env] // async function doRequest(theurl: string, params?: Object) {
// const ps = params || {}
async function doRequest(theurl: string, params?: Object) { // try {
const ps = params || {} // const macLocation = config.loop_macaroon_location
try { // if (!macLocation) {
const macLocation=config.loop_macaroon_location // throw new Error('no macaroon')
if(!macLocation) { // }
throw new Error('no macaroon') // var macaroonString = fs.readFileSync(macLocation);
} // var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
var macaroonString = fs.readFileSync(macLocation); // const theParams = {
var mac = Buffer.from(macaroonString, 'utf8').toString('hex'); // agent,
const theParams = { // headers: {
agent, // 'Grpc-Metadata-macaroon': mac
headers: { // },
'Grpc-Metadata-macaroon': mac // ...ps
}, // }
...ps // const r = await fetch(theurl, theParams)
} // const j = await r.json()
const r = await fetch(theurl, theParams) // return j
const j = await r.json() // } catch (e) {
return j // throw e
} catch (e) { // }
throw e // }
}
}
Loading…
Cancel
Save