@metamask/utils
Version:
Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase
280 lines • 11.9 kB
text/typescript
import type { Infer } from "@metamask/superstruct";
export declare const CAIP_CHAIN_ID_REGEX: RegExp;
export declare const CAIP_NAMESPACE_REGEX: RegExp;
export declare const CAIP_REFERENCE_REGEX: RegExp;
export declare const CAIP_ACCOUNT_ID_REGEX: RegExp;
export declare const CAIP_ACCOUNT_ADDRESS_REGEX: RegExp;
export declare const CAIP_ASSET_NAMESPACE_REGEX: RegExp;
export declare const CAIP_ASSET_REFERENCE_REGEX: RegExp;
export declare const CAIP_TOKEN_ID_REGEX: RegExp;
export declare const CAIP_ASSET_TYPE_REGEX: RegExp;
export declare const CAIP_ASSET_ID_REGEX: RegExp;
/**
* A CAIP-2 chain ID, i.e., a human-readable namespace and reference.
*/
export declare const CaipChainIdStruct: import("@metamask/superstruct").Struct<`${string}:${string}`, null>;
export type CaipChainId = Infer<typeof CaipChainIdStruct>;
/**
* A CAIP-2 namespace, i.e., the first part of a CAIP chain ID.
*/
export declare const CaipNamespaceStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipNamespace = Infer<typeof CaipNamespaceStruct>;
/**
* A CAIP-2 reference, i.e., the second part of a CAIP chain ID.
*/
export declare const CaipReferenceStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipReference = Infer<typeof CaipReferenceStruct>;
/**
* A CAIP-10 account ID, i.e., a human-readable namespace, reference, and account address.
*/
export declare const CaipAccountIdStruct: import("@metamask/superstruct").Struct<`${string}:${string}:${string}`, null>;
export type CaipAccountId = Infer<typeof CaipAccountIdStruct>;
/**
* A CAIP-10 account address, i.e., the third part of the CAIP account ID.
*/
export declare const CaipAccountAddressStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipAccountAddress = Infer<typeof CaipAccountAddressStruct>;
/**
* A CAIP-19 asset namespace, i.e., a namespace domain of an asset.
*/
export declare const CaipAssetNamespaceStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipAssetNamespace = Infer<typeof CaipAssetNamespaceStruct>;
/**
* A CAIP-19 asset reference, i.e., an identifier for an asset within a given namespace.
*/
export declare const CaipAssetReferenceStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipAssetReference = Infer<typeof CaipAssetReferenceStruct>;
/**
* A CAIP-19 asset token ID, i.e., a unique identifier for an addressable asset of a given type
*/
export declare const CaipTokenIdStruct: import("@metamask/superstruct").Struct<string, null>;
export type CaipTokenId = Infer<typeof CaipTokenIdStruct>;
/**
* A CAIP-19 asset type identifier, i.e., a human-readable type of asset identifier.
*/
export declare const CaipAssetTypeStruct: import("@metamask/superstruct").Struct<`${string}:${string}/${string}:${string}`, null>;
export type CaipAssetType = Infer<typeof CaipAssetTypeStruct>;
/**
* A CAIP-19 asset ID identifier, i.e., a human-readable type of asset ID.
*/
export declare const CaipAssetIdStruct: import("@metamask/superstruct").Struct<`${string}:${string}/${string}:${string}/${string}`, null>;
export type CaipAssetId = Infer<typeof CaipAssetIdStruct>;
/**
* A CAIP-19 asset type or asset ID identifier, i.e., a human-readable type of asset identifier.
*/
export declare const CaipAssetTypeOrIdStruct: import("@metamask/superstruct").Struct<`${string}:${string}/${string}:${string}` | `${string}:${string}/${string}:${string}/${string}`, null>;
export type CaipAssetTypeOrId = Infer<typeof CaipAssetTypeOrIdStruct>;
/** Known CAIP namespaces. */
export declare enum KnownCaipNamespace {
/** BIP-122 (Bitcoin) compatible chains. */
Bip122 = "bip122",
/** Solana compatible chains */
Solana = "solana",
/** EIP-155 compatible chains. */
Eip155 = "eip155",
Wallet = "wallet"
}
/**
* Check if the given value is a {@link CaipChainId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipChainId}.
*/
export declare function isCaipChainId(value: unknown): value is CaipChainId;
/**
* Check if the given value is a {@link CaipNamespace}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipNamespace}.
*/
export declare function isCaipNamespace(value: unknown): value is CaipNamespace;
/**
* Check if the given value is a {@link CaipReference}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipReference}.
*/
export declare function isCaipReference(value: unknown): value is CaipReference;
/**
* Check if the given value is a {@link CaipAccountId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAccountId}.
*/
export declare function isCaipAccountId(value: unknown): value is CaipAccountId;
/**
* Check if a value is a {@link CaipAccountAddress}.
*
* @param value - The value to validate.
* @returns True if the value is a valid {@link CaipAccountAddress}.
*/
export declare function isCaipAccountAddress(value: unknown): value is CaipAccountAddress;
/**
* Check if the given value is a {@link CaipAssetNamespace}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetNamespace}.
*/
export declare function isCaipAssetNamespace(value: unknown): value is CaipAssetNamespace;
/**
* Check if the given value is a {@link CaipAssetReference}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetReference}.
*/
export declare function isCaipAssetReference(value: unknown): value is CaipAssetReference;
/**
* Check if the given value is a {@link CaipTokenId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipTokenId}.
*/
export declare function isCaipTokenId(value: unknown): value is CaipTokenId;
/**
* Check if the given value is a {@link CaipAssetType}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetType}.
*/
export declare function isCaipAssetType(value: unknown): value is CaipAssetType;
/**
* Check if the given value is a {@link CaipAssetId}.
*
* @param value - The value to check.
* @returns Whether the value is a {@link CaipAssetId}.
*/
export declare function isCaipAssetId(value: unknown): value is CaipAssetId;
/**
* Parse a CAIP-2 chain ID to an object containing the namespace and reference.
* This validates the CAIP-2 chain ID before parsing it.
*
* @param caipChainId - The CAIP-2 chain ID to validate and parse.
* @returns The parsed CAIP-2 chain ID.
*/
export declare function parseCaipChainId(caipChainId: CaipChainId): {
namespace: CaipNamespace;
reference: CaipReference;
};
/**
* Parse an CAIP-10 account ID to an object containing the chain ID, parsed chain ID, and account address.
* This validates the CAIP-10 account ID before parsing it.
*
* @param caipAccountId - The CAIP-10 account ID to validate and parse.
* @returns The parsed CAIP-10 account ID.
*/
export declare function parseCaipAccountId(caipAccountId: CaipAccountId): {
address: CaipAccountAddress;
chainId: CaipChainId;
chain: {
namespace: CaipNamespace;
reference: CaipReference;
};
};
/**
* Parse a CAIP-19 asset type to an object containing the chain ID, parsed chain ID,
* asset namespace, and asset reference
*
* This validates the CAIP-19 asset type before parsing it.
*
* @param caipAssetType - The CAIP-19 asset type to validate and parse.
* @returns The parsed CAIP-19 asset type.
*/
export declare function parseCaipAssetType(caipAssetType: CaipAssetType): {
assetNamespace: CaipAssetNamespace;
assetReference: CaipAssetReference;
chainId: CaipChainId;
chain: {
namespace: CaipNamespace;
reference: CaipReference;
};
};
/**
* Parse a CAIP-19 asset ID to an object containing the chain ID, parsed chain ID,
* asset namespace, asset reference, and token ID.
*
* This validates the CAIP-19 asset ID before parsing it.
*
* @param caipAssetId - The CAIP-19 asset ID to validate and parse.
* @returns The parsed CAIP-19 asset ID.
*/
export declare function parseCaipAssetId(caipAssetId: CaipAssetId): {
assetNamespace: CaipAssetNamespace;
assetReference: CaipAssetReference;
tokenId: CaipTokenId;
chainId: CaipChainId;
chain: {
namespace: CaipNamespace;
reference: CaipReference;
};
};
/**
* Chain ID as defined per the CAIP-2
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md}.
*
* It defines a way to uniquely identify any blockchain in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identify of a blockchain within a given namespace.
* @throws {@link Error}
* This exception is thrown if the inputs does not comply with the CAIP-2
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-2.md#syntax}.
* @returns A CAIP chain ID.
*/
export declare function toCaipChainId(namespace: CaipNamespace, reference: CaipReference): CaipChainId;
/**
* Account ID as defined per the CAIP-10
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md}.
*
* It defines a way to uniquely identify any blockchain account in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param accountAddress - The address of the blockchain account.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-10
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-10.md#syntax}.
* @returns A CAIP account ID.
*/
export declare function toCaipAccountId(namespace: CaipNamespace, reference: CaipReference, accountAddress: CaipAccountAddress): CaipAccountId;
/**
* Asset Type as defined per the CAIP-19
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md}.
*
* It defines a way to uniquely identify any blockchain asset in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param assetNamespace - The namespace domain of an asset.
* @param assetReference - The identity of an asset within a given namespace.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-19
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#syntax}.
* @returns A CAIP asset type.
*/
export declare function toCaipAssetType(namespace: CaipNamespace, reference: CaipReference, assetNamespace: CaipAssetNamespace, assetReference: CaipAssetReference): CaipAssetType;
/**
* Asset ID as defined per the CAIP-19
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md}.
*
* It defines a way to uniquely identify any blockchain asset in a human-readable
* way.
*
* @param namespace - The standard (ecosystem) of similar blockchains.
* @param reference - Identity of a blockchain within a given namespace.
* @param assetNamespace - The namespace domain of an asset.
* @param assetReference - The identity of an asset within a given namespace.
* @param tokenId - The unique identifier for an addressable asset of a given type.
* @throws {@link Error}
* This exception is thrown if the inputs do not comply with the CAIP-19
* syntax specification
* {@link https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-19.md#syntax}.
* @returns A CAIP asset ID.
*/
export declare function toCaipAssetId(namespace: CaipNamespace, reference: CaipReference, assetNamespace: CaipAssetNamespace, assetReference: CaipAssetReference, tokenId: CaipTokenId): CaipAssetId;
//# sourceMappingURL=caip-types.d.mts.map