@opendatalabs/vana-sdk 3.0.1 → 3.2.0

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.
Files changed (92) hide show
  1. package/README.md +2 -1
  2. package/dist/__tests__/interop-personal-server.test.d.ts +1 -0
  3. package/dist/account/personal-server-lite-owner-binding.cjs +81 -0
  4. package/dist/account/personal-server-lite-owner-binding.cjs.map +1 -0
  5. package/dist/account/personal-server-lite-owner-binding.d.ts +30 -0
  6. package/dist/account/personal-server-lite-owner-binding.js +59 -0
  7. package/dist/account/personal-server-lite-owner-binding.js.map +1 -0
  8. package/dist/account/personal-server-lite-owner-binding.test.d.ts +1 -0
  9. package/dist/account/personal-server-registration.cjs +263 -0
  10. package/dist/account/personal-server-registration.cjs.map +1 -0
  11. package/dist/account/personal-server-registration.d.ts +66 -0
  12. package/dist/account/personal-server-registration.js +240 -0
  13. package/dist/account/personal-server-registration.js.map +1 -0
  14. package/dist/account/personal-server-registration.test.d.ts +1 -0
  15. package/dist/auth/errors.js +1 -1
  16. package/dist/auth/oauth-client.cjs +250 -0
  17. package/dist/auth/oauth-client.cjs.map +1 -0
  18. package/dist/auth/oauth-client.d.ts +90 -0
  19. package/dist/auth/oauth-client.js +228 -0
  20. package/dist/auth/oauth-client.js.map +1 -0
  21. package/dist/auth/oauth-client.test.d.ts +1 -0
  22. package/dist/auth/web3-signed-builder.js +1 -1
  23. package/dist/auth/web3-signed.js +3 -3
  24. package/dist/browser.js +1 -1
  25. package/dist/chains/definitions.js +1 -1
  26. package/dist/chains/index.js +1 -1
  27. package/dist/chains.browser.js +1 -1
  28. package/dist/chains.js +1 -1
  29. package/dist/chains.node.js +1 -1
  30. package/dist/contracts/contractController.js +4 -4
  31. package/dist/core/client.js +1 -1
  32. package/dist/crypto/ecies/base.js +3 -3
  33. package/dist/crypto/ecies/browser.js +1 -1
  34. package/dist/crypto/ecies/index.js +1 -1
  35. package/dist/crypto/ecies/interface.js +1 -1
  36. package/dist/crypto/ecies/node.js +1 -1
  37. package/dist/crypto/services/WalletKeyEncryptionService.js +1 -1
  38. package/dist/generated/abi/index.js +26 -26
  39. package/dist/index.browser.d.ts +5 -0
  40. package/dist/index.browser.js +671 -4
  41. package/dist/index.browser.js.map +4 -4
  42. package/dist/index.node.cjs +689 -6
  43. package/dist/index.node.cjs.map +4 -4
  44. package/dist/index.node.d.ts +5 -0
  45. package/dist/index.node.js +671 -4
  46. package/dist/index.node.js.map +4 -4
  47. package/dist/node.js +1 -1
  48. package/dist/platform/browser-only.js +1 -1
  49. package/dist/platform/browser-safe.js +1 -1
  50. package/dist/platform/browser.js +6 -6
  51. package/dist/platform/index.js +4 -4
  52. package/dist/platform/node.js +8 -8
  53. package/dist/platform/utils.js +2 -2
  54. package/dist/platform.browser.js +3 -3
  55. package/dist/platform.js +4 -4
  56. package/dist/platform.node.js +4 -4
  57. package/dist/protocol/eip712.cjs.map +1 -1
  58. package/dist/protocol/eip712.d.ts +1 -1
  59. package/dist/protocol/eip712.js.map +1 -1
  60. package/dist/protocol/grants.js +1 -1
  61. package/dist/protocol/personal-server-lite-owner-binding.cjs +93 -0
  62. package/dist/protocol/personal-server-lite-owner-binding.cjs.map +1 -0
  63. package/dist/protocol/personal-server-lite-owner-binding.d.ts +44 -0
  64. package/dist/protocol/personal-server-lite-owner-binding.js +65 -0
  65. package/dist/protocol/personal-server-lite-owner-binding.js.map +1 -0
  66. package/dist/protocol/personal-server-lite-owner-binding.test.d.ts +1 -0
  67. package/dist/protocol/personal-server-registration.cjs +122 -0
  68. package/dist/protocol/personal-server-registration.cjs.map +1 -0
  69. package/dist/protocol/personal-server-registration.d.ts +62 -0
  70. package/dist/protocol/personal-server-registration.js +97 -0
  71. package/dist/protocol/personal-server-registration.js.map +1 -0
  72. package/dist/protocol/personal-server-registration.test.d.ts +1 -0
  73. package/dist/storage/default.js +1 -1
  74. package/dist/storage/index.cjs.map +1 -1
  75. package/dist/storage/index.d.ts +1 -1
  76. package/dist/storage/index.js +10 -10
  77. package/dist/storage/index.js.map +1 -1
  78. package/dist/storage/manager.js +1 -1
  79. package/dist/storage/providers/callback-storage.js +1 -1
  80. package/dist/storage/providers/dropbox.js +1 -1
  81. package/dist/storage/providers/google-drive.js +1 -1
  82. package/dist/storage/providers/ipfs.js +2 -2
  83. package/dist/storage/providers/pinata.js +1 -1
  84. package/dist/storage/providers/r2.js +1 -1
  85. package/dist/storage/providers/vana-storage.cjs +1 -1
  86. package/dist/storage/providers/vana-storage.cjs.map +1 -1
  87. package/dist/storage/providers/vana-storage.d.ts +2 -2
  88. package/dist/storage/providers/vana-storage.js +3 -3
  89. package/dist/storage/providers/vana-storage.js.map +1 -1
  90. package/dist/types/index.js +2 -2
  91. package/dist/types.js +1 -1
  92. package/package.json +5 -2
@@ -1173,6 +1173,9 @@ var init_browser2 = __esm({
1173
1173
  // src/index.node.ts
1174
1174
  var index_node_exports = {};
1175
1175
  __export(index_node_exports, {
1176
+ ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT: () => ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT,
1177
+ AccountPersonalServerLiteOwnerBindingError: () => AccountPersonalServerLiteOwnerBindingError,
1178
+ AccountPersonalServerRegistrationError: () => AccountPersonalServerRegistrationError,
1176
1179
  BUILDER_REGISTRATION_TYPES: () => BUILDER_REGISTRATION_TYPES,
1177
1180
  BlockchainError: () => BlockchainError,
1178
1181
  BrowserPlatformAdapter: () => BrowserPlatformAdapter,
@@ -1199,6 +1202,12 @@ __export(index_node_exports, {
1199
1202
  NodeECIESProvider: () => NodeECIESUint8Provider,
1200
1203
  NodePlatformAdapter: () => NodePlatformAdapter,
1201
1204
  NonceError: () => NonceError,
1205
+ OAuthClient: () => OAuthClient,
1206
+ PERSONAL_SERVER_LITE_OWNER_BINDING_PREFIX: () => PERSONAL_SERVER_LITE_OWNER_BINDING_PREFIX,
1207
+ PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE: () => PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE,
1208
+ PERSONAL_SERVER_LITE_OWNER_BINDING_VERSION: () => PERSONAL_SERVER_LITE_OWNER_BINDING_VERSION,
1209
+ PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID: () => PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID,
1210
+ PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT: () => PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT,
1202
1211
  PKCE_CHALLENGE_PATTERN: () => PKCE_CHALLENGE_PATTERN,
1203
1212
  PKCE_VERIFIER_PATTERN: () => PKCE_VERIFIER_PATTERN,
1204
1213
  PSError: () => PSError,
@@ -1220,6 +1229,10 @@ __export(index_node_exports, {
1220
1229
  VanaError: () => VanaError,
1221
1230
  VanaStorage: () => VanaStorage,
1222
1231
  assertValidPkceVerifier: () => assertValidPkceVerifier,
1232
+ buildPersonalServerLiteOwnerBindingMessage: () => buildPersonalServerLiteOwnerBindingMessage,
1233
+ buildPersonalServerLiteOwnerBindingSignature: () => buildPersonalServerLiteOwnerBindingSignature,
1234
+ buildPersonalServerRegistrationSignature: () => buildPersonalServerRegistrationSignature,
1235
+ buildPersonalServerRegistrationTypedData: () => buildPersonalServerRegistrationTypedData,
1223
1236
  buildWeb3SignedHeader: () => buildWeb3SignedHeader,
1224
1237
  builderRegistrationDomain: () => builderRegistrationDomain,
1225
1238
  chains: () => chains,
@@ -1235,6 +1248,8 @@ __export(index_node_exports, {
1235
1248
  createPlatformAdapterFor: () => createPlatformAdapterFor,
1236
1249
  createPlatformAdapterSafe: () => createPlatformAdapterSafe,
1237
1250
  createVanaStorageProvider: () => createVanaStorageProvider,
1251
+ createViemPersonalServerLiteOwnerBindingSigner: () => createViemPersonalServerLiteOwnerBindingSigner,
1252
+ createViemPersonalServerRegistrationSigner: () => createViemPersonalServerRegistrationSigner,
1238
1253
  decryptWithPassword: () => decryptWithPassword,
1239
1254
  deriveMasterKey: () => deriveMasterKey,
1240
1255
  deriveScopeKey: () => deriveScopeKey,
@@ -1264,12 +1279,17 @@ __export(index_node_exports, {
1264
1279
  parsePSError: () => parsePSError,
1265
1280
  parseScope: () => parseScope,
1266
1281
  parseWeb3SignedHeader: () => parseWeb3SignedHeader,
1282
+ personalServerRegistrationDomain: () => personalServerRegistrationDomain,
1267
1283
  recoverServerOwner: () => recoverServerOwner,
1284
+ registerPersonalServerSignature: () => registerPersonalServerSignature,
1268
1285
  scopeCoveredByGrant: () => scopeCoveredByGrant,
1269
1286
  scopeMatchesPattern: () => scopeMatchesPattern,
1270
1287
  scopeToPathSegments: () => scopeToPathSegments,
1271
1288
  serializeECIES: () => serializeECIES,
1272
1289
  serverRegistrationDomain: () => serverRegistrationDomain,
1290
+ signPersonalServerLiteOwnerBinding: () => signPersonalServerLiteOwnerBinding,
1291
+ signPersonalServerLiteOwnerBindingWithAccountClient: () => signPersonalServerLiteOwnerBindingWithAccountClient,
1292
+ signPersonalServerRegistrationWithAccount: () => signPersonalServerRegistrationWithAccount,
1273
1293
  vanaMainnet: () => vanaMainnet2,
1274
1294
  verifyGrantRegistration: () => verifyGrantRegistration,
1275
1295
  verifyPkceChallenge: () => verifyPkceChallenge,
@@ -29139,7 +29159,7 @@ async function buildWeb3SignedHeader(params) {
29139
29159
  }
29140
29160
 
29141
29161
  // src/storage/providers/vana-storage.ts
29142
- var DEFAULT_ENDPOINT = "https://storage.vana.com";
29162
+ var DEFAULT_ENDPOINT = "https://storage.vana.org";
29143
29163
  var BLOB_PATH_PREFIX = "/v1/blobs";
29144
29164
  var DEFAULT_TOKEN_TTL_SECONDS = 300;
29145
29165
  var VanaStorage = class {
@@ -32402,6 +32422,228 @@ var InMemoryTokenStore = class {
32402
32422
  }
32403
32423
  };
32404
32424
 
32425
+ // src/auth/oauth-client.ts
32426
+ var VERIFIER_TTL_SECONDS = 600;
32427
+ var RESERVED_AUTHORIZE_PARAMS = /* @__PURE__ */ new Set([
32428
+ "response_type",
32429
+ "client_id",
32430
+ "redirect_uri",
32431
+ "scope",
32432
+ "state",
32433
+ "code_challenge",
32434
+ "code_challenge_method"
32435
+ ]);
32436
+ var OAuthClient = class {
32437
+ #config;
32438
+ constructor(config) {
32439
+ const fetchImpl = config.fetchImpl ?? globalThis.fetch;
32440
+ if (typeof fetchImpl !== "function") {
32441
+ throw new TypeError(
32442
+ "OAuthClient requires a global `fetch` or an explicit `fetchImpl`"
32443
+ );
32444
+ }
32445
+ this.#config = {
32446
+ authorizationEndpoint: config.authorizationEndpoint,
32447
+ tokenEndpoint: config.tokenEndpoint,
32448
+ clientId: config.clientId,
32449
+ redirectUri: config.redirectUri,
32450
+ scope: config.scope,
32451
+ tokenStore: config.tokenStore ?? new InMemoryTokenStore(),
32452
+ fetchImpl,
32453
+ generateState: config.generateState ?? defaultGenerateState
32454
+ };
32455
+ }
32456
+ /** Build the authorize URL and persist the PKCE verifier keyed by `state`. */
32457
+ async buildAuthorizationUrl(opts = {}) {
32458
+ const state = opts.state ?? this.#config.generateState();
32459
+ const scope = opts.scope ?? this.#config.scope;
32460
+ const verifier = generatePkceVerifier();
32461
+ const challenge = await computePkceChallenge(verifier);
32462
+ await this.#config.tokenStore.set(this.#verifierKey(state), {
32463
+ token: verifier,
32464
+ expiresAt: Math.floor(Date.now() / 1e3) + VERIFIER_TTL_SECONDS
32465
+ });
32466
+ const params = new URLSearchParams();
32467
+ params.set("response_type", "code");
32468
+ params.set("client_id", this.#config.clientId);
32469
+ params.set("redirect_uri", this.#config.redirectUri);
32470
+ if (scope !== void 0 && scope.length > 0) {
32471
+ params.set("scope", scope);
32472
+ }
32473
+ params.set("state", state);
32474
+ params.set("code_challenge", challenge);
32475
+ params.set("code_challenge_method", "S256");
32476
+ if (opts.extraParams !== void 0) {
32477
+ for (const k of Object.keys(opts.extraParams)) {
32478
+ if (RESERVED_AUTHORIZE_PARAMS.has(k)) {
32479
+ throw new Error(
32480
+ `extraParams may not override the reserved OAuth/PKCE parameter "${k}"`
32481
+ );
32482
+ }
32483
+ }
32484
+ for (const [k, v] of Object.entries(opts.extraParams)) {
32485
+ params.set(k, v);
32486
+ }
32487
+ }
32488
+ const sep = this.#config.authorizationEndpoint.includes("?") ? "&" : "?";
32489
+ const url = `${this.#config.authorizationEndpoint}${sep}${params.toString()}`;
32490
+ return { url, state };
32491
+ }
32492
+ /**
32493
+ * Handle the redirect-callback URL. Validates `state`, retrieves the saved
32494
+ * verifier, exchanges the authorization code + verifier for tokens, and
32495
+ * persists them. Returns the access {@link TokenRecord}.
32496
+ */
32497
+ async handleCallback(callbackUrl) {
32498
+ const parsed = new URL(callbackUrl);
32499
+ const params = parsed.searchParams;
32500
+ const errorCode = params.get("error");
32501
+ if (errorCode !== null) {
32502
+ throw new Error(
32503
+ formatOAuthError({
32504
+ error: errorCode,
32505
+ error_description: params.get("error_description") ?? void 0
32506
+ })
32507
+ );
32508
+ }
32509
+ const code = params.get("code");
32510
+ const state = params.get("state");
32511
+ if (code === null || state === null) {
32512
+ throw new Error("OAuth callback is missing `code` or `state`");
32513
+ }
32514
+ const verifierRecord = await this.#config.tokenStore.get(
32515
+ this.#verifierKey(state)
32516
+ );
32517
+ if (verifierRecord === null) {
32518
+ throw new Error(
32519
+ "OAuth callback state does not match any in-flight verifier (possible CSRF or expired flow)"
32520
+ );
32521
+ }
32522
+ const body = new URLSearchParams();
32523
+ body.set("grant_type", "authorization_code");
32524
+ body.set("code", code);
32525
+ body.set("redirect_uri", this.#config.redirectUri);
32526
+ body.set("client_id", this.#config.clientId);
32527
+ body.set("code_verifier", verifierRecord.token);
32528
+ let tokens;
32529
+ try {
32530
+ tokens = await this.#tokenRequest(body);
32531
+ } finally {
32532
+ await this.#config.tokenStore.delete(this.#verifierKey(state));
32533
+ }
32534
+ return this.#persistTokens(tokens);
32535
+ }
32536
+ /**
32537
+ * Exchange a stored refresh token for a fresh access token. Throws if no
32538
+ * refresh token is available.
32539
+ */
32540
+ async refresh() {
32541
+ const refreshRecord = await this.#config.tokenStore.get(this.#refreshKey());
32542
+ if (refreshRecord === null) {
32543
+ throw new Error("OAuth refresh failed: no refresh token stored");
32544
+ }
32545
+ const body = new URLSearchParams();
32546
+ body.set("grant_type", "refresh_token");
32547
+ body.set("refresh_token", refreshRecord.token);
32548
+ body.set("client_id", this.#config.clientId);
32549
+ const tokens = await this.#tokenRequest(body);
32550
+ return this.#persistTokens(tokens, refreshRecord.token);
32551
+ }
32552
+ /**
32553
+ * Get the current access token if valid (refreshing first if expired and a
32554
+ * refresh token is available). Returns `null` when no usable token exists.
32555
+ */
32556
+ async getAccessToken() {
32557
+ const stored = await this.#config.tokenStore.get(this.#accessKey());
32558
+ if (stored !== null) return stored.token;
32559
+ const refresh = await this.#config.tokenStore.get(this.#refreshKey());
32560
+ if (refresh === null) return null;
32561
+ try {
32562
+ const refreshed = await this.refresh();
32563
+ return refreshed.token;
32564
+ } catch {
32565
+ return null;
32566
+ }
32567
+ }
32568
+ /** Forget tokens (logout). Does NOT call any remote revocation endpoint. */
32569
+ async signOut() {
32570
+ await this.#config.tokenStore.delete(this.#accessKey());
32571
+ await this.#config.tokenStore.delete(this.#refreshKey());
32572
+ }
32573
+ #accessKey() {
32574
+ return `oauth:tokens:${this.#config.clientId}`;
32575
+ }
32576
+ #refreshKey() {
32577
+ return `oauth:refresh:${this.#config.clientId}`;
32578
+ }
32579
+ #verifierKey(state) {
32580
+ return `oauth:verifier:${state}`;
32581
+ }
32582
+ async #tokenRequest(body) {
32583
+ const response = await this.#config.fetchImpl(this.#config.tokenEndpoint, {
32584
+ method: "POST",
32585
+ headers: {
32586
+ "Content-Type": "application/x-www-form-urlencoded",
32587
+ Accept: "application/json"
32588
+ },
32589
+ body: body.toString()
32590
+ });
32591
+ const text = await response.text();
32592
+ const parsed = parseJsonBody(text);
32593
+ if (!response.ok) {
32594
+ throw new Error(formatOAuthError(parsed ?? {}, response.status));
32595
+ }
32596
+ if (parsed === null || typeof parsed !== "object" || typeof parsed.access_token !== "string") {
32597
+ throw new Error(
32598
+ "OAuth token endpoint returned a response without an `access_token` string"
32599
+ );
32600
+ }
32601
+ return parsed;
32602
+ }
32603
+ async #persistTokens(tokens, previousRefreshToken) {
32604
+ const record = { token: tokens.access_token };
32605
+ if (typeof tokens.expires_in === "number" && tokens.expires_in > 0) {
32606
+ record.expiresAt = Math.floor(Date.now() / 1e3) + tokens.expires_in;
32607
+ }
32608
+ await this.#config.tokenStore.set(this.#accessKey(), record);
32609
+ const newRefresh = tokens.refresh_token ?? previousRefreshToken;
32610
+ if (newRefresh !== void 0) {
32611
+ await this.#config.tokenStore.set(this.#refreshKey(), {
32612
+ token: newRefresh
32613
+ });
32614
+ }
32615
+ return record;
32616
+ }
32617
+ };
32618
+ function defaultGenerateState() {
32619
+ const bytes = new Uint8Array(24);
32620
+ crypto.getRandomValues(bytes);
32621
+ let binary = "";
32622
+ for (let i = 0; i < bytes.length; i++) {
32623
+ binary += String.fromCharCode(bytes[i]);
32624
+ }
32625
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
32626
+ }
32627
+ function parseJsonBody(text) {
32628
+ if (text.length === 0) return null;
32629
+ try {
32630
+ return JSON.parse(text);
32631
+ } catch {
32632
+ return null;
32633
+ }
32634
+ }
32635
+ function formatOAuthError(body, status) {
32636
+ const parts = ["OAuth token request failed"];
32637
+ if (status !== void 0) parts.push(`(HTTP ${String(status)})`);
32638
+ if (body.error !== void 0 && body.error.length > 0) {
32639
+ parts.push(`: ${body.error}`);
32640
+ if (body.error_description !== void 0 && body.error_description.length > 0) {
32641
+ parts.push(`- ${body.error_description}`);
32642
+ }
32643
+ }
32644
+ return parts.join(" ").replace(" : ", ": ").replace(" - ", " - ");
32645
+ }
32646
+
32405
32647
  // src/protocol/eip712.ts
32406
32648
  var DOMAIN_NAME = "Vana Data Portability";
32407
32649
  var DOMAIN_VERSION = "1";
@@ -32481,8 +32723,429 @@ var BUILDER_REGISTRATION_TYPES = {
32481
32723
  ]
32482
32724
  };
32483
32725
 
32484
- // src/protocol/grants.ts
32726
+ // src/protocol/personal-server-registration.ts
32485
32727
  var import_viem14 = require("viem");
32728
+ var PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID = 1480;
32729
+ var PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT = "0x1483B1F634DBA75AeaE60da7f01A679aabd5ee2c";
32730
+ function assertAddress(value, name) {
32731
+ if (!(0, import_viem14.isAddress)(value)) {
32732
+ throw new Error(`${name} must be a valid EVM address`);
32733
+ }
32734
+ }
32735
+ function getAccountAddress(account) {
32736
+ if (!account) {
32737
+ return void 0;
32738
+ }
32739
+ return typeof account === "string" ? account : account.address;
32740
+ }
32741
+ function isPersonalServerRegistrationSigner(source) {
32742
+ return "address" in source && typeof source.signTypedData === "function";
32743
+ }
32744
+ function createViemPersonalServerRegistrationSigner(source, options = {}) {
32745
+ if (isPersonalServerRegistrationSigner(source)) {
32746
+ return source;
32747
+ }
32748
+ const accountAddress = getAccountAddress(options.account) ?? getAccountAddress(source.account);
32749
+ if (accountAddress) {
32750
+ return {
32751
+ address: accountAddress,
32752
+ signTypedData: (typedData) => source.signTypedData({
32753
+ ...typedData,
32754
+ account: options.account ?? source.account ?? accountAddress
32755
+ })
32756
+ };
32757
+ }
32758
+ throw new Error(
32759
+ "Viem wallet client requires an account option or account property"
32760
+ );
32761
+ }
32762
+ function personalServerRegistrationDomain(input = {}) {
32763
+ if (input.config) {
32764
+ return serverRegistrationDomain(input.config);
32765
+ }
32766
+ const verifyingContract = input.verifyingContract ?? PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT;
32767
+ assertAddress(verifyingContract, "verifyingContract");
32768
+ return {
32769
+ name: "Vana Data Portability",
32770
+ version: "1",
32771
+ chainId: input.chainId ?? PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID,
32772
+ verifyingContract
32773
+ };
32774
+ }
32775
+ function buildPersonalServerRegistrationTypedData(input) {
32776
+ assertAddress(input.ownerAddress, "ownerAddress");
32777
+ assertAddress(input.serverAddress, "serverAddress");
32778
+ return {
32779
+ domain: personalServerRegistrationDomain(input),
32780
+ types: SERVER_REGISTRATION_TYPES,
32781
+ primaryType: "ServerRegistration",
32782
+ message: {
32783
+ ownerAddress: input.ownerAddress,
32784
+ serverAddress: input.serverAddress,
32785
+ publicKey: input.serverPublicKey,
32786
+ serverUrl: input.serverUrl
32787
+ }
32788
+ };
32789
+ }
32790
+ async function buildPersonalServerRegistrationSignature(input) {
32791
+ const typedData = buildPersonalServerRegistrationTypedData({
32792
+ ownerAddress: input.signer.address,
32793
+ serverAddress: input.serverAddress,
32794
+ serverPublicKey: input.serverPublicKey,
32795
+ serverUrl: input.serverUrl,
32796
+ config: input.config,
32797
+ chainId: input.chainId,
32798
+ verifyingContract: input.verifyingContract
32799
+ });
32800
+ const signature = await input.signer.signTypedData(typedData);
32801
+ return {
32802
+ signature,
32803
+ signerAddress: input.signer.address,
32804
+ typedData
32805
+ };
32806
+ }
32807
+ var registerPersonalServerSignature = buildPersonalServerRegistrationSignature;
32808
+
32809
+ // src/protocol/personal-server-lite-owner-binding.ts
32810
+ var import_viem15 = require("viem");
32811
+ var PERSONAL_SERVER_LITE_OWNER_BINDING_VERSION = "vana.account.v1";
32812
+ var PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE = "ps-lite-owner";
32813
+ var PERSONAL_SERVER_LITE_OWNER_BINDING_PREFIX = `${PERSONAL_SERVER_LITE_OWNER_BINDING_VERSION}:${PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE}:`;
32814
+ function assertAddress2(value, name) {
32815
+ if (!(0, import_viem15.isAddress)(value)) {
32816
+ throw new Error(`${name} must be a valid EVM address`);
32817
+ }
32818
+ }
32819
+ function getAccountAddress2(account) {
32820
+ if (!account) {
32821
+ return void 0;
32822
+ }
32823
+ return typeof account === "string" ? account : account.address;
32824
+ }
32825
+ function isPersonalServerLiteOwnerBindingSigner(source) {
32826
+ return "address" in source && typeof source.signMessage === "function";
32827
+ }
32828
+ function buildPersonalServerLiteOwnerBindingMessage(ownerAddress) {
32829
+ assertAddress2(ownerAddress, "ownerAddress");
32830
+ return `${PERSONAL_SERVER_LITE_OWNER_BINDING_PREFIX}${ownerAddress.toLowerCase()}`;
32831
+ }
32832
+ function createViemPersonalServerLiteOwnerBindingSigner(source, options = {}) {
32833
+ if (isPersonalServerLiteOwnerBindingSigner(source)) {
32834
+ return source;
32835
+ }
32836
+ const accountAddress = getAccountAddress2(options.account) ?? getAccountAddress2(source.account);
32837
+ if (accountAddress) {
32838
+ return {
32839
+ address: accountAddress,
32840
+ signMessage: ({ message }) => source.signMessage({
32841
+ account: options.account ?? source.account ?? accountAddress,
32842
+ message
32843
+ })
32844
+ };
32845
+ }
32846
+ throw new Error(
32847
+ "Viem wallet client requires an account option or account property"
32848
+ );
32849
+ }
32850
+ async function buildPersonalServerLiteOwnerBindingSignature(input) {
32851
+ const message = buildPersonalServerLiteOwnerBindingMessage(
32852
+ input.signer.address
32853
+ );
32854
+ const signature = await input.signer.signMessage({ message });
32855
+ return {
32856
+ signature,
32857
+ signerAddress: input.signer.address,
32858
+ message,
32859
+ purpose: PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE
32860
+ };
32861
+ }
32862
+ var signPersonalServerLiteOwnerBinding = buildPersonalServerLiteOwnerBindingSignature;
32863
+
32864
+ // src/account/personal-server-registration.ts
32865
+ var import_viem16 = require("viem");
32866
+ var ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT = "personal_server.server_registration.v1";
32867
+ var AccountPersonalServerRegistrationError = class extends Error {
32868
+ status;
32869
+ code;
32870
+ details;
32871
+ constructor(input) {
32872
+ super(input.message);
32873
+ this.name = "AccountPersonalServerRegistrationError";
32874
+ this.status = input.status;
32875
+ this.code = input.code;
32876
+ this.details = input.details;
32877
+ }
32878
+ };
32879
+ var DEFAULT_ACCOUNT_PS_REGISTRATION_PATH = "/api/v1/intents/personal-server-registration/sign";
32880
+ function trimTrailingSlash(value) {
32881
+ return value.replace(/\/+$/, "");
32882
+ }
32883
+ function assertAddress3(value, name) {
32884
+ if (!(0, import_viem16.isAddress)(value)) {
32885
+ throw new Error(`${name} must be a valid EVM address`);
32886
+ }
32887
+ }
32888
+ async function parseAccountResponse(response) {
32889
+ const body = await response.json().catch(() => void 0);
32890
+ if (!response.ok) {
32891
+ throw new AccountPersonalServerRegistrationError({
32892
+ status: response.status,
32893
+ code: accountErrorCode(body),
32894
+ message: accountErrorMessage(response.status, body),
32895
+ details: body
32896
+ });
32897
+ }
32898
+ return body;
32899
+ }
32900
+ function accountErrorMessage(status, body) {
32901
+ const nestedMessage = nestedAccountErrorField(body, "message");
32902
+ if (nestedMessage) {
32903
+ return nestedMessage;
32904
+ }
32905
+ if (isRecord(body) && typeof body.message === "string") {
32906
+ return body.message;
32907
+ }
32908
+ const code = accountErrorCode(body);
32909
+ if (code) {
32910
+ return `Account PS registration signing failed: ${code}`;
32911
+ }
32912
+ return `Account PS registration signing failed: ${status}`;
32913
+ }
32914
+ function accountErrorCode(body) {
32915
+ const nestedCode = nestedAccountErrorField(body, "code");
32916
+ if (nestedCode) {
32917
+ return nestedCode;
32918
+ }
32919
+ if (isRecord(body)) {
32920
+ if (typeof body.code === "string") {
32921
+ return body.code;
32922
+ }
32923
+ if (typeof body.error === "string") {
32924
+ return body.error;
32925
+ }
32926
+ }
32927
+ return void 0;
32928
+ }
32929
+ function nestedAccountErrorField(body, field) {
32930
+ if (!isRecord(body) || !isRecord(body.error)) {
32931
+ return void 0;
32932
+ }
32933
+ const value = body.error[field];
32934
+ return typeof value === "string" ? value : void 0;
32935
+ }
32936
+ function isRecord(value) {
32937
+ return typeof value === "object" && value !== null;
32938
+ }
32939
+ function normalizeAccountResponse(response) {
32940
+ return {
32941
+ ...response,
32942
+ status: response.status === "fallback_required" ? "confirmation_required" : response.status,
32943
+ signerAddress: response.signerAddress ?? response.signer?.address,
32944
+ typedData: response.typedData ?? response.typed_data
32945
+ };
32946
+ }
32947
+ function buildSignedResult(response, request) {
32948
+ assertAddress3(response.signerAddress, "signerAddress");
32949
+ if (response.typedData) {
32950
+ assertTypedDataMatchesRequest(
32951
+ response.typedData,
32952
+ request,
32953
+ response.signerAddress
32954
+ );
32955
+ }
32956
+ return {
32957
+ signature: response.signature,
32958
+ signerAddress: response.signerAddress,
32959
+ typedData: response.typedData ?? buildPersonalServerRegistrationTypedData({
32960
+ ownerAddress: response.signerAddress,
32961
+ ...request
32962
+ }),
32963
+ intent: ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT
32964
+ };
32965
+ }
32966
+ function assertTypedDataMatchesRequest(typedData, request, expectedSignerAddress) {
32967
+ assertAddress3(
32968
+ typedData.message.ownerAddress,
32969
+ "typedData.message.ownerAddress"
32970
+ );
32971
+ assertAddress3(
32972
+ typedData.message.serverAddress,
32973
+ "typedData.message.serverAddress"
32974
+ );
32975
+ if (expectedSignerAddress && !sameAddress(typedData.message.ownerAddress, expectedSignerAddress)) {
32976
+ throw new Error(
32977
+ "Account typedData ownerAddress must match the expected signer address"
32978
+ );
32979
+ }
32980
+ if (!sameAddress(typedData.message.serverAddress, request.serverAddress)) {
32981
+ throw new Error(
32982
+ "Account typedData serverAddress must match the requested serverAddress"
32983
+ );
32984
+ }
32985
+ if (typedData.message.publicKey !== request.serverPublicKey) {
32986
+ throw new Error(
32987
+ "Account typedData publicKey must match the requested serverPublicKey"
32988
+ );
32989
+ }
32990
+ if (typedData.message.serverUrl !== request.serverUrl) {
32991
+ throw new Error(
32992
+ "Account typedData serverUrl must match the requested serverUrl"
32993
+ );
32994
+ }
32995
+ if (typedData.primaryType !== "ServerRegistration") {
32996
+ throw new Error("Account typedData primaryType must be ServerRegistration");
32997
+ }
32998
+ if (JSON.stringify(typedData.types) !== JSON.stringify(SERVER_REGISTRATION_TYPES)) {
32999
+ throw new Error("Account typedData types must be ServerRegistration types");
33000
+ }
33001
+ const expectedDomain = personalServerRegistrationDomain({
33002
+ config: request.config,
33003
+ chainId: request.chainId,
33004
+ verifyingContract: request.verifyingContract
33005
+ });
33006
+ if (!domainsEqual(typedData.domain, expectedDomain)) {
33007
+ throw new Error("Account typedData domain must match the requested domain");
33008
+ }
33009
+ }
33010
+ function sameAddress(a, b) {
33011
+ return a.toLowerCase() === b.toLowerCase();
33012
+ }
33013
+ function domainsEqual(a, b) {
33014
+ if (!a || !b) {
33015
+ return false;
33016
+ }
33017
+ return a.name === b.name && a.version === b.version && Number(a.chainId) === Number(b.chainId) && String(a.verifyingContract ?? "").toLowerCase() === String(b.verifyingContract ?? "").toLowerCase() && a.salt === b.salt;
33018
+ }
33019
+ async function signPersonalServerRegistrationWithAccount(config, request) {
33020
+ assertAddress3(request.serverAddress, "serverAddress");
33021
+ const fetchImpl = config.fetchImpl ?? globalThis.fetch.bind(globalThis);
33022
+ const endpoint = new URL(
33023
+ config.endpointPath ?? DEFAULT_ACCOUNT_PS_REGISTRATION_PATH,
33024
+ `${trimTrailingSlash(config.accountOrigin)}/`
33025
+ );
33026
+ const response = await fetchImpl(endpoint, {
33027
+ method: "POST",
33028
+ headers: { "content-type": "application/json" },
33029
+ credentials: "include",
33030
+ body: JSON.stringify({
33031
+ intent: ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT,
33032
+ serverAddress: request.serverAddress,
33033
+ serverPublicKey: request.serverPublicKey,
33034
+ serverUrl: request.serverUrl,
33035
+ config: request.config,
33036
+ chainId: request.chainId,
33037
+ verifyingContract: request.verifyingContract
33038
+ })
33039
+ });
33040
+ const body = normalizeAccountResponse(await parseAccountResponse(response));
33041
+ if (body.status === "signed") {
33042
+ if (!body.signature || !body.signerAddress) {
33043
+ throw new Error(
33044
+ "Account signed response must include signature and signerAddress"
33045
+ );
33046
+ }
33047
+ return {
33048
+ status: "signed",
33049
+ result: buildSignedResult(
33050
+ {
33051
+ signature: body.signature,
33052
+ signerAddress: body.signerAddress,
33053
+ typedData: body.typedData
33054
+ },
33055
+ request
33056
+ )
33057
+ };
33058
+ }
33059
+ if (body.status === "confirmation_required") {
33060
+ if (!body.typedData) {
33061
+ throw new Error(
33062
+ "Account confirmation_required response must include typedData"
33063
+ );
33064
+ }
33065
+ assertTypedDataMatchesRequest(body.typedData, request, body.signerAddress);
33066
+ if (!config.fallbackSigner) {
33067
+ return {
33068
+ status: "confirmation_required",
33069
+ typedData: body.typedData,
33070
+ signerAddress: body.signerAddress
33071
+ };
33072
+ }
33073
+ assertTypedDataMatchesRequest(
33074
+ body.typedData,
33075
+ request,
33076
+ config.fallbackSigner.address
33077
+ );
33078
+ const signature = await config.fallbackSigner.signTypedData(body.typedData);
33079
+ return {
33080
+ status: "fallback_signed",
33081
+ accountStatus: "confirmation_required",
33082
+ result: {
33083
+ signature,
33084
+ signerAddress: config.fallbackSigner.address,
33085
+ typedData: body.typedData,
33086
+ intent: ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT
33087
+ }
33088
+ };
33089
+ }
33090
+ throw new Error(
33091
+ `Unsupported Account PS registration signing status: ${String(body.status)}`
33092
+ );
33093
+ }
33094
+
33095
+ // src/account/personal-server-lite-owner-binding.ts
33096
+ var AccountPersonalServerLiteOwnerBindingError = class extends Error {
33097
+ code;
33098
+ details;
33099
+ constructor(input) {
33100
+ super(input.message);
33101
+ this.name = "AccountPersonalServerLiteOwnerBindingError";
33102
+ this.code = input.code;
33103
+ this.details = input.details;
33104
+ }
33105
+ };
33106
+ async function signPersonalServerLiteOwnerBindingWithAccountClient(config) {
33107
+ let address;
33108
+ try {
33109
+ address = await config.client.getAddress();
33110
+ } catch (error) {
33111
+ throw accountOwnerBindingError(error);
33112
+ }
33113
+ if (!address) {
33114
+ throw new AccountPersonalServerLiteOwnerBindingError({
33115
+ message: "Account did not return a wallet address",
33116
+ code: "account_address_required"
33117
+ });
33118
+ }
33119
+ const message = buildPersonalServerLiteOwnerBindingMessage(address);
33120
+ let signature;
33121
+ try {
33122
+ signature = await config.client.signMessage({ message });
33123
+ } catch (error) {
33124
+ throw accountOwnerBindingError(error);
33125
+ }
33126
+ return {
33127
+ signature,
33128
+ signerAddress: address,
33129
+ message,
33130
+ purpose: PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE
33131
+ };
33132
+ }
33133
+ function accountOwnerBindingError(error) {
33134
+ if (error instanceof AccountPersonalServerLiteOwnerBindingError) {
33135
+ return error;
33136
+ }
33137
+ const rpcError = error;
33138
+ const code = rpcError?.code;
33139
+ const message = typeof rpcError?.message === "string" && rpcError.message.length > 0 ? rpcError.message : "Account PS Lite owner-binding signature failed";
33140
+ return new AccountPersonalServerLiteOwnerBindingError({
33141
+ message,
33142
+ code,
33143
+ details: error
33144
+ });
33145
+ }
33146
+
33147
+ // src/protocol/grants.ts
33148
+ var import_viem17 = require("viem");
32486
33149
  function isHexString(value) {
32487
33150
  return typeof value === "string" && value.startsWith("0x");
32488
33151
  }
@@ -32560,7 +33223,7 @@ async function verifyGrantRegistration(input) {
32560
33223
  }
32561
33224
  let valid;
32562
33225
  try {
32563
- valid = await (0, import_viem14.verifyTypedData)({
33226
+ valid = await (0, import_viem17.verifyTypedData)({
32564
33227
  address: input.grantorAddress,
32565
33228
  domain: grantRegistrationDomain(input.gatewayConfig),
32566
33229
  types: GRANT_REGISTRATION_TYPES,
@@ -32891,7 +33554,7 @@ var KNOWN_CODES = /* @__PURE__ */ new Set([
32891
33554
  "server_not_configured",
32892
33555
  "content_too_large"
32893
33556
  ]);
32894
- function isRecord(value) {
33557
+ function isRecord2(value) {
32895
33558
  return value !== null && typeof value === "object" && !Array.isArray(value);
32896
33559
  }
32897
33560
  function normalizeCode(value) {
@@ -32902,10 +33565,10 @@ function normalizeCode(value) {
32902
33565
  return KNOWN_CODES.has(code) ? code : null;
32903
33566
  }
32904
33567
  function extractPSErrorBody(body) {
32905
- if (!isRecord(body)) {
33568
+ if (!isRecord2(body)) {
32906
33569
  return null;
32907
33570
  }
32908
- const nested = isRecord(body.error) ? body.error : null;
33571
+ const nested = isRecord2(body.error) ? body.error : null;
32909
33572
  const code = normalizeCode(
32910
33573
  nested?.errorCode ?? nested?.code ?? body.errorCode ?? body.code
32911
33574
  );
@@ -32930,6 +33593,9 @@ async function parsePSError(response) {
32930
33593
  }
32931
33594
  // Annotate the CommonJS export names for ESM import in node:
32932
33595
  0 && (module.exports = {
33596
+ ACCOUNT_PERSONAL_SERVER_REGISTRATION_INTENT,
33597
+ AccountPersonalServerLiteOwnerBindingError,
33598
+ AccountPersonalServerRegistrationError,
32933
33599
  BUILDER_REGISTRATION_TYPES,
32934
33600
  BlockchainError,
32935
33601
  BrowserPlatformAdapter,
@@ -32956,6 +33622,12 @@ async function parsePSError(response) {
32956
33622
  NodeECIESProvider,
32957
33623
  NodePlatformAdapter,
32958
33624
  NonceError,
33625
+ OAuthClient,
33626
+ PERSONAL_SERVER_LITE_OWNER_BINDING_PREFIX,
33627
+ PERSONAL_SERVER_LITE_OWNER_BINDING_PURPOSE,
33628
+ PERSONAL_SERVER_LITE_OWNER_BINDING_VERSION,
33629
+ PERSONAL_SERVER_REGISTRATION_DEFAULT_CHAIN_ID,
33630
+ PERSONAL_SERVER_REGISTRATION_DEFAULT_VERIFYING_CONTRACT,
32959
33631
  PKCE_CHALLENGE_PATTERN,
32960
33632
  PKCE_VERIFIER_PATTERN,
32961
33633
  PSError,
@@ -32977,6 +33649,10 @@ async function parsePSError(response) {
32977
33649
  VanaError,
32978
33650
  VanaStorage,
32979
33651
  assertValidPkceVerifier,
33652
+ buildPersonalServerLiteOwnerBindingMessage,
33653
+ buildPersonalServerLiteOwnerBindingSignature,
33654
+ buildPersonalServerRegistrationSignature,
33655
+ buildPersonalServerRegistrationTypedData,
32980
33656
  buildWeb3SignedHeader,
32981
33657
  builderRegistrationDomain,
32982
33658
  chains,
@@ -32992,6 +33668,8 @@ async function parsePSError(response) {
32992
33668
  createPlatformAdapterFor,
32993
33669
  createPlatformAdapterSafe,
32994
33670
  createVanaStorageProvider,
33671
+ createViemPersonalServerLiteOwnerBindingSigner,
33672
+ createViemPersonalServerRegistrationSigner,
32995
33673
  decryptWithPassword,
32996
33674
  deriveMasterKey,
32997
33675
  deriveScopeKey,
@@ -33021,12 +33699,17 @@ async function parsePSError(response) {
33021
33699
  parsePSError,
33022
33700
  parseScope,
33023
33701
  parseWeb3SignedHeader,
33702
+ personalServerRegistrationDomain,
33024
33703
  recoverServerOwner,
33704
+ registerPersonalServerSignature,
33025
33705
  scopeCoveredByGrant,
33026
33706
  scopeMatchesPattern,
33027
33707
  scopeToPathSegments,
33028
33708
  serializeECIES,
33029
33709
  serverRegistrationDomain,
33710
+ signPersonalServerLiteOwnerBinding,
33711
+ signPersonalServerLiteOwnerBindingWithAccountClient,
33712
+ signPersonalServerRegistrationWithAccount,
33030
33713
  vanaMainnet,
33031
33714
  verifyGrantRegistration,
33032
33715
  verifyPkceChallenge,