@openzeppelin/ui-react 3.0.0 → 3.1.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.
- package/README.md +7 -0
- package/dist/index.cjs +541 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +222 -4
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +222 -4
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +535 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -51,6 +51,13 @@ It is a foundational package that can be used to ensure consistent wallet and ru
|
|
|
51
51
|
- **`useRuntimeContext()`**: Low-level access to `getRuntimeForNetwork` / loading helpers from `RuntimeProvider`.
|
|
52
52
|
- **`useWalletState()`**: `activeNetworkId`, `activeNetworkConfig`, **`activeRuntime`**, **`isRuntimeLoading`**, `walletFacadeHooks`, `setActiveNetworkId`, `reconfigureActiveUiKit`.
|
|
53
53
|
|
|
54
|
+
### Name resolution (ENS forward + reverse)
|
|
55
|
+
|
|
56
|
+
See the [ENS address input integration guide](../../docs/ens-address-input/README.md) for the full wiring story. Key exports:
|
|
57
|
+
|
|
58
|
+
- **`NameResolutionProvider`** / **`useResolveName`** / **`useResolveAddress`** — SF-2 async engine (debounce, cache, retries, closed error union)
|
|
59
|
+
- **`useRuntimeNameResolver`** — maps the active runtime's `nameResolution` capability into the dumb `NameResolver` shape consumed by `@openzeppelin/ui-components`' `NameResolverProvider`
|
|
60
|
+
|
|
54
61
|
### Derived hooks
|
|
55
62
|
|
|
56
63
|
These abstract wallet interactions across ecosystems:
|
package/dist/index.cjs
CHANGED
|
@@ -29,10 +29,11 @@ let react = require("react");
|
|
|
29
29
|
react = __toESM(react);
|
|
30
30
|
let _openzeppelin_ui_utils = require("@openzeppelin/ui-utils");
|
|
31
31
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
32
|
+
let _tanstack_react_query = require("@tanstack/react-query");
|
|
32
33
|
let _openzeppelin_ui_components = require("@openzeppelin/ui-components");
|
|
33
34
|
|
|
34
35
|
//#region src/version.ts
|
|
35
|
-
const VERSION = "3.
|
|
36
|
+
const VERSION = "3.1.0";
|
|
36
37
|
|
|
37
38
|
//#endregion
|
|
38
39
|
//#region src/hooks/AdapterContext.tsx
|
|
@@ -272,12 +273,12 @@ const WALLET_STATE_CONTEXT_KEY = Symbol.for("@openzeppelin/ui-react/WalletStateC
|
|
|
272
273
|
* 4. For React contexts specifically, having the same context object is what matters,
|
|
273
274
|
* and this pattern guarantees that after initialization
|
|
274
275
|
*/
|
|
275
|
-
function getOrCreateSharedContext() {
|
|
276
|
+
function getOrCreateSharedContext$1() {
|
|
276
277
|
const global = globalThis;
|
|
277
278
|
if (!global[WALLET_STATE_CONTEXT_KEY]) global[WALLET_STATE_CONTEXT_KEY] = (0, react.createContext)(void 0);
|
|
278
279
|
return global[WALLET_STATE_CONTEXT_KEY];
|
|
279
280
|
}
|
|
280
|
-
const WalletStateContext = getOrCreateSharedContext();
|
|
281
|
+
const WalletStateContext = getOrCreateSharedContext$1();
|
|
281
282
|
/**
|
|
282
283
|
* Hook to access wallet state from WalletStateProvider.
|
|
283
284
|
* @throws Error if used outside of WalletStateProvider
|
|
@@ -905,6 +906,536 @@ function useWalletReconnectionHandler(selectedNetworkConfigId, selectedCapabilit
|
|
|
905
906
|
]);
|
|
906
907
|
}
|
|
907
908
|
|
|
909
|
+
//#endregion
|
|
910
|
+
//#region src/hooks/nameResolution/resolutionConfig.ts
|
|
911
|
+
/**
|
|
912
|
+
* Default resolution config. Values sit within the spec's "seconds-to-minutes"
|
|
913
|
+
* caching guidance; forward debounce is 300ms (typed char-by-char), reverse is 0.
|
|
914
|
+
*/
|
|
915
|
+
const DEFAULT_CONFIG = {
|
|
916
|
+
staleTimeMs: 6e4,
|
|
917
|
+
gcTimeMs: 3e5,
|
|
918
|
+
forwardDebounceMs: 300,
|
|
919
|
+
reverseDebounceMs: 0,
|
|
920
|
+
transientRetryCount: 2
|
|
921
|
+
};
|
|
922
|
+
/** Stable prefix for every resolution cache key — namespaces this package's keys. */
|
|
923
|
+
const RESOLUTION_KEY_PREFIX = "oz-name-resolution";
|
|
924
|
+
/**
|
|
925
|
+
* Build a network-scoped, namespace-separated cache key (INV-40). A name resolves
|
|
926
|
+
* differently per network and provenance can be chain-scoped, so `networkId` is
|
|
927
|
+
* part of the key; `namespace` keeps forward and reverse lookups disjoint.
|
|
928
|
+
*
|
|
929
|
+
* @param namespace - `'name'` (forward) or `'addr'` (reverse).
|
|
930
|
+
* @param networkId - Active network id (`''` when no network is selected).
|
|
931
|
+
* @param normalizedInput - Trimmed + lowercased input (INV-26).
|
|
932
|
+
* @returns A readonly tuple suitable for a TanStack Query `queryKey`.
|
|
933
|
+
*/
|
|
934
|
+
function buildResolutionKey(namespace, networkId, normalizedInput) {
|
|
935
|
+
return [
|
|
936
|
+
RESOLUTION_KEY_PREFIX,
|
|
937
|
+
namespace,
|
|
938
|
+
networkId,
|
|
939
|
+
normalizedInput
|
|
940
|
+
];
|
|
941
|
+
}
|
|
942
|
+
/**
|
|
943
|
+
* Whether a resolution error is transient (worth retrying) versus a definitive,
|
|
944
|
+
* stable answer (INV-47). Exhaustive over SF-1's closed error union with no
|
|
945
|
+
* `default`: adding a new error code to `@openzeppelin/ui-types` will fail to
|
|
946
|
+
* compile here until it is classified — deliberately coupling SF-2 to SF-1 INV-7.
|
|
947
|
+
*
|
|
948
|
+
* @param error - A typed {@link NameResolutionError}.
|
|
949
|
+
* @returns `true` for transient failures (timeout / gateway / adapter), `false`
|
|
950
|
+
* for definitive negatives and unsupported-network answers.
|
|
951
|
+
*/
|
|
952
|
+
function isTransientError(error) {
|
|
953
|
+
switch (error.code) {
|
|
954
|
+
case "RESOLUTION_TIMEOUT":
|
|
955
|
+
case "EXTERNAL_GATEWAY_ERROR":
|
|
956
|
+
case "ADAPTER_ERROR": return true;
|
|
957
|
+
case "NAME_NOT_FOUND":
|
|
958
|
+
case "ADDRESS_NOT_FOUND":
|
|
959
|
+
case "UNSUPPORTED_NAME":
|
|
960
|
+
case "UNSUPPORTED_NETWORK": return false;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Construct a QueryClient tuned for name resolution: ambient refetch triggers are
|
|
965
|
+
* disabled at the client level (INV-41) so a resolved value never silently
|
|
966
|
+
* changes under the user on window focus or network reconnect.
|
|
967
|
+
*/
|
|
968
|
+
function createResolutionQueryClient() {
|
|
969
|
+
return new _tanstack_react_query.QueryClient({ defaultOptions: { queries: {
|
|
970
|
+
refetchOnWindowFocus: false,
|
|
971
|
+
refetchOnReconnect: false
|
|
972
|
+
} } });
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Process-global key for the default resolution QueryClient. Uses `Symbol.for`
|
|
976
|
+
* so every duplicated module instance (Vite pre-bundling, npm-installed adapters)
|
|
977
|
+
* shares ONE client — the same cross-bundle-singleton pattern as
|
|
978
|
+
* `WalletStateContext`. This is what makes the cache shared with zero wiring
|
|
979
|
+
* (SC-001) and enables cross-instance dedup / warm-cache reuse (INV-33 / INV-37 / INV-48).
|
|
980
|
+
*/
|
|
981
|
+
const DEFAULT_CLIENT_KEY = Symbol.for("@openzeppelin/ui-react/NameResolutionQueryClient");
|
|
982
|
+
/**
|
|
983
|
+
* Lazily create (exactly once) and return the process-global resolution
|
|
984
|
+
* QueryClient used when no `NameResolutionProvider` is mounted (INV-48).
|
|
985
|
+
*
|
|
986
|
+
* @returns The shared default resolution QueryClient (stable across calls).
|
|
987
|
+
*/
|
|
988
|
+
function getDefaultResolutionQueryClient() {
|
|
989
|
+
const globalScope = globalThis;
|
|
990
|
+
if (!globalScope[DEFAULT_CLIENT_KEY]) globalScope[DEFAULT_CLIENT_KEY] = createResolutionQueryClient();
|
|
991
|
+
return globalScope[DEFAULT_CLIENT_KEY];
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
//#endregion
|
|
995
|
+
//#region src/hooks/nameResolution/NameResolutionContext.ts
|
|
996
|
+
/**
|
|
997
|
+
* Process-global key for the context object. Uses `Symbol.for` so all duplicated
|
|
998
|
+
* module instances share ONE React context — the same cross-bundle-singleton
|
|
999
|
+
* pattern as `WalletStateContext` (bundler pre-bundling / npm-installed adapters).
|
|
1000
|
+
*/
|
|
1001
|
+
const CONTEXT_KEY = Symbol.for("@openzeppelin/ui-react/NameResolutionContext");
|
|
1002
|
+
function getOrCreateSharedContext() {
|
|
1003
|
+
const globalScope = globalThis;
|
|
1004
|
+
if (!globalScope[CONTEXT_KEY]) globalScope[CONTEXT_KEY] = (0, react.createContext)(null);
|
|
1005
|
+
return globalScope[CONTEXT_KEY];
|
|
1006
|
+
}
|
|
1007
|
+
/**
|
|
1008
|
+
* React context for resolution. `null` default triggers the module-singleton
|
|
1009
|
+
* fallback in {@link useNameResolutionContext} — no Provider is required (INV-48).
|
|
1010
|
+
*/
|
|
1011
|
+
const NameResolutionContext = getOrCreateSharedContext();
|
|
1012
|
+
/**
|
|
1013
|
+
* Stable fallback value used when no Provider is mounted. Cached per module so the
|
|
1014
|
+
* returned reference is referentially stable across renders (INV-38); it wraps the
|
|
1015
|
+
* process-global default client, so caches are still shared even across bundles.
|
|
1016
|
+
*/
|
|
1017
|
+
let fallbackValue;
|
|
1018
|
+
function getFallbackContextValue() {
|
|
1019
|
+
if (!fallbackValue) fallbackValue = {
|
|
1020
|
+
queryClient: getDefaultResolutionQueryClient(),
|
|
1021
|
+
config: DEFAULT_CONFIG
|
|
1022
|
+
};
|
|
1023
|
+
return fallbackValue;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* Read the resolution context, falling back to the zero-wiring default (global
|
|
1027
|
+
* singleton client + {@link DEFAULT_CONFIG}) when no `NameResolutionProvider` is
|
|
1028
|
+
* mounted (INV-48). Never throws for a missing provider.
|
|
1029
|
+
*
|
|
1030
|
+
* @returns The active {@link NameResolutionContextValue}.
|
|
1031
|
+
*/
|
|
1032
|
+
function useNameResolutionContext() {
|
|
1033
|
+
return (0, react.useContext)(NameResolutionContext) ?? getFallbackContextValue();
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
//#endregion
|
|
1037
|
+
//#region src/hooks/nameResolution/resolutionState.ts
|
|
1038
|
+
/**
|
|
1039
|
+
* Internal error used to bridge SF-1's `ok: false` results (and defensively-caught
|
|
1040
|
+
* adapter throws) into TanStack Query's thrown-error channel, carrying the typed
|
|
1041
|
+
* {@link NameResolutionError} through so the mapping step can surface it unchanged
|
|
1042
|
+
* (INV-43). Never leaks past the hook boundary.
|
|
1043
|
+
*/
|
|
1044
|
+
var ResolutionQueryError = class extends Error {
|
|
1045
|
+
resolutionError;
|
|
1046
|
+
/** @param resolutionError - The typed error to carry through react-query. */
|
|
1047
|
+
constructor(resolutionError) {
|
|
1048
|
+
super(`name resolution failed: ${resolutionError.code}`);
|
|
1049
|
+
this.name = "ResolutionQueryError";
|
|
1050
|
+
this.resolutionError = resolutionError;
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
/**
|
|
1054
|
+
* Convert any value from react-query's error channel into a typed, closed-union
|
|
1055
|
+
* {@link NameResolutionError} (INV-43). A {@link ResolutionQueryError} is
|
|
1056
|
+
* unwrapped verbatim; anything else (a react-query internal error) is mapped to
|
|
1057
|
+
* `ADAPTER_ERROR` so no untyped throw reaches a component.
|
|
1058
|
+
*
|
|
1059
|
+
* @param error - The raw `query.error` value (`unknown`).
|
|
1060
|
+
* @returns A typed error drawn only from SF-1's closed union.
|
|
1061
|
+
*/
|
|
1062
|
+
function toNameResolutionError(error) {
|
|
1063
|
+
if (error instanceof ResolutionQueryError) return error.resolutionError;
|
|
1064
|
+
return {
|
|
1065
|
+
code: "ADAPTER_ERROR",
|
|
1066
|
+
message: error instanceof Error ? error.message : String(error),
|
|
1067
|
+
cause: error
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Map a settled query's state to exactly one non-gate {@link EngineResult} arm
|
|
1072
|
+
* (INV-42, INV-44). The `input` echoed here is the debounced value the query was
|
|
1073
|
+
* keyed on, so `data`/`error` are always paired with the input that produced them
|
|
1074
|
+
* (INV-24). Success without data degrades to `loading` rather than a resolved arm
|
|
1075
|
+
* missing its payload.
|
|
1076
|
+
*
|
|
1077
|
+
* @param input - The debounced, normalized input keying this query.
|
|
1078
|
+
* @param query - The settled query state.
|
|
1079
|
+
* @param retry - Bound `refetch` for the `error` arm (INV-34).
|
|
1080
|
+
* @returns The `resolved`, `error`, or `loading` arm.
|
|
1081
|
+
*/
|
|
1082
|
+
function mapSettledQuery(input, query, retry) {
|
|
1083
|
+
if (query.isSuccess && query.data !== void 0) return {
|
|
1084
|
+
status: "resolved",
|
|
1085
|
+
input,
|
|
1086
|
+
data: query.data
|
|
1087
|
+
};
|
|
1088
|
+
if (query.isError) return {
|
|
1089
|
+
status: "error",
|
|
1090
|
+
input,
|
|
1091
|
+
error: toNameResolutionError(query.error),
|
|
1092
|
+
retry
|
|
1093
|
+
};
|
|
1094
|
+
return {
|
|
1095
|
+
status: "loading",
|
|
1096
|
+
input
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
//#endregion
|
|
1101
|
+
//#region src/hooks/nameResolution/useResolutionEngine.ts
|
|
1102
|
+
const LOG_SCOPE = "useResolutionEngine";
|
|
1103
|
+
/** No-op retry for synthesized errors that never entered the query path (INV-45). */
|
|
1104
|
+
const NOOP_RETRY = () => {};
|
|
1105
|
+
/**
|
|
1106
|
+
* Shared engine behind `useResolveName` / `useResolveAddress`. Owns input
|
|
1107
|
+
* normalization, debouncing, the `useQuery` wiring (explicit-client form), the
|
|
1108
|
+
* capability-absence gate, and the mapping to a direction-agnostic
|
|
1109
|
+
* {@link EngineResult}. Not exported from the package.
|
|
1110
|
+
*/
|
|
1111
|
+
function useResolutionEngine(params) {
|
|
1112
|
+
const { input, namespace, debounceMs, enabled, shouldAttempt, getMethod } = params;
|
|
1113
|
+
const walletState = (0, react.useContext)(WalletStateContext);
|
|
1114
|
+
const activeRuntime = walletState?.activeRuntime ?? null;
|
|
1115
|
+
const activeNetworkId = walletState?.activeNetworkId ?? null;
|
|
1116
|
+
const isRuntimeLoading = walletState?.isRuntimeLoading ?? false;
|
|
1117
|
+
const { queryClient, config } = useNameResolutionContext();
|
|
1118
|
+
const trimmedLive = (input ?? "").trim();
|
|
1119
|
+
const normalizedLive = trimmedLive.toLowerCase();
|
|
1120
|
+
const [debouncedTrimmed, setDebouncedTrimmed] = (0, react.useState)(trimmedLive);
|
|
1121
|
+
const seededRef = (0, react.useRef)(false);
|
|
1122
|
+
(0, react.useEffect)(() => {
|
|
1123
|
+
if (!seededRef.current) {
|
|
1124
|
+
seededRef.current = true;
|
|
1125
|
+
return;
|
|
1126
|
+
}
|
|
1127
|
+
if (debounceMs <= 0) {
|
|
1128
|
+
setDebouncedTrimmed(trimmedLive);
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
const timer = setTimeout(() => setDebouncedTrimmed(trimmedLive), debounceMs);
|
|
1132
|
+
return () => clearTimeout(timer);
|
|
1133
|
+
}, [trimmedLive, debounceMs]);
|
|
1134
|
+
const effectiveTrimmed = debounceMs <= 0 ? trimmedLive : debouncedTrimmed;
|
|
1135
|
+
const normalizedKey = effectiveTrimmed.toLowerCase();
|
|
1136
|
+
const capability = activeRuntime?.nameResolution;
|
|
1137
|
+
const networkId = activeNetworkId ?? "";
|
|
1138
|
+
const method = capability ? getMethod(capability) : void 0;
|
|
1139
|
+
const liveAttemptable = capability ? shouldAttempt(capability, normalizedLive) : false;
|
|
1140
|
+
const keyAttemptable = capability ? shouldAttempt(capability, normalizedKey) : false;
|
|
1141
|
+
const queryEnabled = enabled && trimmedLive !== "" && capability != null && method != null && normalizedKey !== "" && keyAttemptable;
|
|
1142
|
+
const query = (0, _tanstack_react_query.useQuery)({
|
|
1143
|
+
queryKey: buildResolutionKey(namespace, networkId, normalizedKey),
|
|
1144
|
+
queryFn: async () => {
|
|
1145
|
+
if (!method) throw new ResolutionQueryError({
|
|
1146
|
+
code: "ADAPTER_ERROR",
|
|
1147
|
+
message: "resolution method unexpectedly missing"
|
|
1148
|
+
});
|
|
1149
|
+
const adapterArg = namespace === "addr" ? effectiveTrimmed : normalizedKey;
|
|
1150
|
+
let result;
|
|
1151
|
+
try {
|
|
1152
|
+
result = await method(adapterArg);
|
|
1153
|
+
} catch (cause) {
|
|
1154
|
+
_openzeppelin_ui_utils.logger.warn(LOG_SCOPE, "resolution method threw instead of returning ok:false", cause);
|
|
1155
|
+
throw new ResolutionQueryError({
|
|
1156
|
+
code: "ADAPTER_ERROR",
|
|
1157
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
1158
|
+
cause
|
|
1159
|
+
});
|
|
1160
|
+
}
|
|
1161
|
+
if (!result.ok) throw new ResolutionQueryError(result.error);
|
|
1162
|
+
return result.value;
|
|
1163
|
+
},
|
|
1164
|
+
enabled: queryEnabled,
|
|
1165
|
+
staleTime: config.staleTimeMs,
|
|
1166
|
+
gcTime: config.gcTimeMs,
|
|
1167
|
+
refetchOnWindowFocus: false,
|
|
1168
|
+
refetchOnReconnect: false,
|
|
1169
|
+
retry: (failureCount, error) => (error instanceof ResolutionQueryError ? isTransientError(error.resolutionError) : true) && failureCount < config.transientRetryCount
|
|
1170
|
+
}, queryClient);
|
|
1171
|
+
if (!enabled) return { status: "idle" };
|
|
1172
|
+
if (trimmedLive === "") return { status: "idle" };
|
|
1173
|
+
if (capability) {
|
|
1174
|
+
if (!liveAttemptable) return { status: "idle" };
|
|
1175
|
+
if (!method) return unsupportedNetwork(normalizedLive, networkId);
|
|
1176
|
+
if (normalizedKey !== normalizedLive) return {
|
|
1177
|
+
status: "debouncing",
|
|
1178
|
+
input: normalizedLive
|
|
1179
|
+
};
|
|
1180
|
+
} else {
|
|
1181
|
+
if (isRuntimeLoading) return {
|
|
1182
|
+
status: "loading",
|
|
1183
|
+
input: normalizedLive
|
|
1184
|
+
};
|
|
1185
|
+
return unsupportedNetwork(normalizedLive, networkId);
|
|
1186
|
+
}
|
|
1187
|
+
const retry = () => {
|
|
1188
|
+
query.refetch();
|
|
1189
|
+
};
|
|
1190
|
+
return mapSettledQuery(normalizedKey, query, retry);
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Synthesize an `UNSUPPORTED_NETWORK` error arm without touching react-query
|
|
1194
|
+
* (INV-45). Drawn from SF-1's closed union so it is indistinguishable from an
|
|
1195
|
+
* adapter-returned `UNSUPPORTED_NETWORK`; `networkId` may be `''` (INV-49). Retry
|
|
1196
|
+
* is a no-op — there is no query to refetch.
|
|
1197
|
+
*/
|
|
1198
|
+
function unsupportedNetwork(input, networkId) {
|
|
1199
|
+
return {
|
|
1200
|
+
status: "error",
|
|
1201
|
+
input,
|
|
1202
|
+
error: {
|
|
1203
|
+
code: "UNSUPPORTED_NETWORK",
|
|
1204
|
+
networkId
|
|
1205
|
+
},
|
|
1206
|
+
retry: NOOP_RETRY
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
//#endregion
|
|
1211
|
+
//#region src/hooks/nameResolution/useResolveName.ts
|
|
1212
|
+
/**
|
|
1213
|
+
* Forward-resolve a name to an address. Soft-reads the active runtime from
|
|
1214
|
+
* `WalletStateContext` (never throws when no provider is mounted — degrades to
|
|
1215
|
+
* idle / UNSUPPORTED_NETWORK) and calls `runtime.nameResolution?.resolveName`.
|
|
1216
|
+
* Debounces `name`, caches per (network, name) via the owned QueryClient, and is
|
|
1217
|
+
* protected against out-of-order responses (distinct inputs → distinct query keys).
|
|
1218
|
+
*
|
|
1219
|
+
* Returns a discriminated union keyed on `status`; it never throws for expected
|
|
1220
|
+
* failures (they surface in the `error` arm) and never pairs a name with a
|
|
1221
|
+
* different name's resolved address (INV-24).
|
|
1222
|
+
*
|
|
1223
|
+
* @param name - The name to resolve. `null` / empty / a value failing the
|
|
1224
|
+
* adapter's `isValidName` yields `status: 'idle'` (never `error`).
|
|
1225
|
+
* @param options - `debounceMs` / `enabled` overrides.
|
|
1226
|
+
* @returns The current {@link UseResolveNameResult}.
|
|
1227
|
+
*/
|
|
1228
|
+
function useResolveName(name, options) {
|
|
1229
|
+
const { config } = useNameResolutionContext();
|
|
1230
|
+
return toNameResult(useResolutionEngine({
|
|
1231
|
+
input: name,
|
|
1232
|
+
namespace: "name",
|
|
1233
|
+
debounceMs: options?.debounceMs ?? config.forwardDebounceMs,
|
|
1234
|
+
enabled: options?.enabled ?? true,
|
|
1235
|
+
shouldAttempt: (cap, normalized) => cap.isValidName(normalized),
|
|
1236
|
+
getMethod: (cap) => cap.resolveName
|
|
1237
|
+
}));
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* Remap the engine's generic `input` to `name` (INV-24: keyed off the debounced
|
|
1241
|
+
* input carried in the engine result, not the live prop). Exhaustive over the
|
|
1242
|
+
* union so a new status cannot silently fall through (INV-23 / INV-42).
|
|
1243
|
+
*/
|
|
1244
|
+
function toNameResult(engine) {
|
|
1245
|
+
switch (engine.status) {
|
|
1246
|
+
case "idle": return { status: "idle" };
|
|
1247
|
+
case "debouncing": return {
|
|
1248
|
+
status: "debouncing",
|
|
1249
|
+
name: engine.input
|
|
1250
|
+
};
|
|
1251
|
+
case "loading": return {
|
|
1252
|
+
status: "loading",
|
|
1253
|
+
name: engine.input
|
|
1254
|
+
};
|
|
1255
|
+
case "resolved": return {
|
|
1256
|
+
status: "resolved",
|
|
1257
|
+
name: engine.input,
|
|
1258
|
+
data: engine.data
|
|
1259
|
+
};
|
|
1260
|
+
case "error": return {
|
|
1261
|
+
status: "error",
|
|
1262
|
+
name: engine.input,
|
|
1263
|
+
error: engine.error,
|
|
1264
|
+
retry: engine.retry
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
//#endregion
|
|
1270
|
+
//#region src/hooks/nameResolution/useResolveAddress.ts
|
|
1271
|
+
/**
|
|
1272
|
+
* Reverse-resolve an address to a name. Soft-reads the active runtime from
|
|
1273
|
+
* `WalletStateContext` (never throws when no provider is mounted — degrades to
|
|
1274
|
+
* idle / UNSUPPORTED_NETWORK) and calls `runtime.nameResolution?.resolveAddress`.
|
|
1275
|
+
* Caches per (network, address) via the owned QueryClient. Not debounced by
|
|
1276
|
+
* default — addresses are pasted, not typed char-by-char.
|
|
1277
|
+
*
|
|
1278
|
+
* Applies no client-side address-shape check (INV-32): resolution is attempted on
|
|
1279
|
+
* any non-empty input and malformed addresses are rejected via the adapter's typed
|
|
1280
|
+
* error union. Address-shape validation is SF-3's concern.
|
|
1281
|
+
*
|
|
1282
|
+
* @param address - The address to reverse-resolve. `null` / empty yields
|
|
1283
|
+
* `status: 'idle'`.
|
|
1284
|
+
* @param options - `debounceMs` / `enabled` overrides.
|
|
1285
|
+
* @returns The current {@link UseResolveAddressResult}.
|
|
1286
|
+
*/
|
|
1287
|
+
function useResolveAddress(address, options) {
|
|
1288
|
+
const { config } = useNameResolutionContext();
|
|
1289
|
+
return toAddressResult(useResolutionEngine({
|
|
1290
|
+
input: address,
|
|
1291
|
+
namespace: "addr",
|
|
1292
|
+
debounceMs: options?.debounceMs ?? config.reverseDebounceMs,
|
|
1293
|
+
enabled: options?.enabled ?? true,
|
|
1294
|
+
shouldAttempt: () => true,
|
|
1295
|
+
getMethod: (cap) => cap.resolveAddress
|
|
1296
|
+
}));
|
|
1297
|
+
}
|
|
1298
|
+
/**
|
|
1299
|
+
* Remap the engine's generic `input` to `address` (INV-24). A `debouncing` arm —
|
|
1300
|
+
* only reachable when a caller passes a non-zero reverse `debounceMs` — collapses
|
|
1301
|
+
* to `loading` so {@link UseResolveAddressResult} keeps no `debouncing` variant.
|
|
1302
|
+
*/
|
|
1303
|
+
function toAddressResult(engine) {
|
|
1304
|
+
switch (engine.status) {
|
|
1305
|
+
case "idle": return { status: "idle" };
|
|
1306
|
+
case "debouncing":
|
|
1307
|
+
case "loading": return {
|
|
1308
|
+
status: "loading",
|
|
1309
|
+
address: engine.input
|
|
1310
|
+
};
|
|
1311
|
+
case "resolved": return {
|
|
1312
|
+
status: "resolved",
|
|
1313
|
+
address: engine.input,
|
|
1314
|
+
data: engine.data
|
|
1315
|
+
};
|
|
1316
|
+
case "error": return {
|
|
1317
|
+
status: "error",
|
|
1318
|
+
address: engine.input,
|
|
1319
|
+
error: engine.error,
|
|
1320
|
+
retry: engine.retry
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
//#endregion
|
|
1326
|
+
//#region src/hooks/nameResolution/NameResolutionProvider.tsx
|
|
1327
|
+
/**
|
|
1328
|
+
* Optional provider that overrides resolution config and/or the QueryClient for
|
|
1329
|
+
* its subtree. It does NOT render a `QueryClientProvider`: the client is passed
|
|
1330
|
+
* explicitly to `useQuery` (INV-48), so no ambient react-query provider is needed.
|
|
1331
|
+
*
|
|
1332
|
+
* Config is merged field-by-field over {@link DEFAULT_CONFIG} (INV-30), and the
|
|
1333
|
+
* context value is memoized so a Provider re-render does not cascade to every hook
|
|
1334
|
+
* (INV-38).
|
|
1335
|
+
*/
|
|
1336
|
+
function NameResolutionProvider({ children, config, queryClient }) {
|
|
1337
|
+
const value = (0, react.useMemo)(() => ({
|
|
1338
|
+
queryClient: queryClient ?? getDefaultResolutionQueryClient(),
|
|
1339
|
+
config: {
|
|
1340
|
+
...DEFAULT_CONFIG,
|
|
1341
|
+
...config
|
|
1342
|
+
}
|
|
1343
|
+
}), [queryClient, config]);
|
|
1344
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NameResolutionContext.Provider, {
|
|
1345
|
+
value,
|
|
1346
|
+
children
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
//#endregion
|
|
1351
|
+
//#region src/hooks/nameResolution/useRuntimeNameResolver.ts
|
|
1352
|
+
/**
|
|
1353
|
+
* Stable empty resolver returned when no runtime / capability is available.
|
|
1354
|
+
* No methods ⇒ the consuming field treats a typed name as unsupported and
|
|
1355
|
+
* surfaces `UNSUPPORTED_NETWORK` (INV-119) while hex behavior stays legacy.
|
|
1356
|
+
*/
|
|
1357
|
+
const EMPTY_RESOLVER = {};
|
|
1358
|
+
/**
|
|
1359
|
+
* Project the active runtime's `NameResolutionCapability` into the injected
|
|
1360
|
+
* {@link NameResolver} seam consumed by `@openzeppelin/ui-components` (SF-3, D3).
|
|
1361
|
+
* Mounted ambiently by the renderer:
|
|
1362
|
+
*
|
|
1363
|
+
* ```tsx
|
|
1364
|
+
* <NameResolverProvider {...useRuntimeNameResolver()}>{form}</NameResolverProvider>
|
|
1365
|
+
* ```
|
|
1366
|
+
*
|
|
1367
|
+
* Each imperative call is backed by SF-2's **owned** resolution `QueryClient`
|
|
1368
|
+
* via `fetchQuery`, reusing SF-2's exact query-key convention
|
|
1369
|
+
* (`buildResolutionKey('name', networkId, normalizedName)`) — so a name
|
|
1370
|
+
* resolved through the field and the same name resolved by a bare
|
|
1371
|
+
* `useResolveName` hit the SAME cache entry: shared cache, dedupe, and
|
|
1372
|
+
* out-of-order safety, never a parallel cache (INV-119).
|
|
1373
|
+
*
|
|
1374
|
+
* Degradation, in order (all method-omission, never a throw):
|
|
1375
|
+
* - No `WalletStateProvider` mounted → empty resolver. The context is read
|
|
1376
|
+
* directly (not via `useWalletState()`, which throws) so the ambient
|
|
1377
|
+
* renderer mount stays safe for wallet-less usage.
|
|
1378
|
+
* - No active runtime, or runtime without the capability → empty resolver.
|
|
1379
|
+
* - Capability without `resolveName` → `isValidName` only; forward unsupported.
|
|
1380
|
+
*
|
|
1381
|
+
* When the capability is present but the network itself is unsupported, the
|
|
1382
|
+
* adapter's `resolveName` resolves `{ ok: false, error: UNSUPPORTED_NETWORK }`
|
|
1383
|
+
* and that result passes through unchanged.
|
|
1384
|
+
*
|
|
1385
|
+
* @returns A referentially stable {@link NameResolver} for the active runtime.
|
|
1386
|
+
*/
|
|
1387
|
+
function useRuntimeNameResolver() {
|
|
1388
|
+
const walletState = (0, react.useContext)(WalletStateContext);
|
|
1389
|
+
const { queryClient, config } = useNameResolutionContext();
|
|
1390
|
+
const capability = walletState?.activeRuntime?.nameResolution;
|
|
1391
|
+
const networkId = walletState?.activeNetworkId ?? "";
|
|
1392
|
+
return (0, react.useMemo)(() => {
|
|
1393
|
+
if (!capability) return EMPTY_RESOLVER;
|
|
1394
|
+
const resolveNameMethod = capability.resolveName;
|
|
1395
|
+
return {
|
|
1396
|
+
isValidName: (name) => capability.isValidName(name),
|
|
1397
|
+
resolveName: resolveNameMethod ? async (name) => {
|
|
1398
|
+
const normalized = name.trim().toLowerCase();
|
|
1399
|
+
try {
|
|
1400
|
+
return {
|
|
1401
|
+
ok: true,
|
|
1402
|
+
value: await queryClient.fetchQuery({
|
|
1403
|
+
queryKey: buildResolutionKey("name", networkId, normalized),
|
|
1404
|
+
queryFn: async () => {
|
|
1405
|
+
let result;
|
|
1406
|
+
try {
|
|
1407
|
+
result = await resolveNameMethod(normalized);
|
|
1408
|
+
} catch (cause) {
|
|
1409
|
+
throw new ResolutionQueryError({
|
|
1410
|
+
code: "ADAPTER_ERROR",
|
|
1411
|
+
message: cause instanceof Error ? cause.message : String(cause),
|
|
1412
|
+
cause
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
if (!result.ok) throw new ResolutionQueryError(result.error);
|
|
1416
|
+
return result.value;
|
|
1417
|
+
},
|
|
1418
|
+
staleTime: config.staleTimeMs,
|
|
1419
|
+
gcTime: config.gcTimeMs,
|
|
1420
|
+
retry: (failureCount, error) => (error instanceof ResolutionQueryError ? isTransientError(error.resolutionError) : true) && failureCount < config.transientRetryCount
|
|
1421
|
+
})
|
|
1422
|
+
};
|
|
1423
|
+
} catch (error) {
|
|
1424
|
+
return {
|
|
1425
|
+
ok: false,
|
|
1426
|
+
error: toNameResolutionError(error)
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
} : void 0
|
|
1430
|
+
};
|
|
1431
|
+
}, [
|
|
1432
|
+
capability,
|
|
1433
|
+
queryClient,
|
|
1434
|
+
config,
|
|
1435
|
+
networkId
|
|
1436
|
+
]);
|
|
1437
|
+
}
|
|
1438
|
+
|
|
908
1439
|
//#endregion
|
|
909
1440
|
//#region src/components/WalletConnectionUI.tsx
|
|
910
1441
|
/**
|
|
@@ -1114,6 +1645,9 @@ const NetworkSwitchManager = ({ wallet, networkCatalog, targetNetworkId, onNetwo
|
|
|
1114
1645
|
//#endregion
|
|
1115
1646
|
exports.AnalyticsContext = AnalyticsContext;
|
|
1116
1647
|
exports.AnalyticsProvider = AnalyticsProvider;
|
|
1648
|
+
exports.DEFAULT_CONFIG = DEFAULT_CONFIG;
|
|
1649
|
+
exports.NameResolutionContext = NameResolutionContext;
|
|
1650
|
+
exports.NameResolutionProvider = NameResolutionProvider;
|
|
1117
1651
|
exports.NetworkSwitchManager = NetworkSwitchManager;
|
|
1118
1652
|
exports.RuntimeContext = RuntimeContext;
|
|
1119
1653
|
exports.RuntimeProvider = RuntimeProvider;
|
|
@@ -1128,7 +1662,11 @@ exports.useDerivedChainInfo = useDerivedChainInfo;
|
|
|
1128
1662
|
exports.useDerivedConnectStatus = useDerivedConnectStatus;
|
|
1129
1663
|
exports.useDerivedDisconnect = useDerivedDisconnect;
|
|
1130
1664
|
exports.useDerivedSwitchChainStatus = useDerivedSwitchChainStatus;
|
|
1665
|
+
exports.useNameResolutionContext = useNameResolutionContext;
|
|
1666
|
+
exports.useResolveAddress = useResolveAddress;
|
|
1667
|
+
exports.useResolveName = useResolveName;
|
|
1131
1668
|
exports.useRuntimeContext = useRuntimeContext;
|
|
1669
|
+
exports.useRuntimeNameResolver = useRuntimeNameResolver;
|
|
1132
1670
|
exports.useWalletComponents = useWalletComponents;
|
|
1133
1671
|
exports.useWalletReconnectionHandler = useWalletReconnectionHandler;
|
|
1134
1672
|
exports.useWalletState = useWalletState;
|