self encrypt and self seed provider
This commit is contained in:
parent
fce4fee2cb
commit
0c8a6189ca
2 changed files with 55 additions and 2 deletions
|
|
@ -6,6 +6,8 @@ const logger = require('winston')
|
||||||
const Common = require('shock-common')
|
const Common = require('shock-common')
|
||||||
const { Constants, Schema } = Common
|
const { Constants, Schema } = Common
|
||||||
const Gun = require('gun')
|
const Gun = require('gun')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const fetch = require('node-fetch')
|
||||||
|
|
||||||
const { ErrorCode } = Constants
|
const { ErrorCode } = Constants
|
||||||
|
|
||||||
|
|
@ -927,7 +929,7 @@ const sendHRWithInitialMsg = async (
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
* @typedef {object} OrderRes
|
* @typedef {object} OrderRes
|
||||||
* @prop {PaymentV2} payment
|
* @prop {PaymentV2|null} payment
|
||||||
* @prop {object=} orderAck
|
* @prop {object=} orderAck
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
|
|
@ -951,6 +953,54 @@ const sendSpontaneousPayment = async (
|
||||||
try {
|
try {
|
||||||
const SEA = require('../Mediator').mySEA
|
const SEA = require('../Mediator').mySEA
|
||||||
const getUser = () => require('../Mediator').getUser()
|
const getUser = () => require('../Mediator').getUser()
|
||||||
|
const myPub = getUser()._.sea.pub
|
||||||
|
if (
|
||||||
|
to === myPub &&
|
||||||
|
opts.type === 'torrentSeed' &&
|
||||||
|
opts.ackInfo &&
|
||||||
|
!isNaN(parseInt(opts.ackInfo, 10))
|
||||||
|
) {
|
||||||
|
//user requested a seed to themselves
|
||||||
|
const numberOfTokens = Number(opts.ackInfo)
|
||||||
|
if (isNaN(numberOfTokens)) {
|
||||||
|
throw new Error('ackInfo provided is not a valid number')
|
||||||
|
}
|
||||||
|
const seedUrl = process.env.TORRENT_SEED_URL
|
||||||
|
const seedToken = process.env.TORRENT_SEED_TOKEN
|
||||||
|
if (!seedUrl || !seedToken) {
|
||||||
|
throw new Error('torrentSeed service not available')
|
||||||
|
}
|
||||||
|
console.log('SEED URL OK')
|
||||||
|
const tokens = Array(numberOfTokens)
|
||||||
|
for (let i = 0; i < numberOfTokens; i++) {
|
||||||
|
tokens[i] = crypto.randomBytes(32).toString('hex')
|
||||||
|
}
|
||||||
|
/**@param {string} token */
|
||||||
|
const enrollToken = async token => {
|
||||||
|
const reqData = {
|
||||||
|
seed_token: seedToken,
|
||||||
|
wallet_token: token
|
||||||
|
}
|
||||||
|
//@ts-expect-error
|
||||||
|
const res = await fetch(`${seedUrl}/api/enroll_token`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(reqData)
|
||||||
|
})
|
||||||
|
if (res.status !== 200) {
|
||||||
|
throw new Error('torrentSeed service currently not available')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await Promise.all(tokens.map(enrollToken))
|
||||||
|
console.log('RES SEED OK')
|
||||||
|
const ackData = JSON.stringify({ seedUrl, tokens })
|
||||||
|
return {
|
||||||
|
payment: null,
|
||||||
|
orderAck: { response: ackData, type: 'orderAck' }
|
||||||
|
}
|
||||||
|
}
|
||||||
const recipientEpub = await Utils.pubToEpub(to)
|
const recipientEpub = await Utils.pubToEpub(to)
|
||||||
const ourSecret = await SEA.secret(recipientEpub, getUser()._.sea)
|
const ourSecret = await SEA.secret(recipientEpub, getUser()._.sea)
|
||||||
|
|
||||||
|
|
@ -1179,6 +1229,9 @@ const sendSpontaneousPayment = async (
|
||||||
*/
|
*/
|
||||||
const sendPayment = async (to, amount, memo, feeLimit) => {
|
const sendPayment = async (to, amount, memo, feeLimit) => {
|
||||||
const res = await sendSpontaneousPayment(to, amount, memo, feeLimit)
|
const res = await sendSpontaneousPayment(to, amount, memo, feeLimit)
|
||||||
|
if (!res.payment) {
|
||||||
|
throw new Error('invalid payment params') //only if it's a torrentSeed request to self
|
||||||
|
}
|
||||||
return res.payment.payment_preimage
|
return res.payment.payment_preimage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ async function deepEncryptIfNeeded(value) {
|
||||||
|
|
||||||
let encryptedValue = ''
|
let encryptedValue = ''
|
||||||
|
|
||||||
if (pk === u.is.pub) {
|
if (pk === u.is.pub || pk === 'me') {
|
||||||
encryptedValue = await SEA.encrypt(actualValue, getMySecret())
|
encryptedValue = await SEA.encrypt(actualValue, getMySecret())
|
||||||
} else {
|
} else {
|
||||||
const sec = await SEA.secret(await pubToEpub(pk), u._.sea)
|
const sec = await SEA.secret(await pubToEpub(pk), u._.sea)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue