@1sat/connect 0.0.2 → 0.0.4
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77 -2
- package/dist/wallet.d.ts +80 -0
- package/dist/wallet.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { RpcMethods } from './types';
|
|
|
4
4
|
export { ErrorCodes, type ErrorCode, OneSatError, UserRejectedError, WalletLockedError, WalletNotConnectedError, InsufficientFundsError, PopupBlockedError, PopupClosedError, TimeoutError, fromErrorResponse, } from './errors';
|
|
5
5
|
export { PROTOCOL_VERSION, MessageTypes, type MessageType, type BaseMessage, type RequestMessage, type ResponseMessage, type ProtocolMessage, createRequest, createResponse, createErrorResponse, isValidMessage, isResponse, } from './messages';
|
|
6
6
|
export { saveConnection, loadConnection, clearConnection, hasStoredConnection, type StoredConnection, } from './storage';
|
|
7
|
+
export { parsePopupParams, sendResponse, sendErrorResponse, rejectRequest, walletLockedError, walletNotConnectedError, closePopup, isPopupContext, getPopupContext, type PopupParams, } from './wallet';
|
|
7
8
|
export { OneSatBrowserProvider } from './provider';
|
|
8
9
|
export { PopupManager, type PopupConfig, type PendingRequest } from './popup';
|
|
9
10
|
/**
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAG3D,YAAY,EACX,aAAa,EACb,YAAY,EACZ,cAAc,EACd,WAAW,EACX,YAAY,EACZ,SAAS,EACT,sBAAsB,EACtB,qBAAqB,EACrB,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,UAAU,EACV,oBAAoB,EACpB,aAAa,EACb,sBAA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C,cAAc,CAAC,CAuBrE"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAG3D,YAAY,EACX,aAAa,EACb,YAAY,EACZ,cAAc,EACd,WAAW,EACX,YAAY,EACZ,SAAS,EACT,sBAAsB,EACtB,qBAAqB,EACrB,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,UAAU,EACV,oBAAoB,EACpB,aAAa,EACb,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,WAAW,EACX,IAAI,EACJ,WAAW,GACX,MAAM,SAAS,CAAA;AAEhB,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAGpC,OAAO,EACN,UAAU,EACV,KAAK,SAAS,EACd,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,uBAAuB,EACvB,sBAAsB,EACtB,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,iBAAiB,GACjB,MAAM,UAAU,CAAA;AAGjB,OAAO,EACN,gBAAgB,EAChB,YAAY,EACZ,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,aAAa,EACb,cAAc,EACd,mBAAmB,EACnB,cAAc,EACd,UAAU,GACV,MAAM,YAAY,CAAA;AAGnB,OAAO,EACN,cAAc,EACd,cAAc,EACd,eAAe,EACf,mBAAmB,EACnB,KAAK,gBAAgB,GACrB,MAAM,WAAW,CAAA;AAGlB,OAAO,EACN,gBAAgB,EAChB,YAAY,EACZ,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,uBAAuB,EACvB,UAAU,EACV,cAAc,EACd,eAAe,EACf,KAAK,WAAW,GAChB,MAAM,UAAU,CAAA;AAGjB,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,YAAY,EAAE,KAAK,WAAW,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7E;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,OAAO,CAM1C;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,GAAG,SAAS,CAK9D;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,cAAc,CAgBlE;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,MAAM,CAAC,EAAE,YAAY,GAAG,cAAc,CAE/D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,SAAO,GAAG,OAAO,CAAC,cAAc,CAAC,CAuBrE"}
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,7 @@ var __toESM = (mod, isNodeMode, target) => {
|
|
|
16
16
|
};
|
|
17
17
|
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
18
|
|
|
19
|
-
// ../../node_modules
|
|
19
|
+
// ../../node_modules/eventemitter3/index.js
|
|
20
20
|
var require_eventemitter3 = __commonJS((exports, module) => {
|
|
21
21
|
var has = Object.prototype.hasOwnProperty;
|
|
22
22
|
var prefix = "~";
|
|
@@ -194,7 +194,7 @@ var require_eventemitter3 = __commonJS((exports, module) => {
|
|
|
194
194
|
}
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
-
// ../../node_modules
|
|
197
|
+
// ../../node_modules/eventemitter3/index.mjs
|
|
198
198
|
var import__ = __toESM(require_eventemitter3(), 1);
|
|
199
199
|
var eventemitter3_default = import__.default;
|
|
200
200
|
|
|
@@ -650,6 +650,72 @@ class OneSatBrowserProvider {
|
|
|
650
650
|
this.events.removeAllListeners();
|
|
651
651
|
}
|
|
652
652
|
}
|
|
653
|
+
// src/wallet.ts
|
|
654
|
+
function parsePopupParams(searchParams) {
|
|
655
|
+
const requestId = searchParams.get("requestId");
|
|
656
|
+
const origin = searchParams.get("origin");
|
|
657
|
+
const method = searchParams.get("method");
|
|
658
|
+
const appName = searchParams.get("appName");
|
|
659
|
+
const paramsStr = searchParams.get("params");
|
|
660
|
+
let params = undefined;
|
|
661
|
+
if (paramsStr) {
|
|
662
|
+
try {
|
|
663
|
+
params = JSON.parse(decodeURIComponent(paramsStr));
|
|
664
|
+
} catch {}
|
|
665
|
+
}
|
|
666
|
+
return { requestId, origin, method, appName, params };
|
|
667
|
+
}
|
|
668
|
+
function sendResponse(requestId, result, origin) {
|
|
669
|
+
if (typeof window === "undefined")
|
|
670
|
+
return;
|
|
671
|
+
if (!window.opener) {
|
|
672
|
+
console.error("[onesat] No opener window found");
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const response = createResponse(requestId, result, origin);
|
|
676
|
+
window.opener.postMessage(response, origin);
|
|
677
|
+
window.close();
|
|
678
|
+
}
|
|
679
|
+
function sendErrorResponse(requestId, code, message, origin, data) {
|
|
680
|
+
if (typeof window === "undefined")
|
|
681
|
+
return;
|
|
682
|
+
if (!window.opener) {
|
|
683
|
+
console.error("[onesat] No opener window found");
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
const response = createErrorResponse(requestId, code, message, origin, data);
|
|
687
|
+
window.opener.postMessage(response, origin);
|
|
688
|
+
window.close();
|
|
689
|
+
}
|
|
690
|
+
function rejectRequest(requestId, origin, reason = "User rejected the request") {
|
|
691
|
+
sendErrorResponse(requestId, ErrorCodes.USER_REJECTED, reason, origin);
|
|
692
|
+
}
|
|
693
|
+
function walletLockedError(requestId, origin) {
|
|
694
|
+
sendErrorResponse(requestId, ErrorCodes.WALLET_LOCKED, "Wallet is locked. Please unlock your wallet.", origin);
|
|
695
|
+
}
|
|
696
|
+
function walletNotConnectedError(requestId, origin) {
|
|
697
|
+
sendErrorResponse(requestId, ErrorCodes.WALLET_NOT_CONNECTED, "Wallet is not connected", origin);
|
|
698
|
+
}
|
|
699
|
+
function closePopup() {
|
|
700
|
+
if (typeof window !== "undefined") {
|
|
701
|
+
window.close();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
function isPopupContext() {
|
|
705
|
+
if (typeof window === "undefined")
|
|
706
|
+
return false;
|
|
707
|
+
return window.opener !== null;
|
|
708
|
+
}
|
|
709
|
+
function getPopupContext(searchParams) {
|
|
710
|
+
const { requestId, origin, method, appName, params } = parsePopupParams(searchParams);
|
|
711
|
+
if (!requestId) {
|
|
712
|
+
throw new Error("Missing requestId in popup parameters");
|
|
713
|
+
}
|
|
714
|
+
if (!origin) {
|
|
715
|
+
throw new Error("Missing origin in popup parameters");
|
|
716
|
+
}
|
|
717
|
+
return { requestId, origin, method, appName, params };
|
|
718
|
+
}
|
|
653
719
|
|
|
654
720
|
// src/index.ts
|
|
655
721
|
function isOneSatInjected() {
|
|
@@ -699,14 +765,22 @@ function waitForOneSat(timeout = 3000) {
|
|
|
699
765
|
});
|
|
700
766
|
}
|
|
701
767
|
export {
|
|
768
|
+
walletNotConnectedError,
|
|
769
|
+
walletLockedError,
|
|
702
770
|
waitForOneSat,
|
|
771
|
+
sendResponse,
|
|
772
|
+
sendErrorResponse,
|
|
703
773
|
saveConnection,
|
|
774
|
+
rejectRequest,
|
|
775
|
+
parsePopupParams,
|
|
704
776
|
loadConnection,
|
|
705
777
|
isValidMessage,
|
|
706
778
|
isResponse,
|
|
779
|
+
isPopupContext,
|
|
707
780
|
isOneSatInjected,
|
|
708
781
|
isOneSatAvailable,
|
|
709
782
|
hasStoredConnection,
|
|
783
|
+
getPopupContext,
|
|
710
784
|
getOneSat,
|
|
711
785
|
getInjectedOneSat,
|
|
712
786
|
fromErrorResponse,
|
|
@@ -714,6 +788,7 @@ export {
|
|
|
714
788
|
createRequest,
|
|
715
789
|
createOneSat,
|
|
716
790
|
createErrorResponse,
|
|
791
|
+
closePopup,
|
|
717
792
|
clearConnection,
|
|
718
793
|
WalletNotConnectedError,
|
|
719
794
|
WalletLockedError,
|
package/dist/wallet.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet-side utilities for popup protocol
|
|
3
|
+
*
|
|
4
|
+
* Use these in your wallet's popup pages to handle requests from dApps.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { parsePopupParams, sendResponse, sendErrorResponse, ErrorCodes } from '@1sat/connect'
|
|
9
|
+
*
|
|
10
|
+
* // In your popup page (e.g., /connect)
|
|
11
|
+
* const { requestId, origin, method, params } = parsePopupParams(
|
|
12
|
+
* new URLSearchParams(window.location.search)
|
|
13
|
+
* )
|
|
14
|
+
*
|
|
15
|
+
* // After user approves
|
|
16
|
+
* sendResponse(requestId, { paymentAddress, ordinalAddress }, origin)
|
|
17
|
+
*
|
|
18
|
+
* // Or on error
|
|
19
|
+
* sendErrorResponse(requestId, ErrorCodes.USER_REJECTED, 'User rejected', origin)
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export interface PopupParams {
|
|
23
|
+
/** Unique request identifier for matching response */
|
|
24
|
+
requestId: string | null;
|
|
25
|
+
/** Origin of the requesting dApp */
|
|
26
|
+
origin: string | null;
|
|
27
|
+
/** RPC method being called (connect, signTransaction, etc.) */
|
|
28
|
+
method: string | null;
|
|
29
|
+
/** App name for display purposes */
|
|
30
|
+
appName: string | null;
|
|
31
|
+
/** Parsed request parameters */
|
|
32
|
+
params: unknown;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse popup URL query parameters
|
|
36
|
+
*
|
|
37
|
+
* Call this in your popup page to extract the request data from the URL.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parsePopupParams(searchParams: URLSearchParams): PopupParams;
|
|
40
|
+
/**
|
|
41
|
+
* Send a success response to the opener window and close the popup
|
|
42
|
+
*/
|
|
43
|
+
export declare function sendResponse(requestId: string, result: unknown, origin: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* Send an error response to the opener window and close the popup
|
|
46
|
+
*/
|
|
47
|
+
export declare function sendErrorResponse(requestId: string, code: number, message: string, origin: string, data?: unknown): void;
|
|
48
|
+
/**
|
|
49
|
+
* Reject a request with USER_REJECTED error code
|
|
50
|
+
*/
|
|
51
|
+
export declare function rejectRequest(requestId: string, origin: string, reason?: string): void;
|
|
52
|
+
/**
|
|
53
|
+
* Send a wallet locked error response
|
|
54
|
+
*/
|
|
55
|
+
export declare function walletLockedError(requestId: string, origin: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Send a wallet not connected error response
|
|
58
|
+
*/
|
|
59
|
+
export declare function walletNotConnectedError(requestId: string, origin: string): void;
|
|
60
|
+
/**
|
|
61
|
+
* Close the popup without sending a response
|
|
62
|
+
* The dApp will receive a POPUP_CLOSED error via the PopupManager
|
|
63
|
+
*/
|
|
64
|
+
export declare function closePopup(): void;
|
|
65
|
+
/**
|
|
66
|
+
* Check if this page was opened as a popup by a dApp
|
|
67
|
+
*/
|
|
68
|
+
export declare function isPopupContext(): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Get popup context with validated parameters
|
|
71
|
+
* Throws if required parameters are missing
|
|
72
|
+
*/
|
|
73
|
+
export declare function getPopupContext(searchParams: URLSearchParams): {
|
|
74
|
+
requestId: string;
|
|
75
|
+
origin: string;
|
|
76
|
+
method: string | null;
|
|
77
|
+
appName: string | null;
|
|
78
|
+
params: unknown;
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=wallet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet.d.ts","sourceRoot":"","sources":["../src/wallet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAKH,MAAM,WAAW,WAAW;IAC3B,sDAAsD;IACtD,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,oCAAoC;IACpC,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