Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
ic-use-actor
Advanced tools
React Hook and context provider to make interacting with Internet Computer canisters more fun!
A React context provider for managing Internet Computer (IC) actors with enhanced features like type safety and request/response interceptors. ic-use-actor
makes interacting with Internet Computer canisters more fun!
onRequest
, onResponse
, onRequestError
, and onResponseError
callbacks allow for intercepting and processing requests and responses.ic-use-actor
needs an Internet Computer (IC) identity to work. The examples below uses ic-use-siwe-identity
as an identity provider. You can use any other identity provider as long as it returns a valid IC identity.
npm install ic-use-actor
To use ic-use-actor
in your React application, follow these steps:
First, create an actor context and a corresponding hook for each IC canister you would like to access. Export the hook to be able to use it in your components. The hook returned by createUseActorHook
can be named anything you want. If using ic-use-actor
with multiple canisters, you might want to name the hook after the canister to make it easier to identify which hook is for which canister - for example, useMyCanister
, useMyOtherCanister
, etc.
import {
createActorContext,
createUseActorHook,
} from "ic-use-actor";
import { _SERVICE } from "path-to/your-service.did";
const actorContext = createActorContext<_SERVICE>();
export const useActor = createUseActorHook<_SERVICE>(actorContext);
Create one or more ActorProvider components to provide access to your canisters. ActorProviders can be nested to provide access to multiple canisters.
// Actors.tsx
import { ReactNode } from "react";
import {
ActorProvider,
createActorContext,
createUseActorHook,
} from "ic-use-actor";
import {
canisterId,
idlFactory,
} from "path-to/your-service/index";
import { _SERVICE } from "path-to/your-service.did";
import { useSiweIdentity } from "ic-use-siwe-identity";
const actorContext = createActorContext<_SERVICE>();
export const useActor = createUseActorHook<_SERVICE>(actorContext);
export default function Actors({ children }: { children: ReactNode }) {
const { identity } = useSiweIdentity();
return (
<ActorProvider<_SERVICE>
canisterId={canisterId}
context={actorContext}
identity={identity}
idlFactory={idlFactory}
>
{children}
</ActorProvider>
);
}
Wrap your application root component with the ActorProvider component(s) you created in the previous step to provide access to your canisters.
// App.tsx
import Actors from "./Actors";
function App() {
return (
<Actors>
<MyApplication />
</Actors>
);
}
In your components, use the useActor hook to access the actor:
// AnyComponent.tsx
import { useActor } from "path-to/useActor";
function AnyComponent() {
const { actor } = useActor();
// Use the actor for calling methods on your canister
React.useEffect(() => {
actor
.my_method()
.then((result) => {
// Do something with the result
})
.catch((error) => {
// Handle the error
});
}, []);
}
Interceptors can be used to intercept requests and responses. You can use them to modify requests, log requests and responses, or perform other actions.
export default function Actor({ children }: { children: ReactNode }) {
const { identity } = useSiweIdentity();
const handleRequest = (data: InterceptorRequestData) => {
// Do something
// data: { args: unknown[], methodName: string }
return data.args;
};
const handleResponse = (data: InterceptorResponseData) => {
// Do something
// data: { args: unknown[], methodName: string, response: unknown }
return data.response;
};
const handleError = (data: InterceptorErrorData) => {
// Do something
// data: { args: unknown[], methodName: string, error: unknown }
return data.error;
};
return (
<ActorProvider<_SERVICE>
canisterId={canisterId}
context={actorContext}
identity={identity}
idlFactory={idlFactory}
onRequest={handleRequest}
onRequestError={handleError}
onResponse={handleResponse}
onResponseError={handleError}
>
{children}
</ActorProvider>
);
}
See the CHANGELOG for details on updates.
Contributions are welcome. Please submit your pull requests or open issues to propose changes or report bugs.
This project is licensed under the MIT License. See the LICENSE file for more details.
[0.0.5] - 2024-01-18
FAQs
React Hook and context provider to make interacting with Internet Computer canisters more fun!
The npm package ic-use-actor receives a total of 44 weekly downloads. As such, ic-use-actor popularity was classified as not popular.
We found that ic-use-actor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.