@1sat/connect 0.0.1
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/errors.d.ts +64 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +732 -0
- package/dist/messages.d.ts +64 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/popup.d.ts +46 -0
- package/dist/popup.d.ts.map +1 -0
- package/dist/provider.d.ts +57 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/storage.d.ts +27 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/types.d.ts +216 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +27 -0
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standard error codes for the protocol
|
|
3
|
+
*/
|
|
4
|
+
export declare const ErrorCodes: {
|
|
5
|
+
readonly PARSE_ERROR: -32700;
|
|
6
|
+
readonly INVALID_REQUEST: -32600;
|
|
7
|
+
readonly METHOD_NOT_FOUND: -32601;
|
|
8
|
+
readonly INVALID_PARAMS: -32602;
|
|
9
|
+
readonly INTERNAL_ERROR: -32603;
|
|
10
|
+
readonly USER_REJECTED: 4001;
|
|
11
|
+
readonly WALLET_LOCKED: 4002;
|
|
12
|
+
readonly WALLET_NOT_CONNECTED: 4003;
|
|
13
|
+
readonly INSUFFICIENT_FUNDS: 4004;
|
|
14
|
+
readonly INVALID_TRANSACTION: 4005;
|
|
15
|
+
readonly POPUP_BLOCKED: 4006;
|
|
16
|
+
readonly POPUP_CLOSED: 4007;
|
|
17
|
+
readonly TIMEOUT: 4008;
|
|
18
|
+
readonly NETWORK_ERROR: 4009;
|
|
19
|
+
readonly ORIGIN_NOT_ALLOWED: 4010;
|
|
20
|
+
};
|
|
21
|
+
export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
22
|
+
/**
|
|
23
|
+
* Base error class for 1Sat SDK
|
|
24
|
+
*/
|
|
25
|
+
export declare class OneSatError extends Error {
|
|
26
|
+
readonly code: ErrorCode;
|
|
27
|
+
readonly data?: unknown;
|
|
28
|
+
constructor(code: ErrorCode, message: string, data?: unknown);
|
|
29
|
+
toJSON(): {
|
|
30
|
+
code: ErrorCode;
|
|
31
|
+
message: string;
|
|
32
|
+
data: unknown;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export declare class UserRejectedError extends OneSatError {
|
|
36
|
+
constructor(message?: string);
|
|
37
|
+
}
|
|
38
|
+
export declare class WalletLockedError extends OneSatError {
|
|
39
|
+
constructor(message?: string);
|
|
40
|
+
}
|
|
41
|
+
export declare class WalletNotConnectedError extends OneSatError {
|
|
42
|
+
constructor(message?: string);
|
|
43
|
+
}
|
|
44
|
+
export declare class InsufficientFundsError extends OneSatError {
|
|
45
|
+
constructor(message?: string);
|
|
46
|
+
}
|
|
47
|
+
export declare class PopupBlockedError extends OneSatError {
|
|
48
|
+
constructor(message?: string);
|
|
49
|
+
}
|
|
50
|
+
export declare class PopupClosedError extends OneSatError {
|
|
51
|
+
constructor(message?: string);
|
|
52
|
+
}
|
|
53
|
+
export declare class TimeoutError extends OneSatError {
|
|
54
|
+
constructor(message?: string);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create an error from an error response
|
|
58
|
+
*/
|
|
59
|
+
export declare function fromErrorResponse(error: {
|
|
60
|
+
code: number;
|
|
61
|
+
message: string;
|
|
62
|
+
data?: unknown;
|
|
63
|
+
}): OneSatError;
|
|
64
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;CAmBb,CAAA;AAEV,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAA;AAEpE;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;IACxB,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAA;gBAEX,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;IAO5D,MAAM;;;;;CAON;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBACrC,OAAO,SAA8B;CAIjD;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBACrC,OAAO,SAAqB;CAIxC;AAED,qBAAa,uBAAwB,SAAQ,WAAW;gBAC3C,OAAO,SAA4B;CAI/C;AAED,qBAAa,sBAAuB,SAAQ,WAAW;gBAC1C,OAAO,SAAuB;CAI1C;AAED,qBAAa,iBAAkB,SAAQ,WAAW;gBACrC,OAAO,SAAqC;CAIxD;AAED,qBAAa,gBAAiB,SAAQ,WAAW;gBACpC,OAAO,SAAmD;CAItE;AAED,qBAAa,YAAa,SAAQ,WAAW;gBAChC,OAAO,SAAsB;CAIzC;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACxC,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,OAAO,CAAA;CACd,GAAG,WAAW,CAmBd"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { OneSatConfig, OneSatProvider } from './types';
|
|
2
|
+
export type { ConnectResult, OneSatConfig, OneSatProvider, OneSatEvent, EventHandler, RpcMethod, SignTransactionRequest, SignTransactionResult, SignMessageResult, InscribeRequest, InscribeResult, SendOrdinalsRequest, SendResult, CreateListingRequest, ListingResult, PurchaseListingRequest, CancelListingRequest, TransferTokenRequest, BalanceResult, OrdinalOutput, TokenOutput, Utxo, ListOptions, } from './types';
|
|
3
|
+
export { RpcMethods } from './types';
|
|
4
|
+
export { ErrorCodes, type ErrorCode, OneSatError, UserRejectedError, WalletLockedError, WalletNotConnectedError, InsufficientFundsError, PopupBlockedError, PopupClosedError, TimeoutError, fromErrorResponse, } from './errors';
|
|
5
|
+
export { PROTOCOL_VERSION, MessageTypes, type MessageType, type BaseMessage, type RequestMessage, type ResponseMessage, type ProtocolMessage, createRequest, createResponse, createErrorResponse, isValidMessage, isResponse, } from './messages';
|
|
6
|
+
export { saveConnection, loadConnection, clearConnection, hasStoredConnection, type StoredConnection, } from './storage';
|
|
7
|
+
export { OneSatBrowserProvider } from './provider';
|
|
8
|
+
export { PopupManager, type PopupConfig, type PendingRequest } from './popup';
|
|
9
|
+
/**
|
|
10
|
+
* Check if the OneSat provider is injected by browser extension
|
|
11
|
+
* Extensions inject window.onesat with isOneSat: true
|
|
12
|
+
*/
|
|
13
|
+
export declare function isOneSatInjected(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Get the injected OneSat provider from browser extension
|
|
16
|
+
* Returns undefined if no extension is installed
|
|
17
|
+
*/
|
|
18
|
+
export declare function getInjectedOneSat(): OneSatProvider | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Create or get a OneSat provider
|
|
21
|
+
*
|
|
22
|
+
* Detection priority:
|
|
23
|
+
* 1. If browser extension is installed (window.onesat.isOneSat), use it
|
|
24
|
+
* 2. Otherwise, create a popup-based provider
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { createOneSat } from '@1sat/connect'
|
|
29
|
+
*
|
|
30
|
+
* const onesat = createOneSat({
|
|
31
|
+
* appName: 'My dApp',
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* // Connect to wallet (opens popup if no extension)
|
|
35
|
+
* const { paymentAddress, ordinalAddress } = await onesat.connect()
|
|
36
|
+
*
|
|
37
|
+
* // Sign a transaction
|
|
38
|
+
* const result = await onesat.signTransaction({ rawtx })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function createOneSat(config?: OneSatConfig): OneSatProvider;
|
|
42
|
+
/**
|
|
43
|
+
* Get the existing provider from window.onesat or create a new popup provider
|
|
44
|
+
* @deprecated Use createOneSat() instead - it handles detection automatically
|
|
45
|
+
*/
|
|
46
|
+
export declare function getOneSat(config?: OneSatConfig): OneSatProvider;
|
|
47
|
+
/**
|
|
48
|
+
* Check if any OneSat provider is available (extension or popup)
|
|
49
|
+
*/
|
|
50
|
+
export declare function isOneSatAvailable(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Wait for the OneSat extension to be injected
|
|
53
|
+
* Useful for detecting extension after page load
|
|
54
|
+
*
|
|
55
|
+
* @param timeout - Max time to wait in ms (default: 3000)
|
|
56
|
+
* @returns The injected provider, or throws if timeout
|
|
57
|
+
*/
|
|
58
|
+
export declare function waitForOneSat(timeout?: number): Promise<OneSatProvider>;
|
|
59
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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,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,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
ADDED
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
7
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
8
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
9
|
+
for (let key of __getOwnPropNames(mod))
|
|
10
|
+
if (!__hasOwnProp.call(to, key))
|
|
11
|
+
__defProp(to, key, {
|
|
12
|
+
get: () => mod[key],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
|
|
18
|
+
|
|
19
|
+
// ../../node_modules/.bun/eventemitter3@5.0.1/node_modules/eventemitter3/index.js
|
|
20
|
+
var require_eventemitter3 = __commonJS((exports, module) => {
|
|
21
|
+
var has = Object.prototype.hasOwnProperty;
|
|
22
|
+
var prefix = "~";
|
|
23
|
+
function Events() {}
|
|
24
|
+
if (Object.create) {
|
|
25
|
+
Events.prototype = Object.create(null);
|
|
26
|
+
if (!new Events().__proto__)
|
|
27
|
+
prefix = false;
|
|
28
|
+
}
|
|
29
|
+
function EE(fn, context, once) {
|
|
30
|
+
this.fn = fn;
|
|
31
|
+
this.context = context;
|
|
32
|
+
this.once = once || false;
|
|
33
|
+
}
|
|
34
|
+
function addListener(emitter, event, fn, context, once) {
|
|
35
|
+
if (typeof fn !== "function") {
|
|
36
|
+
throw new TypeError("The listener must be a function");
|
|
37
|
+
}
|
|
38
|
+
var listener = new EE(fn, context || emitter, once), evt = prefix ? prefix + event : event;
|
|
39
|
+
if (!emitter._events[evt])
|
|
40
|
+
emitter._events[evt] = listener, emitter._eventsCount++;
|
|
41
|
+
else if (!emitter._events[evt].fn)
|
|
42
|
+
emitter._events[evt].push(listener);
|
|
43
|
+
else
|
|
44
|
+
emitter._events[evt] = [emitter._events[evt], listener];
|
|
45
|
+
return emitter;
|
|
46
|
+
}
|
|
47
|
+
function clearEvent(emitter, evt) {
|
|
48
|
+
if (--emitter._eventsCount === 0)
|
|
49
|
+
emitter._events = new Events;
|
|
50
|
+
else
|
|
51
|
+
delete emitter._events[evt];
|
|
52
|
+
}
|
|
53
|
+
function EventEmitter() {
|
|
54
|
+
this._events = new Events;
|
|
55
|
+
this._eventsCount = 0;
|
|
56
|
+
}
|
|
57
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
58
|
+
var names = [], events, name;
|
|
59
|
+
if (this._eventsCount === 0)
|
|
60
|
+
return names;
|
|
61
|
+
for (name in events = this._events) {
|
|
62
|
+
if (has.call(events, name))
|
|
63
|
+
names.push(prefix ? name.slice(1) : name);
|
|
64
|
+
}
|
|
65
|
+
if (Object.getOwnPropertySymbols) {
|
|
66
|
+
return names.concat(Object.getOwnPropertySymbols(events));
|
|
67
|
+
}
|
|
68
|
+
return names;
|
|
69
|
+
};
|
|
70
|
+
EventEmitter.prototype.listeners = function listeners(event) {
|
|
71
|
+
var evt = prefix ? prefix + event : event, handlers = this._events[evt];
|
|
72
|
+
if (!handlers)
|
|
73
|
+
return [];
|
|
74
|
+
if (handlers.fn)
|
|
75
|
+
return [handlers.fn];
|
|
76
|
+
for (var i = 0, l = handlers.length, ee = new Array(l);i < l; i++) {
|
|
77
|
+
ee[i] = handlers[i].fn;
|
|
78
|
+
}
|
|
79
|
+
return ee;
|
|
80
|
+
};
|
|
81
|
+
EventEmitter.prototype.listenerCount = function listenerCount(event) {
|
|
82
|
+
var evt = prefix ? prefix + event : event, listeners = this._events[evt];
|
|
83
|
+
if (!listeners)
|
|
84
|
+
return 0;
|
|
85
|
+
if (listeners.fn)
|
|
86
|
+
return 1;
|
|
87
|
+
return listeners.length;
|
|
88
|
+
};
|
|
89
|
+
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
|
|
90
|
+
var evt = prefix ? prefix + event : event;
|
|
91
|
+
if (!this._events[evt])
|
|
92
|
+
return false;
|
|
93
|
+
var listeners = this._events[evt], len = arguments.length, args, i;
|
|
94
|
+
if (listeners.fn) {
|
|
95
|
+
if (listeners.once)
|
|
96
|
+
this.removeListener(event, listeners.fn, undefined, true);
|
|
97
|
+
switch (len) {
|
|
98
|
+
case 1:
|
|
99
|
+
return listeners.fn.call(listeners.context), true;
|
|
100
|
+
case 2:
|
|
101
|
+
return listeners.fn.call(listeners.context, a1), true;
|
|
102
|
+
case 3:
|
|
103
|
+
return listeners.fn.call(listeners.context, a1, a2), true;
|
|
104
|
+
case 4:
|
|
105
|
+
return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
106
|
+
case 5:
|
|
107
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
108
|
+
case 6:
|
|
109
|
+
return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
110
|
+
}
|
|
111
|
+
for (i = 1, args = new Array(len - 1);i < len; i++) {
|
|
112
|
+
args[i - 1] = arguments[i];
|
|
113
|
+
}
|
|
114
|
+
listeners.fn.apply(listeners.context, args);
|
|
115
|
+
} else {
|
|
116
|
+
var length = listeners.length, j;
|
|
117
|
+
for (i = 0;i < length; i++) {
|
|
118
|
+
if (listeners[i].once)
|
|
119
|
+
this.removeListener(event, listeners[i].fn, undefined, true);
|
|
120
|
+
switch (len) {
|
|
121
|
+
case 1:
|
|
122
|
+
listeners[i].fn.call(listeners[i].context);
|
|
123
|
+
break;
|
|
124
|
+
case 2:
|
|
125
|
+
listeners[i].fn.call(listeners[i].context, a1);
|
|
126
|
+
break;
|
|
127
|
+
case 3:
|
|
128
|
+
listeners[i].fn.call(listeners[i].context, a1, a2);
|
|
129
|
+
break;
|
|
130
|
+
case 4:
|
|
131
|
+
listeners[i].fn.call(listeners[i].context, a1, a2, a3);
|
|
132
|
+
break;
|
|
133
|
+
default:
|
|
134
|
+
if (!args)
|
|
135
|
+
for (j = 1, args = new Array(len - 1);j < len; j++) {
|
|
136
|
+
args[j - 1] = arguments[j];
|
|
137
|
+
}
|
|
138
|
+
listeners[i].fn.apply(listeners[i].context, args);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return true;
|
|
143
|
+
};
|
|
144
|
+
EventEmitter.prototype.on = function on(event, fn, context) {
|
|
145
|
+
return addListener(this, event, fn, context, false);
|
|
146
|
+
};
|
|
147
|
+
EventEmitter.prototype.once = function once(event, fn, context) {
|
|
148
|
+
return addListener(this, event, fn, context, true);
|
|
149
|
+
};
|
|
150
|
+
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
|
|
151
|
+
var evt = prefix ? prefix + event : event;
|
|
152
|
+
if (!this._events[evt])
|
|
153
|
+
return this;
|
|
154
|
+
if (!fn) {
|
|
155
|
+
clearEvent(this, evt);
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
var listeners = this._events[evt];
|
|
159
|
+
if (listeners.fn) {
|
|
160
|
+
if (listeners.fn === fn && (!once || listeners.once) && (!context || listeners.context === context)) {
|
|
161
|
+
clearEvent(this, evt);
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
for (var i = 0, events = [], length = listeners.length;i < length; i++) {
|
|
165
|
+
if (listeners[i].fn !== fn || once && !listeners[i].once || context && listeners[i].context !== context) {
|
|
166
|
+
events.push(listeners[i]);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (events.length)
|
|
170
|
+
this._events[evt] = events.length === 1 ? events[0] : events;
|
|
171
|
+
else
|
|
172
|
+
clearEvent(this, evt);
|
|
173
|
+
}
|
|
174
|
+
return this;
|
|
175
|
+
};
|
|
176
|
+
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
|
|
177
|
+
var evt;
|
|
178
|
+
if (event) {
|
|
179
|
+
evt = prefix ? prefix + event : event;
|
|
180
|
+
if (this._events[evt])
|
|
181
|
+
clearEvent(this, evt);
|
|
182
|
+
} else {
|
|
183
|
+
this._events = new Events;
|
|
184
|
+
this._eventsCount = 0;
|
|
185
|
+
}
|
|
186
|
+
return this;
|
|
187
|
+
};
|
|
188
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
189
|
+
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
|
190
|
+
EventEmitter.prefixed = prefix;
|
|
191
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
192
|
+
if (typeof module !== "undefined") {
|
|
193
|
+
module.exports = EventEmitter;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// ../../node_modules/.bun/eventemitter3@5.0.1/node_modules/eventemitter3/index.mjs
|
|
198
|
+
var import__ = __toESM(require_eventemitter3(), 1);
|
|
199
|
+
var eventemitter3_default = import__.default;
|
|
200
|
+
|
|
201
|
+
// src/errors.ts
|
|
202
|
+
var ErrorCodes = {
|
|
203
|
+
PARSE_ERROR: -32700,
|
|
204
|
+
INVALID_REQUEST: -32600,
|
|
205
|
+
METHOD_NOT_FOUND: -32601,
|
|
206
|
+
INVALID_PARAMS: -32602,
|
|
207
|
+
INTERNAL_ERROR: -32603,
|
|
208
|
+
USER_REJECTED: 4001,
|
|
209
|
+
WALLET_LOCKED: 4002,
|
|
210
|
+
WALLET_NOT_CONNECTED: 4003,
|
|
211
|
+
INSUFFICIENT_FUNDS: 4004,
|
|
212
|
+
INVALID_TRANSACTION: 4005,
|
|
213
|
+
POPUP_BLOCKED: 4006,
|
|
214
|
+
POPUP_CLOSED: 4007,
|
|
215
|
+
TIMEOUT: 4008,
|
|
216
|
+
NETWORK_ERROR: 4009,
|
|
217
|
+
ORIGIN_NOT_ALLOWED: 4010
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
class OneSatError extends Error {
|
|
221
|
+
code;
|
|
222
|
+
data;
|
|
223
|
+
constructor(code, message, data) {
|
|
224
|
+
super(message);
|
|
225
|
+
this.name = "OneSatError";
|
|
226
|
+
this.code = code;
|
|
227
|
+
this.data = data;
|
|
228
|
+
}
|
|
229
|
+
toJSON() {
|
|
230
|
+
return {
|
|
231
|
+
code: this.code,
|
|
232
|
+
message: this.message,
|
|
233
|
+
data: this.data
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
class UserRejectedError extends OneSatError {
|
|
239
|
+
constructor(message = "User rejected the request") {
|
|
240
|
+
super(ErrorCodes.USER_REJECTED, message);
|
|
241
|
+
this.name = "UserRejectedError";
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
class WalletLockedError extends OneSatError {
|
|
246
|
+
constructor(message = "Wallet is locked") {
|
|
247
|
+
super(ErrorCodes.WALLET_LOCKED, message);
|
|
248
|
+
this.name = "WalletLockedError";
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
class WalletNotConnectedError extends OneSatError {
|
|
253
|
+
constructor(message = "Wallet is not connected") {
|
|
254
|
+
super(ErrorCodes.WALLET_NOT_CONNECTED, message);
|
|
255
|
+
this.name = "WalletNotConnectedError";
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
class InsufficientFundsError extends OneSatError {
|
|
260
|
+
constructor(message = "Insufficient funds") {
|
|
261
|
+
super(ErrorCodes.INSUFFICIENT_FUNDS, message);
|
|
262
|
+
this.name = "InsufficientFundsError";
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
class PopupBlockedError extends OneSatError {
|
|
267
|
+
constructor(message = "Popup was blocked by the browser") {
|
|
268
|
+
super(ErrorCodes.POPUP_BLOCKED, message);
|
|
269
|
+
this.name = "PopupBlockedError";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
class PopupClosedError extends OneSatError {
|
|
274
|
+
constructor(message = "Popup was closed before completing the request") {
|
|
275
|
+
super(ErrorCodes.POPUP_CLOSED, message);
|
|
276
|
+
this.name = "PopupClosedError";
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
class TimeoutError extends OneSatError {
|
|
281
|
+
constructor(message = "Request timed out") {
|
|
282
|
+
super(ErrorCodes.TIMEOUT, message);
|
|
283
|
+
this.name = "TimeoutError";
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function fromErrorResponse(error) {
|
|
287
|
+
switch (error.code) {
|
|
288
|
+
case ErrorCodes.USER_REJECTED:
|
|
289
|
+
return new UserRejectedError(error.message);
|
|
290
|
+
case ErrorCodes.WALLET_LOCKED:
|
|
291
|
+
return new WalletLockedError(error.message);
|
|
292
|
+
case ErrorCodes.WALLET_NOT_CONNECTED:
|
|
293
|
+
return new WalletNotConnectedError(error.message);
|
|
294
|
+
case ErrorCodes.INSUFFICIENT_FUNDS:
|
|
295
|
+
return new InsufficientFundsError(error.message);
|
|
296
|
+
case ErrorCodes.POPUP_BLOCKED:
|
|
297
|
+
return new PopupBlockedError(error.message);
|
|
298
|
+
case ErrorCodes.POPUP_CLOSED:
|
|
299
|
+
return new PopupClosedError(error.message);
|
|
300
|
+
case ErrorCodes.TIMEOUT:
|
|
301
|
+
return new TimeoutError(error.message);
|
|
302
|
+
default:
|
|
303
|
+
return new OneSatError(error.code, error.message, error.data);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// src/messages.ts
|
|
308
|
+
var PROTOCOL_VERSION = "1.0.0";
|
|
309
|
+
var MessageTypes = {
|
|
310
|
+
REQUEST: "onesat:request",
|
|
311
|
+
RESPONSE: "onesat:response"
|
|
312
|
+
};
|
|
313
|
+
function createRequest(requestId, method, params, origin) {
|
|
314
|
+
return {
|
|
315
|
+
type: MessageTypes.REQUEST,
|
|
316
|
+
version: PROTOCOL_VERSION,
|
|
317
|
+
requestId,
|
|
318
|
+
origin,
|
|
319
|
+
method,
|
|
320
|
+
params
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function createResponse(requestId, result, origin) {
|
|
324
|
+
return {
|
|
325
|
+
type: MessageTypes.RESPONSE,
|
|
326
|
+
version: PROTOCOL_VERSION,
|
|
327
|
+
requestId,
|
|
328
|
+
origin,
|
|
329
|
+
result
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function createErrorResponse(requestId, code, message, origin, data) {
|
|
333
|
+
return {
|
|
334
|
+
type: MessageTypes.RESPONSE,
|
|
335
|
+
version: PROTOCOL_VERSION,
|
|
336
|
+
requestId,
|
|
337
|
+
origin,
|
|
338
|
+
error: { code, message, data }
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
function isValidMessage(data) {
|
|
342
|
+
if (typeof data !== "object" || data === null)
|
|
343
|
+
return false;
|
|
344
|
+
const msg = data;
|
|
345
|
+
if (typeof msg.type !== "string")
|
|
346
|
+
return false;
|
|
347
|
+
if (typeof msg.version !== "string")
|
|
348
|
+
return false;
|
|
349
|
+
if (typeof msg.requestId !== "string")
|
|
350
|
+
return false;
|
|
351
|
+
if (typeof msg.origin !== "string")
|
|
352
|
+
return false;
|
|
353
|
+
if (msg.type === MessageTypes.REQUEST) {
|
|
354
|
+
if (typeof msg.method !== "string")
|
|
355
|
+
return false;
|
|
356
|
+
return true;
|
|
357
|
+
}
|
|
358
|
+
if (msg.type === MessageTypes.RESPONSE) {
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
function isResponse(msg) {
|
|
364
|
+
return msg.type === MessageTypes.RESPONSE;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/popup.ts
|
|
368
|
+
var DEFAULT_POPUP_URL = "https://1sat.market";
|
|
369
|
+
var DEFAULT_TIMEOUT = 300000;
|
|
370
|
+
var POPUP_CHECK_INTERVAL = 500;
|
|
371
|
+
|
|
372
|
+
class PopupManager {
|
|
373
|
+
config;
|
|
374
|
+
pendingRequests = new Map;
|
|
375
|
+
constructor(config) {
|
|
376
|
+
this.config = {
|
|
377
|
+
popupUrl: config?.popupUrl ?? DEFAULT_POPUP_URL,
|
|
378
|
+
timeout: config?.timeout ?? DEFAULT_TIMEOUT,
|
|
379
|
+
appName: config?.appName
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
openPopup(method, requestId, params) {
|
|
383
|
+
return new Promise((resolve, reject) => {
|
|
384
|
+
const url = new URL(`${this.config.popupUrl}/connect`);
|
|
385
|
+
url.searchParams.set("method", method);
|
|
386
|
+
url.searchParams.set("requestId", requestId);
|
|
387
|
+
url.searchParams.set("origin", window.location.origin);
|
|
388
|
+
if (this.config.appName) {
|
|
389
|
+
url.searchParams.set("appName", this.config.appName);
|
|
390
|
+
}
|
|
391
|
+
if (params) {
|
|
392
|
+
url.searchParams.set("params", JSON.stringify(params));
|
|
393
|
+
}
|
|
394
|
+
const width = 420;
|
|
395
|
+
const height = 640;
|
|
396
|
+
const left = Math.max(0, (window.screen.width - width) / 2);
|
|
397
|
+
const top = Math.max(0, (window.screen.height - height) / 2);
|
|
398
|
+
const popup = window.open(url.toString(), "onesat-wallet", `width=${width},height=${height},left=${left},top=${top},popup=1,scrollbars=1`);
|
|
399
|
+
if (!popup) {
|
|
400
|
+
reject(new PopupBlockedError);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
popup.focus();
|
|
404
|
+
const timeoutId = setTimeout(() => {
|
|
405
|
+
this.cleanup(requestId);
|
|
406
|
+
reject(new TimeoutError);
|
|
407
|
+
}, this.config.timeout);
|
|
408
|
+
const checkIntervalId = setInterval(() => {
|
|
409
|
+
if (popup.closed) {
|
|
410
|
+
this.cleanup(requestId);
|
|
411
|
+
reject(new PopupClosedError);
|
|
412
|
+
}
|
|
413
|
+
}, POPUP_CHECK_INTERVAL);
|
|
414
|
+
this.pendingRequests.set(requestId, {
|
|
415
|
+
resolve,
|
|
416
|
+
reject,
|
|
417
|
+
popup,
|
|
418
|
+
timeoutId,
|
|
419
|
+
checkIntervalId
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
resolveRequest(requestId, result) {
|
|
424
|
+
const pending = this.pendingRequests.get(requestId);
|
|
425
|
+
if (!pending)
|
|
426
|
+
return false;
|
|
427
|
+
this.cleanup(requestId);
|
|
428
|
+
pending.resolve(result);
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
rejectRequest(requestId, error) {
|
|
432
|
+
const pending = this.pendingRequests.get(requestId);
|
|
433
|
+
if (!pending)
|
|
434
|
+
return false;
|
|
435
|
+
this.cleanup(requestId);
|
|
436
|
+
pending.reject(error);
|
|
437
|
+
return true;
|
|
438
|
+
}
|
|
439
|
+
hasPendingRequest(requestId) {
|
|
440
|
+
return this.pendingRequests.has(requestId);
|
|
441
|
+
}
|
|
442
|
+
cleanup(requestId) {
|
|
443
|
+
const pending = this.pendingRequests.get(requestId);
|
|
444
|
+
if (!pending)
|
|
445
|
+
return;
|
|
446
|
+
clearTimeout(pending.timeoutId);
|
|
447
|
+
clearInterval(pending.checkIntervalId);
|
|
448
|
+
if (pending.popup && !pending.popup.closed) {
|
|
449
|
+
pending.popup.close();
|
|
450
|
+
}
|
|
451
|
+
this.pendingRequests.delete(requestId);
|
|
452
|
+
}
|
|
453
|
+
destroy() {
|
|
454
|
+
for (const requestId of this.pendingRequests.keys()) {
|
|
455
|
+
this.cleanup(requestId);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// src/storage.ts
|
|
461
|
+
var STORAGE_KEY = "onesat:connection";
|
|
462
|
+
function saveConnection(connection) {
|
|
463
|
+
const stored = {
|
|
464
|
+
...connection,
|
|
465
|
+
connectedAt: Date.now()
|
|
466
|
+
};
|
|
467
|
+
try {
|
|
468
|
+
localStorage.setItem(STORAGE_KEY, JSON.stringify(stored));
|
|
469
|
+
} catch {}
|
|
470
|
+
}
|
|
471
|
+
function loadConnection() {
|
|
472
|
+
try {
|
|
473
|
+
const data = localStorage.getItem(STORAGE_KEY);
|
|
474
|
+
if (!data)
|
|
475
|
+
return null;
|
|
476
|
+
return JSON.parse(data);
|
|
477
|
+
} catch {
|
|
478
|
+
return null;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function clearConnection() {
|
|
482
|
+
try {
|
|
483
|
+
localStorage.removeItem(STORAGE_KEY);
|
|
484
|
+
} catch {}
|
|
485
|
+
}
|
|
486
|
+
function hasStoredConnection() {
|
|
487
|
+
return loadConnection() !== null;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// src/types.ts
|
|
491
|
+
var RpcMethods = {
|
|
492
|
+
CONNECT: "connect",
|
|
493
|
+
DISCONNECT: "disconnect",
|
|
494
|
+
SIGN_TRANSACTION: "signTransaction",
|
|
495
|
+
SIGN_MESSAGE: "signMessage",
|
|
496
|
+
INSCRIBE: "inscribe",
|
|
497
|
+
SEND_ORDINALS: "sendOrdinals",
|
|
498
|
+
CREATE_LISTING: "createListing",
|
|
499
|
+
PURCHASE_LISTING: "purchaseListing",
|
|
500
|
+
CANCEL_LISTING: "cancelListing",
|
|
501
|
+
TRANSFER_TOKEN: "transferToken",
|
|
502
|
+
GET_BALANCE: "getBalance",
|
|
503
|
+
GET_ORDINALS: "getOrdinals",
|
|
504
|
+
GET_TOKENS: "getTokens",
|
|
505
|
+
GET_UTXOS: "getUtxos"
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
// src/provider.ts
|
|
509
|
+
var DEFAULT_POPUP_URL2 = "https://1sat.market";
|
|
510
|
+
var DEFAULT_TIMEOUT2 = 300000;
|
|
511
|
+
|
|
512
|
+
class OneSatBrowserProvider {
|
|
513
|
+
isOneSat = true;
|
|
514
|
+
popupManager;
|
|
515
|
+
connection = null;
|
|
516
|
+
events = new eventemitter3_default;
|
|
517
|
+
popupOrigin;
|
|
518
|
+
constructor(config) {
|
|
519
|
+
const popupUrl = config?.popupUrl ?? DEFAULT_POPUP_URL2;
|
|
520
|
+
this.popupManager = new PopupManager({
|
|
521
|
+
popupUrl,
|
|
522
|
+
timeout: config?.timeout ?? DEFAULT_TIMEOUT2,
|
|
523
|
+
appName: config?.appName
|
|
524
|
+
});
|
|
525
|
+
this.popupOrigin = new URL(popupUrl).origin;
|
|
526
|
+
this.connection = loadConnection();
|
|
527
|
+
if (typeof window !== "undefined") {
|
|
528
|
+
window.addEventListener("message", this.handleMessage);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
handleMessage = (event) => {
|
|
532
|
+
if (event.origin !== this.popupOrigin)
|
|
533
|
+
return;
|
|
534
|
+
if (!isValidMessage(event.data))
|
|
535
|
+
return;
|
|
536
|
+
if (!isResponse(event.data))
|
|
537
|
+
return;
|
|
538
|
+
const { requestId, result, error } = event.data;
|
|
539
|
+
if (!this.popupManager.hasPendingRequest(requestId))
|
|
540
|
+
return;
|
|
541
|
+
if (error) {
|
|
542
|
+
this.popupManager.rejectRequest(requestId, fromErrorResponse(error));
|
|
543
|
+
} else {
|
|
544
|
+
this.popupManager.resolveRequest(requestId, result);
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
generateRequestId() {
|
|
548
|
+
return crypto.randomUUID();
|
|
549
|
+
}
|
|
550
|
+
async sendRequest(method, params) {
|
|
551
|
+
const requestId = this.generateRequestId();
|
|
552
|
+
return this.popupManager.openPopup(method, requestId, params);
|
|
553
|
+
}
|
|
554
|
+
async connect() {
|
|
555
|
+
const result = await this.sendRequest(RpcMethods.CONNECT);
|
|
556
|
+
this.connection = {
|
|
557
|
+
...result,
|
|
558
|
+
connectedAt: Date.now()
|
|
559
|
+
};
|
|
560
|
+
saveConnection(result);
|
|
561
|
+
this.events.emit("connect", result);
|
|
562
|
+
return result;
|
|
563
|
+
}
|
|
564
|
+
async disconnect() {
|
|
565
|
+
this.connection = null;
|
|
566
|
+
clearConnection();
|
|
567
|
+
this.events.emit("disconnect", undefined);
|
|
568
|
+
}
|
|
569
|
+
isConnected() {
|
|
570
|
+
return this.connection !== null;
|
|
571
|
+
}
|
|
572
|
+
requireConnection() {
|
|
573
|
+
if (!this.connection) {
|
|
574
|
+
throw new WalletNotConnectedError;
|
|
575
|
+
}
|
|
576
|
+
return this.connection;
|
|
577
|
+
}
|
|
578
|
+
async signTransaction(request) {
|
|
579
|
+
this.requireConnection();
|
|
580
|
+
return this.sendRequest(RpcMethods.SIGN_TRANSACTION, request);
|
|
581
|
+
}
|
|
582
|
+
async signMessage(message) {
|
|
583
|
+
this.requireConnection();
|
|
584
|
+
return this.sendRequest(RpcMethods.SIGN_MESSAGE, {
|
|
585
|
+
message
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
async inscribe(request) {
|
|
589
|
+
this.requireConnection();
|
|
590
|
+
return this.sendRequest(RpcMethods.INSCRIBE, request);
|
|
591
|
+
}
|
|
592
|
+
async sendOrdinals(request) {
|
|
593
|
+
this.requireConnection();
|
|
594
|
+
return this.sendRequest(RpcMethods.SEND_ORDINALS, request);
|
|
595
|
+
}
|
|
596
|
+
async createListing(request) {
|
|
597
|
+
this.requireConnection();
|
|
598
|
+
return this.sendRequest(RpcMethods.CREATE_LISTING, request);
|
|
599
|
+
}
|
|
600
|
+
async purchaseListing(request) {
|
|
601
|
+
this.requireConnection();
|
|
602
|
+
return this.sendRequest(RpcMethods.PURCHASE_LISTING, request);
|
|
603
|
+
}
|
|
604
|
+
async cancelListing(request) {
|
|
605
|
+
this.requireConnection();
|
|
606
|
+
return this.sendRequest(RpcMethods.CANCEL_LISTING, request);
|
|
607
|
+
}
|
|
608
|
+
async transferToken(request) {
|
|
609
|
+
this.requireConnection();
|
|
610
|
+
return this.sendRequest(RpcMethods.TRANSFER_TOKEN, request);
|
|
611
|
+
}
|
|
612
|
+
async getBalance() {
|
|
613
|
+
this.requireConnection();
|
|
614
|
+
return this.sendRequest(RpcMethods.GET_BALANCE);
|
|
615
|
+
}
|
|
616
|
+
async getOrdinals(options) {
|
|
617
|
+
this.requireConnection();
|
|
618
|
+
return this.sendRequest(RpcMethods.GET_ORDINALS, options);
|
|
619
|
+
}
|
|
620
|
+
async getTokens(options) {
|
|
621
|
+
this.requireConnection();
|
|
622
|
+
return this.sendRequest(RpcMethods.GET_TOKENS, options);
|
|
623
|
+
}
|
|
624
|
+
async getUtxos() {
|
|
625
|
+
this.requireConnection();
|
|
626
|
+
return this.sendRequest(RpcMethods.GET_UTXOS);
|
|
627
|
+
}
|
|
628
|
+
on(event, handler) {
|
|
629
|
+
this.events.on(event, handler);
|
|
630
|
+
}
|
|
631
|
+
off(event, handler) {
|
|
632
|
+
this.events.off(event, handler);
|
|
633
|
+
}
|
|
634
|
+
getAddresses() {
|
|
635
|
+
if (!this.connection)
|
|
636
|
+
return null;
|
|
637
|
+
return {
|
|
638
|
+
paymentAddress: this.connection.paymentAddress,
|
|
639
|
+
ordinalAddress: this.connection.ordinalAddress
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
getIdentityPubKey() {
|
|
643
|
+
return this.connection?.identityPubKey ?? null;
|
|
644
|
+
}
|
|
645
|
+
destroy() {
|
|
646
|
+
if (typeof window !== "undefined") {
|
|
647
|
+
window.removeEventListener("message", this.handleMessage);
|
|
648
|
+
}
|
|
649
|
+
this.popupManager.destroy();
|
|
650
|
+
this.events.removeAllListeners();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// src/index.ts
|
|
655
|
+
function isOneSatInjected() {
|
|
656
|
+
return typeof window !== "undefined" && window.onesat !== undefined && window.onesat.isOneSat === true;
|
|
657
|
+
}
|
|
658
|
+
function getInjectedOneSat() {
|
|
659
|
+
if (isOneSatInjected()) {
|
|
660
|
+
return window.onesat;
|
|
661
|
+
}
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
function createOneSat(config) {
|
|
665
|
+
const injected = getInjectedOneSat();
|
|
666
|
+
if (injected) {
|
|
667
|
+
return injected;
|
|
668
|
+
}
|
|
669
|
+
const provider = new OneSatBrowserProvider(config);
|
|
670
|
+
if (typeof window !== "undefined" && !window.onesat) {
|
|
671
|
+
window.onesat = provider;
|
|
672
|
+
}
|
|
673
|
+
return provider;
|
|
674
|
+
}
|
|
675
|
+
function getOneSat(config) {
|
|
676
|
+
return createOneSat(config);
|
|
677
|
+
}
|
|
678
|
+
function isOneSatAvailable() {
|
|
679
|
+
return typeof window !== "undefined" && window.onesat?.isOneSat === true;
|
|
680
|
+
}
|
|
681
|
+
function waitForOneSat(timeout = 3000) {
|
|
682
|
+
return new Promise((resolve, reject) => {
|
|
683
|
+
if (isOneSatInjected()) {
|
|
684
|
+
resolve(window.onesat);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const startTime = Date.now();
|
|
688
|
+
const checkInterval = setInterval(() => {
|
|
689
|
+
if (isOneSatInjected()) {
|
|
690
|
+
clearInterval(checkInterval);
|
|
691
|
+
resolve(window.onesat);
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
if (Date.now() - startTime > timeout) {
|
|
695
|
+
clearInterval(checkInterval);
|
|
696
|
+
reject(new Error("OneSat extension not detected"));
|
|
697
|
+
}
|
|
698
|
+
}, 100);
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
export {
|
|
702
|
+
waitForOneSat,
|
|
703
|
+
saveConnection,
|
|
704
|
+
loadConnection,
|
|
705
|
+
isValidMessage,
|
|
706
|
+
isResponse,
|
|
707
|
+
isOneSatInjected,
|
|
708
|
+
isOneSatAvailable,
|
|
709
|
+
hasStoredConnection,
|
|
710
|
+
getOneSat,
|
|
711
|
+
getInjectedOneSat,
|
|
712
|
+
fromErrorResponse,
|
|
713
|
+
createResponse,
|
|
714
|
+
createRequest,
|
|
715
|
+
createOneSat,
|
|
716
|
+
createErrorResponse,
|
|
717
|
+
clearConnection,
|
|
718
|
+
WalletNotConnectedError,
|
|
719
|
+
WalletLockedError,
|
|
720
|
+
UserRejectedError,
|
|
721
|
+
TimeoutError,
|
|
722
|
+
RpcMethods,
|
|
723
|
+
PopupManager,
|
|
724
|
+
PopupClosedError,
|
|
725
|
+
PopupBlockedError,
|
|
726
|
+
PROTOCOL_VERSION,
|
|
727
|
+
OneSatError,
|
|
728
|
+
OneSatBrowserProvider,
|
|
729
|
+
MessageTypes,
|
|
730
|
+
InsufficientFundsError,
|
|
731
|
+
ErrorCodes
|
|
732
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { RpcMethod } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Protocol version for compatibility checking
|
|
4
|
+
*/
|
|
5
|
+
export declare const PROTOCOL_VERSION = "1.0.0";
|
|
6
|
+
/**
|
|
7
|
+
* Message type identifiers
|
|
8
|
+
*/
|
|
9
|
+
export declare const MessageTypes: {
|
|
10
|
+
readonly REQUEST: "onesat:request";
|
|
11
|
+
readonly RESPONSE: "onesat:response";
|
|
12
|
+
};
|
|
13
|
+
export type MessageType = (typeof MessageTypes)[keyof typeof MessageTypes];
|
|
14
|
+
/**
|
|
15
|
+
* Base message structure
|
|
16
|
+
*/
|
|
17
|
+
export interface BaseMessage {
|
|
18
|
+
type: MessageType;
|
|
19
|
+
version: string;
|
|
20
|
+
requestId: string;
|
|
21
|
+
origin: string;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Request message from dApp to wallet
|
|
25
|
+
*/
|
|
26
|
+
export interface RequestMessage extends BaseMessage {
|
|
27
|
+
type: typeof MessageTypes.REQUEST;
|
|
28
|
+
method: RpcMethod;
|
|
29
|
+
params: unknown;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Response message from wallet to dApp
|
|
33
|
+
*/
|
|
34
|
+
export interface ResponseMessage extends BaseMessage {
|
|
35
|
+
type: typeof MessageTypes.RESPONSE;
|
|
36
|
+
result?: unknown;
|
|
37
|
+
error?: {
|
|
38
|
+
code: number;
|
|
39
|
+
message: string;
|
|
40
|
+
data?: unknown;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export type ProtocolMessage = RequestMessage | ResponseMessage;
|
|
44
|
+
/**
|
|
45
|
+
* Create a request message
|
|
46
|
+
*/
|
|
47
|
+
export declare function createRequest(requestId: string, method: RpcMethod, params: unknown, origin: string): RequestMessage;
|
|
48
|
+
/**
|
|
49
|
+
* Create a success response message
|
|
50
|
+
*/
|
|
51
|
+
export declare function createResponse(requestId: string, result: unknown, origin: string): ResponseMessage;
|
|
52
|
+
/**
|
|
53
|
+
* Create an error response message
|
|
54
|
+
*/
|
|
55
|
+
export declare function createErrorResponse(requestId: string, code: number, message: string, origin: string, data?: unknown): ResponseMessage;
|
|
56
|
+
/**
|
|
57
|
+
* Validate that a message conforms to the protocol
|
|
58
|
+
*/
|
|
59
|
+
export declare function isValidMessage(data: unknown): data is ProtocolMessage;
|
|
60
|
+
/**
|
|
61
|
+
* Check if message is a response
|
|
62
|
+
*/
|
|
63
|
+
export declare function isResponse(msg: ProtocolMessage): msg is ResponseMessage;
|
|
64
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAExC;;GAEG;AACH,eAAO,MAAM,gBAAgB,UAAU,CAAA;AAEvC;;GAEG;AACH,eAAO,MAAM,YAAY;;;CAGf,CAAA;AAEV,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,YAAY,CAAC,CAAC,MAAM,OAAO,YAAY,CAAC,CAAA;AAE1E;;GAEG;AACH,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,WAAW,CAAA;IACjB,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,WAAW;IAClD,IAAI,EAAE,OAAO,YAAY,CAAC,OAAO,CAAA;IACjC,MAAM,EAAE,SAAS,CAAA;IACjB,MAAM,EAAE,OAAO,CAAA;CACf;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,WAAW;IACnD,IAAI,EAAE,OAAO,YAAY,CAAC,QAAQ,CAAA;IAClC,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,KAAK,CAAC,EAAE;QACP,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,IAAI,CAAC,EAAE,OAAO,CAAA;KACd,CAAA;CACD;AAED,MAAM,MAAM,eAAe,GAAG,cAAc,GAAG,eAAe,CAAA;AAE9D;;GAEG;AACH,wBAAgB,aAAa,CAC5B,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,GACZ,cAAc,CAShB;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC7B,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,MAAM,GACZ,eAAe,CAQjB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAClC,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,OAAO,GACZ,eAAe,CAQjB;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,IAAI,eAAe,CAoBrE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,eAAe,GAAG,GAAG,IAAI,eAAe,CAEvE"}
|
package/dist/popup.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { RpcMethod } from './types';
|
|
2
|
+
export interface PopupConfig {
|
|
3
|
+
popupUrl: string;
|
|
4
|
+
timeout: number;
|
|
5
|
+
appName?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PendingRequest<T> {
|
|
8
|
+
resolve: (value: T) => void;
|
|
9
|
+
reject: (error: Error) => void;
|
|
10
|
+
popup: Window | null;
|
|
11
|
+
timeoutId: ReturnType<typeof setTimeout>;
|
|
12
|
+
checkIntervalId: ReturnType<typeof setInterval>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Manages popup windows for wallet requests
|
|
16
|
+
*/
|
|
17
|
+
export declare class PopupManager {
|
|
18
|
+
private config;
|
|
19
|
+
private pendingRequests;
|
|
20
|
+
constructor(config?: Partial<PopupConfig>);
|
|
21
|
+
/**
|
|
22
|
+
* Open a popup window for a wallet request
|
|
23
|
+
*/
|
|
24
|
+
openPopup<T>(method: RpcMethod, requestId: string, params?: unknown): Promise<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Resolve a pending request with a result
|
|
27
|
+
*/
|
|
28
|
+
resolveRequest(requestId: string, result: unknown): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Reject a pending request with an error
|
|
31
|
+
*/
|
|
32
|
+
rejectRequest(requestId: string, error: Error): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Check if there's a pending request
|
|
35
|
+
*/
|
|
36
|
+
hasPendingRequest(requestId: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Clean up a pending request
|
|
39
|
+
*/
|
|
40
|
+
private cleanup;
|
|
41
|
+
/**
|
|
42
|
+
* Clean up all pending requests
|
|
43
|
+
*/
|
|
44
|
+
destroy(): void;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=popup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"popup.d.ts","sourceRoot":"","sources":["../src/popup.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAMxC,MAAM,WAAW,WAAW;IAC3B,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,WAAW,cAAc,CAAC,CAAC;IAChC,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAA;IAC3B,MAAM,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;IAC9B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,SAAS,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,CAAA;IACxC,eAAe,EAAE,UAAU,CAAC,OAAO,WAAW,CAAC,CAAA;CAC/C;AAED;;GAEG;AACH,qBAAa,YAAY;IACxB,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,eAAe,CAA6C;gBAExD,MAAM,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC;IAQzC;;OAEG;IACH,SAAS,CAAC,CAAC,EACV,MAAM,EAAE,SAAS,EACjB,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,OAAO,GACd,OAAO,CAAC,CAAC,CAAC;IA8Db;;OAEG;IACH,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO;IAS3D;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO;IASvD;;OAEG;IACH,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO;IAI7C;;OAEG;IACH,OAAO,CAAC,OAAO;IAcf;;OAEG;IACH,OAAO,IAAI,IAAI;CAKf"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type BalanceResult, type CancelListingRequest, type ConnectResult, type CreateListingRequest, type EventHandler, type InscribeRequest, type InscribeResult, type ListOptions, type ListingResult, type OneSatConfig, type OneSatEvent, type OneSatProvider, type OrdinalOutput, type PurchaseListingRequest, type SendOrdinalsRequest, type SendResult, type SignMessageResult, type SignTransactionRequest, type SignTransactionResult, type TokenOutput, type TransferTokenRequest, type Utxo } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Browser implementation of the OneSat wallet provider
|
|
4
|
+
* Uses popup-based communication with the wallet
|
|
5
|
+
*/
|
|
6
|
+
export declare class OneSatBrowserProvider implements OneSatProvider {
|
|
7
|
+
/** Identifies this as the OneSat provider (for detection like Phantom's isPhantom) */
|
|
8
|
+
readonly isOneSat: true;
|
|
9
|
+
private popupManager;
|
|
10
|
+
private connection;
|
|
11
|
+
private events;
|
|
12
|
+
private popupOrigin;
|
|
13
|
+
constructor(config?: OneSatConfig);
|
|
14
|
+
/**
|
|
15
|
+
* Handle messages from the wallet popup
|
|
16
|
+
*/
|
|
17
|
+
private handleMessage;
|
|
18
|
+
/**
|
|
19
|
+
* Generate a unique request ID
|
|
20
|
+
*/
|
|
21
|
+
private generateRequestId;
|
|
22
|
+
/**
|
|
23
|
+
* Send a request that requires popup approval
|
|
24
|
+
*/
|
|
25
|
+
private sendRequest;
|
|
26
|
+
connect(): Promise<ConnectResult>;
|
|
27
|
+
disconnect(): Promise<void>;
|
|
28
|
+
isConnected(): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Get the current connection (throws if not connected)
|
|
31
|
+
*/
|
|
32
|
+
private requireConnection;
|
|
33
|
+
signTransaction(request: SignTransactionRequest): Promise<SignTransactionResult>;
|
|
34
|
+
signMessage(message: string): Promise<SignMessageResult>;
|
|
35
|
+
inscribe(request: InscribeRequest): Promise<InscribeResult>;
|
|
36
|
+
sendOrdinals(request: SendOrdinalsRequest): Promise<SendResult>;
|
|
37
|
+
createListing(request: CreateListingRequest): Promise<ListingResult>;
|
|
38
|
+
purchaseListing(request: PurchaseListingRequest): Promise<SendResult>;
|
|
39
|
+
cancelListing(request: CancelListingRequest): Promise<SendResult>;
|
|
40
|
+
transferToken(request: TransferTokenRequest): Promise<SendResult>;
|
|
41
|
+
getBalance(): Promise<BalanceResult>;
|
|
42
|
+
getOrdinals(options?: ListOptions): Promise<OrdinalOutput[]>;
|
|
43
|
+
getTokens(options?: ListOptions): Promise<TokenOutput[]>;
|
|
44
|
+
getUtxos(): Promise<Utxo[]>;
|
|
45
|
+
on<T = unknown>(event: OneSatEvent, handler: EventHandler<T>): void;
|
|
46
|
+
off(event: OneSatEvent, handler: EventHandler): void;
|
|
47
|
+
getAddresses(): {
|
|
48
|
+
paymentAddress: string;
|
|
49
|
+
ordinalAddress: string;
|
|
50
|
+
} | null;
|
|
51
|
+
getIdentityPubKey(): string | null;
|
|
52
|
+
/**
|
|
53
|
+
* Clean up resources
|
|
54
|
+
*/
|
|
55
|
+
destroy(): void;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAUA,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,sBAAsB,EAG3B,KAAK,mBAAmB,EACxB,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,IAAI,EACT,MAAM,SAAS,CAAA;AAKhB;;;GAGG;AACH,qBAAa,qBAAsB,YAAW,cAAc;IAC3D,sFAAsF;IACtF,QAAQ,CAAC,QAAQ,EAAG,IAAI,CAAS;IAEjC,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,UAAU,CAAgC;IAClD,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,WAAW,CAAQ;gBAEf,MAAM,CAAC,EAAE,YAAY;IAqBjC;;OAEG;IACH,OAAO,CAAC,aAAa,CAoBpB;IAED;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAIzB;;OAEG;YACW,WAAW;IAUnB,OAAO,IAAI,OAAO,CAAC,aAAa,CAAC;IAgBjC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAMjC,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,OAAO,CAAC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type { ConnectResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Connection state stored in localStorage
|
|
4
|
+
*/
|
|
5
|
+
export interface StoredConnection {
|
|
6
|
+
paymentAddress: string;
|
|
7
|
+
ordinalAddress: string;
|
|
8
|
+
identityPubKey: string;
|
|
9
|
+
connectedAt: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Save connection state to localStorage
|
|
13
|
+
*/
|
|
14
|
+
export declare function saveConnection(connection: ConnectResult): void;
|
|
15
|
+
/**
|
|
16
|
+
* Load connection state from localStorage
|
|
17
|
+
*/
|
|
18
|
+
export declare function loadConnection(): StoredConnection | null;
|
|
19
|
+
/**
|
|
20
|
+
* Clear connection state from localStorage
|
|
21
|
+
*/
|
|
22
|
+
export declare function clearConnection(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Check if a valid connection exists
|
|
25
|
+
*/
|
|
26
|
+
export declare function hasStoredConnection(): boolean;
|
|
27
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAI5C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAChC,cAAc,EAAE,MAAM,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;IACtB,WAAW,EAAE,MAAM,CAAA;CACnB;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,aAAa,GAAG,IAAI,CAU9D;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,gBAAgB,GAAG,IAAI,CAQxD;AAED;;GAEG;AACH,wBAAgB,eAAe,IAAI,IAAI,CAMtC;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,OAAO,CAE7C"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection result returned after successful wallet connection
|
|
3
|
+
*/
|
|
4
|
+
export interface ConnectResult {
|
|
5
|
+
paymentAddress: string;
|
|
6
|
+
ordinalAddress: string;
|
|
7
|
+
identityPubKey: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Provider configuration options
|
|
11
|
+
*/
|
|
12
|
+
export interface OneSatConfig {
|
|
13
|
+
/** Name of the dApp (shown in approval popup) */
|
|
14
|
+
appName?: string;
|
|
15
|
+
/** Base URL for the wallet popup (default: https://1sat.market) */
|
|
16
|
+
popupUrl?: string;
|
|
17
|
+
/** Request timeout in milliseconds (default: 300000 = 5 minutes) */
|
|
18
|
+
timeout?: number;
|
|
19
|
+
/** Network to use (default: main) */
|
|
20
|
+
network?: 'main' | 'test';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* RPC method names for wallet communication
|
|
24
|
+
*/
|
|
25
|
+
export declare const RpcMethods: {
|
|
26
|
+
readonly CONNECT: "connect";
|
|
27
|
+
readonly DISCONNECT: "disconnect";
|
|
28
|
+
readonly SIGN_TRANSACTION: "signTransaction";
|
|
29
|
+
readonly SIGN_MESSAGE: "signMessage";
|
|
30
|
+
readonly INSCRIBE: "inscribe";
|
|
31
|
+
readonly SEND_ORDINALS: "sendOrdinals";
|
|
32
|
+
readonly CREATE_LISTING: "createListing";
|
|
33
|
+
readonly PURCHASE_LISTING: "purchaseListing";
|
|
34
|
+
readonly CANCEL_LISTING: "cancelListing";
|
|
35
|
+
readonly TRANSFER_TOKEN: "transferToken";
|
|
36
|
+
readonly GET_BALANCE: "getBalance";
|
|
37
|
+
readonly GET_ORDINALS: "getOrdinals";
|
|
38
|
+
readonly GET_TOKENS: "getTokens";
|
|
39
|
+
readonly GET_UTXOS: "getUtxos";
|
|
40
|
+
};
|
|
41
|
+
export type RpcMethod = (typeof RpcMethods)[keyof typeof RpcMethods];
|
|
42
|
+
/**
|
|
43
|
+
* Request to sign a transaction
|
|
44
|
+
*/
|
|
45
|
+
export interface SignTransactionRequest {
|
|
46
|
+
rawtx: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Result of transaction signing
|
|
51
|
+
*/
|
|
52
|
+
export interface SignTransactionResult {
|
|
53
|
+
rawtx: string;
|
|
54
|
+
txid: string;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Result of message signing (BSM format)
|
|
58
|
+
*/
|
|
59
|
+
export interface SignMessageResult {
|
|
60
|
+
message: string;
|
|
61
|
+
signature: string;
|
|
62
|
+
address: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Request to create an inscription
|
|
66
|
+
*/
|
|
67
|
+
export interface InscribeRequest {
|
|
68
|
+
dataB64: string;
|
|
69
|
+
contentType: string;
|
|
70
|
+
destinationAddress?: string;
|
|
71
|
+
metaData?: Record<string, string>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Result of inscription creation
|
|
75
|
+
*/
|
|
76
|
+
export interface InscribeResult {
|
|
77
|
+
txid: string;
|
|
78
|
+
origin: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Request to send ordinals
|
|
82
|
+
*/
|
|
83
|
+
export interface SendOrdinalsRequest {
|
|
84
|
+
outpoints: string[];
|
|
85
|
+
destinationAddress: string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Generic send/transfer result
|
|
89
|
+
*/
|
|
90
|
+
export interface SendResult {
|
|
91
|
+
txid: string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Request to create a listing
|
|
95
|
+
*/
|
|
96
|
+
export interface CreateListingRequest {
|
|
97
|
+
outpoints: string[];
|
|
98
|
+
priceSatoshis: number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Result of listing creation
|
|
102
|
+
*/
|
|
103
|
+
export interface ListingResult {
|
|
104
|
+
txid: string;
|
|
105
|
+
listingOutpoints: string[];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Request to purchase a listing
|
|
109
|
+
*/
|
|
110
|
+
export interface PurchaseListingRequest {
|
|
111
|
+
listingOutpoint: string;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Request to cancel listings
|
|
115
|
+
*/
|
|
116
|
+
export interface CancelListingRequest {
|
|
117
|
+
listingOutpoints: string[];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Request to transfer tokens
|
|
121
|
+
*/
|
|
122
|
+
export interface TransferTokenRequest {
|
|
123
|
+
tokenId: string;
|
|
124
|
+
amount: string;
|
|
125
|
+
destinationAddress: string;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Balance result
|
|
129
|
+
*/
|
|
130
|
+
export interface BalanceResult {
|
|
131
|
+
satoshis: number;
|
|
132
|
+
usd?: number;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Ordinal output
|
|
136
|
+
*/
|
|
137
|
+
export interface OrdinalOutput {
|
|
138
|
+
outpoint: string;
|
|
139
|
+
satoshis: number;
|
|
140
|
+
origin?: string;
|
|
141
|
+
contentType?: string;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Token output (BSV20/21)
|
|
145
|
+
*/
|
|
146
|
+
export interface TokenOutput {
|
|
147
|
+
outpoint: string;
|
|
148
|
+
satoshis: number;
|
|
149
|
+
tokenId: string;
|
|
150
|
+
amount: string;
|
|
151
|
+
symbol?: string;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* UTXO for payment
|
|
155
|
+
*/
|
|
156
|
+
export interface Utxo {
|
|
157
|
+
txid: string;
|
|
158
|
+
vout: number;
|
|
159
|
+
satoshis: number;
|
|
160
|
+
script: string;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* List options for queries
|
|
164
|
+
*/
|
|
165
|
+
export interface ListOptions {
|
|
166
|
+
limit?: number;
|
|
167
|
+
offset?: number;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Event types emitted by the provider
|
|
171
|
+
*/
|
|
172
|
+
export type OneSatEvent = 'connect' | 'disconnect' | 'accountChange';
|
|
173
|
+
/**
|
|
174
|
+
* Event handler function type
|
|
175
|
+
*/
|
|
176
|
+
export type EventHandler<T = unknown> = (data: T) => void;
|
|
177
|
+
/**
|
|
178
|
+
* Window interface extension for TypeScript
|
|
179
|
+
* Browser extensions inject window.onesat with isOneSat: true
|
|
180
|
+
*/
|
|
181
|
+
declare global {
|
|
182
|
+
interface Window {
|
|
183
|
+
onesat?: OneSatProvider;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Main provider interface
|
|
188
|
+
* Implemented by both popup provider and browser extension
|
|
189
|
+
*/
|
|
190
|
+
export interface OneSatProvider {
|
|
191
|
+
/** Identifies this as the OneSat provider (for detection) */
|
|
192
|
+
readonly isOneSat: true;
|
|
193
|
+
connect(): Promise<ConnectResult>;
|
|
194
|
+
disconnect(): Promise<void>;
|
|
195
|
+
isConnected(): boolean;
|
|
196
|
+
signTransaction(request: SignTransactionRequest): Promise<SignTransactionResult>;
|
|
197
|
+
signMessage(message: string): Promise<SignMessageResult>;
|
|
198
|
+
inscribe(request: InscribeRequest): Promise<InscribeResult>;
|
|
199
|
+
sendOrdinals(request: SendOrdinalsRequest): Promise<SendResult>;
|
|
200
|
+
createListing(request: CreateListingRequest): Promise<ListingResult>;
|
|
201
|
+
purchaseListing(request: PurchaseListingRequest): Promise<SendResult>;
|
|
202
|
+
cancelListing(request: CancelListingRequest): Promise<SendResult>;
|
|
203
|
+
transferToken(request: TransferTokenRequest): Promise<SendResult>;
|
|
204
|
+
getBalance(): Promise<BalanceResult>;
|
|
205
|
+
getOrdinals(options?: ListOptions): Promise<OrdinalOutput[]>;
|
|
206
|
+
getTokens(options?: ListOptions): Promise<TokenOutput[]>;
|
|
207
|
+
getUtxos(): Promise<Utxo[]>;
|
|
208
|
+
on<T = unknown>(event: OneSatEvent, handler: EventHandler<T>): void;
|
|
209
|
+
off(event: OneSatEvent, handler: EventHandler): void;
|
|
210
|
+
getAddresses(): {
|
|
211
|
+
paymentAddress: string;
|
|
212
|
+
ordinalAddress: string;
|
|
213
|
+
} | null;
|
|
214
|
+
getIdentityPubKey(): string | null;
|
|
215
|
+
}
|
|
216
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,aAAa;IAC7B,cAAc,EAAE,MAAM,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;IACtB,cAAc,EAAE,MAAM,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC5B,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,mEAAmE;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,qCAAqC;IACrC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;CACzB;AAED;;GAEG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;CA0Bb,CAAA;AAEV,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,OAAO,UAAU,CAAC,CAAA;AAEpE;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACtC,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;CACZ;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IACjC,OAAO,EAAE,MAAM,CAAA;IACf,SAAS,EAAE,MAAM,CAAA;IACjB,O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management",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target browser && tsc --emitDeclarationOnly",
|
|
17
|
+
"dev": "tsc --watch"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["1sat", "bsv", "ordinals", "wallet", "sdk", "connect"],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"eventemitter3": "^5.0.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"typescript": "^5.9.3"
|
|
26
|
+
}
|
|
27
|
+
}
|