@tuwaio/siwx-core 1.0.0-fix-initialize-alpha.10.167aa43 → 1.0.0-fix-initialize-alpha.12.626c94c

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.
package/README.md CHANGED
@@ -75,6 +75,17 @@ if (!valid) console.error(errors);
75
75
  const { valid: isValidExpired } = validateMessage(parsedMessage, { skipExpiration: true });
76
76
  ```
77
77
 
78
+ ### `isSessionMatchingTarget(session: SiwxSessionLike | null | undefined, targetAddress: string, targetChainId?: string | number): boolean`
79
+
80
+ Validates whether a SIWX session matches a target wallet address and optional chainId. Handles EVM case-insensitivity, Solana case-sensitivity, and CAIP-10/CAIP-2 normalization.
81
+
82
+ ```ts
83
+ import { isSessionMatchingTarget } from '@tuwaio/siwx-core';
84
+
85
+ const isMatch = isSessionMatchingTarget(session, '0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B', 1);
86
+ // Handles cross-chain namespace checks (EVM vs Solana) and optional chainId matching
87
+ ```
88
+
78
89
  ### `generateNonce(): string`
79
90
 
80
91
  Generates a 32-character cryptographically secure hex nonce using the Web Crypto API (`globalThis.crypto.getRandomValues`). Native in Node 20+, browsers, and Edge runtimes.
package/dist/index.d.mts CHANGED
@@ -298,5 +298,27 @@ declare function validateMessage(fields: SiwxMessageFields, options?: {
298
298
  * ```
299
299
  */
300
300
  declare function generateNonce(): string;
301
+ /**
302
+ * Minimal interface for a SIWX session or parsed CAIP-122 message.
303
+ */
304
+ interface SiwxSessionLike {
305
+ address: string;
306
+ chainId?: string;
307
+ }
308
+ /**
309
+ * Validates whether a SIWX session matches a target wallet address and optional chainId.
310
+ * Handles EVM case-insensitivity, Solana case-sensitivity, and CAIP-10/CAIP-2 normalization.
311
+ *
312
+ * @param session - Active SIWX session or parsed message
313
+ * @param targetAddress - Target account address (plain or CAIP-10)
314
+ * @param targetChainId - Target chain reference or CAIP-2 identifier
315
+ * @returns True if the session matches the target address and chainId; false otherwise.
316
+ *
317
+ * @example
318
+ * ```ts
319
+ * const isValid = isSessionMatchingTarget(session, '0x123...', 1);
320
+ * ```
321
+ */
322
+ declare function isSessionMatchingTarget(session: SiwxSessionLike | null | undefined, targetAddress: string, targetChainId?: string | number): boolean;
301
323
 
302
- export { type ParsedSiwxMessage, type SiwxAdapter, type SiwxChainId, type SiwxChainNamespace, SiwxError, SiwxExpiredSessionError, type SiwxMessageFields, SiwxNonceReplayError, SiwxParseError, type SiwxStatus, SiwxUnsupportedNamespaceError, SiwxValidationError, type SiwxValidationResult, SiwxVerificationError, type SiwxVerifyPayload, type SiwxVerifyResult, buildMessage, generateNonce, parseMessage, validateMessage };
324
+ export { type ParsedSiwxMessage, type SiwxAdapter, type SiwxChainId, type SiwxChainNamespace, SiwxError, SiwxExpiredSessionError, type SiwxMessageFields, SiwxNonceReplayError, SiwxParseError, type SiwxSessionLike, type SiwxStatus, SiwxUnsupportedNamespaceError, SiwxValidationError, type SiwxValidationResult, SiwxVerificationError, type SiwxVerifyPayload, type SiwxVerifyResult, buildMessage, generateNonce, isSessionMatchingTarget, parseMessage, validateMessage };
package/dist/index.d.ts CHANGED
@@ -298,5 +298,27 @@ declare function validateMessage(fields: SiwxMessageFields, options?: {
298
298
  * ```
299
299
  */
300
300
  declare function generateNonce(): string;
301
+ /**
302
+ * Minimal interface for a SIWX session or parsed CAIP-122 message.
303
+ */
304
+ interface SiwxSessionLike {
305
+ address: string;
306
+ chainId?: string;
307
+ }
308
+ /**
309
+ * Validates whether a SIWX session matches a target wallet address and optional chainId.
310
+ * Handles EVM case-insensitivity, Solana case-sensitivity, and CAIP-10/CAIP-2 normalization.
311
+ *
312
+ * @param session - Active SIWX session or parsed message
313
+ * @param targetAddress - Target account address (plain or CAIP-10)
314
+ * @param targetChainId - Target chain reference or CAIP-2 identifier
315
+ * @returns True if the session matches the target address and chainId; false otherwise.
316
+ *
317
+ * @example
318
+ * ```ts
319
+ * const isValid = isSessionMatchingTarget(session, '0x123...', 1);
320
+ * ```
321
+ */
322
+ declare function isSessionMatchingTarget(session: SiwxSessionLike | null | undefined, targetAddress: string, targetChainId?: string | number): boolean;
301
323
 
302
- export { type ParsedSiwxMessage, type SiwxAdapter, type SiwxChainId, type SiwxChainNamespace, SiwxError, SiwxExpiredSessionError, type SiwxMessageFields, SiwxNonceReplayError, SiwxParseError, type SiwxStatus, SiwxUnsupportedNamespaceError, SiwxValidationError, type SiwxValidationResult, SiwxVerificationError, type SiwxVerifyPayload, type SiwxVerifyResult, buildMessage, generateNonce, parseMessage, validateMessage };
324
+ export { type ParsedSiwxMessage, type SiwxAdapter, type SiwxChainId, type SiwxChainNamespace, SiwxError, SiwxExpiredSessionError, type SiwxMessageFields, SiwxNonceReplayError, SiwxParseError, type SiwxSessionLike, type SiwxStatus, SiwxUnsupportedNamespaceError, SiwxValidationError, type SiwxValidationResult, SiwxVerificationError, type SiwxVerifyPayload, type SiwxVerifyResult, buildMessage, generateNonce, isSessionMatchingTarget, parseMessage, validateMessage };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- 'use strict';function A(e){let{domain:t,address:n,statement:c,uri:l,version:r,chainId:g,nonce:s,issuedAt:p,expirationTime:i,notBefore:h,requestId:a,resources:f}=e,o=[`${t} wants you to sign in with your blockchain account:`,n,""];if(c&&o.push(c,""),o.push(`URI: ${l}`,`Version: ${r}`,`Chain ID: ${g}`,`Nonce: ${s}`,`Issued At: ${p}`),i&&o.push(`Expiration Time: ${i}`),h&&o.push(`Not Before: ${h}`),a&&o.push(`Request ID: ${a}`),f&&f.length>0){o.push("Resources:");for(let y of f)o.push(`- ${y}`);}return o.join(`
2
- `)}var d=class extends Error{constructor(n,c="SIWX_ERROR"){super(n);this.code=c;this.name="SiwxError",Object.setPrototypeOf(this,new.target.prototype);}code},u=class extends d{constructor(t){super(t,"SIWX_PARSE_ERROR"),this.name="SiwxParseError";}},m=class extends d{constructor(n){super(`CAIP-122 message validation failed: ${n.join(", ")}`,"SIWX_VALIDATION_ERROR");this.errors=n;this.name="SiwxValidationError";}errors},x=class extends d{constructor(t){super(t,"SIWX_VERIFICATION_ERROR"),this.name="SiwxVerificationError";}},E=class extends d{constructor(n){super(`SIWX session expired at ${n}`,"SIWX_EXPIRED_SESSION");this.expirationTime=n;this.name="SiwxExpiredSessionError";}expirationTime},w=class extends d{constructor(n){super(`Nonce has already been used: ${n}`,"SIWX_NONCE_REPLAY");this.nonce=n;this.name="SiwxNonceReplayError";}nonce},I=class extends d{constructor(n){super(`Unsupported CAIP-2 namespace: "${n}". Supported: eip155, solana`,"SIWX_UNSUPPORTED_NAMESPACE");this.namespace=n;this.name="SiwxUnsupportedNamespaceError";}namespace};var $=/^(?<domain>.+) wants you to sign in with your blockchain account:$/,R=/^(?<key>[A-Za-z ]+): (?<value>.+)$/,b=/^- (?<resource>.+)$/;function v(e){let t=e.split(`
3
- `);if(t.length<5)throw new u("Message is too short to be a valid CAIP-122 message.");let n=t[0]?.match($);if(!n?.groups?.domain)throw new u('Invalid CAIP-122 header line. Expected: "{domain} wants you to sign in with your blockchain account:"');let c=n.groups.domain,l=t[1];if(!l)throw new u("Missing address on line 2.");if(t[2]!=="")throw new u("Expected blank line after address (line 3).");let r=3,g;if(t[r]&&!R.test(t[r])){if(g=t[r],r++,t[r]!=="")throw new u("Expected blank line after statement.");r++;}let s={},p=[],i=false;for(;r<t.length;){let a=t[r];if(a===void 0){r++;continue}if(a==="Resources:"){i=true,r++;continue}if(i){let o=a.match(b);if(o?.groups?.resource){p.push(o.groups.resource),r++;continue}i=false;}let f=a.match(R);f?.groups?.key&&f.groups.value&&(s[f.groups.key]=f.groups.value),r++;}let h=["URI","Version","Chain ID","Nonce","Issued At"];for(let a of h)if(!s[a])throw new u(`Missing required field in CAIP-122 message: "${a}"`);return {domain:c,address:l,statement:g,uri:s.URI,version:s.Version,chainId:s["Chain ID"],nonce:s.Nonce,issuedAt:s["Issued At"],expirationTime:s["Expiration Time"],notBefore:s["Not Before"],requestId:s["Request ID"],resources:p.length>0?p:void 0}}var _=/^https?:\/\/.+/,C=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,M=/^[a-z0-9]+:[a-zA-Z0-9-]+:.+$/,P=/^[a-z0-9]+:[a-zA-Z0-9-]+$/;function N(e){if(!e||e.trim().length===0)return "domain must be a non-empty string.";if(e.includes(`
4
- `))return "domain must not contain newline characters."}function X(e){if(!M.test(e))return `address must be CAIP-10 compliant (namespace:chainRef:address). Got: "${e}"`}function D(e){if(!_.test(e))return `uri must be a valid RFC 3986 URI starting with http(s)://. Got: "${e}"`}function G(e){if(!P.test(e))return `chainId must be a valid CAIP-2 identifier (namespace:reference). Got: "${e}"`}function O(e){if(!e||e.length<8)return `nonce must be at least 8 characters long. Got length: ${e?.length??0}`;if(!/^[a-zA-Z0-9]+$/.test(e))return `nonce must be alphanumeric. Got: "${e}"`}function S(e,t){if(!C.test(e))return `${t} must be a valid ISO 8601 datetime string. Got: "${e}"`}function T(e){let t=new Date(e);if(isNaN(t.getTime()))return `expirationTime is not a valid date: "${e}"`;if(t<new Date)return `Message has expired. expirationTime was: ${e}`}function V(e,t){let n=[],c=N(e.domain);c&&n.push(c);let l=X(e.address);l&&n.push(l);let r=D(e.uri);r&&n.push(r),e.version!=="1"&&n.push(`version must be "1". Got: "${e.version}"`);let g=G(e.chainId);g&&n.push(g);let s=O(e.nonce);s&&n.push(s);let p=S(e.issuedAt,"issuedAt");if(p&&n.push(p),e.expirationTime){let i=S(e.expirationTime,"expirationTime");if(i)n.push(i);else if(!t?.skipExpiration){let h=T(e.expirationTime);h&&n.push(h);}}if(e.notBefore){let i=S(e.notBefore,"notBefore");i&&n.push(i);}return e.statement&&e.statement.includes(`
5
- `)&&n.push("statement must not contain newline characters."),{valid:n.length===0,errors:n}}function U(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}exports.SiwxError=d;exports.SiwxExpiredSessionError=E;exports.SiwxNonceReplayError=w;exports.SiwxParseError=u;exports.SiwxUnsupportedNamespaceError=I;exports.SiwxValidationError=m;exports.SiwxVerificationError=x;exports.buildMessage=A;exports.generateNonce=U;exports.parseMessage=v;exports.validateMessage=V;
1
+ 'use strict';function A(e){let{domain:t,address:n,statement:o,uri:c,version:s,chainId:d,nonce:i,issuedAt:p,expirationTime:a,notBefore:r,requestId:u,resources:g}=e,f=[`${t} wants you to sign in with your blockchain account:`,n,""];if(o&&f.push(o,""),f.push(`URI: ${c}`,`Version: ${s}`,`Chain ID: ${d}`,`Nonce: ${i}`,`Issued At: ${p}`),a&&f.push(`Expiration Time: ${a}`),r&&f.push(`Not Before: ${r}`),u&&f.push(`Request ID: ${u}`),g&&g.length>0){f.push("Resources:");for(let y of g)f.push(`- ${y}`);}return f.join(`
2
+ `)}var h=class extends Error{constructor(n,o="SIWX_ERROR"){super(n);this.code=o;this.name="SiwxError",Object.setPrototypeOf(this,new.target.prototype);}code},l=class extends h{constructor(t){super(t,"SIWX_PARSE_ERROR"),this.name="SiwxParseError";}},m=class extends h{constructor(n){super(`CAIP-122 message validation failed: ${n.join(", ")}`,"SIWX_VALIDATION_ERROR");this.errors=n;this.name="SiwxValidationError";}errors},x=class extends h{constructor(t){super(t,"SIWX_VERIFICATION_ERROR"),this.name="SiwxVerificationError";}},E=class extends h{constructor(n){super(`SIWX session expired at ${n}`,"SIWX_EXPIRED_SESSION");this.expirationTime=n;this.name="SiwxExpiredSessionError";}expirationTime},w=class extends h{constructor(n){super(`Nonce has already been used: ${n}`,"SIWX_NONCE_REPLAY");this.nonce=n;this.name="SiwxNonceReplayError";}nonce},S=class extends h{constructor(n){super(`Unsupported CAIP-2 namespace: "${n}". Supported: eip155, solana`,"SIWX_UNSUPPORTED_NAMESPACE");this.namespace=n;this.name="SiwxUnsupportedNamespaceError";}namespace};var $=/^(?<domain>.+) wants you to sign in with your blockchain account:$/,R=/^(?<key>[A-Za-z ]+): (?<value>.+)$/,b=/^- (?<resource>.+)$/;function v(e){let t=e.split(`
3
+ `);if(t.length<5)throw new l("Message is too short to be a valid CAIP-122 message.");let n=t[0]?.match($);if(!n?.groups?.domain)throw new l('Invalid CAIP-122 header line. Expected: "{domain} wants you to sign in with your blockchain account:"');let o=n.groups.domain,c=t[1];if(!c)throw new l("Missing address on line 2.");if(t[2]!=="")throw new l("Expected blank line after address (line 3).");let s=3,d;if(t[s]&&!R.test(t[s])){if(d=t[s],s++,t[s]!=="")throw new l("Expected blank line after statement.");s++;}let i={},p=[],a=false;for(;s<t.length;){let u=t[s];if(u===void 0){s++;continue}if(u==="Resources:"){a=true,s++;continue}if(a){let f=u.match(b);if(f?.groups?.resource){p.push(f.groups.resource),s++;continue}a=false;}let g=u.match(R);g?.groups?.key&&g.groups.value&&(i[g.groups.key]=g.groups.value),s++;}let r=["URI","Version","Chain ID","Nonce","Issued At"];for(let u of r)if(!i[u])throw new l(`Missing required field in CAIP-122 message: "${u}"`);return {domain:o,address:c,statement:d,uri:i.URI,version:i.Version,chainId:i["Chain ID"],nonce:i.Nonce,issuedAt:i["Issued At"],expirationTime:i["Expiration Time"],notBefore:i["Not Before"],requestId:i["Request ID"],resources:p.length>0?p:void 0}}var M=/^https?:\/\/.+/,C=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,_=/^[a-z0-9]+:[a-zA-Z0-9-]+:.+$/,P=/^[a-z0-9]+:[a-zA-Z0-9-]+$/;function N(e){if(!e||e.trim().length===0)return "domain must be a non-empty string.";if(e.includes(`
4
+ `))return "domain must not contain newline characters."}function T(e){if(!_.test(e))return `address must be CAIP-10 compliant (namespace:chainRef:address). Got: "${e}"`}function X(e){if(!M.test(e))return `uri must be a valid RFC 3986 URI starting with http(s)://. Got: "${e}"`}function D(e){if(!P.test(e))return `chainId must be a valid CAIP-2 identifier (namespace:reference). Got: "${e}"`}function G(e){if(!e||e.length<8)return `nonce must be at least 8 characters long. Got length: ${e?.length??0}`;if(!/^[a-zA-Z0-9]+$/.test(e))return `nonce must be alphanumeric. Got: "${e}"`}function I(e,t){if(!C.test(e))return `${t} must be a valid ISO 8601 datetime string. Got: "${e}"`}function O(e){let t=new Date(e);if(isNaN(t.getTime()))return `expirationTime is not a valid date: "${e}"`;if(t<new Date)return `Message has expired. expirationTime was: ${e}`}function W(e,t){let n=[],o=N(e.domain);o&&n.push(o);let c=T(e.address);c&&n.push(c);let s=X(e.uri);s&&n.push(s),e.version!=="1"&&n.push(`version must be "1". Got: "${e.version}"`);let d=D(e.chainId);d&&n.push(d);let i=G(e.nonce);i&&n.push(i);let p=I(e.issuedAt,"issuedAt");if(p&&n.push(p),e.expirationTime){let a=I(e.expirationTime,"expirationTime");if(a)n.push(a);else if(!t?.skipExpiration){let r=O(e.expirationTime);r&&n.push(r);}}if(e.notBefore){let a=I(e.notBefore,"notBefore");a&&n.push(a);}return e.statement&&e.statement.includes(`
5
+ `)&&n.push("statement must not contain newline characters."),{valid:n.length===0,errors:n}}function k(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}function V(e,t,n){if(!e||!e.address||!t)return false;let o=e.address,c=o.startsWith("eip155:"),s=o.startsWith("solana:"),d=t.includes(":")?t.split(":").pop():t,i=o.includes(":")?o.split(":").pop():o,p=d.startsWith("0x")||t.startsWith("eip155:");if(p&&!c||!p&&!s||!(c?i.toLowerCase()===d.toLowerCase():i===d))return false;if(n!=null){let r=String(n),u=c?r.startsWith("eip155:")?r:`eip155:${r}`:r.startsWith("solana:")?r:`solana:${r}`;if(e.chainId&&e.chainId!==u&&e.chainId!==r)return false}return true}exports.SiwxError=h;exports.SiwxExpiredSessionError=E;exports.SiwxNonceReplayError=w;exports.SiwxParseError=l;exports.SiwxUnsupportedNamespaceError=S;exports.SiwxValidationError=m;exports.SiwxVerificationError=x;exports.buildMessage=A;exports.generateNonce=k;exports.isSessionMatchingTarget=V;exports.parseMessage=v;exports.validateMessage=W;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- function A(e){let{domain:t,address:n,statement:c,uri:l,version:r,chainId:g,nonce:s,issuedAt:p,expirationTime:i,notBefore:h,requestId:a,resources:f}=e,o=[`${t} wants you to sign in with your blockchain account:`,n,""];if(c&&o.push(c,""),o.push(`URI: ${l}`,`Version: ${r}`,`Chain ID: ${g}`,`Nonce: ${s}`,`Issued At: ${p}`),i&&o.push(`Expiration Time: ${i}`),h&&o.push(`Not Before: ${h}`),a&&o.push(`Request ID: ${a}`),f&&f.length>0){o.push("Resources:");for(let y of f)o.push(`- ${y}`);}return o.join(`
2
- `)}var d=class extends Error{constructor(n,c="SIWX_ERROR"){super(n);this.code=c;this.name="SiwxError",Object.setPrototypeOf(this,new.target.prototype);}code},u=class extends d{constructor(t){super(t,"SIWX_PARSE_ERROR"),this.name="SiwxParseError";}},m=class extends d{constructor(n){super(`CAIP-122 message validation failed: ${n.join(", ")}`,"SIWX_VALIDATION_ERROR");this.errors=n;this.name="SiwxValidationError";}errors},x=class extends d{constructor(t){super(t,"SIWX_VERIFICATION_ERROR"),this.name="SiwxVerificationError";}},E=class extends d{constructor(n){super(`SIWX session expired at ${n}`,"SIWX_EXPIRED_SESSION");this.expirationTime=n;this.name="SiwxExpiredSessionError";}expirationTime},w=class extends d{constructor(n){super(`Nonce has already been used: ${n}`,"SIWX_NONCE_REPLAY");this.nonce=n;this.name="SiwxNonceReplayError";}nonce},I=class extends d{constructor(n){super(`Unsupported CAIP-2 namespace: "${n}". Supported: eip155, solana`,"SIWX_UNSUPPORTED_NAMESPACE");this.namespace=n;this.name="SiwxUnsupportedNamespaceError";}namespace};var $=/^(?<domain>.+) wants you to sign in with your blockchain account:$/,R=/^(?<key>[A-Za-z ]+): (?<value>.+)$/,b=/^- (?<resource>.+)$/;function v(e){let t=e.split(`
3
- `);if(t.length<5)throw new u("Message is too short to be a valid CAIP-122 message.");let n=t[0]?.match($);if(!n?.groups?.domain)throw new u('Invalid CAIP-122 header line. Expected: "{domain} wants you to sign in with your blockchain account:"');let c=n.groups.domain,l=t[1];if(!l)throw new u("Missing address on line 2.");if(t[2]!=="")throw new u("Expected blank line after address (line 3).");let r=3,g;if(t[r]&&!R.test(t[r])){if(g=t[r],r++,t[r]!=="")throw new u("Expected blank line after statement.");r++;}let s={},p=[],i=false;for(;r<t.length;){let a=t[r];if(a===void 0){r++;continue}if(a==="Resources:"){i=true,r++;continue}if(i){let o=a.match(b);if(o?.groups?.resource){p.push(o.groups.resource),r++;continue}i=false;}let f=a.match(R);f?.groups?.key&&f.groups.value&&(s[f.groups.key]=f.groups.value),r++;}let h=["URI","Version","Chain ID","Nonce","Issued At"];for(let a of h)if(!s[a])throw new u(`Missing required field in CAIP-122 message: "${a}"`);return {domain:c,address:l,statement:g,uri:s.URI,version:s.Version,chainId:s["Chain ID"],nonce:s.Nonce,issuedAt:s["Issued At"],expirationTime:s["Expiration Time"],notBefore:s["Not Before"],requestId:s["Request ID"],resources:p.length>0?p:void 0}}var _=/^https?:\/\/.+/,C=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,M=/^[a-z0-9]+:[a-zA-Z0-9-]+:.+$/,P=/^[a-z0-9]+:[a-zA-Z0-9-]+$/;function N(e){if(!e||e.trim().length===0)return "domain must be a non-empty string.";if(e.includes(`
4
- `))return "domain must not contain newline characters."}function X(e){if(!M.test(e))return `address must be CAIP-10 compliant (namespace:chainRef:address). Got: "${e}"`}function D(e){if(!_.test(e))return `uri must be a valid RFC 3986 URI starting with http(s)://. Got: "${e}"`}function G(e){if(!P.test(e))return `chainId must be a valid CAIP-2 identifier (namespace:reference). Got: "${e}"`}function O(e){if(!e||e.length<8)return `nonce must be at least 8 characters long. Got length: ${e?.length??0}`;if(!/^[a-zA-Z0-9]+$/.test(e))return `nonce must be alphanumeric. Got: "${e}"`}function S(e,t){if(!C.test(e))return `${t} must be a valid ISO 8601 datetime string. Got: "${e}"`}function T(e){let t=new Date(e);if(isNaN(t.getTime()))return `expirationTime is not a valid date: "${e}"`;if(t<new Date)return `Message has expired. expirationTime was: ${e}`}function V(e,t){let n=[],c=N(e.domain);c&&n.push(c);let l=X(e.address);l&&n.push(l);let r=D(e.uri);r&&n.push(r),e.version!=="1"&&n.push(`version must be "1". Got: "${e.version}"`);let g=G(e.chainId);g&&n.push(g);let s=O(e.nonce);s&&n.push(s);let p=S(e.issuedAt,"issuedAt");if(p&&n.push(p),e.expirationTime){let i=S(e.expirationTime,"expirationTime");if(i)n.push(i);else if(!t?.skipExpiration){let h=T(e.expirationTime);h&&n.push(h);}}if(e.notBefore){let i=S(e.notBefore,"notBefore");i&&n.push(i);}return e.statement&&e.statement.includes(`
5
- `)&&n.push("statement must not contain newline characters."),{valid:n.length===0,errors:n}}function U(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}export{d as SiwxError,E as SiwxExpiredSessionError,w as SiwxNonceReplayError,u as SiwxParseError,I as SiwxUnsupportedNamespaceError,m as SiwxValidationError,x as SiwxVerificationError,A as buildMessage,U as generateNonce,v as parseMessage,V as validateMessage};
1
+ function A(e){let{domain:t,address:n,statement:o,uri:c,version:s,chainId:d,nonce:i,issuedAt:p,expirationTime:a,notBefore:r,requestId:u,resources:g}=e,f=[`${t} wants you to sign in with your blockchain account:`,n,""];if(o&&f.push(o,""),f.push(`URI: ${c}`,`Version: ${s}`,`Chain ID: ${d}`,`Nonce: ${i}`,`Issued At: ${p}`),a&&f.push(`Expiration Time: ${a}`),r&&f.push(`Not Before: ${r}`),u&&f.push(`Request ID: ${u}`),g&&g.length>0){f.push("Resources:");for(let y of g)f.push(`- ${y}`);}return f.join(`
2
+ `)}var h=class extends Error{constructor(n,o="SIWX_ERROR"){super(n);this.code=o;this.name="SiwxError",Object.setPrototypeOf(this,new.target.prototype);}code},l=class extends h{constructor(t){super(t,"SIWX_PARSE_ERROR"),this.name="SiwxParseError";}},m=class extends h{constructor(n){super(`CAIP-122 message validation failed: ${n.join(", ")}`,"SIWX_VALIDATION_ERROR");this.errors=n;this.name="SiwxValidationError";}errors},x=class extends h{constructor(t){super(t,"SIWX_VERIFICATION_ERROR"),this.name="SiwxVerificationError";}},E=class extends h{constructor(n){super(`SIWX session expired at ${n}`,"SIWX_EXPIRED_SESSION");this.expirationTime=n;this.name="SiwxExpiredSessionError";}expirationTime},w=class extends h{constructor(n){super(`Nonce has already been used: ${n}`,"SIWX_NONCE_REPLAY");this.nonce=n;this.name="SiwxNonceReplayError";}nonce},S=class extends h{constructor(n){super(`Unsupported CAIP-2 namespace: "${n}". Supported: eip155, solana`,"SIWX_UNSUPPORTED_NAMESPACE");this.namespace=n;this.name="SiwxUnsupportedNamespaceError";}namespace};var $=/^(?<domain>.+) wants you to sign in with your blockchain account:$/,R=/^(?<key>[A-Za-z ]+): (?<value>.+)$/,b=/^- (?<resource>.+)$/;function v(e){let t=e.split(`
3
+ `);if(t.length<5)throw new l("Message is too short to be a valid CAIP-122 message.");let n=t[0]?.match($);if(!n?.groups?.domain)throw new l('Invalid CAIP-122 header line. Expected: "{domain} wants you to sign in with your blockchain account:"');let o=n.groups.domain,c=t[1];if(!c)throw new l("Missing address on line 2.");if(t[2]!=="")throw new l("Expected blank line after address (line 3).");let s=3,d;if(t[s]&&!R.test(t[s])){if(d=t[s],s++,t[s]!=="")throw new l("Expected blank line after statement.");s++;}let i={},p=[],a=false;for(;s<t.length;){let u=t[s];if(u===void 0){s++;continue}if(u==="Resources:"){a=true,s++;continue}if(a){let f=u.match(b);if(f?.groups?.resource){p.push(f.groups.resource),s++;continue}a=false;}let g=u.match(R);g?.groups?.key&&g.groups.value&&(i[g.groups.key]=g.groups.value),s++;}let r=["URI","Version","Chain ID","Nonce","Issued At"];for(let u of r)if(!i[u])throw new l(`Missing required field in CAIP-122 message: "${u}"`);return {domain:o,address:c,statement:d,uri:i.URI,version:i.Version,chainId:i["Chain ID"],nonce:i.Nonce,issuedAt:i["Issued At"],expirationTime:i["Expiration Time"],notBefore:i["Not Before"],requestId:i["Request ID"],resources:p.length>0?p:void 0}}var M=/^https?:\/\/.+/,C=/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/,_=/^[a-z0-9]+:[a-zA-Z0-9-]+:.+$/,P=/^[a-z0-9]+:[a-zA-Z0-9-]+$/;function N(e){if(!e||e.trim().length===0)return "domain must be a non-empty string.";if(e.includes(`
4
+ `))return "domain must not contain newline characters."}function T(e){if(!_.test(e))return `address must be CAIP-10 compliant (namespace:chainRef:address). Got: "${e}"`}function X(e){if(!M.test(e))return `uri must be a valid RFC 3986 URI starting with http(s)://. Got: "${e}"`}function D(e){if(!P.test(e))return `chainId must be a valid CAIP-2 identifier (namespace:reference). Got: "${e}"`}function G(e){if(!e||e.length<8)return `nonce must be at least 8 characters long. Got length: ${e?.length??0}`;if(!/^[a-zA-Z0-9]+$/.test(e))return `nonce must be alphanumeric. Got: "${e}"`}function I(e,t){if(!C.test(e))return `${t} must be a valid ISO 8601 datetime string. Got: "${e}"`}function O(e){let t=new Date(e);if(isNaN(t.getTime()))return `expirationTime is not a valid date: "${e}"`;if(t<new Date)return `Message has expired. expirationTime was: ${e}`}function W(e,t){let n=[],o=N(e.domain);o&&n.push(o);let c=T(e.address);c&&n.push(c);let s=X(e.uri);s&&n.push(s),e.version!=="1"&&n.push(`version must be "1". Got: "${e.version}"`);let d=D(e.chainId);d&&n.push(d);let i=G(e.nonce);i&&n.push(i);let p=I(e.issuedAt,"issuedAt");if(p&&n.push(p),e.expirationTime){let a=I(e.expirationTime,"expirationTime");if(a)n.push(a);else if(!t?.skipExpiration){let r=O(e.expirationTime);r&&n.push(r);}}if(e.notBefore){let a=I(e.notBefore,"notBefore");a&&n.push(a);}return e.statement&&e.statement.includes(`
5
+ `)&&n.push("statement must not contain newline characters."),{valid:n.length===0,errors:n}}function k(){let e=new Uint8Array(16);return globalThis.crypto.getRandomValues(e),Array.from(e).map(t=>t.toString(16).padStart(2,"0")).join("")}function V(e,t,n){if(!e||!e.address||!t)return false;let o=e.address,c=o.startsWith("eip155:"),s=o.startsWith("solana:"),d=t.includes(":")?t.split(":").pop():t,i=o.includes(":")?o.split(":").pop():o,p=d.startsWith("0x")||t.startsWith("eip155:");if(p&&!c||!p&&!s||!(c?i.toLowerCase()===d.toLowerCase():i===d))return false;if(n!=null){let r=String(n),u=c?r.startsWith("eip155:")?r:`eip155:${r}`:r.startsWith("solana:")?r:`solana:${r}`;if(e.chainId&&e.chainId!==u&&e.chainId!==r)return false}return true}export{h as SiwxError,E as SiwxExpiredSessionError,w as SiwxNonceReplayError,l as SiwxParseError,S as SiwxUnsupportedNamespaceError,m as SiwxValidationError,x as SiwxVerificationError,A as buildMessage,k as generateNonce,V as isSessionMatchingTarget,v as parseMessage,W as validateMessage};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tuwaio/siwx-core",
3
- "version": "1.0.0-fix-initialize-alpha.10.167aa43",
3
+ "version": "1.0.0-fix-initialize-alpha.12.626c94c",
4
4
  "private": false,
5
5
  "author": "Oleksandr Tkach",
6
6
  "license": "Apache-2.0",