Skip to content

Commit

Permalink
Merge pull request #800 from stakwork/fix/pay-by-bot
Browse files Browse the repository at this point in the history
Fix/pay by bot
  • Loading branch information
Evanfeenstra authored Jan 30, 2024
2 parents f0fef09 + 36773e3 commit fc11ae7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
35 changes: 18 additions & 17 deletions dist/src/controllers/botapi/pay.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/controllers/botapi/pay.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions src/controllers/botapi/pay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ import * as socket from '../../utils/socket'
import constants from '../../constants'
import { sphinxLogger } from '../../utils/logger'
import { errMsgString } from '../../utils/errMsgString'
import * as rsa from '../../crypto/rsa'
import { Action, validateAction } from './index'

export default async function pay(a: Action): Promise<void> {
const { amount, bot_name, msg_uuid, reply_uuid, recipient_id, parent_id } = a
const {
amount,
bot_name,
msg_uuid,
reply_uuid,
recipient_id,
parent_id,
content,
} = a

sphinxLogger.info(`=> BOT PAY ${JSON.stringify(a, null, 2)}`)
if (!a.recipient_id) return sphinxLogger.error(`no recipient_id`)
Expand All @@ -19,13 +28,18 @@ export default async function pay(a: Action): Promise<void> {
const tenant: number = owner.id
const alias = bot_name || owner.alias
const botContactId = -1
const encryptedForMeText = rsa.encrypt(owner.contactKey, content || '')
const encryptedText = rsa.encrypt(chat.groupKey, content || '')
const textMap = { chat: encryptedText }

const date = new Date()
date.setMilliseconds(0)
const msg: { [k: string]: string | number | Date } = {
chatId: chat.id,
uuid: msg_uuid || short.generate(),
type: constants.message_types.boost,
type: reply_uuid
? constants.message_types.boost
: constants.message_types.direct_payment,
sender: botContactId, // tribe owner is escrow holder
amount: amount || 0,
date: date,
Expand All @@ -35,12 +49,18 @@ export default async function pay(a: Action): Promise<void> {
updatedAt: date,
senderAlias: alias,
tenant,
...(reply_uuid
? {}
: {
messageContent: encryptedForMeText,
remoteMessageContent: JSON.stringify(textMap),
}),
}
if (parent_id) msg.parentId = parent_id
const message: Message = (await models.Message.create(msg)) as Message
socket.sendJson(
{
type: 'boost',
type: reply_uuid ? 'boost' : 'direct_payment',
response: jsonUtils.messageToJson(message, chat, owner),
},
tenant
Expand All @@ -62,7 +82,10 @@ export default async function pay(a: Action): Promise<void> {
replyUuid: message.replyUuid,
parentId: message.parentId || 0,
},
type: constants.message_types.boost,
amount: amount || 0,
type: reply_uuid
? constants.message_types.boost
: constants.message_types.direct_payment,
success: () => ({ success: true }),
failure: async (e) => {
const errorMsg = errMsgString(e)
Expand Down

0 comments on commit fc11ae7

Please sign in to comment.