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

36 lines
1.0 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,
address,
2022-06-14 15:00:47 +03:00
currentBlockNumber,
2022-06-09 19:41:41 +03:00
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`)
}
2022-06-14 15:00:47 +03:00
const encryptedMessage = unpackEncryptedMessage(lastEvent.encryptedAccount)
2022-04-22 06:05:56 +03:00
const encryptedKey = Buffer.from(JSON.stringify(encryptedMessage)).toString('hex')
return {
2022-06-14 15:00:47 +03:00
encryptedKey,
backup: lastEvent.address
2022-04-22 06:05:56 +03:00
}
} catch (err) {
throw new Error(`Method getAccountFromAddress has error: ${err.message}`)
}
}