@w3ux/react-connect-kit 1.2.1 → 1.3.0-alpha.2
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/ExtensionAccountsProvider/Extensions.js +75 -167
- package/ExtensionAccountsProvider/Extensions.js.map +1 -1
- package/ExtensionAccountsProvider/defaults.js +7 -5
- package/ExtensionAccountsProvider/defaults.js.map +1 -1
- package/ExtensionAccountsProvider/index.js +139 -256
- package/ExtensionAccountsProvider/index.js.map +1 -1
- package/ExtensionAccountsProvider/snap.js +52 -121
- package/ExtensionAccountsProvider/snap.js.map +1 -1
- package/ExtensionAccountsProvider/types.js +2 -0
- package/ExtensionAccountsProvider/types.js.map +1 -1
- package/ExtensionAccountsProvider/useImportExtension.js +24 -44
- package/ExtensionAccountsProvider/useImportExtension.js.map +1 -1
- package/ExtensionAccountsProvider/utils.js +14 -17
- package/ExtensionAccountsProvider/utils.js.map +1 -1
- package/ExtensionsProvider/defaults.js +8 -6
- package/ExtensionsProvider/defaults.js.map +1 -1
- package/ExtensionsProvider/index.js +56 -115
- package/ExtensionsProvider/index.js.map +1 -1
- package/ExtensionsProvider/types.js +2 -0
- package/ExtensionsProvider/types.js.map +1 -1
- package/ExtensionsProvider/utils.js +21 -73
- package/ExtensionsProvider/utils.js.map +1 -1
- package/LedgerAccountsProvider/defaults.js +9 -7
- package/LedgerAccountsProvider/defaults.js.map +1 -1
- package/LedgerAccountsProvider/index.js +39 -68
- package/LedgerAccountsProvider/index.js.map +1 -1
- package/LedgerAccountsProvider/types.js +2 -0
- package/LedgerAccountsProvider/types.js.map +1 -1
- package/LedgerAccountsProvider/utils.js +16 -25
- package/LedgerAccountsProvider/utils.js.map +1 -1
- package/VaultAccountsProvider/defaults.js +9 -7
- package/VaultAccountsProvider/defaults.js.map +1 -1
- package/VaultAccountsProvider/index.js +40 -66
- package/VaultAccountsProvider/index.js.map +1 -1
- package/VaultAccountsProvider/types.js +2 -0
- package/VaultAccountsProvider/types.js.map +1 -1
- package/VaultAccountsProvider/utils.js +6 -4
- package/VaultAccountsProvider/utils.js.map +1 -1
- package/index.js +2 -0
- package/index.js.map +1 -1
- package/package.json +5 -5
- package/types.js +2 -0
- package/types.js.map +1 -1
|
@@ -1,50 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
15
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
16
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
17
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
18
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
49
2
|
import { setStateWithRef, withTimeout } from "@w3ux/utils";
|
|
50
3
|
import { useEffect, useRef, useState, createContext, useContext, } from "react";
|
|
@@ -52,78 +5,64 @@ import { defaultExtensionsContext } from "./defaults";
|
|
|
52
5
|
import { polkadotSnapAvailable } from "./utils";
|
|
53
6
|
import extensions from "@w3ux/extension-assets";
|
|
54
7
|
import { web3Enable } from "@polkagate/extension-dapp";
|
|
55
|
-
export
|
|
56
|
-
export
|
|
57
|
-
export
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var handleClearInterval = function (hasInjectedWeb3) {
|
|
8
|
+
export const ExtensionsContext = createContext(defaultExtensionsContext);
|
|
9
|
+
export const useExtensions = () => useContext(ExtensionsContext);
|
|
10
|
+
export const ExtensionsProvider = ({ children, options, }) => {
|
|
11
|
+
const [checkingInjectedWeb3, setCheckingInjectedWeb3] = useState(true);
|
|
12
|
+
const checkingInjectedWeb3Ref = useRef(checkingInjectedWeb3);
|
|
13
|
+
const intervalInitialisedRef = useRef(false);
|
|
14
|
+
const [extensionsStatus, setExtensionsStatus] = useState({});
|
|
15
|
+
const extensionsStatusRef = useRef(extensionsStatus);
|
|
16
|
+
const [chainSafeSnapEnabled] = useState(options?.chainSafeSnapEnabled || false);
|
|
17
|
+
const [polkaGateSnapEnabled] = useState(options?.polkagateSnapEnabled || false);
|
|
18
|
+
let injectedWeb3Interval;
|
|
19
|
+
const injectCounter = useRef(0);
|
|
20
|
+
const handleClearInterval = (hasInjectedWeb3) => {
|
|
69
21
|
clearInterval(injectedWeb3Interval);
|
|
70
22
|
handleSnapInjection(hasInjectedWeb3);
|
|
71
23
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
case 2:
|
|
82
|
-
_a.sent();
|
|
83
|
-
_a.label = 3;
|
|
84
|
-
case 3:
|
|
85
|
-
if (hasInjectedWeb3 || snapAvailable) {
|
|
86
|
-
setStateWithRef(getExtensionsStatus(snapAvailable), setExtensionsStatus, extensionsStatusRef);
|
|
87
|
-
}
|
|
88
|
-
setStateWithRef(false, setCheckingInjectedWeb3, checkingInjectedWeb3Ref);
|
|
89
|
-
return [2 ];
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}); };
|
|
93
|
-
var setExtensionStatus = function (id, status) {
|
|
94
|
-
var _a;
|
|
95
|
-
setStateWithRef(__assign(__assign({}, extensionsStatusRef.current), (_a = {}, _a[id] = status, _a)), setExtensionsStatus, extensionsStatusRef);
|
|
24
|
+
const handleSnapInjection = async (hasInjectedWeb3) => {
|
|
25
|
+
const snapAvailable = (await polkadotSnapAvailable()) && chainSafeSnapEnabled;
|
|
26
|
+
if (polkaGateSnapEnabled) {
|
|
27
|
+
await withTimeout(500, web3Enable("snap_only"));
|
|
28
|
+
}
|
|
29
|
+
if (hasInjectedWeb3 || snapAvailable) {
|
|
30
|
+
setStateWithRef(getExtensionsStatus(snapAvailable), setExtensionsStatus, extensionsStatusRef);
|
|
31
|
+
}
|
|
32
|
+
setStateWithRef(false, setCheckingInjectedWeb3, checkingInjectedWeb3Ref);
|
|
96
33
|
};
|
|
97
|
-
|
|
98
|
-
|
|
34
|
+
const setExtensionStatus = (id, status) => {
|
|
35
|
+
setStateWithRef({
|
|
36
|
+
...extensionsStatusRef.current,
|
|
37
|
+
[id]: status,
|
|
38
|
+
}, setExtensionsStatus, extensionsStatusRef);
|
|
39
|
+
};
|
|
40
|
+
const removeExtensionStatus = (id) => {
|
|
41
|
+
const newExtensionsStatus = { ...extensionsStatusRef.current };
|
|
99
42
|
delete newExtensionsStatus[id];
|
|
100
43
|
setStateWithRef(newExtensionsStatus, setExtensionsStatus, extensionsStatusRef);
|
|
101
44
|
};
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
45
|
+
const getExtensionsStatus = (snapAvailable) => {
|
|
46
|
+
const { injectedWeb3 } = window;
|
|
47
|
+
const newExtensionsStatus = { ...extensionsStatus };
|
|
105
48
|
if (snapAvailable) {
|
|
106
49
|
newExtensionsStatus["metamask-polkadot-snap"] = "installed";
|
|
107
50
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
extensionsAsArray.forEach(
|
|
51
|
+
const extensionsAsArray = Object.entries(extensions).map(([key, value]) => ({
|
|
52
|
+
id: key,
|
|
53
|
+
...value,
|
|
54
|
+
}));
|
|
55
|
+
extensionsAsArray.forEach((e) => {
|
|
113
56
|
if (injectedWeb3[e.id] !== undefined) {
|
|
114
57
|
newExtensionsStatus[e.id] = "installed";
|
|
115
58
|
}
|
|
116
59
|
});
|
|
117
60
|
return newExtensionsStatus;
|
|
118
61
|
};
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return extensionInstalled(id) && extensionsStatus[id] !== "connected";
|
|
124
|
-
};
|
|
125
|
-
var extensionHasFeature = function (id, feature) {
|
|
126
|
-
var features = extensions[id].features;
|
|
62
|
+
const extensionInstalled = (id) => extensionsStatus[id] !== undefined;
|
|
63
|
+
const extensionCanConnect = (id) => extensionInstalled(id) && extensionsStatus[id] !== "connected";
|
|
64
|
+
const extensionHasFeature = (id, feature) => {
|
|
65
|
+
const { features } = extensions[id];
|
|
127
66
|
if (features === "*" || features.includes(feature)) {
|
|
128
67
|
return true;
|
|
129
68
|
}
|
|
@@ -131,35 +70,37 @@ export var ExtensionsProvider = function (_a) {
|
|
|
131
70
|
return false;
|
|
132
71
|
}
|
|
133
72
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
useEffect(
|
|
73
|
+
const checkEveryMs = 300;
|
|
74
|
+
const totalChecks = 10;
|
|
75
|
+
useEffect(() => {
|
|
137
76
|
if (!intervalInitialisedRef.current) {
|
|
138
77
|
intervalInitialisedRef.current = true;
|
|
139
|
-
injectedWeb3Interval = setInterval(
|
|
78
|
+
injectedWeb3Interval = setInterval(() => {
|
|
140
79
|
injectCounter.current++;
|
|
141
80
|
if (injectCounter.current === totalChecks) {
|
|
142
81
|
handleClearInterval(false);
|
|
143
82
|
}
|
|
144
83
|
else {
|
|
145
|
-
|
|
84
|
+
const injectedWeb3 = window?.injectedWeb3 || null;
|
|
146
85
|
if (injectedWeb3 !== null) {
|
|
147
86
|
handleClearInterval(true);
|
|
148
87
|
}
|
|
149
88
|
}
|
|
150
89
|
}, checkEveryMs);
|
|
151
90
|
}
|
|
152
|
-
return
|
|
91
|
+
return () => clearInterval(injectedWeb3Interval);
|
|
153
92
|
});
|
|
154
93
|
return (_jsx(ExtensionsContext.Provider, { value: {
|
|
155
94
|
extensionsStatus: extensionsStatusRef.current,
|
|
156
|
-
checkingInjectedWeb3
|
|
157
|
-
setExtensionStatus
|
|
158
|
-
removeExtensionStatus
|
|
159
|
-
extensionInstalled
|
|
160
|
-
extensionCanConnect
|
|
161
|
-
extensionHasFeature
|
|
95
|
+
checkingInjectedWeb3,
|
|
96
|
+
setExtensionStatus,
|
|
97
|
+
removeExtensionStatus,
|
|
98
|
+
extensionInstalled,
|
|
99
|
+
extensionCanConnect,
|
|
100
|
+
extensionHasFeature,
|
|
162
101
|
}, children: children }));
|
|
163
102
|
};
|
|
164
103
|
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
165
105
|
|
|
106
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ExtensionsProvider/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;wCACwC;AAExC,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAEL,SAAS,EACT,MAAM,EACN,QAAQ,EACR,aAAa,EACb,UAAU,GACX,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD,MAAM,CAAC,IAAM,iBAAiB,GAAG,aAAa,CAC5C,wBAAwB,CACzB,CAAC;AAEF,MAAM,CAAC,I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AE,CAAC;YACpC,sBAAsB,CAAC,OAAO,GAAG,IAAI,CAAC;YAEtC,oBAAoB,GAAG,WAAW,CAAC;gBACjC,aAAa,CAAC,OAAO,EAAE,CAAC;gBAExB,IAAI,aAAa,CAAC,OAAO,KAAK,WAAW,EAAE,CAAC;oBAC1C,mBAAmB,CAAC,KAAK,CAAC,CAAC;gBAC7B,CAAC;qBAAM,CAAC;oBACN,4BAA4B;oBAC5B,IAAM,YAAY,GAAG,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,YAAY,KAAI,IAAI,CAAC;oBAClD,IAAI,YAAY,KAAK,IAAI,EAAE,CAAC;wBAC1B,mBAAmB,CAAC,IAAI,CAAC,CAAC;oBAC5B,CAAC;gBACH,CAAC;YACH,CAAC,EAAE,YAAY,CAAC,CAAC;QACnB,CAAC;QAED,OAAO,cAAM,OAAA,aAAa,CAAC,oBAAoB,CAAC,EAAnC,CAAmC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,OAAO,CACL,KAAC,iBAAiB,CAAC,QAAQ,IACzB,KAAK,EAAE;YACL,gBAAgB,EAAE,mBAAmB,CAAC,OAAO;YAC7C,oBAAoB,sBAAA;YACpB,kBAAkB,oBAAA;YAClB,qBAAqB,uBAAA;YACrB,kBAAkB,oBAAA;YAClB,mBAAmB,qBAAA;YACnB,mBAAmB,qBAAA;SACpB,YAEA,QAAQ,GACkB,CAC9B,CAAC;AACJ,CAAC,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { setStateWithRef, withTimeout } from \"@w3ux/utils\";\nimport {\n ReactNode,\n useEffect,\n useRef,\n useState,\n createContext,\n useContext,\n} from \"react\";\nimport type { ExtensionStatus, ExtensionsContextInterface } from \"./types\";\nimport { defaultExtensionsContext } from \"./defaults\";\nimport { polkadotSnapAvailable } from \"./utils\";\nimport extensions from \"@w3ux/extension-assets\";\nimport { web3Enable } from \"@polkagate/extension-dapp\";\n\nexport const ExtensionsContext = createContext<ExtensionsContextInterface>(\n defaultExtensionsContext\n);\n\nexport const useExtensions = () => useContext(ExtensionsContext);\n\nexport const ExtensionsProvider = ({\n children,\n options,\n}: {\n children: ReactNode;\n options?: {\n chainSafeSnapEnabled?: boolean;\n polkagateSnapEnabled?: boolean;\n };\n}) => {\n // Store whether initial `injectedWeb3` checking is underway.\n //\n // Injecting `injectedWeb3` is an asynchronous process, so we need to check for its existence for\n // a period of time.\n const [checkingInjectedWeb3, setCheckingInjectedWeb3] =\n useState<boolean>(true);\n const checkingInjectedWeb3Ref = useRef(checkingInjectedWeb3);\n\n // Store whether injected interval has been initialised.\n const intervalInitialisedRef = useRef<boolean>(false);\n\n // Store each extension's status in state.\n const [extensionsStatus, setExtensionsStatus] = useState<\n Record<string, ExtensionStatus>\n >({});\n const extensionsStatusRef = useRef(extensionsStatus);\n\n // Store whether Metamask Snaps are enabled.\n const [chainSafeSnapEnabled] = useState<boolean>(\n options?.chainSafeSnapEnabled || false\n );\n\n // Store whether Metamask Snaps are enabled.\n const [polkaGateSnapEnabled] = useState<boolean>(\n options?.polkagateSnapEnabled || false\n );\n\n // Listen for window.injectedWeb3 with an interval.\n let injectedWeb3Interval: ReturnType<typeof setInterval>;\n const injectCounter = useRef<number>(0);\n\n // Handle completed interval check for `injectedWeb3`.\n //\n // Clear interval and move on to checking for Metamask Polkadot Snap.\n const handleClearInterval = (hasInjectedWeb3: boolean) => {\n clearInterval(injectedWeb3Interval);\n // Check if Metamask Polkadot Snap is available.\n handleSnapInjection(hasInjectedWeb3);\n };\n\n // Handle injecting of `metamask-polkadot-snap` into injectedWeb3 if avaialble, and complete\n // `injectedWeb3` syncing process.\n const handleSnapInjection = async (hasInjectedWeb3: boolean) => {\n // Inject ChainSafe Snap if enabled.\n const snapAvailable =\n (await polkadotSnapAvailable()) && chainSafeSnapEnabled;\n\n // Inject PolkaGate Snap if enabled.\n if (polkaGateSnapEnabled) {\n await withTimeout(500, web3Enable(\"snap_only\"));\n }\n\n if (hasInjectedWeb3 || snapAvailable) {\n setStateWithRef(\n getExtensionsStatus(snapAvailable),\n setExtensionsStatus,\n extensionsStatusRef\n );\n }\n\n setStateWithRef(false, setCheckingInjectedWeb3, checkingInjectedWeb3Ref);\n };\n\n // Setter for an extension status.\n const setExtensionStatus = (id: string, status: ExtensionStatus) => {\n setStateWithRef(\n {\n ...extensionsStatusRef.current,\n [id]: status,\n },\n setExtensionsStatus,\n extensionsStatusRef\n );\n };\n\n // Removes an extension from the `extensionsStatus` state.\n const removeExtensionStatus = (id: string) => {\n const newExtensionsStatus = { ...extensionsStatusRef.current };\n delete newExtensionsStatus[id];\n\n setStateWithRef(\n newExtensionsStatus,\n setExtensionsStatus,\n extensionsStatusRef\n );\n };\n\n // Getter for the currently installed extensions.\n //\n // Loops through the supported extensios and checks if they are present in `injectedWeb3`. Adds\n // `installed` status to the extension if it is present.\n const getExtensionsStatus = (snapAvailable: boolean) => {\n const { injectedWeb3 } = window;\n\n const newExtensionsStatus = { ...extensionsStatus };\n if (snapAvailable) {\n newExtensionsStatus[\"metamask-polkadot-snap\"] = \"installed\";\n }\n\n const extensionsAsArray = Object.entries(extensions).map(\n ([key, value]) => ({\n id: key,\n ...value,\n })\n );\n\n extensionsAsArray.forEach((e) => {\n if (injectedWeb3[e.id] !== undefined) {\n newExtensionsStatus[e.id] = \"installed\";\n }\n });\n\n return newExtensionsStatus;\n };\n\n // Checks if an extension has been installed.\n const extensionInstalled = (id: string): boolean =>\n extensionsStatus[id] !== undefined;\n\n // Checks whether an extension can be connected to.\n const extensionCanConnect = (id: string): boolean =>\n extensionInstalled(id) && extensionsStatus[id] !== \"connected\";\n\n // Checks whether an extension supports a feature.\n const extensionHasFeature = (id: string, feature: string): boolean => {\n const { features } = extensions[id];\n if (features === \"*\" || features.includes(feature)) {\n return true;\n } else {\n return false;\n }\n };\n\n // Check for `injectedWeb3` and Metamask Snap on mount. To trigger interval on soft page\n // refreshes, no empty dependency array is provided to this `useEffect`. Checks for `injectedWeb3`\n // for a total of 3 seconds before giving up.\n //\n // Interval duration.\n const checkEveryMs = 300;\n // Total interval iterations.\n const totalChecks = 10;\n useEffect(() => {\n if (!intervalInitialisedRef.current) {\n intervalInitialisedRef.current = true;\n\n injectedWeb3Interval = setInterval(() => {\n injectCounter.current++;\n\n if (injectCounter.current === totalChecks) {\n handleClearInterval(false);\n } else {\n // `injectedWeb3` is present\n const injectedWeb3 = window?.injectedWeb3 || null;\n if (injectedWeb3 !== null) {\n handleClearInterval(true);\n }\n }\n }, checkEveryMs);\n }\n\n return () => clearInterval(injectedWeb3Interval);\n });\n\n return (\n <ExtensionsContext.Provider\n value={{\n extensionsStatus: extensionsStatusRef.current,\n checkingInjectedWeb3,\n setExtensionStatus,\n removeExtensionStatus,\n extensionInstalled,\n extensionCanConnect,\n extensionHasFeature,\n }}\n >\n {children}\n </ExtensionsContext.Provider>\n );\n};\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/ExtensionsProvider/index.tsx"],"names":[],"mappings":";AAGA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAEL,SAAS,EACT,MAAM,EACN,QAAQ,EACR,aAAa,EACb,UAAU,GACX,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAChD,OAAO,UAAU,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAEvD,MAAM,CAAC,MAAM,iBAAiB,GAAG,aAAa,CAC5C,wBAAwB,CACzB,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAC;AAEjE,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,EACjC,QAAQ,EACR,OAAO,GAOR,EAAE,EAAE;IAKH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AC;IAEH,OAAO,CACL,KAAC,iBAAiB,CAAC,QAAQ,IACzB,KAAK,EAAE;YACL,gBAAgB,EAAE,mBAAmB,CAAC,OAAO;YAC7C,oBAAoB;YACpB,kBAAkB;YAClB,qBAAqB;YACrB,kBAAkB;YAClB,mBAAmB;YACnB,mBAAmB;SACpB,YAEA,QAAQ,GACkB,CAC9B,CAAC;AACJ,CAAC,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { setStateWithRef, withTimeout } from \"@w3ux/utils\";\nimport {\n ReactNode,\n useEffect,\n useRef,\n useState,\n createContext,\n useContext,\n} from \"react\";\nimport type { ExtensionStatus, ExtensionsContextInterface } from \"./types\";\nimport { defaultExtensionsContext } from \"./defaults\";\nimport { polkadotSnapAvailable } from \"./utils\";\nimport extensions from \"@w3ux/extension-assets\";\nimport { web3Enable } from \"@polkagate/extension-dapp\";\n\nexport const ExtensionsContext = createContext<ExtensionsContextInterface>(\n defaultExtensionsContext\n);\n\nexport const useExtensions = () => useContext(ExtensionsContext);\n\nexport const ExtensionsProvider = ({\n children,\n options,\n}: {\n children: ReactNode;\n options?: {\n chainSafeSnapEnabled?: boolean;\n polkagateSnapEnabled?: boolean;\n };\n}) => {\n // Store whether initial `injectedWeb3` checking is underway.\n //\n // Injecting `injectedWeb3` is an asynchronous process, so we need to check for its existence for\n // a period of time.\n const [checkingInjectedWeb3, setCheckingInjectedWeb3] =\n useState<boolean>(true);\n const checkingInjectedWeb3Ref = useRef(checkingInjectedWeb3);\n\n // Store whether injected interval has been initialised.\n const intervalInitialisedRef = useRef<boolean>(false);\n\n // Store each extension's status in state.\n const [extensionsStatus, setExtensionsStatus] = useState<\n Record<string, ExtensionStatus>\n >({});\n const extensionsStatusRef = useRef(extensionsStatus);\n\n // Store whether Metamask Snaps are enabled.\n const [chainSafeSnapEnabled] = useState<boolean>(\n options?.chainSafeSnapEnabled || false\n );\n\n // Store whether Metamask Snaps are enabled.\n const [polkaGateSnapEnabled] = useState<boolean>(\n options?.polkagateSnapEnabled || false\n );\n\n // Listen for window.injectedWeb3 with an interval.\n let injectedWeb3Interval: ReturnType<typeof setInterval>;\n const injectCounter = useRef<number>(0);\n\n // Handle completed interval check for `injectedWeb3`.\n //\n // Clear interval and move on to checking for Metamask Polkadot Snap.\n const handleClearInterval = (hasInjectedWeb3: boolean) => {\n clearInterval(injectedWeb3Interval);\n // Check if Metamask Polkadot Snap is available.\n handleSnapInjection(hasInjectedWeb3);\n };\n\n // Handle injecting of `metamask-polkadot-snap` into injectedWeb3 if avaialble, and complete\n // `injectedWeb3` syncing process.\n const handleSnapInjection = async (hasInjectedWeb3: boolean) => {\n // Inject ChainSafe Snap if enabled.\n const snapAvailable =\n (await polkadotSnapAvailable()) && chainSafeSnapEnabled;\n\n // Inject PolkaGate Snap if enabled.\n if (polkaGateSnapEnabled) {\n await withTimeout(500, web3Enable(\"snap_only\"));\n }\n\n if (hasInjectedWeb3 || snapAvailable) {\n setStateWithRef(\n getExtensionsStatus(snapAvailable),\n setExtensionsStatus,\n extensionsStatusRef\n );\n }\n\n setStateWithRef(false, setCheckingInjectedWeb3, checkingInjectedWeb3Ref);\n };\n\n // Setter for an extension status.\n const setExtensionStatus = (id: string, status: ExtensionStatus) => {\n setStateWithRef(\n {\n ...extensionsStatusRef.current,\n [id]: status,\n },\n setExtensionsStatus,\n extensionsStatusRef\n );\n };\n\n // Removes an extension from the `extensionsStatus` state.\n const removeExtensionStatus = (id: string) => {\n const newExtensionsStatus = { ...extensionsStatusRef.current };\n delete newExtensionsStatus[id];\n\n setStateWithRef(\n newExtensionsStatus,\n setExtensionsStatus,\n extensionsStatusRef\n );\n };\n\n // Getter for the currently installed extensions.\n //\n // Loops through the supported extensios and checks if they are present in `injectedWeb3`. Adds\n // `installed` status to the extension if it is present.\n const getExtensionsStatus = (snapAvailable: boolean) => {\n const { injectedWeb3 } = window;\n\n const newExtensionsStatus = { ...extensionsStatus };\n if (snapAvailable) {\n newExtensionsStatus[\"metamask-polkadot-snap\"] = \"installed\";\n }\n\n const extensionsAsArray = Object.entries(extensions).map(\n ([key, value]) => ({\n id: key,\n ...value,\n })\n );\n\n extensionsAsArray.forEach((e) => {\n if (injectedWeb3[e.id] !== undefined) {\n newExtensionsStatus[e.id] = \"installed\";\n }\n });\n\n return newExtensionsStatus;\n };\n\n // Checks if an extension has been installed.\n const extensionInstalled = (id: string): boolean =>\n extensionsStatus[id] !== undefined;\n\n // Checks whether an extension can be connected to.\n const extensionCanConnect = (id: string): boolean =>\n extensionInstalled(id) && extensionsStatus[id] !== \"connected\";\n\n // Checks whether an extension supports a feature.\n const extensionHasFeature = (id: string, feature: string): boolean => {\n const { features } = extensions[id];\n if (features === \"*\" || features.includes(feature)) {\n return true;\n } else {\n return false;\n }\n };\n\n // Check for `injectedWeb3` and Metamask Snap on mount. To trigger interval on soft page\n // refreshes, no empty dependency array is provided to this `useEffect`. Checks for `injectedWeb3`\n // for a total of 3 seconds before giving up.\n //\n // Interval duration.\n const checkEveryMs = 300;\n // Total interval iterations.\n const totalChecks = 10;\n useEffect(() => {\n if (!intervalInitialisedRef.current) {\n intervalInitialisedRef.current = true;\n\n injectedWeb3Interval = setInterval(() => {\n injectCounter.current++;\n\n if (injectCounter.current === totalChecks) {\n handleClearInterval(false);\n } else {\n // `injectedWeb3` is present\n const injectedWeb3 = window?.injectedWeb3 || null;\n if (injectedWeb3 !== null) {\n handleClearInterval(true);\n }\n }\n }, checkEveryMs);\n }\n\n return () => clearInterval(injectedWeb3Interval);\n });\n\n return (\n <ExtensionsContext.Provider\n value={{\n extensionsStatus: extensionsStatusRef.current,\n checkingInjectedWeb3,\n setExtensionStatus,\n removeExtensionStatus,\n extensionInstalled,\n extensionCanConnect,\n extensionHasFeature,\n }}\n >\n {children}\n </ExtensionsContext.Provider>\n );\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ExtensionsProvider/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/ExtensionsProvider/types.ts"],"names":[],"mappings":"","file":"types.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { FunctionComponent, SVGProps } from \"react\";\nimport { ExternalAccountAddedBy } from \"../types\";\nimport { AnyJson, VoidFn } from \"@w3ux/types\";\n\n// Extensions context interface.\nexport interface ExtensionsContextInterface {\n checkingInjectedWeb3: boolean;\n extensionsStatus: Record<string, ExtensionStatus>;\n setExtensionStatus: (id: string, status: ExtensionStatus) => void;\n removeExtensionStatus: (id: string) => void;\n extensionInstalled: (id: string) => boolean;\n extensionCanConnect: (id: string) => boolean;\n extensionHasFeature: (id: string, feature: string) => boolean;\n}\n\n// Top level required properties the extension must expose via their `injectedWeb3` entry.\nexport interface ExtensionInjected extends ExtensionConfig {\n id: string;\n enable: (n: string) => Promise<ExtensionInterface>;\n}\n\n// Required properties `enable` must provide after resolution.\nexport interface ExtensionInterface {\n accounts: {\n subscribe: (a: (b: ExtensionAccount[]) => void) => VoidFn;\n get: () => Promise<ExtensionAccount[]>;\n };\n provider: AnyJson;\n metadata: AnyJson;\n signer: AnyJson;\n}\n\n// Required properties returned after subscribing to accounts.\nexport interface ExtensionAccount extends ExtensionMetadata {\n address: string;\n meta?: AnyJson;\n name: string;\n signer?: AnyJson;\n}\n\n// Configuration item of an extension.\nexport interface ExtensionConfig {\n id: string;\n title: string;\n icon: FunctionComponent<\n SVGProps<SVGSVGElement> & { title?: string | undefined }\n >;\n url: string;\n}\n\n// Miscellaneous metadata added to an extension.\nexport interface ExtensionMetadata {\n addedBy?: ExternalAccountAddedBy;\n source: string;\n}\n\nexport type ExtensionStatus = \"installed\" | \"not_authenticated\" | \"connected\";\n"]}
|
|
@@ -1,78 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
import { hasMetaMask, } from "@chainsafe/metamask-polkadot-adapter/src/utils";
|
|
2
|
+
import { withTimeout } from "@w3ux/utils";
|
|
3
|
+
const getWalletSnaps = async () => {
|
|
4
|
+
const ethRequest = window?.ethereum?.request ? true : false;
|
|
5
|
+
if (ethRequest) {
|
|
6
|
+
const response = await withTimeout(200, window.ethereum.request({ method: "wallet_getSnaps" }));
|
|
7
|
+
return response;
|
|
8
|
+
}
|
|
9
|
+
return;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
11
|
+
export const polkadotSnapAvailable = async () => {
|
|
12
|
+
if (!hasMetaMask()) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
await getWalletSnaps();
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
return false;
|
|
35
21
|
}
|
|
36
22
|
};
|
|
37
|
-
import { hasMetaMask, } from "@chainsafe/metamask-polkadot-adapter/src/utils";
|
|
38
|
-
import { withTimeout } from "@w3ux/utils";
|
|
39
|
-
var getWalletSnaps = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
40
|
-
var ethRequest, response;
|
|
41
|
-
var _a;
|
|
42
|
-
return __generator(this, function (_b) {
|
|
43
|
-
switch (_b.label) {
|
|
44
|
-
case 0:
|
|
45
|
-
ethRequest = ((_a = window === null || window === void 0 ? void 0 : window.ethereum) === null || _a === void 0 ? void 0 : _a.request) ? true : false;
|
|
46
|
-
if (!ethRequest) return [3 , 2];
|
|
47
|
-
return [4 , withTimeout(200, window.ethereum.request({ method: "wallet_getSnaps" }))];
|
|
48
|
-
case 1:
|
|
49
|
-
response = _b.sent();
|
|
50
|
-
return [2 , response];
|
|
51
|
-
case 2: return [2 ];
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}); };
|
|
55
|
-
export var polkadotSnapAvailable = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
56
|
-
var e_1;
|
|
57
|
-
return __generator(this, function (_a) {
|
|
58
|
-
switch (_a.label) {
|
|
59
|
-
case 0:
|
|
60
|
-
if (!hasMetaMask()) {
|
|
61
|
-
return [2 , false];
|
|
62
|
-
}
|
|
63
|
-
_a.label = 1;
|
|
64
|
-
case 1:
|
|
65
|
-
_a.trys.push([1, 3, , 4]);
|
|
66
|
-
return [4 , getWalletSnaps()];
|
|
67
|
-
case 2:
|
|
68
|
-
_a.sent();
|
|
69
|
-
return [2 , true];
|
|
70
|
-
case 3:
|
|
71
|
-
e_1 = _a.sent();
|
|
72
|
-
return [2 , false];
|
|
73
|
-
case 4: return [2 ];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}); };
|
|
77
23
|
|
|
24
|
+
//# sourceMappingURL=utils.js.map
|
|
78
25
|
|
|
26
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ExtensionsProvider/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/ExtensionsProvider/utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,WAAW,GACZ,MAAM,gDAAgD,CAAC;AAGxD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAwB1C,MAAM,cAAc,GAAG,KAAK,IAA+B,EAAE;IAC3D,MAAM,UAAU,GAAG,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IAC5D,IAAI,UAAU,EAAE,CAAC;QACf,MAAM,QAAQ,GAAG,MAAM,WAAW,CAChC,GAAG,EACH,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,CACvD,CAAC;QACF,OAAO,QAAqC,CAAC;IAC/C,CAAC;IACD,OAAO;AACT,CAAC,CAAC;AAGF,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,IAAsB,EAAE;IAChE,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;QACnB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,MAAM,cAAc,EAAE,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC,CAAC","file":"utils.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport {\n GetSnapsResponse,\n hasMetaMask,\n} from \"@chainsafe/metamask-polkadot-adapter/src/utils\";\nimport { SnapRpcMethodRequest } from \"@chainsafe/metamask-polkadot-types\";\nimport { AnyJson } from \"@w3ux/types\";\nimport { withTimeout } from \"@w3ux/utils\";\n\n// Workaround for current `ethereum` snap types. See\n// https://github.com/ChainSafe/metamask-snap-polkadot/blob/e0f3d4fc0be7366c62211e29d3a276e4fab5669e/packages/adapter/src/types.ts#L40\n// for full type.\ndeclare global {\n interface Window {\n injectedWeb3?: AnyJson;\n ethereum: {\n isMetaMask: boolean;\n send: (\n request: SnapRpcMethodRequest | { method: string; params?: never[] }\n ) => Promise<unknown>;\n on: (eventName: unknown, callback: unknown) => unknown;\n request: <T>(\n request: SnapRpcMethodRequest | { method: string; params?: AnyJson }\n ) => Promise<T>;\n };\n }\n}\n\n// Checks if snaps are supported. Note that other extensions may inject `window.ethereum`, which may\n// break the request. We wrap the request in a timeout to ensure it doesn't hang the extension\n// discovery process.\nconst getWalletSnaps = async (): Promise<GetSnapsResponse> => {\n const ethRequest = window?.ethereum?.request ? true : false;\n if (ethRequest) {\n const response = await withTimeout(\n 200,\n window.ethereum.request({ method: \"wallet_getSnaps\" })\n );\n return response as Promise<GetSnapsResponse>;\n }\n return;\n};\n\n// Determines if Metamask Polkadot Snap is available and supported.\nexport const polkadotSnapAvailable = async (): Promise<boolean> => {\n if (!hasMetaMask()) {\n return false;\n }\n\n try {\n await getWalletSnaps();\n return true;\n } catch (e) {\n return false;\n }\n};\n"]}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
export
|
|
2
|
-
ledgerAccountExists:
|
|
3
|
-
addLedgerAccount:
|
|
4
|
-
removeLedgerAccount:
|
|
5
|
-
renameLedgerAccount:
|
|
6
|
-
getLedgerAccount:
|
|
7
|
-
getLedgerAccounts:
|
|
1
|
+
export const defaultLedgerAccountsContext = {
|
|
2
|
+
ledgerAccountExists: (network, address) => false,
|
|
3
|
+
addLedgerAccount: (network, address, index, callback) => null,
|
|
4
|
+
removeLedgerAccount: (network, address, callback) => { },
|
|
5
|
+
renameLedgerAccount: (network, address, newName) => { },
|
|
6
|
+
getLedgerAccount: (network, address) => null,
|
|
7
|
+
getLedgerAccounts: (network) => [],
|
|
8
8
|
ledgerAccounts: [],
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
//# sourceMappingURL=defaults.js.map
|
|
11
12
|
|
|
13
|
+
//# sourceMappingURL=defaults.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/LedgerAccountsProvider/defaults.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/LedgerAccountsProvider/defaults.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,4BAA4B,GAAmC;IAC1E,mBAAmB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,KAAK;IAChD,gBAAgB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI;IAC7D,mBAAmB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,GAAE,CAAC;IACvD,mBAAmB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,GAAE,CAAC;IACtD,gBAAgB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,IAAI;IAC5C,iBAAiB,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE;IAClC,cAAc,EAAE,EAAE;CACnB,CAAC","file":"defaults.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function, no-unused-vars */\n\nimport { LedgerAccountsContextInterface } from \"./types\";\n\nexport const defaultLedgerAccountsContext: LedgerAccountsContextInterface = {\n ledgerAccountExists: (network, address) => false,\n addLedgerAccount: (network, address, index, callback) => null,\n removeLedgerAccount: (network, address, callback) => {},\n renameLedgerAccount: (network, address, newName) => {},\n getLedgerAccount: (network, address) => null,\n getLedgerAccounts: (network) => [],\n ledgerAccounts: [],\n};\n"]}
|
|
@@ -1,58 +1,29 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
13
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
14
|
-
if (ar || !(i in from)) {
|
|
15
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
16
|
-
ar[i] = from[i];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
20
|
-
};
|
|
21
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
22
2
|
import { createContext, useContext, useRef, useState } from "react";
|
|
23
3
|
import { defaultLedgerAccountsContext } from "./defaults";
|
|
24
4
|
import { getLocalLedgerAccounts, getLocalLedgerAddresses, isLocalLedgerAccount, renameLocalLedgerAddress, } from "./utils";
|
|
25
5
|
import { setStateWithRef } from "@w3ux/utils";
|
|
26
|
-
export
|
|
27
|
-
export
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
var addLedgerAccount = function (network, address, index, callback) {
|
|
38
|
-
var newLedgerAccounts = getLocalLedgerAccounts();
|
|
39
|
-
var ledgerAddress = getLocalLedgerAddresses().find(function (a) {
|
|
40
|
-
return isLocalLedgerAccount(network, a, address);
|
|
41
|
-
});
|
|
6
|
+
export const LedgerAccountsContext = createContext(defaultLedgerAccountsContext);
|
|
7
|
+
export const useLedgerAccounts = () => useContext(LedgerAccountsContext);
|
|
8
|
+
export const LedgerAccountsProvider = ({ children, }) => {
|
|
9
|
+
const [ledgerAccounts, setLedgerAccountsState] = useState(getLocalLedgerAccounts());
|
|
10
|
+
const ledgerAccountsRef = useRef(ledgerAccounts);
|
|
11
|
+
const ledgerAccountExists = (network, address) => !!getLocalLedgerAccounts().find((account) => isLocalLedgerAccount(network, account, address));
|
|
12
|
+
const addLedgerAccount = (network, address, index, callback) => {
|
|
13
|
+
let newLedgerAccounts = getLocalLedgerAccounts();
|
|
14
|
+
const ledgerAddress = getLocalLedgerAddresses().find((a) => isLocalLedgerAccount(network, a, address));
|
|
42
15
|
if (ledgerAddress &&
|
|
43
|
-
!newLedgerAccounts.find(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
address: address,
|
|
48
|
-
network: network,
|
|
16
|
+
!newLedgerAccounts.find((account) => isLocalLedgerAccount(network, account, address))) {
|
|
17
|
+
const newAccount = {
|
|
18
|
+
address,
|
|
19
|
+
network,
|
|
49
20
|
name: ledgerAddress.name,
|
|
50
21
|
source: "ledger",
|
|
51
|
-
index
|
|
22
|
+
index,
|
|
52
23
|
};
|
|
53
|
-
newLedgerAccounts =
|
|
24
|
+
newLedgerAccounts = [...newLedgerAccounts].concat(newAccount);
|
|
54
25
|
localStorage.setItem("ledger_accounts", JSON.stringify(newLedgerAccounts));
|
|
55
|
-
setStateWithRef(newLedgerAccounts.filter(
|
|
26
|
+
setStateWithRef(newLedgerAccounts.filter((account) => account.network === network), setLedgerAccountsState, ledgerAccountsRef);
|
|
56
27
|
if (typeof callback === "function") {
|
|
57
28
|
callback();
|
|
58
29
|
}
|
|
@@ -60,8 +31,8 @@ export var LedgerAccountsProvider = function (_a) {
|
|
|
60
31
|
}
|
|
61
32
|
return null;
|
|
62
33
|
};
|
|
63
|
-
|
|
64
|
-
|
|
34
|
+
const removeLedgerAccount = (network, address, callback) => {
|
|
35
|
+
const newLedgerAccounts = getLocalLedgerAccounts().filter((account) => {
|
|
65
36
|
if (account.address !== address) {
|
|
66
37
|
return true;
|
|
67
38
|
}
|
|
@@ -76,41 +47,41 @@ export var LedgerAccountsProvider = function (_a) {
|
|
|
76
47
|
else {
|
|
77
48
|
localStorage.setItem("ledger_accounts", JSON.stringify(newLedgerAccounts));
|
|
78
49
|
}
|
|
79
|
-
setStateWithRef(newLedgerAccounts.filter(
|
|
50
|
+
setStateWithRef(newLedgerAccounts.filter((account) => account.network === network), setLedgerAccountsState, ledgerAccountsRef);
|
|
80
51
|
if (typeof callback === "function") {
|
|
81
52
|
callback();
|
|
82
53
|
}
|
|
83
54
|
};
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
55
|
+
const renameLedgerAccount = (network, address, newName) => {
|
|
56
|
+
const newLedgerAccounts = getLocalLedgerAccounts().map((account) => isLocalLedgerAccount(network, account, address)
|
|
57
|
+
? {
|
|
58
|
+
...account,
|
|
59
|
+
name: newName,
|
|
60
|
+
}
|
|
61
|
+
: account);
|
|
89
62
|
renameLocalLedgerAddress(address, newName, network);
|
|
90
63
|
localStorage.setItem("ledger_accounts", JSON.stringify(newLedgerAccounts));
|
|
91
|
-
setStateWithRef(newLedgerAccounts.filter(
|
|
64
|
+
setStateWithRef(newLedgerAccounts.filter((account) => account.network === network), setLedgerAccountsState, ledgerAccountsRef);
|
|
92
65
|
};
|
|
93
|
-
|
|
94
|
-
|
|
66
|
+
const getLedgerAccount = (network, address) => {
|
|
67
|
+
const localLedgerAccounts = getLocalLedgerAccounts();
|
|
95
68
|
if (!localLedgerAccounts) {
|
|
96
69
|
return null;
|
|
97
70
|
}
|
|
98
|
-
return (localLedgerAccounts.find(
|
|
99
|
-
return isLocalLedgerAccount(network, account, address);
|
|
100
|
-
}) || null);
|
|
101
|
-
};
|
|
102
|
-
var getLedgerAccounts = function (network) {
|
|
103
|
-
return ledgerAccountsRef.current.filter(function (a) { return a.network === network; });
|
|
71
|
+
return (localLedgerAccounts.find((account) => isLocalLedgerAccount(network, account, address)) || null);
|
|
104
72
|
};
|
|
73
|
+
const getLedgerAccounts = (network) => ledgerAccountsRef.current.filter((a) => a.network === network);
|
|
105
74
|
return (_jsx(LedgerAccountsContext.Provider, { value: {
|
|
106
|
-
ledgerAccountExists
|
|
107
|
-
getLedgerAccount
|
|
108
|
-
addLedgerAccount
|
|
109
|
-
removeLedgerAccount
|
|
110
|
-
renameLedgerAccount
|
|
111
|
-
getLedgerAccounts
|
|
75
|
+
ledgerAccountExists,
|
|
76
|
+
getLedgerAccount,
|
|
77
|
+
addLedgerAccount,
|
|
78
|
+
removeLedgerAccount,
|
|
79
|
+
renameLedgerAccount,
|
|
80
|
+
getLedgerAccounts,
|
|
112
81
|
ledgerAccounts: ledgerAccountsRef.current,
|
|
113
82
|
}, children: children }));
|
|
114
83
|
};
|
|
115
84
|
|
|
85
|
+
//# sourceMappingURL=index.js.map
|
|
116
86
|
|
|
87
|
+
//# sourceMappingURL=index.js.map
|