2022-06-10 02:41:41 +10:00
|
|
|
import { getEventsFromBlockPart } from '../utils'
|
2022-04-22 13:05:56 +10: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-10 02:41:41 +10:00
|
|
|
const events = await getEventsFromBlockPart({
|
|
|
|
netId,
|
|
|
|
address,
|
2022-06-14 22:00:47 +10:00
|
|
|
currentBlockNumber,
|
2022-06-10 02:41:41 +10:00
|
|
|
echoContract: getters.echoContract
|
|
|
|
})
|
2022-04-22 13:05:56 +10:00
|
|
|
|
|
|
|
const [lastEvent] = events.slice(-1)
|
|
|
|
|
|
|
|
if (!lastEvent) {
|
|
|
|
throw new Error(`Please setup account, account doesn't exist for this address`)
|
|
|
|
}
|
|
|
|
|
2022-06-14 22:00:47 +10:00
|
|
|
const encryptedMessage = unpackEncryptedMessage(lastEvent.encryptedAccount)
|
2022-04-22 13:05:56 +10:00
|
|
|
const encryptedKey = Buffer.from(JSON.stringify(encryptedMessage)).toString('hex')
|
|
|
|
|
|
|
|
return {
|
2022-06-14 22:00:47 +10:00
|
|
|
encryptedKey,
|
|
|
|
backup: lastEvent.address
|
2022-04-22 13:05:56 +10:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Method getAccountFromAddress has error: ${err.message}`)
|
|
|
|
}
|
|
|
|
}
|