support keysend + hook fix

This commit is contained in:
boufni95 2020-08-05 15:09:38 +02:00
parent 1821988184
commit ceb6a039bb

View file

@ -100,7 +100,7 @@ module.exports = async (
success: true success: true
}) })
}) })
}) })
const checkHealth = async () => { const checkHealth = async () => {
logger.info('Getting service status...') logger.info('Getting service status...')
@ -203,7 +203,7 @@ module.exports = async (
message: sanitizeLNDError(err.message) message: sanitizeLNDError(err.message)
}) })
} }
}) })
// Hack to check whether or not a wallet exists // Hack to check whether or not a wallet exists
const walletExists = async () => { const walletExists = async () => {
@ -264,35 +264,42 @@ module.exports = async (
logger.error('Unknown Device') logger.error('Unknown Device')
return res.status(401).json(error) return res.status(401).json(error)
} }
if (!req.body.encryptionKey && !req.body.iv && !req.headers["x-shock-encryption-token"]){ if (
!req.body.encryptionKey &&
!req.body.iv &&
!req.headers['x-shock-encryption-token']
) {
return next() return next()
} }
let encryptedToken,encryptedKey,IV,data let reqData = null
if(req.method === 'GET' || req.method === 'DELETE'){ let IV = null
if(req.headers["x-shock-encryption-token"]){ let encryptedKey = null
encryptedToken = req.headers["x-shock-encryption-token"] let encryptedToken = null
encryptedKey =req.headers["x-shock-encryption-key"] if (req.method === 'GET' || req.method === 'DELETE') {
IV =req.headers["x-shock-encryption-iv"] if (req.headers['x-shock-encryption-token']) {
encryptedToken = req.headers['x-shock-encryption-token']
encryptedKey = req.headers['x-shock-encryption-key']
IV = req.headers['x-shock-encryption-iv']
} }
} else { } else {
encryptedToken = req.body.token encryptedToken = req.body.token
encryptedKey = req.body.encryptionKey encryptedKey = req.body.encryptionKey
IV = req.body.iv IV = req.body.iv
data = req.body.data reqData = req.body.data
} }
const decryptedKey = Encryption.decryptKey({ const decryptedKey = Encryption.decryptKey({
deviceId, deviceId,
message: encryptedKey message: encryptedKey
}) })
if(data){ if (reqData) {
const decryptedMessage = Encryption.decryptMessage({ const decryptedMessage = Encryption.decryptMessage({
message: data, message: reqData,
key: decryptedKey, key: decryptedKey,
iv: IV iv: IV
}) })
req.body = JSON.parse(decryptedMessage) req.body = JSON.parse(decryptedMessage)
} }
const decryptedToken = encryptedToken const decryptedToken = encryptedToken
? Encryption.decryptMessage({ ? Encryption.decryptMessage({
message: encryptedToken, message: encryptedToken,
@ -300,7 +307,6 @@ module.exports = async (
iv: IV iv: IV
}) })
: null : null
if (decryptedToken) { if (decryptedToken) {
req.headers.authorization = decryptedToken req.headers.authorization = decryptedToken
@ -1372,16 +1378,48 @@ module.exports = async (
app.post('/api/lnd/sendpayment', (req, res) => { app.post('/api/lnd/sendpayment', (req, res) => {
const { router } = LightningServices.services const { router } = LightningServices.services
// this is the recommended value from lightning labs // this is the recommended value from lightning labs
const { maxParts = 3, payreq } = req.body let paymentRequest = {}
const { keysend, maxParts = 3, timeoutSeconds = 5 } = req.body
if (keysend) {
const { dest, amt, finalCltvDelta = 40 } = req.body
if (!dest || !amt) {
return res.status(500).json({
errorMessage: 'please provide "dest" and "amt" for keysend payments'
})
}
const preimage = Crypto.randomBytes(32)
const r_hash = Crypto.createHash('sha256')
.update(preimage)
.digest()
//https://github.com/lightningnetwork/lnd/blob/master/record/experimental.go#L5:2
//might break in future updates
const KeySendType = 5482373484
//https://api.lightning.community/#featurebit
const TLV_ONION_REQ = 8
paymentRequest = {
dest: Buffer.from(dest, 'hex'),
amt,
final_cltv_delta: finalCltvDelta,
dest_features: [TLV_ONION_REQ],
dest_custom_records: {
[KeySendType]: preimage
},
payment_hash: r_hash,
max_parts: maxParts,
timeout_seconds: timeoutSeconds
}
} else {
const { payreq } = req.body
const paymentRequest = { paymentRequest = {
payment_request: payreq, payment_request: payreq,
max_parts: maxParts, max_parts: maxParts,
timeout_seconds: 5 timeout_seconds: timeoutSeconds
} }
if (req.body.amt) { if (req.body.amt) {
paymentRequest.amt = req.body.amt paymentRequest.amt = req.body.amt
}
} }
logger.info('Sending payment', paymentRequest) logger.info('Sending payment', paymentRequest)