@opendatalabs/vana-sdk
Version:
A TypeScript library for interacting with Vana Network smart contracts.
50 lines • 1.43 kB
JavaScript
import { z } from "zod";
const SEGMENT_RE = /^[a-z0-9][a-z0-9_]*$/;
const ScopeSchema = z.string().refine(
(scope) => {
const parts = scope.split(".");
return parts.length >= 2 && parts.length <= 3 && parts.every((part) => SEGMENT_RE.test(part));
},
{
message: "Scope must be {source}.{category}[.{subcategory}] with lowercase alphanumeric segments (may start with a letter or digit)"
}
);
function parseScope(scope) {
const validated = ScopeSchema.parse(scope);
const parts = validated.split(".");
return {
source: parts[0],
category: parts[1],
subcategory: parts[2],
raw: validated
};
}
function scopeToPathSegments(scope) {
const parsed = parseScope(scope);
const segments = [parsed.source, parsed.category];
if (parsed.subcategory) {
segments.push(parsed.subcategory);
}
return segments;
}
function scopeMatchesPattern(requestedScope, grantPattern) {
if (grantPattern === "*") return true;
if (grantPattern.endsWith(".*")) {
const prefix = grantPattern.slice(0, -1);
return requestedScope.startsWith(prefix);
}
return requestedScope === grantPattern;
}
function scopeCoveredByGrant(requestedScope, grantedScopes) {
return grantedScopes.some(
(pattern) => scopeMatchesPattern(requestedScope, pattern)
);
}
export {
ScopeSchema,
parseScope,
scopeCoveredByGrant,
scopeMatchesPattern,
scopeToPathSegments
};
//# sourceMappingURL=scopes.js.map