Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Embedded SDK #623

Draft
wants to merge 33 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
eb66523
feat: init wander-embedded-sdk
kranthicodes Jan 22, 2025
e0c6738
feat: extend and modularize sdk
kranthicodes Jan 23, 2025
f679e6f
Add scaffolding for the Embedded SDK and API.
Danziger Jan 23, 2025
d9a794e
Improve logic to send resize and close messages to the parent window.
Danziger Jan 24, 2025
8164550
Fix useAuthRequestsLocation() location.
Danziger Jan 24, 2025
4cde537
Merge branch 'development' into feature/embedded-sdk-and-api-scaffolding
Danziger Jan 24, 2025
572c7bc
refactor: remove custom button ref option from sdk
kranthicodes Jan 24, 2025
216d590
feat: add new types to sdk made in scaffolding pr by dani
kranthicodes Jan 24, 2025
8d13dae
feat: use setupWalletSdk from core src
kranthicodes Jan 25, 2025
cf4260b
refactor: use proxy pattern with wallet api injection
kranthicodes Jan 26, 2025
87642c7
Fix message id validation.
Danziger Jan 26, 2025
299b3fc
refactor: update button styles and refactor mounting
kranthicodes Jan 26, 2025
d499ef5
refactor: conditionally render button when buttonStyles is not none
kranthicodes Jan 26, 2025
e6d6fd7
Merge branch 'development' into feature/embedded-sdk-and-api-scaffolding
Danziger Jan 27, 2025
fcb0d6f
refactor: dont check if mod exists in setupWalletSDK
kranthicodes Jan 27, 2025
15e20c6
Merge branch 'development' into feature/embedded-sdk-and-api-scaffolding
Danziger Jan 27, 2025
f89feb9
refactor: check if mod exists before executing
kranthicodes Jan 27, 2025
895f0f5
chore: lite docs for sdk
kranthicodes Jan 27, 2025
c450ff8
refactor: remove connect disconnect outgoing events
kranthicodes Jan 27, 2025
8bd069c
refactor: rename sdk
kranthicodes Jan 29, 2025
412d0b1
refactor: use new entry path with vite config
kranthicodes Jan 29, 2025
008a711
refactor: update setupWalletSDK to initialize proxy with walletAPI
kranthicodes Jan 29, 2025
6cadd88
Merge pull request #631 from kranthicodes/kranthi/embedded-sdk
Danziger Feb 7, 2025
abc989b
Merge develop.
Danziger Feb 7, 2025
be18a3e
Merge develop.
Danziger Feb 7, 2025
141870c
Rename ArConnect to Wander (for Embedded SDK).
Danziger Feb 7, 2025
ebf7f8a
WIP additional options / TS support for Embedded SDK.
Danziger Feb 7, 2025
8df12f5
Restructure message types, add missing options, expose CSS variables …
Danziger Feb 10, 2025
e25222f
Add some additional styling, transitions and make some progress towar…
Danziger Feb 11, 2025
79464d7
Keep working on the UI.
Danziger Feb 12, 2025
f703a8d
Clean up unused options and finish styling for all 4 iframe variants.
Danziger Feb 13, 2025
9078c78
Implement click outisde to close and fix some issues with default opt…
Danziger Feb 13, 2025
a651d2d
Add some comments regarding theming.
Danziger Feb 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# dependencies
/node_modules
node_modules
/.pnp
.pnp.js

Expand Down Expand Up @@ -28,7 +28,8 @@ yarn-error.log*
out/
build/
dist/

sdk-dist
wander-embedded-sdk/wallet-api-dist/
# plasmo - https://www.plasmo.com
.plasmo

Expand All @@ -39,4 +40,4 @@ keys.json
.tsbuildinfo

# temporary remove development icon
assets/icon.development.png
assets/icon.development.png
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# WanderEmbedded SDK Documentation

WanderEmbedded SDK provides a seamless way to integrate Wander functionality into your web application.

## Installation

```bash
npm install wander-embedded-sdk
# or
yarn add wander-embedded-sdk
```

## Quick Start

```javascript
import { WanderEmbedded } from "wander-embedded-sdk";

// Initialize with default options
const wander = new WanderEmbedded();

// Or initialize with custom options
const wander = new WanderEmbedded({
buttonStyles: "custom",
iframeStyles: {
/* custom styles */
},
logo: "custom-logo-url"
});
```

## API Reference

### Constructor Options

The `WanderEmbedded` constructor accepts an optional configuration object with the following properties:

| **Option** | **Type** | **Default** | **Description** |
| -------------- | ---------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------- |
| `buttonStyles` | object \| 'none' | `undefined` | Customize the button’s appearance: - **object**: inline style object ; **'none'**: hide the button |
| `iframeRef` | `HTMLIFrameElement` | `undefined` | Use an existing iframe element instead of creating a new one. |
| `iframeStyles` | `object` | `undefined` | Inline styles for the iframe (e.g., `{ border: 'none', borderRadius: '12px' }`). |
| `logo` | `string` | `undefined` | URL for a custom logo displayed on the Wander button. |
| `balance` | `string` | `undefined` | Show balance information on the button. |
| `onOpen` | `() => void` | `undefined` | Callback fired when the Wander iframe is opened. |
| `onClose` | `() => void` | `undefined` | Callback fired when the Wander iframe is closed. |
| `onResize` | `(data: ResizeData) => void` | `undefined` | Callback fired when the Wander iframe is resized. The `data` object typically includes new width and height values. |

### Methods

#### `open()`

Opens the Wander iframe.

```javascript
wander.open();
```

#### `close()`

Closes the Wander iframe.

```javascript
wander.close();
```

#### `destroy()`

Removes all Wander elements from the DOM and cleans up resources.

```javascript
wander.destroy();
```

## Examples

### Basic Integration

```javascript
const wander = new WanderEmbedded();
```

### Custom Styling

```javascript
const wander = new WanderEmbedded({
buttonStyles: {
backgroundColor: "#000000",
color: "#ffffff"
// Add more custom styles
},
iframeStyles: {
border: "none",
borderRadius: "12px"
// Add more custom styles
}
});
```

### With Event Handlers

```javascript
const wander = new WanderEmbedded({
onOpen: () => {
console.log("Wander iframe opened");
},
onClose: () => {
console.log("Wander iframe closed");
},
onResize: (data) => {
console.log("Iframe resized:", data);
}
});
```

### Using Custom iframe

```javascript
const customIframe = document.getElementById("my-iframe");
const wander = new WanderEmbedded({
iframeRef: customIframe
});
```

### Button-less Integration

```javascript
const wander = new WanderEmbedded({
buttonStyles: "none"
});
```

## Notes

- The SDK automatically injects necessary elements into your webpage unless custom references are provided.
- Make sure to call `destroy()` when cleaning up to prevent memory leaks.
4 changes: 2 additions & 2 deletions assets/popup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const backgroundColor = localStorage.getItem("ARCONNECT_THEME_BACKGROUND_COLOR");
const textColor = localStorage.getItem("ARCONNECT_THEME_TEXT_COLOR");
const backgroundColor = localStorage.getItem("APP_THEME_BACKGROUND_COLOR");
const textColor = localStorage.getItem("APP_THEME_TEXT_COLOR");

if (backgroundColor) document.documentElement.style.setProperty('--backgroundColor', backgroundColor);
if (textColor) document.documentElement.style.setProperty('--textColor', textColor);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dev:iframe": "vite",
"build:iframe": "vite build",
"preview:iframe": "vite preview",
"build:wallet-api": "vite build --config vite.wallet-config.js",
"nuke": "rm -rf node_modules build .plasmo",
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
Expand Down Expand Up @@ -99,6 +100,7 @@
"styled-components": "^5.3.6",
"typed-assert": "^1.0.9",
"uuid": "^9.0.0",
"vite-plugin-dts": "^4.5.0",
"webextension-polyfill": "^0.10.0",
"wouter": "^3.3.5"
},
Expand Down
15 changes: 7 additions & 8 deletions shim.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ declare module "@arconnect/webext-bridge" {

// EMBEDDED:

embedded_open: any;
embedded_close: any;
embedded_resize: any;
// TODO: Maybe yes, maybe not (we should not drift too far away from the BE wallet API)
// embedded_auth: any;
// embedded_balance: any;
// embedded_info: any;
embedded_auth: EmbeddedAuthMessageData;
embedded_balance: EmbeddedBalanceMessageData;
embedded_resize: EmbeddedResizeMessageData;
7i7o marked this conversation as resolved.
Show resolved Hide resolved
embedded_close: void;

// OTHER:

Expand All @@ -78,9 +75,11 @@ declare module "@arconnect/webext-bridge" {
}

interface ApiCall<DataType = any> extends JsonValue {
app: "wander" | "wanderEmbedded";
version: string;
callID: number | string;
type: string;
data?: DataType;
callID: number | string;
}

interface ApiResponse<DataType = any> extends ApiCall<DataType> {
Expand Down
43 changes: 43 additions & 0 deletions src/api/background/background-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "~api/background/handlers/browser/tabs/tabs.handler";
import { log, LOG_GROUP } from "~utils/log/log.utils";
import { handleAuthStateChange } from "./handlers/storage/auth-state-change/auth-state-change.handler";
import { EMBEDDED_PARENT_ORIGIN } from "~utils/embedded/sdk/utils/url/sdk-url.utils";

export function setupBackgroundService() {
log(
Expand All @@ -42,6 +43,48 @@ export function setupBackgroundService() {
onMessage("api_call", handleApiCallMessage);
onMessage("chunk", handleChunkMessage);

if (import.meta.env?.VITE_IS_EMBEDDED_APP === "1") {
window.addEventListener("message", (event: MessageEvent) => {
if (
!event.data ||
event.data.app !== "wanderEmbedded" ||
event.origin !== EMBEDDED_PARENT_ORIGIN
)
return;

console.log("MESSAGE FROM PARENT =", event.data);

handleApiCallMessage({
id: "",
timestamp: Date.now(),
data: event.data,
sender: {
tabId: 0,
context: "content-script"
}
});

// Example: check if the message is from our SDK

/*
if (event.data.type === "FROM_SDK") {
const incomingMsg = event.data.payload;
console.log(
"Iframe received message from WanderEmbedded:",
incomingMsg
);

// Respond back
event.source?.postMessage({
type: "FROM_IFRAME",
payload: `Got your message: ${incomingMsg}`
});
}
*/
});
} else {
}

// LIFECYCLE:

// Open welcome page on extension install.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const handleApiCallMessage: OnMessageCallback<
isExactly(
sender.context,
"content-script",
"Chunk calls are only accepted from the injected-script -> content-script"
"API call messages are only accepted from the injected-script -> content-script"
);
isApiCall(data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const handleChunkMessage: OnMessageCallback<
isExactly(
sender.context,
"content-script",
"Chunk calls are only accepted from the injected-script -> content-script"
"Chunk messages are only accepted from the injected-script -> content-script"
);
isChunk(data.data);

Expand Down
Loading