2021-05-26 17:58:20 +03:00
|
|
|
const { soliditySha3 } = require('web3-utils')
|
|
|
|
|
2019-10-21 15:57:28 +03:00
|
|
|
function strip0x(input) {
|
|
|
|
return input.replace(/^0x/, '')
|
|
|
|
}
|
|
|
|
|
2020-10-31 21:02:56 +03:00
|
|
|
/**
|
|
|
|
* Decodes the datatype byte from the AMB message.
|
|
|
|
* First (the most significant bit) denotes if the message should be forwarded to the manual lane.
|
|
|
|
* @param dataType: number datatype of the received AMB message.
|
|
|
|
* @return {{manualLane: boolean}}
|
|
|
|
*/
|
|
|
|
const decodeAMBDataType = dataType => ({
|
|
|
|
manualLane: (dataType & 128) === 128
|
|
|
|
})
|
|
|
|
|
2019-10-21 15:57:28 +03:00
|
|
|
function parseAMBMessage(message) {
|
|
|
|
message = strip0x(message)
|
|
|
|
|
2020-05-21 23:29:56 +03:00
|
|
|
const messageId = `0x${message.slice(0, 64)}`
|
2019-10-21 15:57:28 +03:00
|
|
|
const sender = `0x${message.slice(64, 104)}`
|
|
|
|
const executor = `0x${message.slice(104, 144)}`
|
2020-10-31 21:02:56 +03:00
|
|
|
const dataType = parseInt(message.slice(156, 158), 16)
|
2019-10-21 15:57:28 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
sender,
|
|
|
|
executor,
|
2020-10-31 21:02:56 +03:00
|
|
|
messageId,
|
|
|
|
dataType,
|
|
|
|
decodedDataType: decodeAMBDataType(dataType)
|
2019-10-21 15:57:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 01:19:49 +03:00
|
|
|
const normalizeAMBMessageEvent = e => {
|
|
|
|
let msgData = e.returnValues.encodedData
|
|
|
|
if (!e.returnValues.messageId) {
|
|
|
|
// append tx hash to an old message, where message id was not used
|
|
|
|
// for old messages, e.messageId is a corresponding transactionHash
|
|
|
|
msgData = e.transactionHash + msgData.slice(2)
|
|
|
|
}
|
|
|
|
return parseAMBMessage(msgData)
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:58:20 +03:00
|
|
|
const ambInformationSignatures = [
|
|
|
|
'eth_call(address,bytes)',
|
|
|
|
'eth_call(address,bytes,uint256)',
|
|
|
|
'eth_call(address,address,uint256,bytes)',
|
|
|
|
'eth_blockNumber()',
|
|
|
|
'eth_getBlockByNumber()',
|
|
|
|
'eth_getBlockByNumber(uint256)',
|
|
|
|
'eth_getBlockByHash(bytes32)',
|
|
|
|
'eth_getBalance(address)',
|
|
|
|
'eth_getBalance(address,uint256)',
|
|
|
|
'eth_getTransactionCount(address)',
|
|
|
|
'eth_getTransactionCount(address,uint256)',
|
|
|
|
'eth_getTransactionByHash(bytes32)',
|
|
|
|
'eth_getTransactionReceipt(bytes32)',
|
|
|
|
'eth_getStorageAt(address,bytes32)',
|
|
|
|
'eth_getStorageAt(address,bytes32,uint256)'
|
|
|
|
]
|
|
|
|
const ambInformationSelectors = Object.fromEntries(ambInformationSignatures.map(sig => [soliditySha3(sig), sig]))
|
|
|
|
const normalizeAMBInfoRequest = e => ({
|
|
|
|
messageId: e.returnValues.messageId,
|
|
|
|
sender: e.returnValues.sender,
|
|
|
|
requestSelector: ambInformationSelectors[e.returnValues.requestSelector] || 'unknown',
|
|
|
|
data: e.returnValues.data
|
|
|
|
})
|
|
|
|
|
2019-10-21 15:57:28 +03:00
|
|
|
module.exports = {
|
2020-12-20 01:19:49 +03:00
|
|
|
strip0x,
|
2019-10-21 15:57:28 +03:00
|
|
|
parseAMBMessage,
|
2021-05-26 17:58:20 +03:00
|
|
|
normalizeAMBMessageEvent,
|
|
|
|
ambInformationSignatures,
|
|
|
|
normalizeAMBInfoRequest
|
2019-10-21 15:57:28 +03:00
|
|
|
}
|