@openzeppelin/adapter-stellar 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/README.md +272 -0
  2. package/dist/config.cjs +21 -0
  3. package/dist/config.cjs.map +1 -0
  4. package/dist/config.d.cts +8 -0
  5. package/dist/config.d.cts.map +1 -0
  6. package/dist/config.d.mts +8 -0
  7. package/dist/config.d.mts.map +1 -0
  8. package/dist/config.mjs +20 -0
  9. package/dist/config.mjs.map +1 -0
  10. package/dist/index.cjs +7564 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/index.d.cts +261 -0
  13. package/dist/index.d.cts.map +1 -0
  14. package/dist/index.d.mts +263 -0
  15. package/dist/index.d.mts.map +1 -0
  16. package/dist/index.mjs +7529 -0
  17. package/dist/index.mjs.map +1 -0
  18. package/dist/metadata.cjs +22 -0
  19. package/dist/metadata.cjs.map +1 -0
  20. package/dist/metadata.d.cts +7 -0
  21. package/dist/metadata.d.cts.map +1 -0
  22. package/dist/metadata.d.mts +7 -0
  23. package/dist/metadata.d.mts.map +1 -0
  24. package/dist/metadata.mjs +21 -0
  25. package/dist/metadata.mjs.map +1 -0
  26. package/dist/networks-BrV516-R.d.cts +15 -0
  27. package/dist/networks-BrV516-R.d.cts.map +1 -0
  28. package/dist/networks-C0MmhJcu.d.mts +15 -0
  29. package/dist/networks-C0MmhJcu.d.mts.map +1 -0
  30. package/dist/networks-DgUFSTiC.cjs +76 -0
  31. package/dist/networks-DgUFSTiC.cjs.map +1 -0
  32. package/dist/networks-QbEPbaGT.mjs +46 -0
  33. package/dist/networks-QbEPbaGT.mjs.map +1 -0
  34. package/dist/networks.cjs +8 -0
  35. package/dist/networks.d.cts +2 -0
  36. package/dist/networks.d.mts +2 -0
  37. package/dist/networks.mjs +3 -0
  38. package/dist/vite-config.cjs +43 -0
  39. package/dist/vite-config.cjs.map +1 -0
  40. package/dist/vite-config.d.cts +35 -0
  41. package/dist/vite-config.d.cts.map +1 -0
  42. package/dist/vite-config.d.mts +35 -0
  43. package/dist/vite-config.d.mts.map +1 -0
  44. package/dist/vite-config.mjs +42 -0
  45. package/dist/vite-config.mjs.map +1 -0
  46. package/package.json +114 -0
  47. package/src/__tests__/getDefaultServiceConfig.test.ts +105 -0
  48. package/src/access-control/actions.ts +214 -0
  49. package/src/access-control/feature-detection.ts +238 -0
  50. package/src/access-control/index.ts +54 -0
  51. package/src/access-control/indexer-client.ts +1474 -0
  52. package/src/access-control/onchain-reader.ts +446 -0
  53. package/src/access-control/service.ts +1431 -0
  54. package/src/access-control/validation.ts +256 -0
  55. package/src/adapter.ts +659 -0
  56. package/src/config.ts +43 -0
  57. package/src/configuration/__tests__/explorer.test.ts +80 -0
  58. package/src/configuration/__tests__/rpc.test.ts +355 -0
  59. package/src/configuration/execution.ts +83 -0
  60. package/src/configuration/explorer.ts +105 -0
  61. package/src/configuration/index.ts +5 -0
  62. package/src/configuration/network-services.ts +210 -0
  63. package/src/configuration/rpc.ts +270 -0
  64. package/src/configuration.ts +2 -0
  65. package/src/contract/__tests__/complete-type-coverage.test.ts +78 -0
  66. package/src/contract/index.ts +3 -0
  67. package/src/contract/loader.ts +498 -0
  68. package/src/contract/transformer.ts +1 -0
  69. package/src/contract/type.ts +65 -0
  70. package/src/index.ts +23 -0
  71. package/src/mapping/constants.ts +89 -0
  72. package/src/mapping/enum-metadata.ts +237 -0
  73. package/src/mapping/field-generator.ts +296 -0
  74. package/src/mapping/index.ts +5 -0
  75. package/src/mapping/struct-fields.ts +106 -0
  76. package/src/mapping/tuple-components.ts +43 -0
  77. package/src/mapping/type-coverage-validator.ts +151 -0
  78. package/src/mapping/type-mapper.ts +203 -0
  79. package/src/metadata.ts +16 -0
  80. package/src/networks/README.md +84 -0
  81. package/src/networks/index.ts +19 -0
  82. package/src/networks/mainnet.ts +20 -0
  83. package/src/networks/testnet.ts +20 -0
  84. package/src/networks.ts +2 -0
  85. package/src/query/handler.ts +411 -0
  86. package/src/query/index.ts +4 -0
  87. package/src/query/view-checker.ts +32 -0
  88. package/src/sac/spec-cache.ts +68 -0
  89. package/src/sac/spec-source.ts +35 -0
  90. package/src/sac/xdr.ts +101 -0
  91. package/src/transaction/components/AdvancedInfo.tsx +34 -0
  92. package/src/transaction/components/FeeConfiguration.tsx +41 -0
  93. package/src/transaction/components/StellarRelayerOptions.tsx +60 -0
  94. package/src/transaction/components/TransactionTiming.tsx +77 -0
  95. package/src/transaction/components/index.ts +5 -0
  96. package/src/transaction/components/useStellarRelayerOptions.ts +114 -0
  97. package/src/transaction/eoa.ts +229 -0
  98. package/src/transaction/execution-strategy.ts +33 -0
  99. package/src/transaction/formatter.ts +296 -0
  100. package/src/transaction/index.ts +4 -0
  101. package/src/transaction/relayer.ts +575 -0
  102. package/src/transaction/sender.ts +156 -0
  103. package/src/transform/index.ts +4 -0
  104. package/src/transform/input-parser.ts +9 -0
  105. package/src/transform/output-formatter.ts +133 -0
  106. package/src/transform/parsers/complex-parser.ts +157 -0
  107. package/src/transform/parsers/generic-parser.ts +171 -0
  108. package/src/transform/parsers/index.ts +86 -0
  109. package/src/transform/parsers/primitive-parser.ts +123 -0
  110. package/src/transform/parsers/scval-converter.ts +405 -0
  111. package/src/transform/parsers/struct-parser.ts +324 -0
  112. package/src/transform/parsers/types.ts +35 -0
  113. package/src/types/__tests__/artifacts.test.ts +89 -0
  114. package/src/types/artifacts.ts +19 -0
  115. package/src/utils/__tests__/artifacts.test.ts +77 -0
  116. package/src/utils/artifacts.ts +30 -0
  117. package/src/utils/formatting.ts +122 -0
  118. package/src/utils/index.ts +6 -0
  119. package/src/utils/input-parsing.ts +336 -0
  120. package/src/utils/safe-type-parser.ts +303 -0
  121. package/src/utils/stellar-types.ts +35 -0
  122. package/src/utils/type-detection.ts +163 -0
  123. package/src/utils/xdr-ordering.ts +36 -0
  124. package/src/validation/__tests__/address.test.ts +267 -0
  125. package/src/validation/address.ts +136 -0
  126. package/src/validation/eoa.ts +33 -0
  127. package/src/validation/index.ts +3 -0
  128. package/src/validation/relayer.ts +13 -0
  129. package/src/vite-config.ts +67 -0
  130. package/src/wallet/README.md +93 -0
  131. package/src/wallet/__tests__/connection.test.ts +72 -0
  132. package/src/wallet/components/StellarWalletUiRoot.tsx +161 -0
  133. package/src/wallet/components/account/AccountDisplay.tsx +50 -0
  134. package/src/wallet/components/connect/ConnectButton.tsx +100 -0
  135. package/src/wallet/components/connect/ConnectorDialog.tsx +125 -0
  136. package/src/wallet/components/index.ts +3 -0
  137. package/src/wallet/connection.ts +151 -0
  138. package/src/wallet/context/StellarWalletContext.ts +32 -0
  139. package/src/wallet/context/index.ts +4 -0
  140. package/src/wallet/context/useStellarWalletContext.ts +17 -0
  141. package/src/wallet/hooks/facade-hooks.ts +31 -0
  142. package/src/wallet/hooks/index.ts +7 -0
  143. package/src/wallet/hooks/useStellarAccount.ts +27 -0
  144. package/src/wallet/hooks/useStellarConnect.ts +60 -0
  145. package/src/wallet/hooks/useStellarDisconnect.ts +47 -0
  146. package/src/wallet/hooks/useUiKitConfig.ts +40 -0
  147. package/src/wallet/implementation/wallets-kit-implementation.ts +379 -0
  148. package/src/wallet/index.ts +11 -0
  149. package/src/wallet/services/__tests__/configResolutionService.test.ts +163 -0
  150. package/src/wallet/services/configResolutionService.ts +65 -0
  151. package/src/wallet/stellar-wallets-kit/StellarWalletsKitConnectButton.tsx +82 -0
  152. package/src/wallet/stellar-wallets-kit/__mocks__/@creit.tech/stellar-wallets-kit.ts +48 -0
  153. package/src/wallet/stellar-wallets-kit/__tests__/export-service.test.ts +93 -0
  154. package/src/wallet/stellar-wallets-kit/__tests__/stellarUiKitManager.test.ts +0 -0
  155. package/src/wallet/stellar-wallets-kit/config-generator.ts +75 -0
  156. package/src/wallet/stellar-wallets-kit/export-service.ts +19 -0
  157. package/src/wallet/stellar-wallets-kit/index.ts +3 -0
  158. package/src/wallet/stellar-wallets-kit/stellarUiKitManager.ts +235 -0
  159. package/src/wallet/types.ts +19 -0
  160. package/src/wallet/utils/__tests__/filterWalletComponents.test.ts +150 -0
  161. package/src/wallet/utils/__tests__/uiKitService.test.ts +189 -0
  162. package/src/wallet/utils/filterWalletComponents.ts +89 -0
  163. package/src/wallet/utils/index.ts +3 -0
  164. package/src/wallet/utils/stellarWalletImplementationManager.ts +118 -0
  165. package/src/wallet/utils/uiKitService.ts +74 -0
@@ -0,0 +1,296 @@
1
+ import { Address, xdr } from '@stellar/stellar-sdk';
2
+
3
+ import type {
4
+ ContractSchema,
5
+ EnumValue,
6
+ FormFieldType,
7
+ FunctionParameter,
8
+ } from '@openzeppelin/ui-types';
9
+ import { isEnumValue } from '@openzeppelin/ui-types';
10
+ import { logger } from '@openzeppelin/ui-utils';
11
+
12
+ import { extractEnumVariants, isEnumType } from '../mapping/enum-metadata';
13
+ import { parseStellarInput } from '../transform';
14
+ import { isPrimitiveParamType } from '../utils/stellar-types';
15
+ import { isBytesNType, isLikelyEnumType } from '../utils/type-detection';
16
+
17
+ /**
18
+ * Stellar transaction data structure that will be passed to signAndBroadcast
19
+ */
20
+ export interface StellarTransactionData {
21
+ contractAddress: string;
22
+ functionName: string;
23
+ args: unknown[];
24
+ argTypes: string[]; // Parameter types for nativeToScVal type hints
25
+ argSchema?: FunctionParameter[]; // Full parameter schema with struct field info
26
+ transactionOptions: Record<string, unknown>;
27
+ }
28
+
29
+ /**
30
+ * Recursively enriches a parameter with enum metadata from specEntries.
31
+ * This ensures enum types get the metadata they need for proper ScVal conversion.
32
+ */
33
+ function enrichParameterWithEnumMetadata(
34
+ param: FunctionParameter,
35
+ specEntries: xdr.ScSpecEntry[] | undefined
36
+ ): FunctionParameter {
37
+ if (!specEntries) {
38
+ return param;
39
+ }
40
+
41
+ const enriched: FunctionParameter = { ...param };
42
+
43
+ // Add enum metadata if this is an enum type
44
+ if (isLikelyEnumType(param.type) && isEnumType(specEntries, param.type)) {
45
+ const enumMetadata = extractEnumVariants(specEntries, param.type);
46
+ if (enumMetadata) {
47
+ enriched.enumMetadata = enumMetadata;
48
+ }
49
+ }
50
+
51
+ // For Vec<EnumType>, extract the element type's enum metadata
52
+ const vecMatch = param.type.match(/^Vec<([^>]+)>$/);
53
+ if (vecMatch) {
54
+ const elementType = vecMatch[1];
55
+ if (isLikelyEnumType(elementType) && isEnumType(specEntries, elementType)) {
56
+ const enumMetadata = extractEnumVariants(specEntries, elementType);
57
+ if (enumMetadata) {
58
+ enriched.enumMetadata = enumMetadata;
59
+ }
60
+ }
61
+ }
62
+
63
+ // Recursively process components for struct types
64
+ if (enriched.components && enriched.components.length > 0) {
65
+ enriched.components = enriched.components.map((component) =>
66
+ enrichParameterWithEnumMetadata(component, specEntries)
67
+ );
68
+ }
69
+
70
+ return enriched;
71
+ }
72
+
73
+ /**
74
+ * Formats transaction data for Stellar chains based on parsed inputs.
75
+ *
76
+ * @param contractSchema The contract schema.
77
+ * @param functionId The ID of the function being called.
78
+ * @param submittedInputs The raw data submitted from the form.
79
+ * @param fields The fields of the form schema.
80
+ * @returns The formatted data payload suitable for signAndBroadcast.
81
+ */
82
+ export function formatStellarTransactionData(
83
+ contractSchema: ContractSchema,
84
+ functionId: string,
85
+ submittedInputs: Record<string, unknown>,
86
+ fields: FormFieldType[]
87
+ ): StellarTransactionData {
88
+ logger.info(
89
+ 'formatStellarTransactionData',
90
+ `Formatting Stellar transaction data for function: ${functionId}`
91
+ );
92
+
93
+ // --- Step 1: Determine Argument Order --- //
94
+ const functionDetails = contractSchema.functions.find((fn) => fn.id === functionId);
95
+ if (!functionDetails) {
96
+ throw new Error(`Function definition for ${functionId} not found in provided contract schema.`);
97
+ }
98
+ const expectedArgs = functionDetails.inputs;
99
+
100
+ // --- Step 2: Iterate and Select Values --- //
101
+
102
+ const orderedRawValues: unknown[] = [];
103
+ for (const expectedArg of expectedArgs) {
104
+ const fieldConfig = fields.find((field: FormFieldType) => field.name === expectedArg.name);
105
+ if (!fieldConfig) {
106
+ throw new Error(`Configuration missing for argument: ${expectedArg.name} in provided fields`);
107
+ }
108
+ let value: unknown;
109
+ if (fieldConfig.isHardcoded) {
110
+ // FIX: If hardcoded value is undefined but we have submitted input, use submitted input instead
111
+ // This handles cases where fields were incorrectly saved with undefined hardcoded values
112
+ if (fieldConfig.hardcodedValue === undefined && fieldConfig.name in submittedInputs) {
113
+ logger.warn(
114
+ 'formatStellarTransactionData',
115
+ `Field '${fieldConfig.name}' is hardcoded with undefined value but has submitted input. Using submitted input instead.`
116
+ );
117
+ value = submittedInputs[fieldConfig.name];
118
+ } else {
119
+ value = fieldConfig.hardcodedValue;
120
+ }
121
+ } else if (fieldConfig.isHidden) {
122
+ throw new Error(`Field '${fieldConfig.name}' cannot be hidden without being hardcoded.`);
123
+ } else {
124
+ if (!(fieldConfig.name in submittedInputs)) {
125
+ throw new Error(`Missing submitted input for required field: ${fieldConfig.name}`);
126
+ }
127
+ value = submittedInputs[fieldConfig.name];
128
+ }
129
+ orderedRawValues.push(value);
130
+ }
131
+
132
+ // --- Step 3: Parse/Transform Values using the imported parser --- //
133
+ const transformedArgs = expectedArgs.map((param, index) => {
134
+ let valueToParse = orderedRawValues[index];
135
+
136
+ // Handle enum values - process payload types using enum metadata
137
+ if (isEnumValue(valueToParse)) {
138
+ const specEntries = contractSchema.metadata?.specEntries as xdr.ScSpecEntry[] | undefined;
139
+ if (specEntries && isEnumType(specEntries, param.type)) {
140
+ const enumMetadata = extractEnumVariants(specEntries, param.type);
141
+ const enumValue = valueToParse as EnumValue;
142
+ if (enumMetadata && enumValue.values) {
143
+ // Find the variant metadata for the selected tag
144
+ const selectedVariant = enumMetadata.variants.find((v) => v.name === enumValue.tag);
145
+ if (selectedVariant && selectedVariant.payloadTypes) {
146
+ // Process each payload value according to its expected type
147
+ const processedValues = enumValue.values.map(
148
+ (rawValue: unknown, payloadIndex: number) => {
149
+ const expectedType = selectedVariant.payloadTypes![payloadIndex];
150
+ if (!expectedType) {
151
+ return rawValue;
152
+ }
153
+ const processedValue = parseStellarInput(rawValue, expectedType);
154
+ // Keep SorobanArgumentValue wrapper for primitive payloads to satisfy tests and simple paths.
155
+ // For complex payloads (structs/tuples/maps/vec/enums), return raw values and let valueToScVal handle serialization.
156
+ const isPrimitivePayload =
157
+ isPrimitiveParamType(expectedType) || isBytesNType(expectedType);
158
+
159
+ if (isPrimitivePayload) {
160
+ return { type: expectedType, value: processedValue };
161
+ }
162
+ return processedValue;
163
+ }
164
+ );
165
+ // Return the enum with processed payload values
166
+ valueToParse = { ...enumValue, values: processedValues };
167
+ }
168
+ }
169
+ }
170
+ }
171
+
172
+ // Handle array parameters - if the value is already an array and the type expects it,
173
+ // we can pass it directly. The Stellar input parser will handle the conversion.
174
+ if (
175
+ typeof param.type === 'string' &&
176
+ param.type.startsWith('Vec<') &&
177
+ Array.isArray(valueToParse)
178
+ ) {
179
+ // For Vec types, pass array as-is to parseStellarInput
180
+ return parseStellarInput(valueToParse, param.type);
181
+ }
182
+
183
+ return parseStellarInput(valueToParse, param.type);
184
+ });
185
+
186
+ // --- Step 4: Validate Contract Address --- //
187
+ if (!contractSchema.address) {
188
+ throw new Error('Contract address is missing or invalid in the provided schema.');
189
+ }
190
+
191
+ try {
192
+ Address.fromString(contractSchema.address);
193
+ } catch {
194
+ throw new Error('Contract address is missing or invalid in the provided schema.');
195
+ }
196
+
197
+ // --- Step 5: Prepare Return Object --- //
198
+ const fieldByName = new Map<string, FormFieldType>();
199
+ fields.forEach((field) => fieldByName.set(field.name, field));
200
+
201
+ // Extract specEntries from contract metadata for enum enrichment
202
+ const specEntries = contractSchema.metadata?.specEntries as xdr.ScSpecEntry[] | undefined;
203
+
204
+ const argSchemaWithComponents = functionDetails.inputs.map((param) => {
205
+ const field = fieldByName.get(param.name);
206
+
207
+ // Build enhanced schema with components and enum metadata from the field
208
+ const enhanced: FunctionParameter = { ...param };
209
+
210
+ // Prefer existing components from param, otherwise use field components
211
+ if (param.components && param.components.length > 0) {
212
+ enhanced.components = param.components;
213
+ } else if (field?.components && field.components.length > 0) {
214
+ enhanced.components = field.components;
215
+ }
216
+
217
+ // Add enum metadata from field if available
218
+ if (field?.enumMetadata) {
219
+ enhanced.enumMetadata = field.enumMetadata;
220
+ }
221
+
222
+ // For array fields, check if elementFieldConfig has enum metadata or components
223
+ // This is needed for Vec<EnumType> or Vec<StructType>
224
+ if (param.type.startsWith('Vec<') && field?.elementFieldConfig) {
225
+ if (field.elementFieldConfig.enumMetadata) {
226
+ enhanced.enumMetadata = field.elementFieldConfig.enumMetadata;
227
+ }
228
+ if (field.elementFieldConfig.components) {
229
+ enhanced.components = field.elementFieldConfig.components;
230
+ }
231
+ }
232
+
233
+ // For struct fields, enrich the components with enum metadata from nested fields
234
+ if (enhanced.components && enhanced.components.length > 0) {
235
+ enhanced.components = enhanced.components.map((component) => {
236
+ // Build the nested field name (e.g., "complex_struct.base_asset")
237
+ const nestedFieldName = `${param.name}.${component.name}`;
238
+ const nestedField = fieldByName.get(nestedFieldName);
239
+
240
+ if (nestedField) {
241
+ const enrichedComponent: FunctionParameter = { ...component };
242
+
243
+ // Add enum metadata from nested field
244
+ if (nestedField.enumMetadata) {
245
+ enrichedComponent.enumMetadata = nestedField.enumMetadata;
246
+ }
247
+
248
+ // Add components from nested field
249
+ if (nestedField.components) {
250
+ enrichedComponent.components = nestedField.components;
251
+ }
252
+
253
+ // For nested array fields, extract elementFieldConfig metadata
254
+ if (component.type.startsWith('Vec<') && nestedField.elementFieldConfig) {
255
+ if (nestedField.elementFieldConfig.enumMetadata) {
256
+ enrichedComponent.enumMetadata = nestedField.elementFieldConfig.enumMetadata;
257
+ }
258
+ if (nestedField.elementFieldConfig.components) {
259
+ enrichedComponent.components = nestedField.elementFieldConfig.components;
260
+ }
261
+ }
262
+
263
+ return enrichedComponent;
264
+ }
265
+
266
+ return component;
267
+ });
268
+ }
269
+
270
+ // Use specEntries to enrich with enum metadata for any enum types that weren't in the fields
271
+ // This handles nested enums in structs where the nested fields aren't in the fields array
272
+ const finalEnhanced = enrichParameterWithEnumMetadata(enhanced, specEntries);
273
+
274
+ return finalEnhanced;
275
+ });
276
+
277
+ const stellarTransactionData: StellarTransactionData = {
278
+ contractAddress: contractSchema.address,
279
+ functionName: functionDetails.name,
280
+ args: transformedArgs,
281
+ argTypes: functionDetails.inputs.map((param) => param.type), // Include parameter types for ScVal conversion
282
+ argSchema: argSchemaWithComponents, // Include full parameter schema with struct/tuple field definitions
283
+ transactionOptions: {
284
+ // Add any Stellar-specific transaction options here
285
+ // For example: fee, timeout, memo, etc.
286
+ },
287
+ };
288
+
289
+ logger.debug(
290
+ 'formatStellarTransactionData',
291
+ 'Formatted transaction data:',
292
+ stellarTransactionData
293
+ );
294
+
295
+ return stellarTransactionData;
296
+ }
@@ -0,0 +1,4 @@
1
+ // Barrel file
2
+
3
+ export * from './formatter';
4
+ export * from './sender';