You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.24;
|
|
|
|
import {SafeCast} from "./math/SafeCast.sol";
|
|
import {Bytes} from "./Bytes.sol";
|
|
import {Strings} from "./Strings.sol";
|
|
|
|
/**
|
|
* @dev Helper library to format and parse CAIP-2 identifiers
|
|
*
|
|
* https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md[CAIP-2] defines chain identifiers as:
|
|
* chain_id: namespace + ":" + reference
|
|
* namespace: [-a-z0-9]{3,8}
|
|
* reference: [-_a-zA-Z0-9]{1,32}
|
|
*/
|
|
library CAIP2 {
|
|
using SafeCast for uint256;
|
|
using Strings for uint256;
|
|
using Bytes for bytes;
|
|
|
|
/// @dev Return the CAIP-2 identifier for the current (local) chain.
|
|
function local() internal view returns (string memory) {
|
|
return format("eip155", block.chainid.toString());
|
|
}
|
|
|
|
/**
|
|
* @dev Return the CAIP-2 identifier for a given namespace and reference.
|
|
*
|
|
* NOTE: This function does not verify that the inputs are properly formatted.
|
|
*/
|
|
function format(string memory namespace, string memory ref) internal pure returns (string memory) {
|
|
return string.concat(namespace, ":", ref);
|
|
}
|
|
|
|
/**
|
|
* @dev Parse a CAIP-2 identifier into its components.
|
|
*
|
|
* NOTE: This function does not verify that the CAIP-2 input is properly formatted.
|
|
*/
|
|
function parse(string memory caip2) internal pure returns (string memory namespace, string memory ref) {
|
|
bytes memory buffer = bytes(caip2);
|
|
|
|
uint256 pos = buffer.indexOf(":");
|
|
return (string(buffer.slice(0, pos)), string(buffer.slice(pos + 1)));
|
|
}
|
|
}
|
|
|