
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
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.
@phantom/browser-sdk
Advanced tools
The Phantom Browser SDK allows you to interact with the Phantom wallet from your web application. It uses a plugin system to support different blockchains.
The Phantom Browser SDK allows you to interact with the Phantom wallet from your web application. It uses a plugin system to support different blockchains.
You can install the SDK using npm or yarn:
npm:
npm install @phantom/browser-sdk
yarn:
yarn add @phantom/browser-sdk
Here's an example of how to import and use the SDK with the Solana plugin:
import { createPhantom } from "@phantom/browser-sdk";
import { createSolanaPlugin } from "@phantom/browser-sdk/solana"; // Import the solana plugin
// Create a Phantom instance with the Solana plugin
const phantom = createPhantom({
chainPlugins: [createSolanaPlugin()],
});
// Now you can use the Solana-specific methods
async function connectAndSign() {
try {
// Attempt to connect to the wallet
const connectionResult = await phantom.solana.connect();
console.log("Connection Result:", connectionResult.address);
// Example: Sign in (if supported by the specific provider/plugin)
// Construct SolanaSignInData according to your needs
const signInData = { domain: window.location.host, statement: "Please sign in to access this dApp." };
const signInResult = await phantom.solana.signIn(signInData);
console.log("Sign In Result:", signInResult.address);
// Example: Sign a message
const message = new TextEncoder().encode("Hello from Phantom Browser SDK!");
const signedMessage = await phantom.solana.signMessage(message, "utf8");
console.log("Signed Message:", signedMessage);
} catch (error) {
console.error("Error interacting with Phantom:", error);
}
}
connectAndSign();
Once the phantom.solana
object is initialized, you can access the following methods:
connect(opts?: { onlyIfTrusted?: boolean }): Promise<string>
onlyIfTrusted
can be set to true to only connect if the dApp is already trusted.disconnect(): Promise<void>
getAccount(): Promise<string | undefined>
signIn(): Promise<SignInResult>
signMessage(message: Uint8Array | string, display?: 'utf8' | 'hex'): Promise<SignedMessage>
signAndSendTransaction(transaction: Transaction): Promise<{ signature: string; address?: string }>
Transaction
and returns the confirmed signature.The SDK also allows you to listen for connect
, disconnect
, and accountChanged
events:
addEventListener(event: PhantomEventType, callback: PhantomEventCallback): () => void
connect
event, the callback receives the public key (as a string) of the connected account.disconnect
event, the callback receives no arguments.accountChanged
event, the callback receives the new public key (as a string).Example:
const phantom = createPhantom({ chainPlugins: [createSolanaPlugin()] });
const handleConnect = (address: string) => {
console.log(`Wallet connected with public key: ${address}`);
};
const clearConnectListener = phantom.solana.addEventListener("connect", handleConnect);
const handleAccountChanged = (newAddress: string) => {
console.log(`Account changed to: ${newAddress}`);
};
const clearAccountChangedListener = phantom.solana.addEventListener("accountChanged", handleAccountChanged);
// To stop listening for a specific event:
// clearConnectListener();
// clearAccountChangedListener();
removeEventListener(event: PhantomEventType, callback: PhantomEventCallback): void
Example:
const phantom = createPhantom({ chainPlugins: [createSolanaPlugin()] });
const handleDisconnect = () => {
console.log("Wallet disconnected");
};
phantom.solana.addEventListener("disconnect", handleDisconnect);
// To stop listening for this specific disconnect event:
// phantom.solana.removeEventListener("disconnect", handleDisconnect);
Phantom's SDK uses the @solana/kit
library to create transactions. You can use the createTransactionMessage
function to create a transaction message.
import {
createSolanaRpc,
pipe,
createTransactionMessage,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
address,
compileTransaction,
} from "@solana/kit";
// Example: Sign and send a transaction
const rpc = createSolanaRpc("https://my-rpc-url.com"); // Replace with your own RPC URL
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const transactionMessage = pipe(
createTransactionMessage({ version: 0 }),
tx => setTransactionMessageFeePayer(address(userPublicKey as string), tx),
tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
);
const transaction = compileTransaction(transactionMessage);
const { signature } = await phantomInstance.solana.signAndSendTransaction(transaction);
FAQs
Browser SDK for Phantom Wallet with unified interface
The npm package @phantom/browser-sdk receives a total of 1,906 weekly downloads. As such, @phantom/browser-sdk popularity was classified as popular.
We found that @phantom/browser-sdk 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
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.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.