hablcast-sdk 1.0.0
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 +87 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.global.js +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# HABLCAST Mini-App Bridge SDK
|
|
2
|
+
|
|
3
|
+
The official SDK for integrating third-party mini-applications with the HABLCAST host application shell.
|
|
4
|
+
|
|
5
|
+
It handles cross-origin message passing (`window.postMessage`) between the child iframe (mini-app) and the parent window (HABLCAST).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
### Via NPM
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @hablcast/sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Via CDN (Script Tag)
|
|
16
|
+
|
|
17
|
+
```html
|
|
18
|
+
<script src="https://hablcast.vercel.app/sdk/v1/hablcast-sdk.js"></script>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Quickstart
|
|
24
|
+
|
|
25
|
+
### 1. Initialize Handshake
|
|
26
|
+
|
|
27
|
+
Your mini-app should call `hablcast.ready()` as soon as it is loaded to start the handshake with the parent application.
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { hablcast } from '@hablcast/sdk';
|
|
31
|
+
|
|
32
|
+
// Start handshake
|
|
33
|
+
hablcast.ready();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### 2. Subscribe to Context Updates
|
|
37
|
+
|
|
38
|
+
Listen to user profiles, current theme, and environment configuration changes.
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
const unsubscribe = hablcast.onContext((context) => {
|
|
42
|
+
console.log("Current Username:", context.user.username);
|
|
43
|
+
console.log("Wallet Address:", context.user.address);
|
|
44
|
+
console.log("Theme:", context.theme); // "light" or "dark"
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// To unsubscribe later:
|
|
48
|
+
// unsubscribe();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. Request Signature
|
|
52
|
+
|
|
53
|
+
Request the user to sign a message using their connected wallet.
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
try {
|
|
57
|
+
const { signature } = await hablcast.requestSignature("Sign in to Predict-a-Friend");
|
|
58
|
+
console.log("Signed message successfully:", signature);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("Signature request rejected:", error.message);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 4. Send Transaction
|
|
65
|
+
|
|
66
|
+
Trigger on-chain interactions. The HABLCAST parent will present a transaction confirmation modal to the user.
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
try {
|
|
70
|
+
const { txHash } = await hablcast.sendTransaction({
|
|
71
|
+
to: "0x000000000000000000000000000000000000dEaD",
|
|
72
|
+
data: "0x",
|
|
73
|
+
value: "10000000000000000" // 0.01 ETH in wei
|
|
74
|
+
});
|
|
75
|
+
console.log("Transaction executed. Hash:", txHash);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
console.error("Transaction failed or was rejected:", error.message);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 5. Close Mini-App
|
|
82
|
+
|
|
83
|
+
Request the parent application to close your mini-app.
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
hablcast.close();
|
|
87
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HABLCAST Mini-App Client SDK
|
|
3
|
+
* This file contains the client-side library that third-party mini-apps run inside their iframes.
|
|
4
|
+
* It establishes a secure postMessage bridge with the HABLCAST host parent window.
|
|
5
|
+
*/
|
|
6
|
+
interface UserProfile {
|
|
7
|
+
address: string;
|
|
8
|
+
username: string;
|
|
9
|
+
avatar?: string;
|
|
10
|
+
}
|
|
11
|
+
interface HablcastContext {
|
|
12
|
+
user: UserProfile;
|
|
13
|
+
theme: "light" | "dark";
|
|
14
|
+
environment: "development" | "production";
|
|
15
|
+
}
|
|
16
|
+
interface TransactionRequest {
|
|
17
|
+
to: `0x${string}`;
|
|
18
|
+
data: `0x${string}`;
|
|
19
|
+
value?: string;
|
|
20
|
+
}
|
|
21
|
+
declare const hablcast: any;
|
|
22
|
+
|
|
23
|
+
export { type HablcastContext, type TransactionRequest, type UserProfile, hablcast };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HABLCAST Mini-App Client SDK
|
|
3
|
+
* This file contains the client-side library that third-party mini-apps run inside their iframes.
|
|
4
|
+
* It establishes a secure postMessage bridge with the HABLCAST host parent window.
|
|
5
|
+
*/
|
|
6
|
+
interface UserProfile {
|
|
7
|
+
address: string;
|
|
8
|
+
username: string;
|
|
9
|
+
avatar?: string;
|
|
10
|
+
}
|
|
11
|
+
interface HablcastContext {
|
|
12
|
+
user: UserProfile;
|
|
13
|
+
theme: "light" | "dark";
|
|
14
|
+
environment: "development" | "production";
|
|
15
|
+
}
|
|
16
|
+
interface TransactionRequest {
|
|
17
|
+
to: `0x${string}`;
|
|
18
|
+
data: `0x${string}`;
|
|
19
|
+
value?: string;
|
|
20
|
+
}
|
|
21
|
+
declare const hablcast: any;
|
|
22
|
+
|
|
23
|
+
export { type HablcastContext, type TransactionRequest, type UserProfile, hablcast };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var hablcast=(()=>{var c=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var p=(i,e)=>{for(var s in e)c(i,s,{get:e[s],enumerable:!0})},f=(i,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of u(e))!h.call(i,t)&&t!==s&&c(i,t,{get:()=>e[t],enumerable:!(n=d(e,t))||n.enumerable});return i};var b=i=>f(c({},"__esModule",{value:!0}),i);var w={};p(w,{hablcast:()=>g});var o=class{constructor(){this.callbacks=new Map;this.context=null;this.contextListeners=[];this.isInitialized=!1;typeof window<"u"&&window.addEventListener("message",this.handleMessage.bind(this))}ready(){if(typeof window>"u")return;this.isInitialized=!0;let e=()=>{this.context||(window.parent.postMessage({type:"hablcast:ready"},"*"),setTimeout(e,500))};e()}onContext(e){return this.contextListeners.push(e),this.context&&e(this.context),()=>{this.contextListeners=this.contextListeners.filter(s=>s!==e)}}getContext(){return this.context}requestSignature(e){return this.sendRequest("hablcast:request_sign",{message:e})}sendTransaction(e){return this.sendRequest("hablcast:request_transaction",e)}close(){typeof window>"u"||window.parent.postMessage({type:"hablcast:close"},"*")}sendRequest(e,s){if(typeof window>"u")return Promise.reject(new Error("SDK can only be used in browser environment"));let n=Math.random().toString(36).substring(2,9);return new Promise((t,a)=>{this.callbacks.set(n,{resolve:t,reject:a}),window.parent.postMessage({type:e,payload:s,requestId:n},"*")})}handleMessage(e){let s=e.data;if(!s||typeof s!="object")return;let{type:n,requestId:t,payload:a}=s;if(!(typeof n!="string"||!n.startsWith("hablcast:"))){if(n==="hablcast:context")this.context=a,this.contextListeners.forEach(r=>{try{r(a)}catch(l){console.error("Error in onContext listener callback:",l)}});else if(n==="hablcast:response"&&t){let r=this.callbacks.get(t);r&&(r.resolve(a),this.callbacks.delete(t))}else if(n==="hablcast:error"&&t){let r=this.callbacks.get(t);r&&(r.reject(new Error(a.message||"Request failed")),this.callbacks.delete(t))}}}},g=typeof window<"u"?window.hablcast=window.hablcast||new o:new o;return b(w);})();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var c=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var u=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var p=(i,e)=>{for(var s in e)c(i,s,{get:e[s],enumerable:!0})},f=(i,e,s,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of u(e))!h.call(i,t)&&t!==s&&c(i,t,{get:()=>e[t],enumerable:!(n=d(e,t))||n.enumerable});return i};var b=i=>f(c({},"__esModule",{value:!0}),i);var w={};p(w,{hablcast:()=>g});module.exports=b(w);var o=class{constructor(){this.callbacks=new Map;this.context=null;this.contextListeners=[];this.isInitialized=!1;typeof window<"u"&&window.addEventListener("message",this.handleMessage.bind(this))}ready(){if(typeof window>"u")return;this.isInitialized=!0;let e=()=>{this.context||(window.parent.postMessage({type:"hablcast:ready"},"*"),setTimeout(e,500))};e()}onContext(e){return this.contextListeners.push(e),this.context&&e(this.context),()=>{this.contextListeners=this.contextListeners.filter(s=>s!==e)}}getContext(){return this.context}requestSignature(e){return this.sendRequest("hablcast:request_sign",{message:e})}sendTransaction(e){return this.sendRequest("hablcast:request_transaction",e)}close(){typeof window>"u"||window.parent.postMessage({type:"hablcast:close"},"*")}sendRequest(e,s){if(typeof window>"u")return Promise.reject(new Error("SDK can only be used in browser environment"));let n=Math.random().toString(36).substring(2,9);return new Promise((t,a)=>{this.callbacks.set(n,{resolve:t,reject:a}),window.parent.postMessage({type:e,payload:s,requestId:n},"*")})}handleMessage(e){let s=e.data;if(!s||typeof s!="object")return;let{type:n,requestId:t,payload:a}=s;if(!(typeof n!="string"||!n.startsWith("hablcast:"))){if(n==="hablcast:context")this.context=a,this.contextListeners.forEach(r=>{try{r(a)}catch(l){console.error("Error in onContext listener callback:",l)}});else if(n==="hablcast:response"&&t){let r=this.callbacks.get(t);r&&(r.resolve(a),this.callbacks.delete(t))}else if(n==="hablcast:error"&&t){let r=this.callbacks.get(t);r&&(r.reject(new Error(a.message||"Request failed")),this.callbacks.delete(t))}}}},g=typeof window<"u"?window.hablcast=window.hablcast||new o:new o;0&&(module.exports={hablcast});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var a=class{constructor(){this.callbacks=new Map;this.context=null;this.contextListeners=[];this.isInitialized=!1;typeof window<"u"&&window.addEventListener("message",this.handleMessage.bind(this))}ready(){if(typeof window>"u")return;this.isInitialized=!0;let e=()=>{this.context||(window.parent.postMessage({type:"hablcast:ready"},"*"),setTimeout(e,500))};e()}onContext(e){return this.contextListeners.push(e),this.context&&e(this.context),()=>{this.contextListeners=this.contextListeners.filter(n=>n!==e)}}getContext(){return this.context}requestSignature(e){return this.sendRequest("hablcast:request_sign",{message:e})}sendTransaction(e){return this.sendRequest("hablcast:request_transaction",e)}close(){typeof window>"u"||window.parent.postMessage({type:"hablcast:close"},"*")}sendRequest(e,n){if(typeof window>"u")return Promise.reject(new Error("SDK can only be used in browser environment"));let t=Math.random().toString(36).substring(2,9);return new Promise((s,r)=>{this.callbacks.set(t,{resolve:s,reject:r}),window.parent.postMessage({type:e,payload:n,requestId:t},"*")})}handleMessage(e){let n=e.data;if(!n||typeof n!="object")return;let{type:t,requestId:s,payload:r}=n;if(!(typeof t!="string"||!t.startsWith("hablcast:"))){if(t==="hablcast:context")this.context=r,this.contextListeners.forEach(i=>{try{i(r)}catch(o){console.error("Error in onContext listener callback:",o)}});else if(t==="hablcast:response"&&s){let i=this.callbacks.get(s);i&&(i.resolve(r),this.callbacks.delete(s))}else if(t==="hablcast:error"&&s){let i=this.callbacks.get(s);i&&(i.reject(new Error(r.message||"Request failed")),this.callbacks.delete(s))}}}},l=typeof window<"u"?window.hablcast=window.hablcast||new a:new a;export{l as hablcast};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "hablcast-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "HABLCAST Mini-App Client SDK for postMessage communication bridge",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsup src/index.ts --format cjs,esm,iife --dts --minify --global-name hablcast",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"hablcast",
|
|
20
|
+
"farcaster",
|
|
21
|
+
"miniapp",
|
|
22
|
+
"sdk"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"tsup": "^8.0.2",
|
|
27
|
+
"typescript": "^5.0.0"
|
|
28
|
+
}
|
|
29
|
+
}
|