aftermath-ts-sdk
Version:
Aftermath TypeScript SDK
135 lines • 5.29 kB
TypeScript
import { CallerConfig, SignMessageCallback, SuiAddress } from "../../types";
import { Caller } from "../../general/utils/caller";
import { RateLimit } from "./authTypes";
/**
* The `Auth` class manages creation and refreshing of access tokens
* to obtain higher rate limits on the Aftermath API. It includes methods
* to initialize authorization either by a direct callback-based approach
* or by importing a local Sui keystore. Optionally, administrative functions
* are provided for creating specialized auth accounts.
*
* @example
* ```typescript
* const auth = new Auth();
* const stopAuth = await auth.init({
* walletAddress: "0x<address>",
* signMessageCallback: async ({ message }) => {
* // sign message
* },
* });
* // ... make authenticated requests ...
* stopAuth(); // stop auto refresh
* ```
*/
export declare class Auth extends Caller {
/**
* Holds the timer reference for scheduled token refreshes.
*/
private refreshTimer;
/**
* Indicates whether the user has canceled auto token refresh.
*/
private isCanceled;
/**
* Creates a new `Auth` instance for token-based rate limit increases.
*
* @param config - Optional caller configuration, including network and access token.
*/
constructor(config?: CallerConfig);
/**
* Initializes the auth system by fetching an access token for the provided wallet address.
* After obtaining the token, it automatically schedules periodic refresh calls until stopped.
*
* @param inputs - An object containing the user's `walletAddress` and a `signMessageCallback` function
* for cryptographically signing messages.
*
* @returns A function that, when called, cancels further token refresh attempts.
*
* @example
* ```typescript
* const auth = new Auth();
* const stopAuth = await auth.init({
* walletAddress: "0x<address>",
* signMessageCallback: async ({ message }) => {
* // sign the message with your private key / keypair
* },
* });
*
* // ... make authorized calls ...
*
* stopAuth(); // Cancel further token refreshes
* ```
*/
init(inputs: {
walletAddress: SuiAddress;
signMessageCallback: SignMessageCallback;
}): Promise<() => void>;
/**
* Initializes the auth system by reading a local Sui keystore file (on the server side),
* using the private keys matching a provided address to sign messages for token creation.
* After the token is obtained, it automatically schedules periodic refresh calls until stopped.
*
* @param inputs - An object containing the target `walletAddress` and an optional path to the `.keystore`.
* If `path` is not provided, it defaults to `~/.sui/sui_config/sui.keystore`.
* @returns A function that, when called, cancels further token refresh attempts.
*
* @throws If this method is called in a browser environment (client-side).
*
* @example
* ```typescript
* // On server:
* const stopAuth = await auth.initFromSuiKeystore({
* walletAddress: "0x<address>",
* path: "/custom/path/to/keystore.json",
* });
* // authorized calls...
* stopAuth();
* ```
*/
/**
* **Admin-only**: Creates a new auth account with specific rate limits for a given
* `accountWalletAddress`. The `walletAddress` performing this action must have
* admin privileges, or the call will fail. Use this to create custom sub-accounts
* with limited scope or usage rates.
*
* @param inputs - Contains:
* - `walletAddress`: The admin's wallet address
* - `signMessageCallback`: The admin's signing callback
* - `accountName`: A short name or identifier for the account
* - `accountWalletAddress`: The Sui address representing this sub-account
* - `rateLimits`: An array specifying the rate limits (method-based) for the sub-account
* @returns A promise resolving to `true` if successful, otherwise throws or returns `false`.
*/
adminCreateAuthAccount(inputs: {
walletAddress: SuiAddress;
signMessageCallback: SignMessageCallback;
accountName: string;
accountWalletAddress: SuiAddress;
rateLimits: RateLimit[];
}): Promise<boolean>;
/**
* Requests a new access token from the API by sending a signed message
* indicating the user wants a token.
*
* @param inputs - Contains the user's `walletAddress` and `signMessageCallback`.
* @returns A response object that includes the `accessToken` and an `expirationTimestamp`.
*/
private getAccessToken;
/**
* Creates a JSON string with a standard format:
* ```json
* {
* "date": <epoch-seconds>,
* "nonce": <random_number>,
* "method": <method_string>,
* "value": <passed_value>
* }
* ```
*
* @param method - A short method name describing the action ("GetAccessToken", "AccountCreate", etc.).
* @param value - The data object to embed under the `value` field.
* @returns A JSON-serialized string for signing.
*/
private static createSerializedJson;
}
//# sourceMappingURL=auth.d.ts.map