Skip to content

Commit 4b0af28

Browse files
authored
feat: add mojito inject docs (#5)
1 parent 1db7697 commit 4b0af28

2 files changed

Lines changed: 185 additions & 0 deletions

File tree

docs/mojito-inject/_category_.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"label": "Mojito Inject",
3+
"position": 1,
4+
"collapsed": false,
5+
"link": {
6+
"type": "generated-index",
7+
"title": "Mojito Inject",
8+
"description": "Browser-injected wallet provider for Mojito. Use it to connect your dApp to the user's Mojito wallet via the browser extension."
9+
}
10+
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
---
2+
title: Getting Started
3+
sidebar_label: Introduction
4+
---
5+
6+
# Getting Started with `window.mojito`
7+
8+
The Mojito Wallet browser extension injects a global object `window.mojito` into every page. This object acts as a lightweight interface between your decentralized app (dApp) and the Mojito wallet, allowing you to request access, send transactions, and react to wallet state changes.
9+
10+
## Version Support
11+
12+
Mojito inject API is available in extension versions 1.4.0 and above.
13+
14+
```ts
15+
const version = await window.mojito.version;
16+
console.log('Mojito version:', version);
17+
// shoudl be 1.4.0 or higher
18+
```
19+
20+
Check for a proper version
21+
```ts
22+
if (!window.mojito) {
23+
alert("Please install or update Mojito Wallet to version 1.4.0 or higher.");
24+
}
25+
```
26+
27+
## Features
28+
29+
- Connect to the user's Mojito wallet with approval
30+
- Restore existing sessions automatically
31+
- Sign transactions via user approval
32+
- Check if a wallet is connected
33+
- Subscribe to wallet events
34+
35+
## Installation
36+
37+
No additional installation is required. If the user has the Mojito browser extension installed and enabled, the `window.mojito` object will be available on page load.
38+
39+
You can check for the presence of the wallet with:
40+
41+
```ts
42+
if (window.mojito?.isExtension) {
43+
console.log("Mojito wallet is available.");
44+
}
45+
```
46+
47+
## API Reference
48+
49+
### mojito.isExtension
50+
51+
`mojito.isExtension: boolean`
52+
53+
Indicates whether the Mojito wallet extension is installed and enabled.
54+
55+
```ts
56+
if (window.mojito?.isExtension) {
57+
console.log("Mojito wallet is available.");
58+
} else {
59+
console.log("Please install Mojito Wallet.");
60+
}
61+
```
62+
63+
### mojito.version
64+
65+
`mojito.version: string`
66+
67+
Returns the version of the Mojito wallet extension.
68+
69+
```ts
70+
const version = await window.mojito.version;
71+
console.log("Mojito version:", version);
72+
// should be 1.4.0 or higher
73+
```
74+
75+
### mojito.connectedAddresses
76+
`mojito.connectedAddresses: Addresses | null`
77+
78+
Returns the currently connected addresses. If no session is active, it returns `null`.
79+
80+
```ts
81+
const addresses = window.mojito.connectedAddresses;
82+
if (addresses) {
83+
console.log("Connected addresses:", addresses.testnet.receiving);
84+
} else {
85+
console.log("No active session.");
86+
}
87+
```
88+
89+
### mojito.connect()
90+
`mojito.connect(): Promise<Addresses>`
91+
92+
Prompts the user to approve wallet connection.
93+
94+
```ts
95+
const addresses = await window.mojito.connect();
96+
console.log("Connected addresses:", addresses.testnet.receiving);
97+
```
98+
99+
### mojito.restore()
100+
`mojito.restore(): Promise<Addresses | null>`
101+
102+
Attempts to restore a previously approved session without user interaction.
103+
104+
```ts
105+
const restored = await window.mojito.restore();
106+
if (restored) {
107+
console.log("Session restored", restored);
108+
}
109+
```
110+
111+
You can skip restore by passing a flag to your SDK client if you want manual connection only.
112+
113+
### mojito.disconnect()
114+
`mojito.disconnect(): void`
115+
116+
Clears the current session and notifies your dApp via an event.
117+
118+
```ts
119+
window.mojito.disconnect();
120+
```
121+
122+
### mojito.request()
123+
`mojito.request(method: string, params?: object): Promise;`
124+
125+
Low-level method to send custom requests to the wallet (e.g., for signing or advanced features).
126+
127+
```ts
128+
const result = await window.mojito.request("signTransaction", {
129+
txData: { /* your transaction here */ }
130+
});
131+
```
132+
133+
### mojito.on()
134+
`mojito.on(event: string, callback: (data: any) => void): void`
135+
136+
Subscribe to wallet events.
137+
138+
Available events:
139+
• accountsChanged: fired when the connected account changes
140+
• disconnect: fired when the user disconnects or session is cleared
141+
142+
```ts
143+
window.mojito.on("disconnect", () => {
144+
console.log("Wallet was disconnected");
145+
});
146+
```
147+
148+
## Example integration
149+
150+
```ts
151+
async function setup() {
152+
if (!window.mojito?.isExtension) {
153+
alert("Mojito Wallet is not installed");
154+
return;
155+
}
156+
157+
const restored = await window.mojito.restore();
158+
if (!restored) {
159+
await window.mojito.connect();
160+
}
161+
162+
// Now the app can use connected addresses
163+
const addresses = window.mojito.connectedAddresses;
164+
console.log("Using addresses:", addresses);
165+
}
166+
```
167+
168+
## Security
169+
170+
- Private keys never leave the wallet extension.
171+
- dApps must explicitly request permission to connect.
172+
- Signing requests must be manually approved by the user.
173+
- The window.mojito object only exposes public information and events.
174+
175+
Happy building! 🚀

0 commit comments

Comments
 (0)