Skip to content

Commit

Permalink
UIA Register - Send notify message, small fix in Recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Apr 14, 2024
1 parent 2a260ef commit 1b310bf
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 23 deletions.
23 changes: 17 additions & 6 deletions src/modules/recover/NotifyingPartner.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,29 @@ class RecoveryStep2 extends React.Component {
})
}

_renderCaptcha = () => {
_getCaptcha = () => {
const { captcha } = this.props.recoveryCfg
if (!captcha && !captcha.recaptcha_v2 || !captcha.recaptcha_v2.enabled) {
console.warn('captcha.recaptcha_v2 is disabled')
return
return { show: false }
}
if (!captcha.recaptcha_v2.site_key) {
console.warn('captcha.recaptcha_v2 has no site_key')
return { show: false, error: 'site_key' }
}
return { show: true, site_key: captcha.recaptcha_v2.site_key }
}

_renderCaptcha = () => {
const captcha = this._getCaptcha()
if (!captcha.show) {
if (captcha.error === 'site_key') {
console.warn('captcha.recaptcha_v2 has no site_key')
return
}
console.warn('captcha.recaptcha_v2 is disabled')
return
}
return (<ReCAPTCHA
sitekey={captcha.recaptcha_v2.site_key}
sitekey={captcha.site_key}
onChange={this._onRecaptchaChange} />)
}

Expand All @@ -107,7 +118,7 @@ class RecoveryStep2 extends React.Component {
{({
handleSubmit, isSubmitting, isValid, dirty, errors, touched, values, handleChange, setFieldValue,
}) => {
const isDisabled = !dirty || !isValid || !captchaValue
const isDisabled = !dirty || !isValid || (!captchaValue && this._getCaptcha().show)
const { errorMessage } = this.state
return (<form
onSubmit={handleSubmit}
Expand Down
31 changes: 14 additions & 17 deletions src/pages/api/recovery/[...all].js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import golos, { auth, messages } from 'golos-lib-js'
import golos from 'golos-lib-js'
import config from 'config'

import { checkCaptcha } from '@/server/captcha'
import { initGolos, } from '@/server/initGolos'
import nextConnect from '@/server/nextConnect'
import { throwErr, } from '@/server/error'
import { emailRegex, slowDownLimitReq, noBodyParser, bodyParams, } from '@/server/misc'
import { sendMsgAsync, } from '@/utils/messages'
import { getHistoryAuthority } from '@/utils/RecoveryUtils'

initGolos()
Expand All @@ -17,22 +18,18 @@ const sendMessage = async (notifier_account, acc, recoveryAcc,email) => {
text += url.toString()
text += ' Для связи пользователь указал e-mail ' + email

let msg = messages.newTextMsg(text, 'golos-messenger', 1)

let data = messages.encode(notifier_account.memo, recoveryAcc[0].memo_key, msg)

const json = JSON.stringify(['private_message', {
from: notifier_account.name,
to: recoveryAcc[0].name,
nonce: data.nonce,
from_memo_key: auth.wifToPublic(notifier_account.memo),
to_memo_key: recoveryAcc[0].memo_key,
checksum: data.checksum,
update: false,
encrypted_message: data.encrypted_message,
}])

await golos.broadcast.customJsonAsync(notifier_account.posting, [], [notifier_account.name], 'private_message', json)
await sendMsgAsync({
msg: text,
encode: {
from_key: notifier_account.memo,
to: recoveryAcc[0].memo_key
},
op: {
from: notifier_account.name,
to: recoveryAcc[0].name,
posting: notifier_account.posting
}
})
}

const afterSecToDate = (afterSec) => {
Expand Down
74 changes: 74 additions & 0 deletions src/utils/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { auth, api, broadcast, messages } from 'golos-lib-js'
import isFunction from 'lodash/isFunction'

// msg/message:
// - function which returns message created by newTextMsg/newImageMsg/etc
// - - or array of such messages
// - - or string (text message)
// - or just string (text message)
export async function sendMsgAsync(params) {
if (!params) throw new Error('params required')

let { msg, message, encode, op, operation } = params

msg = msg || message
if (!msg) throw new Error('`msg` or `message` param requred')

if (isFunction(msg)) {
msg = await msg()
} else {
msg = messages.newTextMsg(msg, 'golos-messenger', 1)
}

let data, from_memo_key, to_acc, to_memo_key
if (encode !== false && !encode) {
throw new Error('For security reasons, you should define `encode` param as a function or `false` if you want unencrypted message.')
}
if (encode) {
if (isFunction(encode)) {
encode = await encode()
}
let { from_key, to } = encode
if (!from_key) throw new Error('`encode.from_key` is required - private memo key of sender.')
if (!to) throw new Error('`encode.to` is required - public memo key of receiver or their account name.')
if (!auth.isPubkey(to)) {
to_acc = to
const accs = await api.getAccounts([to])
if (!accs[0]) throw new Error('`encode.to` - no such account')
to = accs[0].memo_key
}
from_memo_key = auth.wifToPublic(from_key)
to_memo_key = to
data = messages.encode(from_key, to, msg)
} else {
data = JSON.stringify(msg)
}

operation = operation || op
if (!operation) {
return data
}
if (isFunction(operation)) {
operation = await operation()
}
let { from, posting } = operation
if (!from) throw new Error('`operation.from` is required - account name of message sender.')
if (!posting || !auth.isWif(posting)) throw new Error('`operation.posting` is required and should be valid private posting key of message sender.')
if (!to_acc) {
to_acc = operation.to
if (!to_acc) throw new Error('`operation.to` is required - account name of message receiver.')
}

const json = JSON.stringify(['private_message', {
from: from,
to: to_acc,
nonce: data.nonce || 0,
from_memo_key,
to_memo_key,
checksum: data.checksum || 0,
update: false,
encrypted_message: data.encrypted_message || data,
}])

return await broadcast.customJsonAsync(posting, [], [from], 'private_message', json)
}

0 comments on commit 1b310bf

Please sign in to comment.