Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HttpAgent

A HTTP agent allows users to interact with a client of the internet computer using the available methods. It exposes an API that closely follows the public view of the internet computer, and is not intended to be exposed directly to the majority of users due to its low-level interface. There is a pipeline to apply transformations to the request before sending it to the client. This is to decouple signature, nonce generation and other computations so that this class can stay as simple as possible while allowing extensions.

Hierarchy

  • HttpAgent

Implements

Index

Constructors

Properties

#backoffStrategy: BackoffStrategyFactory
#callOptions?: Record<string, unknown>
#credentials: undefined | string
#fetch: { (input: RequestInfo | URL, init?: RequestInit): Promise<Response>; (input: string | Request | URL, init?: RequestInit): Promise<Response> }

Type declaration

    • (input: RequestInfo | URL, init?: RequestInit): Promise<Response>
    • (input: string | Request | URL, init?: RequestInit): Promise<Response>
    • Parameters

      • input: RequestInfo | URL
      • Optional init: RequestInit

      Returns Promise<Response>

    • Parameters

      • input: string | Request | URL
      • Optional init: RequestInit

      Returns Promise<Response>

#fetchOptions?: Record<string, unknown>
#identity: null | Promise<Identity>
#initialClientTime: Date = ...
#initialReplicaTime: Date = ...
#queryPipeline: HttpAgentRequestTransformFn[] = []
#retryTimes: number
#rootKeyFetched: boolean = false
#subnetKeys: ExpirableMap<string, SubnetStatus> = ...
#updatePipeline: HttpAgentRequestTransformFn[] = []
#verifyQuerySignatures: boolean = true
#waterMark: number = 0
_isAgent: true = true
config: HttpAgentOptions = {}
host: URL
log: ObservableLog = ...
rootKey: ArrayBuffer

Accessors

  • get replicaTime(): Date
  • set replicaTime(replicaTime: Date): void
  • get waterMark(): number

Methods

  • #handleReplicaTimeError(error: AgentError): void
  • #requestAndRetry(args: { backoff: BackoffStrategy; tries: number; request: any }): Promise<Response>
  • call(canisterId: string | Principal, options: { arg: ArrayBuffer; callSync?: boolean; effectiveCanisterId?: string | Principal; methodName: string }, identity?: Identity | Promise<Identity>): Promise<SubmitResponse>
  • Parameters

    • canisterId: string | Principal
    • options: { arg: ArrayBuffer; callSync?: boolean; effectiveCanisterId?: string | Principal; methodName: string }
      • arg: ArrayBuffer
      • Optional callSync?: boolean
      • Optional effectiveCanisterId?: string | Principal
      • methodName: string
    • Optional identity: Identity | Promise<Identity>

    Returns Promise<SubmitResponse>

  • fetchRootKey(): Promise<ArrayBuffer>
  • By default, the agent is configured to talk to the main Internet Computer, and verifies responses using a hard-coded public key.

    This function will instruct the agent to ask the endpoint for its public key, and use that instead. This is required when talking to a local test instance, for example.

    Only use this when you are not talking to the main Internet Computer, otherwise you are prone to man-in-the-middle attacks! Do not call this function by default.

    Returns Promise<ArrayBuffer>

  • fetchSubnetKeys(canisterId: string | Principal): Promise<undefined | SubnetStatus>
  • getPrincipal(): Promise<Principal>
  • Returns the principal ID associated with this agent (by default). It only shows the principal of the default identity in the agent, which is the principal used when calls don't specify it.

    Returns Promise<Principal>

  • invalidateIdentity(): void
  • If an application needs to invalidate an identity under certain conditions, an Agent may expose an invalidateIdentity method. Invoking this method will set the inner identity used by the Agent to null.

    A use case for this would be - after a certain period of inactivity, a secure application chooses to invalidate the identity of any HttpAgent instances. An invalid identity can be replaced by Agent.replaceIdentity

    Returns void

  • isLocal(): boolean
  • replaceIdentity(identity: Identity): void
  • If an application needs to replace an identity under certain conditions, an Agent may expose a replaceIdentity method. Invoking this method will set the inner identity used by the Agent to a newly provided identity.

    A use case for this would be - after authenticating using @dfinity/auth-client, you can replace the AnonymousIdentity of your Actor with a DelegationIdentity.

    Actor.agentOf(defaultActor).replaceIdentity(await authClient.getIdentity());

    Parameters

    Returns void

  • status(): Promise<JsonObject>
  • Query the status endpoint of the replica. This normally has a few fields that corresponds to the version of the replica, its root public key, and any other information made public.

    Returns Promise<JsonObject>

    A JsonObject that is essentially a record of fields from the status endpoint.

  • syncTime(canisterId?: Principal): Promise<void>
  • Allows agent to sync its time with the network. Can be called during intialization or mid-lifecycle if the device's clock has drifted away from the network time. This is necessary to set the Expiry for a request

    throws

    {ReplicaTimeError} - this method is not guaranteed to work if the device's clock is off by more than 30 seconds. In such cases, the agent will throw an error.

    Parameters

    • Optional canisterId: Principal

      Pass a canister ID if you need to sync the time with a particular replica. Uses the management canister by default

    Returns Promise<void>

Generated using TypeDoc