@vallum/sdk 0.0.0-prerelease

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.
@@ -0,0 +1,79 @@
1
+ import { VallumAuthError, VallumError, VallumPolicyError } from "./errors.js";
2
+ export async function requestSponsoredAction(options) {
3
+ const fetchImpl = options.fetchImpl ?? fetch;
4
+ const response = await fetchImpl(`${normalizeBaseUrl(options.baseUrl)}/v1/agent/sponsorships`, {
5
+ method: "POST",
6
+ headers: requestHeaders(options.apiKey),
7
+ body: JSON.stringify({
8
+ manifest: options.manifest,
9
+ }),
10
+ });
11
+ const json = await parseJson(response);
12
+ const result = parseSponsoredActionResult(json);
13
+ if (result)
14
+ return result;
15
+ if (!response.ok)
16
+ throw buildError(response.status, json);
17
+ throw new VallumError("Malformed Vallum response: missing sponsored action result.", undefined, json);
18
+ }
19
+ function normalizeBaseUrl(baseUrl) {
20
+ return baseUrl.replace(/\/+$/, "");
21
+ }
22
+ function requestHeaders(apiKey) {
23
+ const headers = {
24
+ "Content-Type": "application/json",
25
+ };
26
+ if (apiKey)
27
+ headers.Authorization = `Bearer ${apiKey}`;
28
+ return headers;
29
+ }
30
+ async function parseJson(response) {
31
+ return response.json().catch(() => ({}));
32
+ }
33
+ function parseSponsoredActionResult(value) {
34
+ const record = asRecord(value);
35
+ const decision = asRecord(record.decision);
36
+ if (record.approved === true &&
37
+ decision.allowed === true &&
38
+ typeof record.mockSponsorshipId === "string" &&
39
+ record.mockSponsorshipId.length > 0) {
40
+ return {
41
+ approved: true,
42
+ decision: { allowed: true },
43
+ mockSponsorshipId: record.mockSponsorshipId,
44
+ };
45
+ }
46
+ if (record.approved === false &&
47
+ decision.allowed === false &&
48
+ typeof decision.reasonCode === "string" &&
49
+ decision.reasonCode.length > 0 &&
50
+ typeof decision.message === "string") {
51
+ return {
52
+ approved: false,
53
+ decision: {
54
+ allowed: false,
55
+ reasonCode: decision.reasonCode,
56
+ message: decision.message,
57
+ },
58
+ };
59
+ }
60
+ return undefined;
61
+ }
62
+ function buildError(status, body) {
63
+ const record = asRecord(body);
64
+ const message = typeof record.message === "string"
65
+ ? record.message
66
+ : typeof record.error === "string"
67
+ ? record.error
68
+ : `Vallum request failed with HTTP ${status}`;
69
+ const reasonCode = typeof record.reasonCode === "string" ? record.reasonCode : undefined;
70
+ if (status === 401)
71
+ return new VallumAuthError(message, status, body);
72
+ if (status === 400 || status === 403 || status === 409 || status === 429) {
73
+ return new VallumPolicyError(message, reasonCode, status, body);
74
+ }
75
+ return new VallumError(message, status, body);
76
+ }
77
+ function asRecord(value) {
78
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
79
+ }
@@ -0,0 +1,5 @@
1
+ import { type AgentResolver, type ResolveAgentResult } from "@vallum/registry";
2
+ export interface ResolveAgentOptions {
3
+ readonly resolver: AgentResolver;
4
+ }
5
+ export declare function resolveAgent(name: string, options: ResolveAgentOptions): Promise<ResolveAgentResult>;
@@ -0,0 +1,4 @@
1
+ import { resolveAgent as resolveRegistryAgent, } from "@vallum/registry";
2
+ export function resolveAgent(name, options) {
3
+ return resolveRegistryAgent(name, options.resolver);
4
+ }
@@ -0,0 +1,65 @@
1
+ import type { PolicyDecision } from "@vallum/shared-types";
2
+ import type { AgentTransactionManifest } from "@vallum/manifest";
3
+ export interface VallumClientOptions {
4
+ baseUrl: string;
5
+ apiKey: string;
6
+ fetchImpl?: typeof fetch;
7
+ }
8
+ export interface ReserveGasRequest {
9
+ gasBudget: number;
10
+ reserveDurationSecs?: number;
11
+ walletAddress?: string;
12
+ packageId?: string;
13
+ functionName?: string;
14
+ }
15
+ export interface PolicySimulationRequest {
16
+ gasBudget?: number;
17
+ walletAddress?: string;
18
+ packageId?: string;
19
+ functionName?: string;
20
+ }
21
+ export type PolicySimulationResponse = PolicyDecision;
22
+ export interface ReserveGasResponse {
23
+ reservationId: string;
24
+ agentRailTransactionId: string;
25
+ sponsorAddress?: string;
26
+ gasCoins?: unknown[];
27
+ raw: unknown;
28
+ }
29
+ export interface ExecuteSponsoredTransactionRequest {
30
+ reservationId: string;
31
+ agentRailTransactionId: string;
32
+ transactionBytes: string;
33
+ userSignature: string;
34
+ }
35
+ export interface ExecuteSponsoredTransactionResponse {
36
+ digest?: string;
37
+ raw: unknown;
38
+ }
39
+ export interface SponsoredActionRequest {
40
+ manifest: AgentTransactionManifest;
41
+ }
42
+ export interface RequestSponsoredActionOptions extends SponsoredActionRequest {
43
+ baseUrl: string;
44
+ apiKey?: string;
45
+ fetchImpl?: typeof fetch;
46
+ }
47
+ export type SponsoredActionDecision = {
48
+ allowed: true;
49
+ } | {
50
+ allowed: false;
51
+ reasonCode: string;
52
+ message: string;
53
+ };
54
+ export type SponsoredActionResult = {
55
+ approved: true;
56
+ decision: Extract<SponsoredActionDecision, {
57
+ allowed: true;
58
+ }>;
59
+ mockSponsorshipId: string;
60
+ } | {
61
+ approved: false;
62
+ decision: Extract<SponsoredActionDecision, {
63
+ allowed: false;
64
+ }>;
65
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@vallum/sdk",
3
+ "version": "0.0.0-prerelease",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
13
+ "license": "Apache-2.0",
14
+ "dependencies": {
15
+ "@vallum/manifest": "0.0.0-prerelease",
16
+ "@vallum/registry": "0.0.0-prerelease",
17
+ "@vallum/receipts": "0.0.0-prerelease",
18
+ "@vallum/shared-types": "0.0.0-prerelease"
19
+ },
20
+ "devDependencies": {
21
+ "@vallum/policy-gateway": "0.0.0-prerelease"
22
+ },
23
+ "description": "TypeScript SDK scaffold for Vallum sponsorship gateways.",
24
+ "files": [
25
+ "dist/**/*.js",
26
+ "dist/**/*.d.ts",
27
+ "LICENSE",
28
+ "README.md"
29
+ ],
30
+ "sideEffects": false,
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.build.json"
33
+ },
34
+ "engines": {
35
+ "node": ">=20"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "tag": "next"
40
+ }
41
+ }