
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
@dfinity/agent
Advanced tools
JavaScript and TypeScript library to interact with the Internet Computer
@dfinity/agent is a JavaScript library that allows developers to interact with the Internet Computer, a decentralized computing platform. It provides tools to create, manage, and communicate with canisters (smart contracts) on the Internet Computer.
Creating an HTTP Agent
This feature allows you to create an HTTP agent to communicate with the Internet Computer. The agent is configured with a host URL, which is the entry point to the network.
const { HttpAgent } = require('@dfinity/agent');
const agent = new HttpAgent({ host: 'https://ic0.app' });
Interacting with Canisters
This feature demonstrates how to interact with a canister using an actor. You define an interface for the canister and create an actor to call its methods, such as 'greet' in this example.
const { Actor, HttpAgent } = require('@dfinity/agent');
const agent = new HttpAgent();
const canisterId = 'your-canister-id';
const interfaceFactory = ({ IDL }) => IDL.Service({
greet: IDL.Func([IDL.Text], [IDL.Text], []),
});
const actor = Actor.createActor(interfaceFactory, { agent, canisterId });
const response = await actor.greet('World');
Querying and Updating Canister State
This feature shows how to query and update the state of a canister. The 'getValue' method is a query call, while 'setValue' is an update call, demonstrating both read and write operations.
const { Actor, HttpAgent } = require('@dfinity/agent');
const agent = new HttpAgent();
const canisterId = 'your-canister-id';
const interfaceFactory = ({ IDL }) => IDL.Service({
getValue: IDL.Func([], [IDL.Nat], ['query']),
setValue: IDL.Func([IDL.Nat], [], []),
});
const actor = Actor.createActor(interfaceFactory, { agent, canisterId });
const currentValue = await actor.getValue();
await actor.setValue(currentValue + 1);
Ethers.js is a library for interacting with the Ethereum blockchain. Like @dfinity/agent, it provides tools for creating and managing connections to a decentralized network, but it is specific to Ethereum rather than the Internet Computer.
Web3.js is another library for interacting with the Ethereum blockchain. It offers similar functionalities to @dfinity/agent, such as sending transactions and interacting with smart contracts, but it is tailored for Ethereum.
Near-api-js is a JavaScript library for interacting with the NEAR blockchain. It provides similar functionalities to @dfinity/agent, such as managing accounts and interacting with smart contracts, but it is designed for the NEAR protocol.
JavaScript and TypeScript library to interact with the Internet Computer for Node.js and browser applications.
Do you want to know more about developing on the Internet Computer? Visit the Developer Docs.
Additional API Documentation can be found here.
Using agent:
npm i --save @dfinity/agent
import * as agent from '@dfinity/agent';
or using individual exports:
import { Actor, HttpAgent } from '@dfinity/agent';
const DfinityAgent = require('@dfinity/agent');
or using individual exports:
const { Actor, HttpAgent } = require('@dfinity/agent');
The agent is a low-level interface that the Actor uses to encode and decode messages to the Internet Computer. It provides call
, query
and readState
methods to the Actor, as well as a few additional utilities. For the most part, calls through the agent are intended to be structured through an Actor, configured with a canister interface that can be automatically generated from a Candid interface.
The most common use for the agent is to create an actor. This is done by calling the Actor.createActor
constructor:
Actor.createActor(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<T>
The interfaceFactory
is a function that returns a runtime interface that the Actor uses to strucure calls to a canister. The interfaceFactory can be written manually, but it is recommended to use the dfx generate
command to generate the interface for your project, or to use the didc
tool to generate the interface for your project.
Actors can also be initialized to include the boundary node http headers, This is done by calling the Actor.createActor
constructor:
Actor.createActorWithHttpDetails(interfaceFactory: InterfaceFactory, configuration: ActorConfig): ActorSubclass<ActorMethodMappedWithHttpDetails<T>>
Use the Actor.agentOf
method to get the agent of an actor:
const defaultAgent = Actor.agentOf(defaultActor);
This is useful if you need to replace or invalidate the identity used by an actor's agent.
For example, if you want to replace the identity of an actor's agent with a newly authenticated identity from Internet Identity, you can do so by calling the Actor.replaceAgent
method:
defaultAgent.replaceIdentity(await authClient.getIdentity());
The agent uses the browser fetch
API to make calls to the Internet Computer. If you are not using the agent in the browser, you can pass a custom fetch
implementation to the agent's constructor. This is useful if you want to use a custom fetch implementation, such as one that adds authentication headers to the request. We recommend using the isomorphic-fetch package to provide a consistent fetch API across Node.js and the browser. You will also need to provide a host
option to the agent's constructor, as the agent will not be able to determine the host from the global context.
For example,
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';
const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://icp-api.io';
const agent = new HttpAgent({ fetch, host });
You can also pass fetchOptions
to the agent's constructor, which will be passed to the fetch
implementation. This is useful if you want to pass additional options to the fetch
implementation, such as a custom header.
For example,
import fetch from 'isomorphic-fetch';
import { HttpAgent } from '@dfinity/agent';
const host = process.env.DFX_NETWORK === 'local' ? 'http://127.0.0.1:4943' : 'https://ic0.app';
/**
* @type {RequestInit}
*/
const fetchOptions = {
headers: {
'X-Custom-Header': 'value',
},
};
const agent = new HttpAgent({ fetch, host, fetchOptions });
FAQs
JavaScript and TypeScript library to interact with the Internet Computer
The npm package @dfinity/agent receives a total of 328,155 weekly downloads. As such, @dfinity/agent popularity was classified as popular.
We found that @dfinity/agent demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.