classic-ui/modules/account/store/actions/recoverAccountFromChain/getAccountFromAddress.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-06-09 19:41:41 +03:00
import { getEventsFromBlockPart } from '../utils'
2022-04-22 06:05:56 +03:00
import { unpackEncryptedMessage } from '@/utils'
export async function getAccountFromAddress({ getters, rootGetters }, address) {
try {
const netId = rootGetters['metamask/netId']
const rpc = rootGetters['settings/currentRpc']
const web3 = this.$provider.getWeb3(rpc.url)
const currentBlockNumber = await web3.eth.getBlockNumber()
2022-06-09 19:41:41 +03:00
const events = await getEventsFromBlockPart({
netId,
currentBlockNumber,
address,
echoContract: getters.echoContract
})
2022-04-22 06:05:56 +03:00
const [lastEvent] = events.slice(-1)
if (!lastEvent) {
throw new Error(`Please setup account, account doesn't exist for this address`)
}
const data = lastEvent.encryptedAccount ? lastEvent.encryptedAccount : lastEvent.returnValues.data
const backup = lastEvent.address ? lastEvent.address : lastEvent.returnValues.who
const encryptedMessage = unpackEncryptedMessage(data)
const encryptedKey = Buffer.from(JSON.stringify(encryptedMessage)).toString('hex')
return {
backup,
encryptedKey
}
} catch (err) {
throw new Error(`Method getAccountFromAddress has error: ${err.message}`)
}
}