Retrieving a Wallet's Public Key in a Browser
To retrieve the public key of a wallet in a browser, use the getPublicKey()
method provided by the OP_WALLET extension.
Learn about Unified Accounts
Click here to learn more about Unified Accounts and how they work in the OP_NET ecosystem.
Step 1: Accessing the Wallet Instance
First, verify the availability of a wallet instance. For OP_WALLET, you can access the wallet instance using the following code:
import { Unisat } from "@btc-vision/transaction";
export interface CustomWindow {
opnet?: Unisat;
}
const _window: CustomWindow = window as CustomWindow;
const opnet: Unisat | undefined = _window.opnet;
Detailed Guide
Learn more about Interacting with a Browser Wallet that Directly Supports OP_NET for detailed information on accessing wallet instances.
Step 2: Fetching the Public Key
Once you have access to the wallet instance, call the getPublicKey()
method:
const publicKey = await opnet.getPublicKey();
console.log("Public Key:", publicKey);
Best Practices
- Always ensure the wallet instance (e.g.,
window.opnet
) is available before calling wallet methods. - Never expose private keys or other sensitive wallet data in your application code or logs.