@hub3js/std 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- var a=Object.defineProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});function c(e){let[,t]=e.state();t("connecting",!0)}o(c,"intoConnecting");var u=[["connect",c]];function s(e){let[,t]=e.state();t("connecting",!1)}o(s,"intoConnectionFinished");var m=[["connect",s]];import{AccountId as p}from"caip";function n(e){try{return p.parse(e),!0}catch{return!1}}o(n,"isValidCaipAddress");function f(e,t){if(!t.every(n))throw new Error(`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${t}`);let[,r]=e.state();return r("accounts",t),r("connected",!0),t}o(f,"connectAndUpdateStateForSingleNetwork");function h(e,t){if(!t.accounts.every(n))throw new Error(`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${t.accounts}`);let[,r]=e.state();return r("accounts",t.accounts),r("network",t.network),r("connected",!0),t}o(h,"connectAndUpdateStateForMultiNetworks");var l=[];var d={4001:"User rejected the request.",4100:"The requested method and/or account has not been authorized by the user.",4200:"The Provider does not support the requested method.",4900:"The Provider is disconnected from all chains.",4901:"The Provider is not connected to the requested chain."};function i(e){if(e instanceof Error)throw e;let t;typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"&&e.message.length>0?t=e.message:typeof e=="object"&&e!==null&&"code"in e&&d[Number(e.code)]?t=d[Number(e.code)]:typeof e=="string"?t=e:t=String(e);let r=new Error(t);throw typeof e=="object"&&e!==null&&("code"in e&&(r.code=e.code),"data"in e&&(r.data=e.data)),r}o(i,"parseErrorAndThrowStandardizeError");function x(e,t){i(t)}o(x,"standardizeAndThrowError");export{m as afterRecommended,l as andRecommended,u as beforeRecommended,h as connectAndUpdateStateForMultiNetworks,f as connectAndUpdateStateForSingleNetwork,c as intoConnecting,s as intoConnectionFinished,x as standardizeAndThrowError};
1
+ var d=Object.defineProperty;var o=(e,t)=>d(e,"name",{value:t,configurable:!0});function c(e){let[,t]=e.state();t("connecting",!0)}o(c,"intoConnecting");var u=[["connect",c]];function s(e){let[,t]=e.state();t("connecting",!1)}o(s,"intoConnectionFinished");var p=[["connect",s]];import{AccountId as m,ChainId as S}from"caip";function n(e){try{return m.parse(e),!0}catch{return!1}}o(n,"isValidCaipAddress");function f(e,t){if(!t.every(n))throw new Error(`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${t}`);let[,r]=e.state();return r("accounts",t),r("connected",!0),t}o(f,"connectAndUpdateStateForSingleNetwork");function h(e,t){if(!t.accounts.every(n))throw new Error(`Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${t.accounts}`);let[,r]=e.state();return r("accounts",t.accounts),r("network",t.network),r("connected",!0),t}o(h,"connectAndUpdateStateForMultiNetworks");var C=[];var i={4001:"User rejected the request.",4100:"The requested method and/or account has not been authorized by the user.",4200:"The Provider does not support the requested method.",4900:"The Provider is disconnected from all chains.",4901:"The Provider is not connected to the requested chain."};function a(e){if(e instanceof Error)throw e;let t;typeof e=="object"&&e!==null&&"message"in e&&typeof e.message=="string"&&e.message.length>0?t=e.message:typeof e=="object"&&e!==null&&"code"in e&&i[Number(e.code)]?t=i[Number(e.code)]:typeof e=="string"?t=e:t=String(e);let r=new Error(t);throw typeof e=="object"&&e!==null&&("code"in e&&(r.code=e.code),"data"in e&&(r.data=e.data)),r}o(a,"parseErrorAndThrowStandardizeError");function l(e,t){a(t)}o(l,"standardizeAndThrowError");export{p as afterRecommended,C as andRecommended,u as beforeRecommended,h as connectAndUpdateStateForMultiNetworks,f as connectAndUpdateStateForSingleNetwork,c as intoConnecting,s as intoConnectionFinished,l as standardizeAndThrowError};
2
2
  //# sourceMappingURL=mod.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/operators/before.ts", "../../src/operators/after.ts", "../../src/utils/caip.ts", "../../src/operators/and.ts", "../../src/utils/error.ts", "../../src/operators/or.ts"],
4
- "sourcesContent": ["import type { Context } from '@hub3js/core';\n\nexport function intoConnecting(context: Context) {\n const [, setState] = context.state();\n setState('connecting', true);\n}\n\n/**\n * Recommended `before` hooks every namespace should wire up. Tuple shape\n * matches what `NamespaceBuilder.before(...)` expects.\n */\nexport const recommended = [['connect', intoConnecting] as const];\n", "import type { Context } from '@hub3js/core';\n\nexport function intoConnectionFinished(context: Context) {\n const [, setState] = context.state();\n setState('connecting', false);\n}\n\nexport const recommended = [['connect', intoConnectionFinished] as const];\n", "import { AccountId } from 'caip';\n\nexport function isValidCaipAddress(address: string): boolean {\n try {\n AccountId.parse(address);\n return true;\n } catch {\n return false;\n }\n}\n", "import type { Accounts, AccountsWithActiveChain } from '../types/accounts.js';\nimport type { Context } from '@hub3js/core';\n\nimport { isValidCaipAddress } from '../utils/caip.js';\n\nexport function connectAndUpdateStateForSingleNetwork(\n context: Context,\n accounts: Accounts,\n) {\n if (!accounts.every(isValidCaipAddress)) {\n throw new Error(\n `Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts}`,\n );\n }\n\n const [, setState] = context.state();\n setState('accounts', accounts);\n setState('connected', true);\n return accounts;\n}\n\nexport function connectAndUpdateStateForMultiNetworks(\n context: Context,\n accounts: AccountsWithActiveChain,\n) {\n if (!accounts.accounts.every(isValidCaipAddress)) {\n throw new Error(\n `Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts.accounts}`,\n );\n }\n\n const [, setState] = context.state();\n setState('accounts', accounts.accounts);\n setState('network', accounts.network);\n setState('connected', true);\n return accounts;\n}\n\nexport const recommended = [];\n", "const EIP_1193_ERROR_CODE_TO_MESSAGE: Record<number, string> = {\n [4001]: 'User rejected the request.',\n [4100]:\n 'The requested method and/or account has not been authorized by the user.',\n [4200]: 'The Provider does not support the requested method.',\n [4900]: 'The Provider is disconnected from all chains.',\n [4901]: 'The Provider is not connected to the requested chain.',\n};\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it.\n * If the input is already an `Error`, it's thrown directly. Otherwise a new\n * `Error` is created with the input's message or string representation,\n * preserving `code` and `data` fields when present.\n *\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function parseErrorAndThrowStandardizeError(e: unknown): never {\n if (e instanceof Error) {\n throw e;\n }\n\n let errorMessage: string;\n if (\n typeof e === 'object' &&\n e !== null &&\n 'message' in e &&\n typeof e.message === 'string' &&\n e.message.length > 0\n ) {\n errorMessage = e.message;\n } else if (\n typeof e === 'object' &&\n e !== null &&\n 'code' in e &&\n EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)]\n ) {\n errorMessage = EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)];\n } else if (typeof e === 'string') {\n errorMessage = e;\n } else {\n errorMessage = String(e);\n }\n\n const err = new Error(errorMessage) as Error & {\n code?: unknown;\n data?: unknown;\n };\n\n if (typeof e === 'object' && e !== null) {\n if ('code' in e) {\n err.code = e.code;\n }\n if ('data' in e) {\n err.data = e.data;\n }\n }\n\n throw err;\n}\n", "import type { Context } from '@hub3js/core';\n\nimport { parseErrorAndThrowStandardizeError } from '../utils/error.js';\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it. If the\n * input is already an `Error`, it's thrown directly. Otherwise a new `Error`\n * is created with the input's message or string representation.\n *\n * The actual standardization lives in `parseErrorAndThrowStandardizeError`,\n * which can be called independently outside an action context.\n *\n * @param _context - The action context (unused).\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function standardizeAndThrowError(_context: Context, e: unknown): never {\n parseErrorAndThrowStandardizeError(e);\n}\n"],
5
- "mappings": "+EAEO,SAASA,EAAeC,EAAkB,CAC/C,GAAM,CAAC,CAAEC,CAAQ,EAAID,EAAQ,MAAM,EACnCC,EAAS,aAAc,EAAI,CAC7B,CAHgBC,EAAAH,EAAA,kBAST,IAAMI,EAAc,CAAC,CAAC,UAAWJ,CAAc,CAAU,ECTzD,SAASK,EAAuBC,EAAkB,CACvD,GAAM,CAAC,CA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,IAAI,CAAC,EAE7CE,EAAeF,EAA+B,OAAO,EAAE,IAAI,CAAC,EACnD,OAAO,GAAM,SACtBE,EAAe,EAEfA,EAAe,OAAO,CAAC,EAGzB,IAAMC,EAAM,IAAI,MAAMD,CAAY,EAKlC,MAAI,OAAO,GAAM,UAAY,IAAM,OAC7B,SAAU,IACZC,EAAI,KAAO,EAAE,MAEX,SAAU,IACZA,EAAI,KAAO,EAAE,OAIXA,CACR,CA1CgBC,EAAAH,EAAA,sCCFT,SAASI,EAAyBC,EAAmBC,EAAmB,CAC7EC,EAAmCD,CAAC,CACtC,CAFgBE,EAAAJ,EAAA",
6
- "names": ["intoConnecting", "context", "setState", "__name", "recommended", "intoConnectionFinished", "context", "setState", "__name", "recommended", "AccountId", "isValidCaipAddress", "address", "AccountId", "__name", "connectAndUpdateStateForSingleNetwork", "context", "accounts", "isValidCaipAddress", "setState", "__name", "connectAndUpdateStateForMultiNetworks", "recommended", "EIP_1193_ERROR_CODE_TO_MESSAGE", "parseErrorAndThrowStandardizeError", "errorMessage", "err", "__name", "standardizeAndThrowError", "_context", "e", "parseErrorAndThrowStandardizeError", "__name"]
4
+ "sourcesContent": ["import type { Context } from '@hub3js/core';\n\nexport function intoConnecting(context: Context) {\n const [, setState] = context.state();\n setState('connecting', true);\n}\n\n/**\n * Recommended `before` hooks every namespace should wire up. Tuple shape\n * matches what `NamespaceBuilder.before(...)` expects.\n */\nexport const recommended = [['connect', intoConnecting] as const];\n", "import type { Context } from '@hub3js/core';\n\nexport function intoConnectionFinished(context: Context) {\n const [, setState] = context.state();\n setState('connecting', false);\n}\n\nexport const recommended = [['connect', intoConnectionFinished] as const];\n", "import { AccountId, ChainId, type ChainIdParams } from 'caip';\n\nexport function isValidCaipAddress(address: string): boolean {\n try {\n AccountId.parse(address);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function parseCaip2ChainId(caip2ChainId: string): ChainIdParams | null {\n try {\n return ChainId.parse(caip2ChainId);\n } catch {\n return null;\n }\n}\n\n/** `eip155` of `eip155:1`. `''` when malformed, so callers can compare without null-checks. */\nexport function getNamespaceFromCaip2ChainId(caip2ChainId: string): string {\n return parseCaip2ChainId(caip2ChainId)?.namespace ?? '';\n}\n\n/** `1` of `eip155:1` \u2014 the CAIP reference. `''` when malformed. */\nexport function getChainIdFromCaip2ChainId(caip2ChainId: string): string {\n return parseCaip2ChainId(caip2ChainId)?.reference ?? '';\n}\n", "import type { Accounts, AccountsWithActiveChain } from '../types/accounts.js';\nimport type { Context } from '@hub3js/core';\n\nimport { isValidCaipAddress } from '../utils/caip.js';\n\nexport function connectAndUpdateStateForSingleNetwork(\n context: Context,\n accounts: Accounts,\n) {\n if (!accounts.every(isValidCaipAddress)) {\n throw new Error(\n `Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts}`,\n );\n }\n\n const [, setState] = context.state();\n setState('accounts', accounts);\n setState('connected', true);\n return accounts;\n}\n\nexport function connectAndUpdateStateForMultiNetworks(\n context: Context,\n accounts: AccountsWithActiveChain,\n) {\n if (!accounts.accounts.every(isValidCaipAddress)) {\n throw new Error(\n `Your provider should format account addresses in CAIP-10 format. Your provided accounts: ${accounts.accounts}`,\n );\n }\n\n const [, setState] = context.state();\n setState('accounts', accounts.accounts);\n setState('network', accounts.network);\n setState('connected', true);\n return accounts;\n}\n\nexport const recommended = [];\n", "const EIP_1193_ERROR_CODE_TO_MESSAGE: Record<number, string> = {\n [4001]: 'User rejected the request.',\n [4100]:\n 'The requested method and/or account has not been authorized by the user.',\n [4200]: 'The Provider does not support the requested method.',\n [4900]: 'The Provider is disconnected from all chains.',\n [4901]: 'The Provider is not connected to the requested chain.',\n};\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it.\n * If the input is already an `Error`, it's thrown directly. Otherwise a new\n * `Error` is created with the input's message or string representation,\n * preserving `code` and `data` fields when present.\n *\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function parseErrorAndThrowStandardizeError(e: unknown): never {\n if (e instanceof Error) {\n throw e;\n }\n\n let errorMessage: string;\n if (\n typeof e === 'object' &&\n e !== null &&\n 'message' in e &&\n typeof e.message === 'string' &&\n e.message.length > 0\n ) {\n errorMessage = e.message;\n } else if (\n typeof e === 'object' &&\n e !== null &&\n 'code' in e &&\n EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)]\n ) {\n errorMessage = EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)];\n } else if (typeof e === 'string') {\n errorMessage = e;\n } else {\n errorMessage = String(e);\n }\n\n const err = new Error(errorMessage) as Error & {\n code?: unknown;\n data?: unknown;\n };\n\n if (typeof e === 'object' && e !== null) {\n if ('code' in e) {\n err.code = e.code;\n }\n if ('data' in e) {\n err.data = e.data;\n }\n }\n\n throw err;\n}\n", "import type { Context } from '@hub3js/core';\n\nimport { parseErrorAndThrowStandardizeError } from '../utils/error.js';\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it. If the\n * input is already an `Error`, it's thrown directly. Otherwise a new `Error`\n * is created with the input's message or string representation.\n *\n * The actual standardization lives in `parseErrorAndThrowStandardizeError`,\n * which can be called independently outside an action context.\n *\n * @param _context - The action context (unused).\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function standardizeAndThrowError(_context: Context, e: unknown): never {\n parseErrorAndThrowStandardizeError(e);\n}\n"],
5
+ "mappings": "+EAEO,SAASA,EAAeC,EAAkB,CAC/C,GAAM,CAAC,CAAEC,CAAQ,EAAID,EAAQ,MAAM,EACnCC,EAAS,aAAc,EAAI,CAC7B,CAHgBC,EAAAH,EAAA,kBAST,IAAMI,EAAc,CAAC,CAAC,UAAWJ,CAAc,CAAU,ECTzD,SAASK,EAAuBC,EAAkB,CACvD,GAAM,CAAC,CAAEC,CAAQ,EAAID,EAAQ,MAAM,EACnCC,EAAS,aAAc,EAAK,CAC9B,CAHgBC,EAAAH,EAAA,0BAKT,IAAMI,EAAc,CAAC,CAAC,UAAWJ,CAAsB,CAAU,ECPxE,OAAS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AAI,MAAMD,CAAY,EAKlC,MAAI,OAAO,GAAM,UAAY,IAAM,OAC7B,SAAU,IACZC,EAAI,KAAO,EAAE,MAEX,SAAU,IACZA,EAAI,KAAO,EAAE,OAIXA,CACR,CA1CgBC,EAAAH,EAAA,sCCFT,SAASI,EAAyBC,EAAmBC,EAAmB,CAC7EC,EAAmCD,CAAC,CACtC,CAFgBE,EAAAJ,EAAA",
6
+ "names": ["intoConnecting", "context", "setState", "__name", "recommended", "intoConnectionFinished", "context", "setState", "__name", "recommended", "AccountId", "ChainId", "isValidCaipAddress", "address", "AccountId", "__name", "connectAndUpdateStateForSingleNetwork", "context", "accounts", "isValidCaipAddress", "setState", "__name", "connectAndUpdateStateForMultiNetworks", "recommended", "EIP_1193_ERROR_CODE_TO_MESSAGE", "parseErrorAndThrowStandardizeError", "errorMessage", "err", "__name", "standardizeAndThrowError", "_context", "e", "parseErrorAndThrowStandardizeError", "__name"]
7
7
  }
@@ -1,2 +1,8 @@
1
+ import { type ChainIdParams } from 'caip';
1
2
  export declare function isValidCaipAddress(address: string): boolean;
3
+ export declare function parseCaip2ChainId(caip2ChainId: string): ChainIdParams | null;
4
+ /** `eip155` of `eip155:1`. `''` when malformed, so callers can compare without null-checks. */
5
+ export declare function getNamespaceFromCaip2ChainId(caip2ChainId: string): string;
6
+ /** `1` of `eip155:1` — the CAIP reference. `''` when malformed. */
7
+ export declare function getChainIdFromCaip2ChainId(caip2ChainId: string): string;
2
8
  //# sourceMappingURL=caip.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"caip.d.ts","sourceRoot":"","sources":["../../src/utils/caip.ts"],"names":[],"mappings":"AAEA,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAO3D"}
1
+ {"version":3,"file":"caip.d.ts","sourceRoot":"","sources":["../../src/utils/caip.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,MAAM,CAAC;AAE9D,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAO3D;AAED,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAM5E;AAED,+FAA+F;AAC/F,wBAAgB,4BAA4B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED,mEAAmE;AACnE,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEvE"}
@@ -1,3 +1,3 @@
1
- export { isValidCaipAddress } from './caip.js';
1
+ export { getChainIdFromCaip2ChainId, getNamespaceFromCaip2ChainId, isValidCaipAddress, parseCaip2ChainId, } from './caip.js';
2
2
  export { parseErrorAndThrowStandardizeError } from './error.js';
3
3
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/utils/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,kCAAkC,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/utils/mod.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAC5B,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,kCAAkC,EAAE,MAAM,YAAY,CAAC"}
package/dist/utils/mod.js CHANGED
@@ -1,2 +1,2 @@
1
- var s=Object.defineProperty;var o=(r,e)=>s(r,"name",{value:e,configurable:!0});import{AccountId as d}from"caip";function i(r){try{return d.parse(r),!0}catch{return!1}}o(i,"isValidCaipAddress");var n={4001:"User rejected the request.",4100:"The requested method and/or account has not been authorized by the user.",4200:"The Provider does not support the requested method.",4900:"The Provider is disconnected from all chains.",4901:"The Provider is not connected to the requested chain."};function a(r){if(r instanceof Error)throw r;let e;typeof r=="object"&&r!==null&&"message"in r&&typeof r.message=="string"&&r.message.length>0?e=r.message:typeof r=="object"&&r!==null&&"code"in r&&n[Number(r.code)]?e=n[Number(r.code)]:typeof r=="string"?e=r:e=String(r);let t=new Error(e);throw typeof r=="object"&&r!==null&&("code"in r&&(t.code=r.code),"data"in r&&(t.data=r.data)),t}o(a,"parseErrorAndThrowStandardizeError");export{i as isValidCaipAddress,a as parseErrorAndThrowStandardizeError};
1
+ var i=Object.defineProperty;var n=(r,e)=>i(r,"name",{value:e,configurable:!0});import{AccountId as s,ChainId as d}from"caip";function c(r){try{return s.parse(r),!0}catch{return!1}}n(c,"isValidCaipAddress");function o(r){try{return d.parse(r)}catch{return null}}n(o,"parseCaip2ChainId");function p(r){return o(r)?.namespace??""}n(p,"getNamespaceFromCaip2ChainId");function h(r){return o(r)?.reference??""}n(h,"getChainIdFromCaip2ChainId");var a={4001:"User rejected the request.",4100:"The requested method and/or account has not been authorized by the user.",4200:"The Provider does not support the requested method.",4900:"The Provider is disconnected from all chains.",4901:"The Provider is not connected to the requested chain."};function u(r){if(r instanceof Error)throw r;let e;typeof r=="object"&&r!==null&&"message"in r&&typeof r.message=="string"&&r.message.length>0?e=r.message:typeof r=="object"&&r!==null&&"code"in r&&a[Number(r.code)]?e=a[Number(r.code)]:typeof r=="string"?e=r:e=String(r);let t=new Error(e);throw typeof r=="object"&&r!==null&&("code"in r&&(t.code=r.code),"data"in r&&(t.data=r.data)),t}n(u,"parseErrorAndThrowStandardizeError");export{h as getChainIdFromCaip2ChainId,p as getNamespaceFromCaip2ChainId,c as isValidCaipAddress,o as parseCaip2ChainId,u as parseErrorAndThrowStandardizeError};
2
2
  //# sourceMappingURL=mod.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/utils/caip.ts", "../../src/utils/error.ts"],
4
- "sourcesContent": ["import { AccountId } from 'caip';\n\nexport function isValidCaipAddress(address: string): boolean {\n try {\n AccountId.parse(address);\n return true;\n } catch {\n return false;\n }\n}\n", "const EIP_1193_ERROR_CODE_TO_MESSAGE: Record<number, string> = {\n [4001]: 'User rejected the request.',\n [4100]:\n 'The requested method and/or account has not been authorized by the user.',\n [4200]: 'The Provider does not support the requested method.',\n [4900]: 'The Provider is disconnected from all chains.',\n [4901]: 'The Provider is not connected to the requested chain.',\n};\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it.\n * If the input is already an `Error`, it's thrown directly. Otherwise a new\n * `Error` is created with the input's message or string representation,\n * preserving `code` and `data` fields when present.\n *\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function parseErrorAndThrowStandardizeError(e: unknown): never {\n if (e instanceof Error) {\n throw e;\n }\n\n let errorMessage: string;\n if (\n typeof e === 'object' &&\n e !== null &&\n 'message' in e &&\n typeof e.message === 'string' &&\n e.message.length > 0\n ) {\n errorMessage = e.message;\n } else if (\n typeof e === 'object' &&\n e !== null &&\n 'code' in e &&\n EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)]\n ) {\n errorMessage = EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)];\n } else if (typeof e === 'string') {\n errorMessage = e;\n } else {\n errorMessage = String(e);\n }\n\n const err = new Error(errorMessage) as Error & {\n code?: unknown;\n data?: unknown;\n };\n\n if (typeof e === 'object' && e !== null) {\n if ('code' in e) {\n err.code = e.code;\n }\n if ('data' in e) {\n err.data = e.data;\n }\n }\n\n throw err;\n}\n"],
5
- "mappings": "+EAAA,OAAS,aAAAA,MAAiB,OAEnB,SAASC,EAAmBC,EAA0B,CAC3D,GAAI,CACF,OAAAC,EAAU,MAAMD,CAAO,EAChB,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAPgBE,EAAAH,EAAA,sBCFhB,IAAMI,EAAyD,CAC5D,KAAO,6BACP,KACC,2EACD,KAAO,sDACP,KAAO,gDACP,KAAO,uDACV,EAWO,SAASC,EAAmCC,EAAmB,CACpE,GAAIA,aAAa,MACf,MAAMA,EAGR,IAAIC,EAEF,OAAOD,GAAM,UACbA,IAAM,MACN,YAAaA,GACb,OAAOA,EAAE,SAAY,UACrBA,EAAE,QAAQ,OAAS,EAEnBC,EAAeD,EAAE,QAEjB,OAAOA,GAAM,UACbA,IAAM,MACN,SAAUA,GACVF,EAA+B,OAAOE,EAAE,IAAI,CAAC,EAE7CC,EAAeH,EAA+B,OAAOE,EAAE,IAAI,CAAC,EACnD,OAAOA,GAAM,SACtBC,EAAeD,EAEfC,EAAe,OAAOD,CAAC,EAGzB,IAAME,EAAM,IAAI,MAAMD,CAAY,EAKlC,MAAI,OAAOD,GAAM,UAAYA,IAAM,OAC7B,SAAUA,IACZE,EAAI,KAAOF,EAAE,MAEX,SAAUA,IACZE,EAAI,KAAOF,EAAE,OAIXE,CACR,CA1CgBC,EAAAJ,EAAA",
6
- "names": ["AccountId", "isValidCaipAddress", "address", "AccountId", "__name", "EIP_1193_ERROR_CODE_TO_MESSAGE", "parseErrorAndThrowStandardizeError", "e", "errorMessage", "err", "__name"]
4
+ "sourcesContent": ["import { AccountId, ChainId, type ChainIdParams } from 'caip';\n\nexport function isValidCaipAddress(address: string): boolean {\n try {\n AccountId.parse(address);\n return true;\n } catch {\n return false;\n }\n}\n\nexport function parseCaip2ChainId(caip2ChainId: string): ChainIdParams | null {\n try {\n return ChainId.parse(caip2ChainId);\n } catch {\n return null;\n }\n}\n\n/** `eip155` of `eip155:1`. `''` when malformed, so callers can compare without null-checks. */\nexport function getNamespaceFromCaip2ChainId(caip2ChainId: string): string {\n return parseCaip2ChainId(caip2ChainId)?.namespace ?? '';\n}\n\n/** `1` of `eip155:1` \u2014 the CAIP reference. `''` when malformed. */\nexport function getChainIdFromCaip2ChainId(caip2ChainId: string): string {\n return parseCaip2ChainId(caip2ChainId)?.reference ?? '';\n}\n", "const EIP_1193_ERROR_CODE_TO_MESSAGE: Record<number, string> = {\n [4001]: 'User rejected the request.',\n [4100]:\n 'The requested method and/or account has not been authorized by the user.',\n [4200]: 'The Provider does not support the requested method.',\n [4900]: 'The Provider is disconnected from all chains.',\n [4901]: 'The Provider is not connected to the requested chain.',\n};\n\n/**\n * Standardizes an unknown error into an `Error` object and throws it.\n * If the input is already an `Error`, it's thrown directly. Otherwise a new\n * `Error` is created with the input's message or string representation,\n * preserving `code` and `data` fields when present.\n *\n * @param e - The unknown error object.\n * @throws {Error} - The standardized `Error` object.\n */\nexport function parseErrorAndThrowStandardizeError(e: unknown): never {\n if (e instanceof Error) {\n throw e;\n }\n\n let errorMessage: string;\n if (\n typeof e === 'object' &&\n e !== null &&\n 'message' in e &&\n typeof e.message === 'string' &&\n e.message.length > 0\n ) {\n errorMessage = e.message;\n } else if (\n typeof e === 'object' &&\n e !== null &&\n 'code' in e &&\n EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)]\n ) {\n errorMessage = EIP_1193_ERROR_CODE_TO_MESSAGE[Number(e.code)];\n } else if (typeof e === 'string') {\n errorMessage = e;\n } else {\n errorMessage = String(e);\n }\n\n const err = new Error(errorMessage) as Error & {\n code?: unknown;\n data?: unknown;\n };\n\n if (typeof e === 'object' && e !== null) {\n if ('code' in e) {\n err.code = e.code;\n }\n if ('data' in e) {\n err.data = e.data;\n }\n }\n\n throw err;\n}\n"],
5
+ "mappings": "+EAAA,OAAS,aAAAA,EAAW,WAAAC,MAAmC,OAEhD,SAASC,EAAmBC,EAA0B,CAC3D,GAAI,CACF,OAAAC,EAAU,MAAMD,CAAO,EAChB,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAPgBE,EAAAH,EAAA,sBAST,SAASI,EAAkBC,EAA4C,CAC5E,GAAI,CACF,OAAOC,EAAQ,MAAMD,CAAY,CACnC,MAAQ,CACN,OAAO,IACT,CACF,CANgBF,EAAAC,EAAA,qBAST,SAASG,EAA6BF,EAA8B,CACzE,OAAOD,EAAkBC,CAAY,GAAG,WAAa,EACvD,CAFgBF,EAAAI,EAAA,gCAKT,SAASC,EAA2BH,EAA8B,CACvE,OAAOD,EAAkBC,CAAY,GAAG,WAAa,EACvD,CAFgBF,EAAAK,EAAA,8BCzBhB,IAAMC,EAAyD,CAC5D,KAAO,6BACP,KACC,2EACD,KAAO,sDACP,KAAO,gDACP,KAAO,uDACV,EAWO,SAASC,EAAmCC,EAAmB,CACpE,GAAIA,aAAa,MACf,MAAMA,EAGR,IAAIC,EAEF,OAAOD,GAAM,UACbA,IAAM,MACN,YAAaA,GACb,OAAOA,EAAE,SAAY,UACrBA,EAAE,QAAQ,OAAS,EAEnBC,EAAeD,EAAE,QAEjB,OAAOA,GAAM,UACbA,IAAM,MACN,SAAUA,GACVF,EAA+B,OAAOE,EAAE,IAAI,CAAC,EAE7CC,EAAeH,EAA+B,OAAOE,EAAE,IAAI,CAAC,EACnD,OAAOA,GAAM,SACtBC,EAAeD,EAEfC,EAAe,OAAOD,CAAC,EAGzB,IAAME,EAAM,IAAI,MAAMD,CAAY,EAKlC,MAAI,OAAOD,GAAM,UAAYA,IAAM,OAC7B,SAAUA,IACZE,EAAI,KAAOF,EAAE,MAEX,SAAUA,IACZE,EAAI,KAAOF,EAAE,OAIXE,CACR,CA1CgBC,EAAAJ,EAAA",
6
+ "names": ["AccountId", "ChainId", "isValidCaipAddress", "address", "AccountId", "__name", "parseCaip2ChainId", "caip2ChainId", "ChainId", "getNamespaceFromCaip2ChainId", "getChainIdFromCaip2ChainId", "EIP_1193_ERROR_CODE_TO_MESSAGE", "parseErrorAndThrowStandardizeError", "e", "errorMessage", "err", "__name"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hub3js/std",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -0,0 +1,44 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import {
4
+ getChainIdFromCaip2ChainId,
5
+ getNamespaceFromCaip2ChainId,
6
+ parseCaip2ChainId,
7
+ } from './caip.js';
8
+
9
+ describe('CAIP-2 chain id parsing', () => {
10
+ it('splits a well-formed id into namespace and reference', () => {
11
+ expect(parseCaip2ChainId('eip155:1')).toEqual({
12
+ namespace: 'eip155',
13
+ reference: '1',
14
+ });
15
+ expect(
16
+ getNamespaceFromCaip2ChainId('bip122:000000000019d6689c085ae165831e93'),
17
+ ).toBe('bip122');
18
+ expect(getChainIdFromCaip2ChainId('eip155:43114')).toBe('43114');
19
+ });
20
+
21
+ /*
22
+ * These accessors are what namespace predicates run on, so a malformed id must match
23
+ * nothing. A lenient split would make a bare 'eip155' match every EVM wallet.
24
+ */
25
+ it.each([
26
+ ['a bare namespace', 'eip155'],
27
+ ['a namespace with an empty reference', 'eip155:'],
28
+ ['an empty reference and namespace', ':'],
29
+ ['a missing namespace', ':1'],
30
+ ['extra segments', 'solana:mainnet:junk'],
31
+ ['an empty string', ''],
32
+ ])('rejects %s', (_, malformed) => {
33
+ expect(parseCaip2ChainId(malformed)).toBeNull();
34
+ expect(getNamespaceFromCaip2ChainId(malformed)).toBe('');
35
+ expect(getChainIdFromCaip2ChainId(malformed)).toBe('');
36
+ });
37
+
38
+ it('never lets a malformed id match a real namespace or reference', () => {
39
+ expect(getNamespaceFromCaip2ChainId('eip155')).not.toBe('eip155');
40
+ expect(getChainIdFromCaip2ChainId('solana:mainnet:junk')).not.toBe(
41
+ 'mainnet',
42
+ );
43
+ });
44
+ });
package/src/utils/caip.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AccountId } from 'caip';
1
+ import { AccountId, ChainId, type ChainIdParams } from 'caip';
2
2
 
3
3
  export function isValidCaipAddress(address: string): boolean {
4
4
  try {
@@ -8,3 +8,21 @@ export function isValidCaipAddress(address: string): boolean {
8
8
  return false;
9
9
  }
10
10
  }
11
+
12
+ export function parseCaip2ChainId(caip2ChainId: string): ChainIdParams | null {
13
+ try {
14
+ return ChainId.parse(caip2ChainId);
15
+ } catch {
16
+ return null;
17
+ }
18
+ }
19
+
20
+ /** `eip155` of `eip155:1`. `''` when malformed, so callers can compare without null-checks. */
21
+ export function getNamespaceFromCaip2ChainId(caip2ChainId: string): string {
22
+ return parseCaip2ChainId(caip2ChainId)?.namespace ?? '';
23
+ }
24
+
25
+ /** `1` of `eip155:1` — the CAIP reference. `''` when malformed. */
26
+ export function getChainIdFromCaip2ChainId(caip2ChainId: string): string {
27
+ return parseCaip2ChainId(caip2ChainId)?.reference ?? '';
28
+ }
package/src/utils/mod.ts CHANGED
@@ -1,2 +1,7 @@
1
- export { isValidCaipAddress } from './caip.js';
1
+ export {
2
+ getChainIdFromCaip2ChainId,
3
+ getNamespaceFromCaip2ChainId,
4
+ isValidCaipAddress,
5
+ parseCaip2ChainId,
6
+ } from './caip.js';
2
7
  export { parseErrorAndThrowStandardizeError } from './error.js';