@moonpay/platform-sdk-core 0.0.0 → 0.2.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.
@@ -0,0 +1,167 @@
1
+ import * as _moonpay_platform_protocol from '@moonpay/platform-protocol';
2
+ import { Result, DevPlatformApiError, GetQuoteParams, GetQuoteError, TransactionWithStages, GetTransactionsError, Transaction, PaginationInfo, ListPaymentMethodsResponse, GetPaymentMethodsError, ProtocolMessage, Quote, DecryptedCredentials } from '@moonpay/platform-protocol';
3
+
4
+ /** Internal state container for authenticated API access. */
5
+ interface ClientContext {
6
+ readonly accessToken: string;
7
+ readonly clientToken: string;
8
+ setAccessToken(token: string): void;
9
+ setClientToken(token: string): void;
10
+ fetch(url: string, options?: RequestInit): Promise<Response>;
11
+ }
12
+ interface ClientContextOptions {
13
+ /** Base URL for the MoonPay Platform API. Defaults to production. */
14
+ apiBaseUrl?: string;
15
+ }
16
+ declare function createClientContext(options?: ClientContextOptions): ClientContext;
17
+
18
+ /** Typed fetch that wraps responses in Result<T, E>. */
19
+ declare function apiFetch<T>(ctx: ClientContext, path: string, options?: RequestInit): Promise<Result<T, DevPlatformApiError>>;
20
+
21
+ /** The API wraps the quote in a `{ data: ... }` envelope. */
22
+ interface GetQuoteApiResponse {
23
+ data: _moonpay_platform_protocol.Quote;
24
+ }
25
+ declare function getQuote(ctx: ClientContext, params: GetQuoteParams): Promise<Result<GetQuoteApiResponse, GetQuoteError>>;
26
+
27
+ interface ListTransactionsParams {
28
+ startDate?: string;
29
+ endDate?: string;
30
+ cursor?: string;
31
+ limit?: number;
32
+ }
33
+ interface ListTransactionsApiResponse {
34
+ data: Transaction[];
35
+ pageInfo: PaginationInfo;
36
+ }
37
+ declare function listTransactions(ctx: ClientContext, params?: ListTransactionsParams): Promise<Result<ListTransactionsApiResponse, GetTransactionsError>>;
38
+ interface GetTransactionApiResponse {
39
+ data: TransactionWithStages;
40
+ }
41
+ declare function getTransaction(ctx: ClientContext, id: string): Promise<Result<GetTransactionApiResponse, GetTransactionsError>>;
42
+
43
+ declare function getPaymentMethods(ctx: ClientContext): Promise<Result<ListPaymentMethodsResponse, GetPaymentMethodsError>>;
44
+ declare function deletePaymentMethod(ctx: ClientContext, paymentMethodId: string): Promise<Result<void, DevPlatformApiError>>;
45
+
46
+ /** Options for creating a frame (iframe or WebView). */
47
+ interface FrameOptions {
48
+ /** Container to render the frame into. Platform-specific. */
49
+ container: unknown;
50
+ /** Whether this is a hidden utility frame (e.g., check-connection). */
51
+ hidden?: boolean;
52
+ }
53
+ /**
54
+ * Platform-agnostic transport interface for frame communication.
55
+ * Implemented by `IframeTransport` (web) and `WebViewTransport` (RN).
56
+ */
57
+ interface FrameTransport {
58
+ /** Create and mount the frame with the given URL. */
59
+ create(url: string, options: FrameOptions): void;
60
+ /** Send a protocol message to the frame. */
61
+ sendMessage(message: ProtocolMessage): void;
62
+ /**
63
+ * Register a handler for incoming protocol messages.
64
+ * Returns an unsubscribe function.
65
+ */
66
+ onMessage(handler: (message: ProtocolMessage) => void): () => void;
67
+ /** Remove the frame and clean up all listeners. */
68
+ dispose(): void;
69
+ }
70
+
71
+ declare const FRAME_PATHS: {
72
+ readonly connect: "/platform/v1/connect";
73
+ readonly checkConnection: "/platform/v1/check-connection";
74
+ readonly applePay: "/platform/v1/apple-pay";
75
+ readonly googlePay: "/platform/v1/google-pay";
76
+ readonly widget: "/platform/v1/widget";
77
+ readonly buy: "/platform/v1/buy";
78
+ readonly buyButton: "/platform/v1/buy-button";
79
+ readonly challenge: "/platform/v1/challenge";
80
+ readonly addCard: "/platform/v1/add-card";
81
+ readonly reset: "/platform/v1/reset";
82
+ };
83
+ interface UrlBuilderOptions {
84
+ /** Base URL for MoonPay frames. Defaults to production. */
85
+ frameBaseUrl?: string;
86
+ }
87
+ /** Build a frame URL with query parameters. */
88
+ declare function buildFrameUrl(path: string, query?: Record<string, unknown>, options?: UrlBuilderOptions): string;
89
+
90
+ interface CoreClientOptions extends ClientContextOptions, UrlBuilderOptions {
91
+ /**
92
+ * Factory to create a platform-specific FrameTransport.
93
+ * Provided by the web or react-native SDK.
94
+ */
95
+ createTransport: () => FrameTransport;
96
+ }
97
+ interface CoreClient {
98
+ /** Access the internal context (for platform SDKs). */
99
+ readonly context: ClientContext;
100
+ /** Frame base URL option (for platform SDKs building frame URLs). */
101
+ readonly frameBaseUrl: string | undefined;
102
+ /** Factory to create transports (for platform SDKs). */
103
+ createTransport(): FrameTransport;
104
+ getQuote(params: GetQuoteParams): Promise<Result<{
105
+ data: Quote;
106
+ }, GetQuoteError>>;
107
+ getPaymentMethods(): Promise<Result<ListPaymentMethodsResponse, GetPaymentMethodsError>>;
108
+ listTransactions(params?: ListTransactionsParams): Promise<Result<{
109
+ data: Transaction[];
110
+ pageInfo: PaginationInfo;
111
+ }, GetTransactionsError>>;
112
+ getTransaction(id: string): Promise<Result<{
113
+ data: TransactionWithStages;
114
+ }, GetTransactionsError>>;
115
+ deletePaymentMethod(id: string): Promise<Result<void, DevPlatformApiError>>;
116
+ }
117
+ /**
118
+ * Creates the platform-agnostic core client.
119
+ * Platform SDKs (web, react-native) wrap this with frame-specific logic.
120
+ */
121
+ declare function createClientCore(options: CoreClientOptions): CoreClient;
122
+
123
+ /**
124
+ * Decrypt credentials received from the connect/check-connection frame.
125
+ *
126
+ * The frame encrypts credentials using X25519 ECDH + HKDF + AES-GCM.
127
+ * The SDK generated an ephemeral key pair and sent the public key to the frame
128
+ * via query parameters. The frame encrypted using that public key; we decrypt
129
+ * using our private key.
130
+ */
131
+ declare function decryptCredentials(encryptedPayload: string, privateKeyHex: string): DecryptedCredentials;
132
+
133
+ interface KeyPair {
134
+ privateKey: string;
135
+ publicKey: string;
136
+ }
137
+ /** Generate an ephemeral X25519 key pair for the encrypted credential exchange. */
138
+ declare function generateKeyPair(): KeyPair;
139
+
140
+ interface FrameOrchestratorOptions {
141
+ transport: FrameTransport;
142
+ channelId: string;
143
+ url: string;
144
+ container: unknown;
145
+ hidden?: boolean;
146
+ /** Timeout in ms for the handshake. Defaults to 15000. */
147
+ handshakeTimeout?: number;
148
+ }
149
+ interface FrameHandle {
150
+ /** Send a typed message to the frame. */
151
+ sendMessage(message: ProtocolMessage): void;
152
+ /** Register a message handler. Returns unsubscribe function. */
153
+ onMessage(handler: (message: ProtocolMessage) => void): () => void;
154
+ /** Tear down the frame and all listeners. */
155
+ dispose(): void;
156
+ }
157
+ /**
158
+ * Orchestrates the handshake and message routing between the SDK and a frame.
159
+ *
160
+ * 1. Creates the frame via the transport
161
+ * 2. Waits for the `handshake` message from the frame
162
+ * 3. Responds with `ack`
163
+ * 4. Returns a handle for sending/receiving typed messages
164
+ */
165
+ declare function createFrameOrchestrator(options: FrameOrchestratorOptions): Promise<FrameHandle>;
166
+
167
+ export { type ClientContext, type ClientContextOptions, type CoreClient, type CoreClientOptions, FRAME_PATHS, type FrameHandle, type FrameOptions, type FrameOrchestratorOptions, type FrameTransport, type KeyPair, type ListTransactionsParams, type UrlBuilderOptions, apiFetch, buildFrameUrl, createClientContext, createClientCore, createFrameOrchestrator, decryptCredentials, deletePaymentMethod, generateKeyPair, getPaymentMethods, getQuote, getTransaction, listTransactions };
@@ -0,0 +1,167 @@
1
+ import * as _moonpay_platform_protocol from '@moonpay/platform-protocol';
2
+ import { Result, DevPlatformApiError, GetQuoteParams, GetQuoteError, TransactionWithStages, GetTransactionsError, Transaction, PaginationInfo, ListPaymentMethodsResponse, GetPaymentMethodsError, ProtocolMessage, Quote, DecryptedCredentials } from '@moonpay/platform-protocol';
3
+
4
+ /** Internal state container for authenticated API access. */
5
+ interface ClientContext {
6
+ readonly accessToken: string;
7
+ readonly clientToken: string;
8
+ setAccessToken(token: string): void;
9
+ setClientToken(token: string): void;
10
+ fetch(url: string, options?: RequestInit): Promise<Response>;
11
+ }
12
+ interface ClientContextOptions {
13
+ /** Base URL for the MoonPay Platform API. Defaults to production. */
14
+ apiBaseUrl?: string;
15
+ }
16
+ declare function createClientContext(options?: ClientContextOptions): ClientContext;
17
+
18
+ /** Typed fetch that wraps responses in Result<T, E>. */
19
+ declare function apiFetch<T>(ctx: ClientContext, path: string, options?: RequestInit): Promise<Result<T, DevPlatformApiError>>;
20
+
21
+ /** The API wraps the quote in a `{ data: ... }` envelope. */
22
+ interface GetQuoteApiResponse {
23
+ data: _moonpay_platform_protocol.Quote;
24
+ }
25
+ declare function getQuote(ctx: ClientContext, params: GetQuoteParams): Promise<Result<GetQuoteApiResponse, GetQuoteError>>;
26
+
27
+ interface ListTransactionsParams {
28
+ startDate?: string;
29
+ endDate?: string;
30
+ cursor?: string;
31
+ limit?: number;
32
+ }
33
+ interface ListTransactionsApiResponse {
34
+ data: Transaction[];
35
+ pageInfo: PaginationInfo;
36
+ }
37
+ declare function listTransactions(ctx: ClientContext, params?: ListTransactionsParams): Promise<Result<ListTransactionsApiResponse, GetTransactionsError>>;
38
+ interface GetTransactionApiResponse {
39
+ data: TransactionWithStages;
40
+ }
41
+ declare function getTransaction(ctx: ClientContext, id: string): Promise<Result<GetTransactionApiResponse, GetTransactionsError>>;
42
+
43
+ declare function getPaymentMethods(ctx: ClientContext): Promise<Result<ListPaymentMethodsResponse, GetPaymentMethodsError>>;
44
+ declare function deletePaymentMethod(ctx: ClientContext, paymentMethodId: string): Promise<Result<void, DevPlatformApiError>>;
45
+
46
+ /** Options for creating a frame (iframe or WebView). */
47
+ interface FrameOptions {
48
+ /** Container to render the frame into. Platform-specific. */
49
+ container: unknown;
50
+ /** Whether this is a hidden utility frame (e.g., check-connection). */
51
+ hidden?: boolean;
52
+ }
53
+ /**
54
+ * Platform-agnostic transport interface for frame communication.
55
+ * Implemented by `IframeTransport` (web) and `WebViewTransport` (RN).
56
+ */
57
+ interface FrameTransport {
58
+ /** Create and mount the frame with the given URL. */
59
+ create(url: string, options: FrameOptions): void;
60
+ /** Send a protocol message to the frame. */
61
+ sendMessage(message: ProtocolMessage): void;
62
+ /**
63
+ * Register a handler for incoming protocol messages.
64
+ * Returns an unsubscribe function.
65
+ */
66
+ onMessage(handler: (message: ProtocolMessage) => void): () => void;
67
+ /** Remove the frame and clean up all listeners. */
68
+ dispose(): void;
69
+ }
70
+
71
+ declare const FRAME_PATHS: {
72
+ readonly connect: "/platform/v1/connect";
73
+ readonly checkConnection: "/platform/v1/check-connection";
74
+ readonly applePay: "/platform/v1/apple-pay";
75
+ readonly googlePay: "/platform/v1/google-pay";
76
+ readonly widget: "/platform/v1/widget";
77
+ readonly buy: "/platform/v1/buy";
78
+ readonly buyButton: "/platform/v1/buy-button";
79
+ readonly challenge: "/platform/v1/challenge";
80
+ readonly addCard: "/platform/v1/add-card";
81
+ readonly reset: "/platform/v1/reset";
82
+ };
83
+ interface UrlBuilderOptions {
84
+ /** Base URL for MoonPay frames. Defaults to production. */
85
+ frameBaseUrl?: string;
86
+ }
87
+ /** Build a frame URL with query parameters. */
88
+ declare function buildFrameUrl(path: string, query?: Record<string, unknown>, options?: UrlBuilderOptions): string;
89
+
90
+ interface CoreClientOptions extends ClientContextOptions, UrlBuilderOptions {
91
+ /**
92
+ * Factory to create a platform-specific FrameTransport.
93
+ * Provided by the web or react-native SDK.
94
+ */
95
+ createTransport: () => FrameTransport;
96
+ }
97
+ interface CoreClient {
98
+ /** Access the internal context (for platform SDKs). */
99
+ readonly context: ClientContext;
100
+ /** Frame base URL option (for platform SDKs building frame URLs). */
101
+ readonly frameBaseUrl: string | undefined;
102
+ /** Factory to create transports (for platform SDKs). */
103
+ createTransport(): FrameTransport;
104
+ getQuote(params: GetQuoteParams): Promise<Result<{
105
+ data: Quote;
106
+ }, GetQuoteError>>;
107
+ getPaymentMethods(): Promise<Result<ListPaymentMethodsResponse, GetPaymentMethodsError>>;
108
+ listTransactions(params?: ListTransactionsParams): Promise<Result<{
109
+ data: Transaction[];
110
+ pageInfo: PaginationInfo;
111
+ }, GetTransactionsError>>;
112
+ getTransaction(id: string): Promise<Result<{
113
+ data: TransactionWithStages;
114
+ }, GetTransactionsError>>;
115
+ deletePaymentMethod(id: string): Promise<Result<void, DevPlatformApiError>>;
116
+ }
117
+ /**
118
+ * Creates the platform-agnostic core client.
119
+ * Platform SDKs (web, react-native) wrap this with frame-specific logic.
120
+ */
121
+ declare function createClientCore(options: CoreClientOptions): CoreClient;
122
+
123
+ /**
124
+ * Decrypt credentials received from the connect/check-connection frame.
125
+ *
126
+ * The frame encrypts credentials using X25519 ECDH + HKDF + AES-GCM.
127
+ * The SDK generated an ephemeral key pair and sent the public key to the frame
128
+ * via query parameters. The frame encrypted using that public key; we decrypt
129
+ * using our private key.
130
+ */
131
+ declare function decryptCredentials(encryptedPayload: string, privateKeyHex: string): DecryptedCredentials;
132
+
133
+ interface KeyPair {
134
+ privateKey: string;
135
+ publicKey: string;
136
+ }
137
+ /** Generate an ephemeral X25519 key pair for the encrypted credential exchange. */
138
+ declare function generateKeyPair(): KeyPair;
139
+
140
+ interface FrameOrchestratorOptions {
141
+ transport: FrameTransport;
142
+ channelId: string;
143
+ url: string;
144
+ container: unknown;
145
+ hidden?: boolean;
146
+ /** Timeout in ms for the handshake. Defaults to 15000. */
147
+ handshakeTimeout?: number;
148
+ }
149
+ interface FrameHandle {
150
+ /** Send a typed message to the frame. */
151
+ sendMessage(message: ProtocolMessage): void;
152
+ /** Register a message handler. Returns unsubscribe function. */
153
+ onMessage(handler: (message: ProtocolMessage) => void): () => void;
154
+ /** Tear down the frame and all listeners. */
155
+ dispose(): void;
156
+ }
157
+ /**
158
+ * Orchestrates the handshake and message routing between the SDK and a frame.
159
+ *
160
+ * 1. Creates the frame via the transport
161
+ * 2. Waits for the `handshake` message from the frame
162
+ * 3. Responds with `ack`
163
+ * 4. Returns a handle for sending/receiving typed messages
164
+ */
165
+ declare function createFrameOrchestrator(options: FrameOrchestratorOptions): Promise<FrameHandle>;
166
+
167
+ export { type ClientContext, type ClientContextOptions, type CoreClient, type CoreClientOptions, FRAME_PATHS, type FrameHandle, type FrameOptions, type FrameOrchestratorOptions, type FrameTransport, type KeyPair, type ListTransactionsParams, type UrlBuilderOptions, apiFetch, buildFrameUrl, createClientContext, createClientCore, createFrameOrchestrator, decryptCredentials, deletePaymentMethod, generateKeyPair, getPaymentMethods, getQuote, getTransaction, listTransactions };