@phantom/browser-injected-sdk 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +161 -0
- package/dist/auto-confirm/index.d.ts +37 -0
- package/dist/auto-confirm/index.js +107 -0
- package/dist/auto-confirm/index.mjs +82 -0
- package/dist/chunk-GV6AIHPN.mjs +18 -0
- package/dist/chunk-QNVIOBKG.mjs +382 -0
- package/dist/chunk-WUKYLWAZ.mjs +0 -0
- package/dist/ethereum/index.d.ts +1 -0
- package/dist/ethereum/index.js +416 -0
- package/dist/ethereum/index.mjs +8 -0
- package/dist/index-2f448acb.d.ts +168 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +452 -0
- package/dist/index.mjs +41 -0
- package/dist/solana/index.d.ts +139 -0
- package/dist/solana/index.js +413 -0
- package/dist/solana/index.mjs +379 -0
- package/package.json +61 -0
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# Phantom Browser Injected SDK
|
|
2
|
+
|
|
3
|
+
The Phantom Browser Injected SDK allows you to interact with the Phantom wallet from your web application. (Browser Extension and Mobile)
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
You can install the SDK using npm or yarn:
|
|
8
|
+
|
|
9
|
+
**npm:**
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @phantom/browser-injected-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**yarn:**
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
yarn add @phantom/browser-injected-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Here's an example of how to import and use the SDK with the Solana plugin:
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { createPhantom } from "@phantom/browser-injected-sdk";
|
|
27
|
+
import { createSolanaPlugin } from "@phantom/browser-injected-sdk/solana"; // Import the solana plugin
|
|
28
|
+
import { createEthereumPlugin } from "@phantom/browser-injected-sdk/ethereum";
|
|
29
|
+
|
|
30
|
+
// Create a Phantom instance with the Solana plugin
|
|
31
|
+
const phantom = createPhantom({
|
|
32
|
+
plugins: [createSolanaPlugin(), createEthereumPlugin()],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Now you can use the Solana-specific methods
|
|
36
|
+
async function connectAndSign() {
|
|
37
|
+
try {
|
|
38
|
+
// Attempt to connect to the wallet
|
|
39
|
+
const connectionResult = await phantom.solana.connect();
|
|
40
|
+
console.log("Connection Result:", connectionResult.address);
|
|
41
|
+
|
|
42
|
+
// Example: Sign in (if supported by the specific provider/plugin)
|
|
43
|
+
// Construct SolanaSignInData according to your needs
|
|
44
|
+
const signInData = { domain: window.location.host, statement: "Please sign in to access this dApp." };
|
|
45
|
+
const signInResult = await phantom.solana.signIn(signInData);
|
|
46
|
+
console.log("Sign In Result:", signInResult.address);
|
|
47
|
+
|
|
48
|
+
// Example: Sign a message
|
|
49
|
+
const message = new TextEncoder().encode("Hello from Phantom Browser SDK!");
|
|
50
|
+
const signedMessage = await phantom.solana.signMessage(message, "utf8");
|
|
51
|
+
console.log("Signed Message:", signedMessage);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error("Error interacting with Phantom:", error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
connectAndSign();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Available Solana Methods
|
|
61
|
+
|
|
62
|
+
Once the `phantom.solana` object is initialized, you can access the following methods:
|
|
63
|
+
|
|
64
|
+
- `connect(opts?: { onlyIfTrusted?: boolean }): Promise<string>`
|
|
65
|
+
- Connects to the Phantom wallet. Optionally, `onlyIfTrusted` can be set to true to only connect if the dApp is already trusted.
|
|
66
|
+
- `disconnect(): Promise<void>`
|
|
67
|
+
- Disconnects from the Phantom wallet.
|
|
68
|
+
- `getAccount(): Promise<string | undefined>`
|
|
69
|
+
- Gets the current connected address
|
|
70
|
+
- `signIn(): Promise<SignInResult>`
|
|
71
|
+
- Initiates a sign-in request to the wallet.
|
|
72
|
+
- `signMessage(message: Uint8Array | string, display?: 'utf8' | 'hex'): Promise<SignedMessage>`
|
|
73
|
+
- Prompts the user to sign a given message.
|
|
74
|
+
- `signAndSendTransaction(transaction: Transaction): Promise<{ signature: string; address?: string }>`
|
|
75
|
+
- Prompts the user to sign **and send** a Kit `Transaction` and returns the confirmed signature.
|
|
76
|
+
|
|
77
|
+
### Event Handling
|
|
78
|
+
|
|
79
|
+
The SDK also allows you to listen for `connect`, `disconnect`, and `accountChanged` events:
|
|
80
|
+
|
|
81
|
+
- `addEventListener(event: PhantomEventType, callback: PhantomEventCallback): () => void`
|
|
82
|
+
|
|
83
|
+
- Registers a callback that will be invoked when the specified event occurs.
|
|
84
|
+
- For the `connect` event, the callback receives the public key (as a string) of the connected account.
|
|
85
|
+
- For the `disconnect` event, the callback receives no arguments.
|
|
86
|
+
- For the `accountChanged` event, the callback receives the new public key (as a string).
|
|
87
|
+
- Returns a function that, when called, will unregister the callback.
|
|
88
|
+
- Multiple callbacks can be registered for the same event.
|
|
89
|
+
|
|
90
|
+
**Example:**
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const phantom = createPhantom({ plugins: [createSolanaPlugin()] });
|
|
94
|
+
|
|
95
|
+
const handleConnect = (address: string) => {
|
|
96
|
+
console.log(`Wallet connected with public key: ${address}`);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const clearConnectListener = phantom.solana.addEventListener("connect", handleConnect);
|
|
100
|
+
|
|
101
|
+
const handleAccountChanged = (newAddress: string) => {
|
|
102
|
+
console.log(`Account changed to: ${newAddress}`);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const clearAccountChangedListener = phantom.solana.addEventListener("accountChanged", handleAccountChanged);
|
|
106
|
+
|
|
107
|
+
// To stop listening for a specific event:
|
|
108
|
+
// clearConnectListener();
|
|
109
|
+
// clearAccountChangedListener();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- `removeEventListener(event: PhantomEventType, callback: PhantomEventCallback): void`
|
|
113
|
+
|
|
114
|
+
- Unregisters a previously registered callback for the specified event.
|
|
115
|
+
|
|
116
|
+
**Example:**
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
const phantom = createPhantom({ plugins: [createSolanaPlugin()] });
|
|
120
|
+
|
|
121
|
+
const handleDisconnect = () => {
|
|
122
|
+
console.log("Wallet disconnected");
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
phantom.solana.addEventListener("disconnect", handleDisconnect);
|
|
126
|
+
|
|
127
|
+
// To stop listening for this specific disconnect event:
|
|
128
|
+
// phantom.solana.removeEventListener("disconnect", handleDisconnect);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Creating a transaction
|
|
132
|
+
|
|
133
|
+
Phantom's SDK uses the `@solana/kit` library to create transactions. You can use the `createTransactionMessage` function to create a transaction message.
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import {
|
|
137
|
+
createSolanaRpc,
|
|
138
|
+
pipe,
|
|
139
|
+
createTransactionMessage,
|
|
140
|
+
setTransactionMessageFeePayer,
|
|
141
|
+
setTransactionMessageLifetimeUsingBlockhash,
|
|
142
|
+
address,
|
|
143
|
+
compileTransaction,
|
|
144
|
+
} from "@solana/kit";
|
|
145
|
+
|
|
146
|
+
// Example: Sign and send a transaction
|
|
147
|
+
|
|
148
|
+
const rpc = createSolanaRpc("https://my-rpc-url.com"); // Replace with your own RPC URL
|
|
149
|
+
|
|
150
|
+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
|
|
151
|
+
|
|
152
|
+
const transactionMessage = pipe(
|
|
153
|
+
createTransactionMessage({ version: 0 }),
|
|
154
|
+
tx => setTransactionMessageFeePayer(address(userPublicKey as string), tx),
|
|
155
|
+
tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const transaction = compileTransaction(transactionMessage);
|
|
159
|
+
|
|
160
|
+
const { signature } = await phantomInstance.solana.signAndSendTransaction(transaction);
|
|
161
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { P as Plugin } from '../index-2f448acb.js';
|
|
2
|
+
|
|
3
|
+
type NetworkID = "bip122:000000000019d6689c085ae165831e93" | "bip122:000000000933ea01ad0ee984209779ba" | "solana:101" | "solana:102" | "solana:103" | "solana:localnet" | "eip155:1" | "eip155:11155111" | "eip155:137" | "eip155:80002" | "eip155:8453" | "eip155:84532" | "eip155:143" | "eip155:10143" | "eip155:41454" | "eip155:42161" | "eip155:421614" | "hypercore:mainnet" | "hypercore:testnet" | "sui:mainnet" | "sui:testnet";
|
|
4
|
+
type AutoConfirmEnableParams = {
|
|
5
|
+
chains?: NetworkID[];
|
|
6
|
+
};
|
|
7
|
+
type AutoConfirmResult = {
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
chains: NetworkID[];
|
|
10
|
+
};
|
|
11
|
+
type AutoConfirmSupportedChainsResult = {
|
|
12
|
+
chains: NetworkID[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare function autoConfirmEnable(params?: AutoConfirmEnableParams): Promise<AutoConfirmResult>;
|
|
16
|
+
|
|
17
|
+
declare function autoConfirmDisable(): Promise<AutoConfirmResult>;
|
|
18
|
+
|
|
19
|
+
declare function autoConfirmStatus(): Promise<AutoConfirmResult>;
|
|
20
|
+
|
|
21
|
+
declare function autoConfirmSupportedChains(): Promise<AutoConfirmSupportedChainsResult>;
|
|
22
|
+
|
|
23
|
+
type AutoConfirmPlugin = {
|
|
24
|
+
autoConfirmEnable: typeof autoConfirmEnable;
|
|
25
|
+
autoConfirmDisable: typeof autoConfirmDisable;
|
|
26
|
+
autoConfirmStatus: typeof autoConfirmStatus;
|
|
27
|
+
autoConfirmSupportedChains: typeof autoConfirmSupportedChains;
|
|
28
|
+
};
|
|
29
|
+
declare function createAutoConfirmPlugin(): Plugin<AutoConfirmPlugin>;
|
|
30
|
+
|
|
31
|
+
declare module "../index" {
|
|
32
|
+
interface Phantom {
|
|
33
|
+
autoConfirm: AutoConfirmPlugin;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { AutoConfirmEnableParams, AutoConfirmResult, AutoConfirmSupportedChainsResult, NetworkID, createAutoConfirmPlugin };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/auto-confirm/index.ts
|
|
21
|
+
var auto_confirm_exports = {};
|
|
22
|
+
__export(auto_confirm_exports, {
|
|
23
|
+
createAutoConfirmPlugin: () => createAutoConfirmPlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(auto_confirm_exports);
|
|
26
|
+
|
|
27
|
+
// src/auto-confirm/getProvider.ts
|
|
28
|
+
function getProvider() {
|
|
29
|
+
if (typeof window === "undefined") {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const provider = window.phantom?.app;
|
|
33
|
+
if (!provider) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return provider;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/auto-confirm/autoConfirmEnable.ts
|
|
40
|
+
async function autoConfirmEnable(params) {
|
|
41
|
+
const provider = await getProvider();
|
|
42
|
+
if (!provider) {
|
|
43
|
+
throw new Error("Provider not found.");
|
|
44
|
+
}
|
|
45
|
+
const result = await provider.request({
|
|
46
|
+
method: "phantom_auto_confirm_enable",
|
|
47
|
+
params: params || {}
|
|
48
|
+
});
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/auto-confirm/autoConfirmDisable.ts
|
|
53
|
+
async function autoConfirmDisable() {
|
|
54
|
+
const provider = await getProvider();
|
|
55
|
+
if (!provider) {
|
|
56
|
+
throw new Error("Provider not found.");
|
|
57
|
+
}
|
|
58
|
+
const result = await provider.request({
|
|
59
|
+
method: "phantom_auto_confirm_disable",
|
|
60
|
+
params: {}
|
|
61
|
+
});
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/auto-confirm/autoConfirmStatus.ts
|
|
66
|
+
async function autoConfirmStatus() {
|
|
67
|
+
const provider = await getProvider();
|
|
68
|
+
if (!provider) {
|
|
69
|
+
throw new Error("Provider not found.");
|
|
70
|
+
}
|
|
71
|
+
const result = await provider.request({
|
|
72
|
+
method: "phantom_auto_confirm_status",
|
|
73
|
+
params: {}
|
|
74
|
+
});
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/auto-confirm/autoConfirmSupportedChains.ts
|
|
79
|
+
async function autoConfirmSupportedChains() {
|
|
80
|
+
const provider = getProvider();
|
|
81
|
+
if (!provider) {
|
|
82
|
+
throw new Error("Provider not found.");
|
|
83
|
+
}
|
|
84
|
+
const result = await provider.request({
|
|
85
|
+
method: "phantom_auto_confirm_supported_chains",
|
|
86
|
+
params: {}
|
|
87
|
+
});
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/auto-confirm/plugin.ts
|
|
92
|
+
var autoConfirm = {
|
|
93
|
+
autoConfirmEnable,
|
|
94
|
+
autoConfirmDisable,
|
|
95
|
+
autoConfirmStatus,
|
|
96
|
+
autoConfirmSupportedChains
|
|
97
|
+
};
|
|
98
|
+
function createAutoConfirmPlugin() {
|
|
99
|
+
return {
|
|
100
|
+
name: "autoConfirm",
|
|
101
|
+
create: () => autoConfirm
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
createAutoConfirmPlugin
|
|
107
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import "../chunk-GV6AIHPN.mjs";
|
|
2
|
+
|
|
3
|
+
// src/auto-confirm/getProvider.ts
|
|
4
|
+
function getProvider() {
|
|
5
|
+
if (typeof window === "undefined") {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const provider = window.phantom?.app;
|
|
9
|
+
if (!provider) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return provider;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/auto-confirm/autoConfirmEnable.ts
|
|
16
|
+
async function autoConfirmEnable(params) {
|
|
17
|
+
const provider = await getProvider();
|
|
18
|
+
if (!provider) {
|
|
19
|
+
throw new Error("Provider not found.");
|
|
20
|
+
}
|
|
21
|
+
const result = await provider.request({
|
|
22
|
+
method: "phantom_auto_confirm_enable",
|
|
23
|
+
params: params || {}
|
|
24
|
+
});
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/auto-confirm/autoConfirmDisable.ts
|
|
29
|
+
async function autoConfirmDisable() {
|
|
30
|
+
const provider = await getProvider();
|
|
31
|
+
if (!provider) {
|
|
32
|
+
throw new Error("Provider not found.");
|
|
33
|
+
}
|
|
34
|
+
const result = await provider.request({
|
|
35
|
+
method: "phantom_auto_confirm_disable",
|
|
36
|
+
params: {}
|
|
37
|
+
});
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/auto-confirm/autoConfirmStatus.ts
|
|
42
|
+
async function autoConfirmStatus() {
|
|
43
|
+
const provider = await getProvider();
|
|
44
|
+
if (!provider) {
|
|
45
|
+
throw new Error("Provider not found.");
|
|
46
|
+
}
|
|
47
|
+
const result = await provider.request({
|
|
48
|
+
method: "phantom_auto_confirm_status",
|
|
49
|
+
params: {}
|
|
50
|
+
});
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/auto-confirm/autoConfirmSupportedChains.ts
|
|
55
|
+
async function autoConfirmSupportedChains() {
|
|
56
|
+
const provider = getProvider();
|
|
57
|
+
if (!provider) {
|
|
58
|
+
throw new Error("Provider not found.");
|
|
59
|
+
}
|
|
60
|
+
const result = await provider.request({
|
|
61
|
+
method: "phantom_auto_confirm_supported_chains",
|
|
62
|
+
params: {}
|
|
63
|
+
});
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/auto-confirm/plugin.ts
|
|
68
|
+
var autoConfirm = {
|
|
69
|
+
autoConfirmEnable,
|
|
70
|
+
autoConfirmDisable,
|
|
71
|
+
autoConfirmStatus,
|
|
72
|
+
autoConfirmSupportedChains
|
|
73
|
+
};
|
|
74
|
+
function createAutoConfirmPlugin() {
|
|
75
|
+
return {
|
|
76
|
+
name: "autoConfirm",
|
|
77
|
+
create: () => autoConfirm
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
createAutoConfirmPlugin
|
|
82
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __accessCheck = (obj, member, msg) => {
|
|
2
|
+
if (!member.has(obj))
|
|
3
|
+
throw TypeError("Cannot " + msg);
|
|
4
|
+
};
|
|
5
|
+
var __privateAdd = (obj, member, value) => {
|
|
6
|
+
if (member.has(obj))
|
|
7
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
8
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
9
|
+
};
|
|
10
|
+
var __privateMethod = (obj, member, method) => {
|
|
11
|
+
__accessCheck(obj, member, "access private method");
|
|
12
|
+
return method;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
__privateAdd,
|
|
17
|
+
__privateMethod
|
|
18
|
+
};
|