@settlemint/sdk-portal 2.3.2 → 2.3.3

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/portal.js ADDED
@@ -0,0 +1,294 @@
1
+ import { appendHeaders } from "@settlemint/sdk-utils/http";
2
+ import { ensureServer } from "@settlemint/sdk-utils/runtime";
3
+ import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation";
4
+ import { initGraphQLTada, readFragment } from "gql.tada";
5
+ import { GraphQLClient } from "graphql-request";
6
+ import { z } from "zod/v4";
7
+ import { createClient } from "graphql-ws";
8
+ import { createHash } from "node:crypto";
9
+
10
+ //#region src/utils/websocket-client.ts
11
+ /**
12
+ * Creates a GraphQL WebSocket client for the Portal API
13
+ *
14
+ * @param {WebsocketClientOptions} options - The options for the client
15
+ * @returns {Client} The GraphQL WebSocket client
16
+ * @example
17
+ * import { getWebsocketClient } from "@settlemint/sdk-portal";
18
+ *
19
+ * const client = getWebsocketClient({
20
+ * portalGraphqlEndpoint: "https://portal.settlemint.com/graphql",
21
+ * accessToken: "your-access-token",
22
+ * });
23
+ */
24
+ function getWebsocketClient({ portalGraphqlEndpoint, accessToken }) {
25
+ if (!portalGraphqlEndpoint) {
26
+ throw new Error("portalGraphqlEndpoint is required");
27
+ }
28
+ const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));
29
+ return createClient({ url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}` });
30
+ }
31
+ function setWsProtocol(url) {
32
+ if (url.protocol === "ws:" || url.protocol === "wss:") {
33
+ return url;
34
+ }
35
+ if (url.protocol === "http:") {
36
+ url.protocol = "ws:";
37
+ } else {
38
+ url.protocol = "wss:";
39
+ }
40
+ return url;
41
+ }
42
+
43
+ //#endregion
44
+ //#region src/utils/wait-for-transaction-receipt.ts
45
+ /**
46
+ * Waits for a blockchain transaction receipt by subscribing to transaction updates via GraphQL.
47
+ * This function polls until the transaction is confirmed or the timeout is reached.
48
+ *
49
+ * @param transactionHash - The hash of the transaction to wait for
50
+ * @param options - Configuration options for the waiting process
51
+ * @returns The transaction details including receipt information when the transaction is confirmed
52
+ * @throws Error if the transaction receipt cannot be retrieved within the specified timeout
53
+ *
54
+ * @example
55
+ * import { waitForTransactionReceipt } from "@settlemint/sdk-portal";
56
+ *
57
+ * const transaction = await waitForTransactionReceipt("0x123...", {
58
+ * portalGraphqlEndpoint: "https://example.settlemint.com/graphql",
59
+ * accessToken: "your-access-token",
60
+ * timeout: 30000 // 30 seconds timeout
61
+ * });
62
+ */
63
+ async function waitForTransactionReceipt(transactionHash, options) {
64
+ const wsClient = getWebsocketClient(options);
65
+ const subscription = wsClient.iterate({
66
+ query: `subscription getTransaction($transactionHash: String!) {
67
+ getTransaction(transactionHash: $transactionHash) {
68
+ receipt {
69
+ transactionHash
70
+ to
71
+ status
72
+ from
73
+ type
74
+ revertReason
75
+ revertReasonDecoded
76
+ logs
77
+ events
78
+ contractAddress
79
+ }
80
+ transactionHash
81
+ from
82
+ createdAt
83
+ address
84
+ functionName
85
+ isContract
86
+ }
87
+ }`,
88
+ variables: { transactionHash }
89
+ });
90
+ const promises = [getTransactionFromSubscription(subscription)];
91
+ if (options.timeout) {
92
+ promises.push(createTimeoutPromise(options.timeout));
93
+ }
94
+ return Promise.race(promises);
95
+ }
96
+ function createTimeoutPromise(timeout) {
97
+ return new Promise((_, reject) => {
98
+ setTimeout(() => reject(new Error("Transaction receipt not found")), timeout);
99
+ });
100
+ }
101
+ async function getTransactionFromSubscription(subscription) {
102
+ for await (const result of subscription) {
103
+ if (result?.data?.getTransaction?.receipt) {
104
+ return result.data.getTransaction;
105
+ }
106
+ }
107
+ throw new Error("No transaction found");
108
+ }
109
+
110
+ //#endregion
111
+ //#region src/utils/wallet-verification-challenge.ts
112
+ /**
113
+ * Custom error class for challenge-related errors
114
+ */
115
+ var ChallengeError = class extends Error {
116
+ code;
117
+ constructor(message, code) {
118
+ super(message);
119
+ this.name = "ChallengeError";
120
+ this.code = code;
121
+ }
122
+ };
123
+ /**
124
+ * Hashes a pincode with a salt using SHA-256
125
+ * @param pincode - The pincode to hash
126
+ * @param salt - The salt to use in hashing
127
+ * @returns The hashed pincode as a hex string
128
+ */
129
+ function hashPincode(pincode, salt) {
130
+ return createHash("sha256").update(`${salt}${pincode}`).digest("hex");
131
+ }
132
+ /**
133
+ * Generates a challenge response by combining a hashed pincode with a challenge
134
+ * @param pincode - The user's pincode
135
+ * @param salt - The salt provided in the challenge
136
+ * @param challenge - The challenge secret
137
+ * @returns The challenge response as a hex string
138
+ */
139
+ function generateResponse(pincode, salt, challenge) {
140
+ const hashedPincode = hashPincode(pincode, salt);
141
+ return createHash("sha256").update(`${hashedPincode}_${challenge}`).digest("hex");
142
+ }
143
+ /**
144
+ * Handles a wallet verification challenge by generating an appropriate response
145
+ *
146
+ * @param options - The options for handling the wallet verification challenge
147
+ * @returns Promise resolving to an object containing the challenge response and optionally the verification ID
148
+ * @throws {ChallengeError} If the challenge cannot be created or is invalid
149
+ * @example
150
+ * import { createPortalClient } from "@settlemint/sdk-portal";
151
+ * import { handleWalletVerificationChallenge } from "@settlemint/sdk-portal";
152
+ *
153
+ * const { client, graphql } = createPortalClient({
154
+ * instance: "https://portal.example.com/graphql",
155
+ * accessToken: "your-access-token"
156
+ * });
157
+ *
158
+ * const result = await handleWalletVerificationChallenge({
159
+ * portalClient: client,
160
+ * portalGraphql: graphql,
161
+ * verificationId: "verification-123",
162
+ * userWalletAddress: "0x123...",
163
+ * code: "123456",
164
+ * verificationType: "otp"
165
+ * });
166
+ */
167
+ async function handleWalletVerificationChallenge({ portalClient, portalGraphql, verificationId, userWalletAddress, code, verificationType }) {
168
+ try {
169
+ if (verificationType === "otp") {
170
+ return {
171
+ challengeResponse: code.toString(),
172
+ verificationId
173
+ };
174
+ }
175
+ if (verificationType === "secret-code") {
176
+ const formattedCode = code.toString().replace(/(.{5})(?=.)/, "$1-");
177
+ return {
178
+ challengeResponse: formattedCode,
179
+ verificationId
180
+ };
181
+ }
182
+ const verificationChallenges = await portalClient.request(portalGraphql(`
183
+ mutation CreateWalletVerificationChallenges($userWalletAddress: String!, $verificationId: String!) {
184
+ createWalletVerificationChallenges(userWalletAddress: $userWalletAddress, verificationId: $verificationId) {
185
+ challenge
186
+ id
187
+ name
188
+ verificationType
189
+ }
190
+ }
191
+ `), {
192
+ userWalletAddress,
193
+ verificationId
194
+ });
195
+ if (!verificationChallenges.createWalletVerificationChallenges?.length) {
196
+ throw new ChallengeError("No verification challenges received", "NO_CHALLENGES");
197
+ }
198
+ const walletVerificationChallenge = verificationChallenges.createWalletVerificationChallenges.find((challenge) => challenge.id === verificationId);
199
+ if (!walletVerificationChallenge?.challenge?.secret || !walletVerificationChallenge?.challenge?.salt) {
200
+ throw new ChallengeError("Invalid challenge format", "INVALID_CHALLENGE");
201
+ }
202
+ const { secret, salt } = walletVerificationChallenge.challenge;
203
+ const challengeResponse = generateResponse(code.toString(), salt, secret);
204
+ return {
205
+ challengeResponse,
206
+ verificationId
207
+ };
208
+ } catch (error) {
209
+ if (error instanceof ChallengeError) {
210
+ throw error;
211
+ }
212
+ throw new ChallengeError("Failed to process wallet verification challenge", "CHALLENGE_PROCESSING_ERROR");
213
+ }
214
+ }
215
+
216
+ //#endregion
217
+ //#region src/portal.ts
218
+ /**
219
+ * Schema for validating Portal client configuration options.
220
+ */
221
+ const ClientOptionsSchema = z.object({
222
+ instance: UrlOrPathSchema,
223
+ accessToken: ApplicationAccessTokenSchema.optional(),
224
+ cache: z.enum([
225
+ "default",
226
+ "force-cache",
227
+ "no-cache",
228
+ "no-store",
229
+ "only-if-cached",
230
+ "reload"
231
+ ]).optional()
232
+ });
233
+ /**
234
+ * Creates a Portal GraphQL client with the provided configuration.
235
+ *
236
+ * @param options - Configuration options for the Portal client
237
+ * @param clientOptions - Additional GraphQL client configuration options
238
+ * @returns An object containing the configured GraphQL client and graphql helper function
239
+ * @throws If the provided options fail validation
240
+ *
241
+ * @example
242
+ * import { createPortalClient } from "@settlemint/sdk-portal";
243
+ * import { loadEnv } from "@settlemint/sdk-utils/environment";
244
+ * import { createLogger, requestLogger } from "@settlemint/sdk-utils/logging";
245
+ * import type { introspection } from "@schemas/portal-env";
246
+ *
247
+ * const env = await loadEnv(false, false);
248
+ * const logger = createLogger();
249
+ *
250
+ * const { client: portalClient, graphql: portalGraphql } = createPortalClient<{
251
+ * introspection: introspection;
252
+ * disableMasking: true;
253
+ * scalars: {
254
+ * // Change unknown to the type you are using to store metadata
255
+ * JSON: unknown;
256
+ * };
257
+ * }>(
258
+ * {
259
+ * instance: env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT!,
260
+ * accessToken: env.SETTLEMINT_ACCESS_TOKEN!,
261
+ * },
262
+ * {
263
+ * fetch: requestLogger(logger, "portal", fetch) as typeof fetch,
264
+ * },
265
+ * );
266
+ *
267
+ * // Making GraphQL queries
268
+ * const query = portalGraphql(`
269
+ * query GetPendingTransactions {
270
+ * getPendingTransactions {
271
+ * count
272
+ * }
273
+ * }
274
+ * `);
275
+ *
276
+ * const result = await portalClient.request(query);
277
+ */
278
+ function createPortalClient(options, clientOptions) {
279
+ ensureServer();
280
+ const validatedOptions = validate(ClientOptionsSchema, options);
281
+ const graphql = initGraphQLTada();
282
+ const fullUrl = new URL(validatedOptions.instance).toString();
283
+ return {
284
+ client: new GraphQLClient(fullUrl, {
285
+ ...clientOptions,
286
+ headers: appendHeaders(clientOptions?.headers, { "x-auth-token": validatedOptions.accessToken })
287
+ }),
288
+ graphql
289
+ };
290
+ }
291
+
292
+ //#endregion
293
+ export { ClientOptionsSchema, createPortalClient, getWebsocketClient, handleWalletVerificationChallenge, readFragment, waitForTransactionReceipt };
294
+ //# sourceMappingURL=portal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"portal.js","names":["url: URL","transactionHash: string","options: WaitForTransactionReceiptOptions","timeout: number","subscription: AsyncIterableIterator<FormattedExecutionResult<GetTransactionResponse, unknown>>","message: string","code: string","pincode: string","salt: string","challenge: string","options: ClientOptions","clientOptions?: RequestConfig"],"sources":["../src/utils/websocket-client.ts","../src/utils/wait-for-transaction-receipt.ts","../src/utils/wallet-verification-challenge.ts","../src/portal.ts"],"sourcesContent":["import { createClient } from \"graphql-ws\";\n\n/**\n * Options for the GraphQL WebSocket client\n */\nexport interface WebsocketClientOptions {\n /**\n * The GraphQL endpoint URL for the Portal API\n */\n portalGraphqlEndpoint: string;\n /**\n * The access token for authentication with the Portal API\n */\n accessToken?: string;\n}\n\n/**\n * Creates a GraphQL WebSocket client for the Portal API\n *\n * @param {WebsocketClientOptions} options - The options for the client\n * @returns {Client} The GraphQL WebSocket client\n * @example\n * import { getWebsocketClient } from \"@settlemint/sdk-portal\";\n *\n * const client = getWebsocketClient({\n * portalGraphqlEndpoint: \"https://portal.settlemint.com/graphql\",\n * accessToken: \"your-access-token\",\n * });\n */\nexport function getWebsocketClient({ portalGraphqlEndpoint, accessToken }: WebsocketClientOptions) {\n if (!portalGraphqlEndpoint) {\n throw new Error(\"portalGraphqlEndpoint is required\");\n }\n const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));\n return createClient({\n url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}`,\n });\n}\n\nfunction setWsProtocol(url: URL) {\n if (url.protocol === \"ws:\" || url.protocol === \"wss:\") {\n return url;\n }\n if (url.protocol === \"http:\") {\n url.protocol = \"ws:\";\n } else {\n url.protocol = \"wss:\";\n }\n return url;\n}\n","import type { FormattedExecutionResult } from \"graphql-ws\";\nimport type { Address, Hex, TransactionReceipt as TransactionReceiptViem } from \"viem\";\nimport { type WebsocketClientOptions, getWebsocketClient } from \"./websocket-client.js\";\n\n/**\n * Represents an event emitted during a transaction execution\n */\nexport interface TransactionEvent {\n /** The name of the event that was emitted */\n eventName: string;\n /** The arguments emitted by the event */\n args: Record<string, unknown>;\n /** Indexed event parameters used for filtering and searching */\n topics: Hex[];\n}\n\n/**\n * Represents the structure of a blockchain transaction receipt\n */\nexport interface TransactionReceipt extends TransactionReceiptViem<string, number, \"Success\" | \"Reverted\"> {\n /** The raw reason for transaction reversion, if applicable */\n revertReason: string;\n /** Human-readable version of the revert reason */\n revertReasonDecoded: string;\n /** Array of events emitted during the transaction */\n events: TransactionEvent[];\n /** The address of the contract deployed in the transaction */\n contractAddress: Address;\n}\n\n/**\n * Represents the structure of a blockchain transaction with its receipt\n */\nexport interface Transaction {\n receipt: TransactionReceipt;\n /** The hash of the transaction (duplicate of receipt.transactionHash) */\n transactionHash: string;\n /** The sender address (duplicate of receipt.from) */\n from: string;\n /** Timestamp when the transaction was created */\n createdAt: string;\n /** The contract address involved in the transaction */\n address: string;\n /** The name of the function called in the transaction */\n functionName: string;\n /** Whether the transaction is a contract deployment */\n isContract: boolean;\n}\n\ninterface GetTransactionResponse {\n getTransaction: Transaction;\n}\n\n/**\n * Options for waiting for a transaction receipt\n */\nexport interface WaitForTransactionReceiptOptions extends WebsocketClientOptions {\n /** Optional timeout in milliseconds before the operation fails */\n timeout?: number;\n}\n\n/**\n * Waits for a blockchain transaction receipt by subscribing to transaction updates via GraphQL.\n * This function polls until the transaction is confirmed or the timeout is reached.\n *\n * @param transactionHash - The hash of the transaction to wait for\n * @param options - Configuration options for the waiting process\n * @returns The transaction details including receipt information when the transaction is confirmed\n * @throws Error if the transaction receipt cannot be retrieved within the specified timeout\n *\n * @example\n * import { waitForTransactionReceipt } from \"@settlemint/sdk-portal\";\n *\n * const transaction = await waitForTransactionReceipt(\"0x123...\", {\n * portalGraphqlEndpoint: \"https://example.settlemint.com/graphql\",\n * accessToken: \"your-access-token\",\n * timeout: 30000 // 30 seconds timeout\n * });\n */\nexport async function waitForTransactionReceipt(transactionHash: string, options: WaitForTransactionReceiptOptions) {\n const wsClient = getWebsocketClient(options);\n const subscription = wsClient.iterate<GetTransactionResponse>({\n query: `subscription getTransaction($transactionHash: String!) {\n getTransaction(transactionHash: $transactionHash) {\n receipt {\n transactionHash\n to\n status\n from\n type\n revertReason\n revertReasonDecoded\n logs\n events\n contractAddress\n }\n transactionHash\n from\n createdAt\n address\n functionName\n isContract\n }\n }`,\n variables: { transactionHash },\n });\n const promises = [getTransactionFromSubscription(subscription)];\n if (options.timeout) {\n promises.push(createTimeoutPromise(options.timeout));\n }\n\n return Promise.race(promises);\n}\n\nfunction createTimeoutPromise(timeout: number): Promise<never> {\n return new Promise((_, reject) => {\n setTimeout(() => reject(new Error(\"Transaction receipt not found\")), timeout);\n });\n}\n\nasync function getTransactionFromSubscription(\n subscription: AsyncIterableIterator<FormattedExecutionResult<GetTransactionResponse, unknown>>,\n): Promise<Transaction> {\n for await (const result of subscription) {\n if (result?.data?.getTransaction?.receipt) {\n return result.data.getTransaction;\n }\n }\n throw new Error(\"No transaction found\");\n}\n","import { createHash } from \"node:crypto\";\nimport type { AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport type { GraphQLClient } from \"graphql-request\";\nimport type { Address } from \"viem\";\n\n/**\n * Custom error class for challenge-related errors\n */\nexport class ChallengeError extends Error {\n readonly code: string;\n\n constructor(message: string, code: string) {\n super(message);\n this.name = \"ChallengeError\";\n this.code = code;\n }\n}\n\n/**\n * Represents the structure of a wallet verification challenge\n */\ninterface WalletVerificationChallenge {\n challenge: {\n secret: string;\n salt: string;\n };\n id: string;\n name: string;\n verificationType: string;\n}\n\n/**\n * Response type for the CreateWalletVerificationChallenges mutation\n */\ninterface CreateWalletVerificationChallengesResponse {\n createWalletVerificationChallenges: WalletVerificationChallenge[];\n}\n\n/**\n * Hashes a pincode with a salt using SHA-256\n * @param pincode - The pincode to hash\n * @param salt - The salt to use in hashing\n * @returns The hashed pincode as a hex string\n */\nfunction hashPincode(pincode: string, salt: string): string {\n return createHash(\"sha256\").update(`${salt}${pincode}`).digest(\"hex\");\n}\n\n/**\n * Generates a challenge response by combining a hashed pincode with a challenge\n * @param pincode - The user's pincode\n * @param salt - The salt provided in the challenge\n * @param challenge - The challenge secret\n * @returns The challenge response as a hex string\n */\nfunction generateResponse(pincode: string, salt: string, challenge: string): string {\n const hashedPincode = hashPincode(pincode, salt);\n return createHash(\"sha256\").update(`${hashedPincode}_${challenge}`).digest(\"hex\");\n}\n\n/**\n * Options for handling a wallet verification challenge\n */\nexport interface HandleWalletVerificationChallengeOptions<Setup extends AbstractSetupSchema> {\n /** The portal client instance */\n portalClient: GraphQLClient;\n /** The GraphQL query builder */\n portalGraphql: initGraphQLTada<Setup>;\n /** The ID of the verification challenge */\n verificationId: string;\n /** The wallet address to verify */\n userWalletAddress: Address;\n /** The verification code provided by the user */\n code: string | number;\n /** The type of verification being performed */\n verificationType: \"otp\" | \"secret-code\" | \"pincode\";\n}\n\n/**\n * Handles a wallet verification challenge by generating an appropriate response\n *\n * @param options - The options for handling the wallet verification challenge\n * @returns Promise resolving to an object containing the challenge response and optionally the verification ID\n * @throws {ChallengeError} If the challenge cannot be created or is invalid\n * @example\n * import { createPortalClient } from \"@settlemint/sdk-portal\";\n * import { handleWalletVerificationChallenge } from \"@settlemint/sdk-portal\";\n *\n * const { client, graphql } = createPortalClient({\n * instance: \"https://portal.example.com/graphql\",\n * accessToken: \"your-access-token\"\n * });\n *\n * const result = await handleWalletVerificationChallenge({\n * portalClient: client,\n * portalGraphql: graphql,\n * verificationId: \"verification-123\",\n * userWalletAddress: \"0x123...\",\n * code: \"123456\",\n * verificationType: \"otp\"\n * });\n */\nexport async function handleWalletVerificationChallenge<const Setup extends AbstractSetupSchema>({\n portalClient,\n portalGraphql,\n verificationId,\n userWalletAddress,\n code,\n verificationType,\n}: HandleWalletVerificationChallengeOptions<Setup>): Promise<{\n challengeResponse: string;\n verificationId?: string;\n}> {\n try {\n if (verificationType === \"otp\") {\n return {\n challengeResponse: code.toString(),\n verificationId,\n };\n }\n\n if (verificationType === \"secret-code\") {\n // Add - separator to the code\n const formattedCode = code.toString().replace(/(.{5})(?=.)/, \"$1-\");\n return {\n challengeResponse: formattedCode,\n verificationId,\n };\n }\n\n const verificationChallenges = await portalClient.request<CreateWalletVerificationChallengesResponse>(\n portalGraphql(`\n mutation CreateWalletVerificationChallenges($userWalletAddress: String!, $verificationId: String!) {\n createWalletVerificationChallenges(userWalletAddress: $userWalletAddress, verificationId: $verificationId) {\n challenge\n id\n name\n verificationType\n }\n }\n `),\n {\n userWalletAddress,\n verificationId,\n },\n );\n\n if (!verificationChallenges.createWalletVerificationChallenges?.length) {\n throw new ChallengeError(\"No verification challenges received\", \"NO_CHALLENGES\");\n }\n\n const walletVerificationChallenge = verificationChallenges.createWalletVerificationChallenges.find(\n (challenge) => challenge.id === verificationId,\n );\n\n if (!walletVerificationChallenge?.challenge?.secret || !walletVerificationChallenge?.challenge?.salt) {\n throw new ChallengeError(\"Invalid challenge format\", \"INVALID_CHALLENGE\");\n }\n\n const { secret, salt } = walletVerificationChallenge.challenge;\n const challengeResponse = generateResponse(code.toString(), salt, secret);\n return {\n challengeResponse,\n verificationId,\n };\n } catch (error) {\n if (error instanceof ChallengeError) {\n throw error;\n }\n throw new ChallengeError(\"Failed to process wallet verification challenge\", \"CHALLENGE_PROCESSING_ERROR\");\n }\n}\n","import { appendHeaders } from \"@settlemint/sdk-utils/http\";\nimport { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod/v4\";\n\n/**\n * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n */\nexport const ClientOptionsSchema = z.object({\n instance: UrlOrPathSchema,\n accessToken: ApplicationAccessTokenSchema.optional(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from \"@settlemint/sdk-portal\";\n * import { loadEnv } from \"@settlemint/sdk-utils/environment\";\n * import { createLogger, requestLogger } from \"@settlemint/sdk-utils/logging\";\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * const env = await loadEnv(false, false);\n * const logger = createLogger();\n *\n * const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>(\n * {\n * instance: env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT!,\n * accessToken: env.SETTLEMINT_ACCESS_TOKEN!,\n * },\n * {\n * fetch: requestLogger(logger, \"portal\", fetch) as typeof fetch,\n * },\n * );\n *\n * // Making GraphQL queries\n * const query = portalGraphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await portalClient.request(query);\n */\nexport function createPortalClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = new URL(validatedOptions.instance).toString();\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: appendHeaders(clientOptions?.headers, { \"x-auth-token\": validatedOptions.accessToken }),\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\nexport {\n waitForTransactionReceipt,\n type Transaction,\n type TransactionEvent,\n type TransactionReceipt,\n type WaitForTransactionReceiptOptions,\n} from \"./utils/wait-for-transaction-receipt.js\";\nexport {\n handleWalletVerificationChallenge,\n type HandleWalletVerificationChallengeOptions,\n} from \"./utils/wallet-verification-challenge.js\";\nexport { getWebsocketClient, type WebsocketClientOptions } from \"./utils/websocket-client.js\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,mBAAmB,EAAE,uBAAuB,aAAqC,EAAE;AACjG,MAAK,uBAAuB;AAC1B,QAAM,IAAI,MAAM;CACjB;CACD,MAAM,kBAAkB,cAAc,IAAI,IAAI,uBAAuB;AACrE,QAAO,aAAa,EAClB,MAAM,EAAE,gBAAgB,SAAS,IAAI,gBAAgB,KAAK,GAAG,YAAY,EAAE,gBAAgB,SAAS,EAAE,gBAAgB,OAAO,EAC9H,EAAC;AACH;AAED,SAAS,cAAcA,KAAU;AAC/B,KAAI,IAAI,aAAa,SAAS,IAAI,aAAa,QAAQ;AACrD,SAAO;CACR;AACD,KAAI,IAAI,aAAa,SAAS;AAC5B,MAAI,WAAW;CAChB,OAAM;AACL,MAAI,WAAW;CAChB;AACD,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;AC8BD,eAAsB,0BAA0BC,iBAAyBC,SAA2C;CAClH,MAAM,WAAW,mBAAmB,QAAQ;CAC5C,MAAM,eAAe,SAAS,QAAgC;EAC5D,QAAQ;;;;;;;;;;;;;;;;;;;;;;EAsBR,WAAW,EAAE,gBAAiB;CAC/B,EAAC;CACF,MAAM,WAAW,CAAC,+BAA+B,aAAa,AAAC;AAC/D,KAAI,QAAQ,SAAS;AACnB,WAAS,KAAK,qBAAqB,QAAQ,QAAQ,CAAC;CACrD;AAED,QAAO,QAAQ,KAAK,SAAS;AAC9B;AAED,SAAS,qBAAqBC,SAAiC;AAC7D,QAAO,IAAI,QAAQ,CAAC,GAAG,WAAW;AAChC,aAAW,MAAM,OAAO,IAAI,M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,6BAA6B,WAAW,MAAM;AACpG,SAAM,IAAI,eAAe,4BAA4B;EACtD;EAED,MAAM,EAAE,QAAQ,MAAM,GAAG,4BAA4B;EACrD,MAAM,oBAAoB,iBAAiB,KAAK,UAAU,EAAE,MAAM,OAAO;AACzE,SAAO;GACL;GACA;EACD;CACF,SAAQ,OAAO;AACd,MAAI,iBAAiB,gBAAgB;AACnC,SAAM;EACP;AACD,QAAM,IAAI,eAAe,mDAAmD;CAC7E;AACF;;;;;;;AC5JD,MAAa,sBAAsB,EAAE,OAAO;CAC1C,UAAU;CACV,aAAa,6BAA6B,UAAU;CACpD,OAAO,EAAE,KAAK;EAAC;EAAW;EAAe;EAAY;EAAY;EAAkB;CAAS,EAAC,CAAC,UAAU;AACzG,EAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDF,SAAgB,mBACdC,SACAC,eAIA;AACA,eAAc;CACd,MAAM,mBAAmB,SAAS,qBAAqB,QAAQ;CAC/D,MAAM,UAAU,iBAAwB;CACxC,MAAM,UAAU,IAAI,IAAI,iBAAiB,UAAU,UAAU;AAE7D,QAAO;EACL,QAAQ,IAAI,cAAc,SAAS;GACjC,GAAG;GACH,SAAS,cAAc,eAAe,SAAS,EAAE,gBAAgB,iBAAiB,YAAa,EAAC;EACjG;EACD;CACD;AACF"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@settlemint/sdk-portal",
3
3
  "description": "Portal API client module for SettleMint SDK, providing access to smart contract portal services and APIs",
4
- "version": "2.3.2",
4
+ "version": "2.3.3",
5
5
  "type": "module",
6
6
  "private": false,
7
7
  "license": "FSL-1.1-MIT",
@@ -22,14 +22,14 @@
22
22
  },
23
23
  "files": ["dist"],
24
24
  "main": "./dist/portal.cjs",
25
- "module": "./dist/portal.mjs",
25
+ "module": "./dist/portal.js",
26
26
  "types": "./dist/portal.d.ts",
27
27
  "exports": {
28
28
  "./package.json": "./package.json",
29
29
  ".": {
30
30
  "import": {
31
31
  "types": "./dist/portal.d.ts",
32
- "default": "./dist/portal.mjs"
32
+ "default": "./dist/portal.js"
33
33
  },
34
34
  "require": {
35
35
  "types": "./dist/portal.d.cts",
@@ -38,8 +38,8 @@
38
38
  }
39
39
  },
40
40
  "scripts": {
41
- "build": "tsup-node",
42
- "dev": "tsup-node --watch",
41
+ "build": "tsdown",
42
+ "dev": "tsdown --watch",
43
43
  "publint": "publint run --strict",
44
44
  "attw": "attw --pack .",
45
45
  "test": "bun test",
@@ -56,7 +56,7 @@
56
56
  "dependencies": {
57
57
  "gql.tada": "^1",
58
58
  "graphql-ws": "^6",
59
- "@settlemint/sdk-utils": "2.3.2",
59
+ "@settlemint/sdk-utils": "2.3.3",
60
60
  "graphql-request": "^7",
61
61
  "zod": "^3.25.0"
62
62
  },
package/dist/portal.mjs DELETED
@@ -1,188 +0,0 @@
1
- // src/portal.ts
2
- import { ensureServer } from "@settlemint/sdk-utils/runtime";
3
- import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation";
4
- import { initGraphQLTada } from "gql.tada";
5
- import { GraphQLClient } from "graphql-request";
6
- import { z } from "zod/v4";
7
-
8
- // src/utils/wallet-verification-challenge.ts
9
- import { createHash } from "crypto";
10
- var ChallengeError = class extends Error {
11
- code;
12
- constructor(message, code) {
13
- super(message);
14
- this.name = "ChallengeError";
15
- this.code = code;
16
- }
17
- };
18
- function hashPincode(pincode, salt) {
19
- return createHash("sha256").update(`${salt}${pincode}`).digest("hex");
20
- }
21
- function generateResponse(pincode, salt, challenge) {
22
- const hashedPincode = hashPincode(pincode, salt);
23
- return createHash("sha256").update(`${hashedPincode}_${challenge}`).digest("hex");
24
- }
25
- async function handleWalletVerificationChallenge({
26
- portalClient,
27
- portalGraphql,
28
- verificationId,
29
- userWalletAddress,
30
- code,
31
- verificationType
32
- }) {
33
- try {
34
- if (verificationType === "otp") {
35
- return {
36
- challengeResponse: code.toString(),
37
- verificationId
38
- };
39
- }
40
- if (verificationType === "secret-code") {
41
- const formattedCode = code.toString().replace(/(.{5})(?=.)/, "$1-");
42
- return {
43
- challengeResponse: formattedCode,
44
- verificationId
45
- };
46
- }
47
- const verificationChallenges = await portalClient.request(
48
- portalGraphql(`
49
- mutation CreateWalletVerificationChallenges($userWalletAddress: String!, $verificationId: String!) {
50
- createWalletVerificationChallenges(userWalletAddress: $userWalletAddress, verificationId: $verificationId) {
51
- challenge
52
- id
53
- name
54
- verificationType
55
- }
56
- }
57
- `),
58
- {
59
- userWalletAddress,
60
- verificationId
61
- }
62
- );
63
- if (!verificationChallenges.createWalletVerificationChallenges?.length) {
64
- throw new ChallengeError("No verification challenges received", "NO_CHALLENGES");
65
- }
66
- const walletVerificationChallenge = verificationChallenges.createWalletVerificationChallenges.find(
67
- (challenge) => challenge.id === verificationId
68
- );
69
- if (!walletVerificationChallenge?.challenge?.secret || !walletVerificationChallenge?.challenge?.salt) {
70
- throw new ChallengeError("Invalid challenge format", "INVALID_CHALLENGE");
71
- }
72
- const { secret, salt } = walletVerificationChallenge.challenge;
73
- const challengeResponse = generateResponse(code.toString(), salt, secret);
74
- return {
75
- challengeResponse,
76
- verificationId
77
- };
78
- } catch (error) {
79
- if (error instanceof ChallengeError) {
80
- throw error;
81
- }
82
- throw new ChallengeError("Failed to process wallet verification challenge", "CHALLENGE_PROCESSING_ERROR");
83
- }
84
- }
85
-
86
- // src/utils/websocket-client.ts
87
- import { createClient } from "graphql-ws";
88
- function getWebsocketClient({ portalGraphqlEndpoint, accessToken }) {
89
- if (!portalGraphqlEndpoint) {
90
- throw new Error("portalGraphqlEndpoint is required");
91
- }
92
- if (!accessToken) {
93
- throw new Error("accessToken is required");
94
- }
95
- const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));
96
- return createClient({
97
- url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}`
98
- });
99
- }
100
- function setWsProtocol(url) {
101
- if (url.protocol === "ws:" || url.protocol === "wss:") {
102
- return url;
103
- }
104
- if (url.protocol === "http:") {
105
- url.protocol = "ws:";
106
- } else {
107
- url.protocol = "wss:";
108
- }
109
- return url;
110
- }
111
-
112
- // src/utils/wait-for-transaction-receipt.ts
113
- async function waitForTransactionReceipt(transactionHash, options) {
114
- const wsClient = getWebsocketClient(options);
115
- const subscription = wsClient.iterate({
116
- query: `subscription getTransaction($transactionHash: String!) {
117
- getTransaction(transactionHash: $transactionHash) {
118
- receipt {
119
- transactionHash
120
- to
121
- status
122
- from
123
- type
124
- revertReason
125
- revertReasonDecoded
126
- logs
127
- events
128
- contractAddress
129
- }
130
- transactionHash
131
- from
132
- createdAt
133
- address
134
- functionName
135
- isContract
136
- }
137
- }`,
138
- variables: { transactionHash }
139
- });
140
- const promises = [getTransactionFromSubscription(subscription)];
141
- if (options.timeout) {
142
- promises.push(createTimeoutPromise(options.timeout));
143
- }
144
- return Promise.race(promises);
145
- }
146
- function createTimeoutPromise(timeout) {
147
- return new Promise((_, reject) => {
148
- setTimeout(() => reject(new Error("Transaction receipt not found")), timeout);
149
- });
150
- }
151
- async function getTransactionFromSubscription(subscription) {
152
- for await (const result of subscription) {
153
- if (result?.data?.getTransaction?.receipt) {
154
- return result.data.getTransaction;
155
- }
156
- }
157
- throw new Error("No transaction found");
158
- }
159
-
160
- // src/portal.ts
161
- import { readFragment } from "gql.tada";
162
- var ClientOptionsSchema = z.object({
163
- instance: UrlOrPathSchema,
164
- accessToken: ApplicationAccessTokenSchema,
165
- cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
166
- });
167
- function createPortalClient(options, clientOptions) {
168
- ensureServer();
169
- const validatedOptions = validate(ClientOptionsSchema, options);
170
- const graphql = initGraphQLTada();
171
- const fullUrl = new URL(validatedOptions.instance).toString();
172
- return {
173
- client: new GraphQLClient(fullUrl, {
174
- ...clientOptions,
175
- headers: { ...clientOptions?.headers ?? {}, "x-auth-token": validatedOptions.accessToken }
176
- }),
177
- graphql
178
- };
179
- }
180
- export {
181
- ClientOptionsSchema,
182
- createPortalClient,
183
- getWebsocketClient,
184
- handleWalletVerificationChallenge,
185
- readFragment,
186
- waitForTransactionReceipt
187
- };
188
- //# sourceMappingURL=portal.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/portal.ts","../src/utils/wallet-verification-challenge.ts","../src/utils/websocket-client.ts","../src/utils/wait-for-transaction-receipt.ts"],"sourcesContent":["import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod/v4\";\n\n/**\n * Configuration options for the GraphQL client, excluding 'url' and 'exchanges'.\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating Portal client configuration options.\n */\nexport const ClientOptionsSchema = z.object({\n instance: UrlOrPathSchema,\n accessToken: ApplicationAccessTokenSchema,\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type representing the validated client options.\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Creates a Portal GraphQL client with the provided configuration.\n *\n * @param options - Configuration options for the Portal client\n * @param clientOptions - Additional GraphQL client configuration options\n * @returns An object containing the configured GraphQL client and graphql helper function\n * @throws If the provided options fail validation\n *\n * @example\n * import { createPortalClient } from \"@settlemint/sdk-portal\";\n * import { loadEnv } from \"@settlemint/sdk-utils/environment\";\n * import { createLogger, requestLogger } from \"@settlemint/sdk-utils/logging\";\n * import type { introspection } from \"@schemas/portal-env\";\n *\n * const env = await loadEnv(false, false);\n * const logger = createLogger();\n *\n * const { client: portalClient, graphql: portalGraphql } = createPortalClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * // Change unknown to the type you are using to store metadata\n * JSON: unknown;\n * };\n * }>(\n * {\n * instance: env.SETTLEMINT_PORTAL_GRAPHQL_ENDPOINT!,\n * accessToken: env.SETTLEMINT_ACCESS_TOKEN!,\n * },\n * {\n * fetch: requestLogger(logger, \"portal\", fetch) as typeof fetch,\n * },\n * );\n *\n * // Making GraphQL queries\n * const query = portalGraphql(`\n * query GetPendingTransactions {\n * getPendingTransactions {\n * count\n * }\n * }\n * `);\n *\n * const result = await portalClient.request(query);\n */\nexport function createPortalClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = new URL(validatedOptions.instance).toString();\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: { ...(clientOptions?.headers ?? {}), \"x-auth-token\": validatedOptions.accessToken },\n }),\n graphql,\n };\n}\n\nexport {\n handleWalletVerificationChallenge,\n type HandleWalletVerificationChallengeOptions,\n} from \"./utils/wallet-verification-challenge.js\";\nexport {\n waitForTransactionReceipt,\n type Transaction,\n type WaitForTransactionReceiptOptions,\n} from \"./utils/wait-for-transaction-receipt.js\";\nexport { getWebsocketClient, type WebsocketClientOptions } from \"./utils/websocket-client.js\";\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n","import { createHash } from \"node:crypto\";\nimport type { AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport type { GraphQLClient } from \"graphql-request\";\nimport type { Address } from \"viem\";\n\n/**\n * Custom error class for challenge-related errors\n */\nexport class ChallengeError extends Error {\n readonly code: string;\n\n constructor(message: string, code: string) {\n super(message);\n this.name = \"ChallengeError\";\n this.code = code;\n }\n}\n\n/**\n * Represents the structure of a wallet verification challenge\n */\ninterface WalletVerificationChallenge {\n challenge: {\n secret: string;\n salt: string;\n };\n id: string;\n name: string;\n verificationType: string;\n}\n\n/**\n * Response type for the CreateWalletVerificationChallenges mutation\n */\ninterface CreateWalletVerificationChallengesResponse {\n createWalletVerificationChallenges: WalletVerificationChallenge[];\n}\n\n/**\n * Hashes a pincode with a salt using SHA-256\n * @param pincode - The pincode to hash\n * @param salt - The salt to use in hashing\n * @returns The hashed pincode as a hex string\n */\nfunction hashPincode(pincode: string, salt: string): string {\n return createHash(\"sha256\").update(`${salt}${pincode}`).digest(\"hex\");\n}\n\n/**\n * Generates a challenge response by combining a hashed pincode with a challenge\n * @param pincode - The user's pincode\n * @param salt - The salt provided in the challenge\n * @param challenge - The challenge secret\n * @returns The challenge response as a hex string\n */\nfunction generateResponse(pincode: string, salt: string, challenge: string): string {\n const hashedPincode = hashPincode(pincode, salt);\n return createHash(\"sha256\").update(`${hashedPincode}_${challenge}`).digest(\"hex\");\n}\n\n/**\n * Options for handling a wallet verification challenge\n *\n * @typedef {Object} HandleWalletVerificationChallengeOptions\n * @template {AbstractSetupSchema} Setup - The GraphQL schema setup type\n * @property {GraphQLClient} portalClient - The portal client instance\n * @property {initGraphQLTada<Setup>} portalGraphql - The GraphQL query builder\n * @property {string} verificationId - The ID of the verification challenge\n * @property {Address} userWalletAddress - The wallet address to verify\n * @property {string | number} code - The verification code provided by the user\n * @property {\"otp\" | \"secret-code\" | \"pincode\"} verificationType - The type of verification being performed\n */\nexport interface HandleWalletVerificationChallengeOptions<Setup extends AbstractSetupSchema> {\n portalClient: GraphQLClient;\n portalGraphql: initGraphQLTada<Setup>;\n verificationId: string;\n userWalletAddress: Address;\n code: string | number;\n verificationType: \"otp\" | \"secret-code\" | \"pincode\";\n}\n\n/**\n * Handles a wallet verification challenge by generating an appropriate response\n *\n * @param options - The options for handling the wallet verification challenge\n * @returns Promise resolving to an object containing the challenge response and optionally the verification ID\n * @throws {ChallengeError} If the challenge cannot be created or is invalid\n * @example\n * import { createPortalClient } from \"@settlemint/sdk-portal\";\n * import { handleWalletVerificationChallenge } from \"@settlemint/sdk-portal\";\n *\n * const { client, graphql } = createPortalClient({\n * instance: \"https://portal.example.com/graphql\",\n * accessToken: \"your-access-token\"\n * });\n *\n * const result = await handleWalletVerificationChallenge({\n * portalClient: client,\n * portalGraphql: graphql,\n * verificationId: \"verification-123\",\n * userWalletAddress: \"0x123...\",\n * code: \"123456\",\n * verificationType: \"otp\"\n * });\n */\nexport async function handleWalletVerificationChallenge<const Setup extends AbstractSetupSchema>({\n portalClient,\n portalGraphql,\n verificationId,\n userWalletAddress,\n code,\n verificationType,\n}: HandleWalletVerificationChallengeOptions<Setup>): Promise<{\n challengeResponse: string;\n verificationId?: string;\n}> {\n try {\n if (verificationType === \"otp\") {\n return {\n challengeResponse: code.toString(),\n verificationId,\n };\n }\n\n if (verificationType === \"secret-code\") {\n // Add - separator to the code\n const formattedCode = code.toString().replace(/(.{5})(?=.)/, \"$1-\");\n return {\n challengeResponse: formattedCode,\n verificationId,\n };\n }\n\n const verificationChallenges = await portalClient.request<CreateWalletVerificationChallengesResponse>(\n portalGraphql(`\n mutation CreateWalletVerificationChallenges($userWalletAddress: String!, $verificationId: String!) {\n createWalletVerificationChallenges(userWalletAddress: $userWalletAddress, verificationId: $verificationId) {\n challenge\n id\n name\n verificationType\n }\n }\n `),\n {\n userWalletAddress,\n verificationId,\n },\n );\n\n if (!verificationChallenges.createWalletVerificationChallenges?.length) {\n throw new ChallengeError(\"No verification challenges received\", \"NO_CHALLENGES\");\n }\n\n const walletVerificationChallenge = verificationChallenges.createWalletVerificationChallenges.find(\n (challenge) => challenge.id === verificationId,\n );\n\n if (!walletVerificationChallenge?.challenge?.secret || !walletVerificationChallenge?.challenge?.salt) {\n throw new ChallengeError(\"Invalid challenge format\", \"INVALID_CHALLENGE\");\n }\n\n const { secret, salt } = walletVerificationChallenge.challenge;\n const challengeResponse = generateResponse(code.toString(), salt, secret);\n return {\n challengeResponse,\n verificationId,\n };\n } catch (error) {\n if (error instanceof ChallengeError) {\n throw error;\n }\n throw new ChallengeError(\"Failed to process wallet verification challenge\", \"CHALLENGE_PROCESSING_ERROR\");\n }\n}\n","import { createClient } from \"graphql-ws\";\n\n/**\n * Options for the GraphQL WebSocket client\n *\n * @typedef {Object} WebsocketClientOptions\n * @property {string} portalGraphqlEndpoint - The GraphQL endpoint URL for the Portal API\n * @property {string} accessToken - The access token for authentication with the Portal API\n */\nexport interface WebsocketClientOptions {\n portalGraphqlEndpoint: string;\n accessToken: string;\n}\n\n/**\n * Creates a GraphQL WebSocket client for the Portal API\n *\n * @param {WebsocketClientOptions} options - The options for the client\n * @param {string} options.portalGraphqlEndpoint - The GraphQL endpoint URL for the Portal API\n * @param {string} options.accessToken - The access token for authentication with the Portal API\n * @returns {Client} The GraphQL WebSocket client\n */\nexport function getWebsocketClient({ portalGraphqlEndpoint, accessToken }: WebsocketClientOptions) {\n if (!portalGraphqlEndpoint) {\n throw new Error(\"portalGraphqlEndpoint is required\");\n }\n if (!accessToken) {\n throw new Error(\"accessToken is required\");\n }\n const graphqlEndpoint = setWsProtocol(new URL(portalGraphqlEndpoint));\n return createClient({\n url: `${graphqlEndpoint.protocol}//${graphqlEndpoint.host}/${accessToken}${graphqlEndpoint.pathname}${graphqlEndpoint.search}`,\n });\n}\n\nfunction setWsProtocol(url: URL) {\n if (url.protocol === \"ws:\" || url.protocol === \"wss:\") {\n return url;\n }\n if (url.protocol === \"http:\") {\n url.protocol = \"ws:\";\n } else {\n url.protocol = \"wss:\";\n }\n return url;\n}\n","import type { FormattedExecutionResult } from \"graphql-ws\";\nimport { type WebsocketClientOptions, getWebsocketClient } from \"./websocket-client.js\";\n\n/**\n * Represents the structure of a blockchain transaction with its receipt\n *\n * @typedef {Object} Transaction\n * @property {Object} receipt - The transaction receipt details\n * @property {string} receipt.transactionHash - The hash of the transaction\n * @property {string} receipt.to - The recipient address of the transaction\n * @property {string} receipt.status - The status of the transaction (success/failure)\n * @property {string} receipt.from - The sender address of the transaction\n * @property {string} receipt.type - The type of the transaction\n * @property {string} receipt.revertReason - The reason for transaction reversion, if applicable\n * @property {string} receipt.revertReasonDecoded - Human-readable version of the revert reason\n * @property {string} receipt.contractAddress - The address of the contract deployed in the transaction\n * @property {string[]} receipt.logs - Array of log entries generated by the transaction\n * @property {string[]} receipt.events - Array of events emitted during the transaction\n * @property {string} transactionHash - The hash of the transaction (duplicate of receipt.transactionHash)\n * @property {string} from - The sender address (duplicate of receipt.from)\n * @property {string} createdAt - Timestamp when the transaction was created\n * @property {string} address - The contract address involved in the transaction\n * @property {string} functionName - The name of the function called in the transaction\n * @property {boolean} isContract - Whether the transaction is a contract deployment\n */\nexport interface Transaction {\n receipt: {\n transactionHash: string;\n to: string;\n status: string;\n from: string;\n type: string;\n revertReason: string;\n revertReasonDecoded: string;\n logs: string[];\n events: string[];\n contractAddress: string;\n };\n transactionHash: string;\n from: string;\n createdAt: string;\n address: string;\n functionName: string;\n isContract: boolean;\n}\n\ninterface GetTransactionResponse {\n getTransaction: Transaction;\n}\n\n/**\n * Options for waiting for a transaction receipt\n *\n * @typedef {Object} WaitForTransactionReceiptOptions\n * @property {number} [timeout] - Optional timeout in milliseconds before the operation fails\n */\nexport interface WaitForTransactionReceiptOptions extends WebsocketClientOptions {\n timeout?: number;\n}\n\n/**\n * Waits for a blockchain transaction receipt by subscribing to transaction updates via GraphQL.\n * This function polls until the transaction is confirmed or the timeout is reached.\n *\n * @param transactionHash - The hash of the transaction to wait for\n * @param options - Configuration options for the waiting process\n * @returns The transaction details including receipt information when the transaction is confirmed\n * @throws Error if the transaction receipt cannot be retrieved within the specified timeout\n *\n * @example\n * import { waitForTransactionReceipt } from \"@settlemint/sdk-portal\";\n *\n * const transaction = await waitForTransactionReceipt(\"0x123...\", {\n * portalGraphqlEndpoint: \"https://example.settlemint.com/graphql\",\n * accessToken: \"your-access-token\",\n * timeout: 30000 // 30 seconds timeout\n * });\n */\nexport async function waitForTransactionReceipt(transactionHash: string, options: WaitForTransactionReceiptOptions) {\n const wsClient = getWebsocketClient(options);\n const subscription = wsClient.iterate<GetTransactionResponse>({\n query: `subscription getTransaction($transactionHash: String!) {\n getTransaction(transactionHash: $transactionHash) {\n receipt {\n transactionHash\n to\n status\n from\n type\n revertReason\n revertReasonDecoded\n logs\n events\n contractAddress\n }\n transactionHash\n from\n createdAt\n address\n functionName\n isContract\n }\n }`,\n variables: { transactionHash },\n });\n const promises = [getTransactionFromSubscription(subscription)];\n if (options.timeout) {\n promises.push(createTimeoutPromise(options.timeout));\n }\n\n return Promise.race(promises);\n}\n\nfunction createTimeoutPromise(timeout: number): Promise<never> {\n return new Promise((_, reject) => {\n setTimeout(() => reject(new Error(\"Transaction receipt not found\")), timeout);\n });\n}\n\nasync function getTransactionFromSubscription(\n subscription: AsyncIterableIterator<FormattedExecutionResult<GetTransactionResponse, unknown>>,\n): Promise<Transaction> {\n for await (const result of subscription) {\n if (result?.data?.getTransaction?.receipt) {\n return result.data.getTransaction;\n }\n }\n throw new Error(\"No transaction found\");\n}\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,8BAA8B,iBAAiB,gBAAgB;AACxE,SAAmC,uBAAuB;AAC1D,SAAS,qBAAqB;AAC9B,SAAS,SAAS;;;ACJlB,SAAS,kBAAkB;AAQpB,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAC/B;AAAA,EAET,YAAY,SAAiB,MAAc;AACzC,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,OAAO;AAAA,EACd;AACF;AA4BA,SAAS,YAAY,SAAiB,MAAsB;AAC1D,SAAO,WAAW,QAAQ,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,EAAE,EAAE,OAAO,KAAK;AACtE;AASA,SAAS,iBAAiB,SAAiB,MAAc,WAA2B;AAClF,QAAM,gBAAgB,YAAY,SAAS,IAAI;AAC/C,SAAO,WAAW,QAAQ,EAAE,OAAO,GAAG,aAAa,IAAI,SAAS,EAAE,EAAE,OAAO,KAAK;AAClF;AA+CA,eAAsB,kCAA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