import { Account, AccountAddressInput } from '@aptos-labs/ts-sdk'; import { ShelbyBlob } from '../blobs.js'; import { BlobCommitments } from '../commitments.js'; import { ShelbyIndexerClient } from '../operations/index.js'; import { ChallengeResponse } from '../rpc-responses.js'; import { StorageProviderAck } from '../types/blobs.js'; import { SenderBuiltMicropayment } from '../types/payments.js'; import { ShelbyRPCConfig, ShelbyClientConfig } from './ShelbyClientConfig.js'; import '../layout.js'; import 'zod'; import '../../clay-codes-DdXABBDx.js'; import '@shelby-protocol/clay-codes'; import 'graphql-request'; import '../operations/generated/sdk.js'; import 'graphql'; import '../networks.js'; /** * Auth scheme identifier sent in X-Shelby-Auth-Scheme. * - "ed25519": standard Ed25519 account (default when header is absent) * - "derivable": Derived Abstracted Account (Solana, Ethereum, etc.) */ type AuthScheme = "ed25519" | "derivable"; interface BlobOwnerAuthBase { challenge: string; signature: Uint8Array; publicKey: Uint8Array; } interface Ed25519BlobOwnerAuth extends BlobOwnerAuthBase { authScheme?: "ed25519"; } interface DerivableBlobOwnerAuth extends BlobOwnerAuthBase { authScheme: "derivable"; /** External identity (e.g. Solana base58 pubkey). */ identity: string; /** dApp domain used during key derivation. */ domain: string; /** On-chain authentication function (e.g. "0x1::solana_derivable_account::authenticate"). */ authFunction: string; } /** * Authentication credentials for blob owner operations. * * Ed25519 (default): only the base fields are needed. * Derivable: requires identity, domain, and authFunction. */ type BlobOwnerAuth = Ed25519BlobOwnerAuth | DerivableBlobOwnerAuth; /** * Who owns the blob being written. * * An `Account` can sign the ownership challenge, so the SDK fetches one and * signs it. A signer that holds no private key — a browser wallet — can only * name its address, and the write goes out unauthenticated; the server accepts * it unless it is configured to require upload auth. */ type BlobOwnerCredentials = { account: Account; } | { accountAddress: AccountAddressInput; }; type BlobDataSource = Uint8Array | ReadableStream; type PutBlobChunksetsProgress = { phase: "uploading"; chunksetIdx: number; totalChunksets: number; chunksetBytes: number; uploadedBytes: number; totalBytes: number; acksReceived?: number; /** SP blob acknowledgements from this chunkset (if any) */ spAcks?: StorageProviderAck[]; }; /** * Result of putBlobChunksets operation. */ type PutBlobChunksetsResult = { /** * Aggregated SP blob acknowledgements collected across all chunksets. * Deduplicated by slot (latest signature wins if an SP acks multiple times). */ spAcks: StorageProviderAck[]; }; declare class ShelbyRPCClient { #private; readonly baseUrl: string; readonly apiKey: string | undefined; readonly rpcConfig: ShelbyRPCConfig; readonly indexer: ShelbyIndexerClient; /** * Creates a new ShelbyRPCClient for interacting with Shelby RPC nodes. * This client handles blob storage operations including upload and download. * * @param config - The client configuration object. * @param config.network - The Shelby network to use. * @param options.signChallengeHandler - Optional override for challenge * signing. When set, `putBlobChunksets` uses this instead of the built-in * `signChallenge`. Intended for kit-level overrides (e.g. Solana DAA). * * @example * ```typescript * const client = new ShelbyRPCClient({ * network: Network.SHELBYNET, * apiKey: "AG-***", * }); * ``` */ constructor(config: ShelbyClientConfig, options?: { signChallengeHandler?: (account: Account, challenge: string) => BlobOwnerAuth; }); /** * Request an authentication challenge for the given account. * The challenge must be signed and included in subsequent authenticated requests. * * @param account - The Aptos account address to authenticate as. * @returns The challenge string and expiration timestamp. * * @example * ```typescript * const { challenge, expiresAt } = await client.getChallenge(account.accountAddress); * const auth = client.signChallenge(account, challenge); * ``` */ getChallenge(account: AccountAddressInput): Promise; /** * Sign a challenge using the given account and return auth credentials. * * @param account - The Aptos account to sign with. * @param challenge - The hex-encoded challenge string from getChallenge(). * @returns BlobOwnerAuth credentials for authenticated requests. */ signChallenge(account: Account, challenge: string): BlobOwnerAuth; /** * Uploads blob data to the Shelby RPC node using the v2 chunkset API. * This method authenticates using challenge-response and uploads chunksets * directly to storage providers via the RPC's worker pool. * * This method: * - Sends raw chunkset data directly to the RPC for erasure encoding * - Requires pre-computed blob commitments to generate inclusion proofs * - Does not support resume (each chunkset is idempotent on the SP side) * * @param params.account - The Aptos Account (with signing capability) that owns the blob. * Signers that cannot sign raw bytes pass `accountAddress` instead, which * sends the write unauthenticated. * @param params.uid - The blob's on-chain UID (from `BlobRegisteredEvent` at registration). * @param params.blobData - The raw blob data as a Uint8Array or ReadableStream. * @param params.commitments - Pre-computed blob commitments (from generateCommitments). * @param params.totalBytes - Total byte length. Required for streams; optional for Uint8Array. * @param params.chunksetConcurrency - Number of chunksets to upload in parallel. Defaults to 4. * @param params.onProgress - Optional callback for upload progress. * @param params.signal - Optional AbortSignal for cancellation. When aborted, in-flight * HTTP requests are cancelled and an AbortError is thrown. * * @example * ```typescript * // First, generate commitments for the blob * const commitments = await generateCommitments(provider, fileData); * * // Register the blob on chain, then read its UID from the register tx's * // BlobRegisteredEvent (see ShelbyBlobClient.registeredBlobUids). * * // Upload using chunkset API * await rpcClient.putBlobChunksets({ * account: myAccount, * uid: blobUid, * blobData: fileData, * commitments, * chunksetConcurrency: 8, * }); * ``` */ putBlobChunksets(params: BlobOwnerCredentials & { uid: bigint; blobData: BlobDataSource; commitments: BlobCommitments; totalBytes?: number; chunksetConcurrency?: number; onProgress?: (progress: PutBlobChunksetsProgress) => void; signal?: AbortSignal; }): Promise; /** * Downloads a blob from the Shelby RPC node. * Returns a streaming response with validation to ensure data integrity. * * @param params.account - The account that owns the blob. * @param params.blobName - The name/path of the blob (e.g. "folder/file.txt"). * @param params.range - Optional byte range for partial downloads. * @param params.range.start - Starting byte position (inclusive). * @param params.range.end - Ending byte position (inclusive, optional). * @param params.micropayment - Optional micropayment to attach to the request. * * @returns A ShelbyBlob object containing the account, name, readable stream, and content length. * * @throws Error if the download fails or content length doesn't match. * @throws StaleChannelStateError if the micropayment is stale (server has newer state). * * @example * ```typescript * // Download entire blob * const blob = await client.getBlob({ * account: AccountAddress.from("0x1"), * blobName: "documents/report.pdf" * }); * * // Download partial content (bytes 100-199) * const partial = await client.getBlob({ * account: AccountAddress.from("0x1"), * blobName: "large-file.bin", * range: { start: 100, end: 199 } * }); * * // Download with micropayment * const blob = await client.getBlob({ * account: AccountAddress.from("0x1"), * blobName: "documents/report.pdf", * micropayment: senderBuiltMicropayment * }); * ``` */ getBlob(params: { account: AccountAddressInput; blobName: string; range?: { start: number; end?: number; }; micropayment?: SenderBuiltMicropayment; }): Promise; } export { type AuthScheme, type BlobDataSource, type BlobOwnerAuth, type BlobOwnerCredentials, type DerivableBlobOwnerAuth, type Ed25519BlobOwnerAuth, type PutBlobChunksetsProgress, type PutBlobChunksetsResult, ShelbyRPCClient };