-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
48 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,18 @@ | ||
import { arrayify, keccak256 } from "ethers/lib/utils"; | ||
import { Environment } from "src/libs/types"; | ||
import { RPCInfoType } from "../constants/chain"; | ||
import { arrayify, concat, hexlify, hexZeroPad, keccak256 } from "ethers/lib/utils"; | ||
|
||
export const getCommandId = ( | ||
chainName: string, | ||
txHash: string, | ||
sourceEventIndex: number, | ||
environment: Environment, | ||
rpcInfo: RPCInfoType | ||
) => { | ||
const chainID: number = rpcInfo[environment].networkInfo[chainName.toLowerCase()]?.chainId; | ||
if (!chainID) return ""; | ||
const seiArr = arrayify(sourceEventIndex).reverse(); | ||
const txHashWithEventIndex = new Uint8Array([ | ||
...arrayify(txHash), | ||
...new Uint8Array(8).map((a, i) => seiArr[i] || a), | ||
]); | ||
const chainIdByteArray = arrayify(chainID); | ||
const dataToHash = new Uint8Array(txHashWithEventIndex.length + chainIdByteArray.length); | ||
dataToHash.set(txHashWithEventIndex, 0); | ||
dataToHash.set(chainIdByteArray, txHashWithEventIndex.length); | ||
return keccak256(dataToHash).slice(2); // remove 0x prefix | ||
const stringToCharcodeArray = (text: string) => Array.from(text, (char) => char.charCodeAt(0)); | ||
|
||
// This function should be called from evm source chain only. It doesn't work properly if it's called from cosmos-based or others chains. | ||
export const getCommandId = (messageId: string, sourceEventIndex: number, chainId: number) => { | ||
if (messageId.includes("-")) { | ||
return keccak256(concat([stringToCharcodeArray(messageId), hexlify(chainId)])); | ||
} else { | ||
return keccak256( | ||
concat([ | ||
messageId, | ||
arrayify(hexZeroPad(hexlify(sourceEventIndex), 8)).reverse(), | ||
hexlify(chainId), | ||
]) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters