|
1 |
| -import ow from "ow" |
2 |
| - |
3 | 1 | export function validateInit(init) {
|
4 |
| - ow( |
5 |
| - init, |
6 |
| - "Initialization Options", |
7 |
| - ow.object.exactShape({ |
8 |
| - networkId: ow.number, |
9 |
| - dappId: ow.string, |
10 |
| - subscriptions: ow.optional.object.exactShape({ |
11 |
| - address: ow.optional.function, |
12 |
| - network: ow.optional.function, |
13 |
| - balance: ow.optional.function, |
14 |
| - wallet: ow.optional.function |
15 |
| - }), |
16 |
| - modules: ow.object.exactShape({ |
17 |
| - walletSelect: ow.object.exactShape({ |
18 |
| - heading: ow.string, |
19 |
| - description: ow.string, |
20 |
| - wallets: ow.object.exactShape({ |
21 |
| - mobile: ow.optional.array.nonEmpty.ofType( |
22 |
| - ow.object.exactShape({ |
23 |
| - name: ow.string, |
24 |
| - iconSrc: ow.optional.string, |
25 |
| - iconSrcSet: ow.optional.string, |
26 |
| - svg: ow.optional.string, |
27 |
| - wallet: ow.function, |
28 |
| - link: ow.optional.string, |
29 |
| - installMessage: ow.optional.function, |
30 |
| - preferred: ow.optional.boolean |
31 |
| - }) |
32 |
| - ), |
33 |
| - desktop: ow.optional.array.nonEmpty.ofType( |
34 |
| - ow.object.exactShape({ |
35 |
| - name: ow.string, |
36 |
| - iconSrc: ow.optional.string, |
37 |
| - iconSrcSet: ow.optional.string, |
38 |
| - svg: ow.optional.string, |
39 |
| - wallet: ow.function, |
40 |
| - link: ow.optional.string, |
41 |
| - installMessage: ow.optional.function, |
42 |
| - preferred: ow.optional.boolean |
43 |
| - }) |
44 |
| - ) |
45 |
| - }) |
46 |
| - }), |
47 |
| - walletReady: ow.array.nonEmpty.ofType(ow.function) |
48 |
| - }) |
| 2 | + if (!init || typeof init !== "object") { |
| 3 | + throw new Error( |
| 4 | + `"onboard" must be called with an initialization object, received: ${init}` |
| 5 | + ) |
| 6 | + } |
| 7 | + |
| 8 | + const { networkId, dappId, subscriptions, modules } = init |
| 9 | + |
| 10 | + if (!dappId || typeof dappId !== "string") { |
| 11 | + throw new Error(`"dappId" must be of type string, received: ${dappId}`) |
| 12 | + } |
| 13 | + |
| 14 | + if (!networkId || typeof networkId !== "number") { |
| 15 | + throw new Error( |
| 16 | + `"networkId" must be of type number, received: ${networkId}` |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + validateSubscriptions(subscriptions) |
| 21 | + validateModules(modules) |
| 22 | +} |
| 23 | + |
| 24 | +function validateSubscriptions(subscriptions) { |
| 25 | + if (subscriptions) { |
| 26 | + if (typeof subscriptions !== "object") { |
| 27 | + throw new Error( |
| 28 | + `"subscriptions" must be of type object, received: ${subscriptions}` |
| 29 | + ) |
| 30 | + } |
| 31 | + |
| 32 | + Object.keys(subscriptions).forEach(key => { |
| 33 | + if (!validSubscriptionKey(subscriptions[key])) { |
| 34 | + throw new Error( |
| 35 | + `${key} is not a valid subscription listener, received: ${key}` |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + if (subscriptions[key] !== "function") { |
| 40 | + throw new Error( |
| 41 | + `subscription listener must be a function, received: ${subscriptions[key]}` |
| 42 | + ) |
| 43 | + } |
49 | 44 | })
|
50 |
| - ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function validateModules(modules) { |
| 49 | + if (!modules || typeof modules !== "object") { |
| 50 | + throw new Error( |
| 51 | + `"onboard" must be called with a modules object, received: ${modules}` |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + const { walletSelect, walletReady } = modules |
| 56 | + |
| 57 | + validateWalletSelect(walletSelect) |
| 58 | + validateWalletReady(walletReady) |
| 59 | +} |
| 60 | + |
| 61 | +function validateWalletSelect(walletSelect) { |
| 62 | + if (!walletSelect || typeof walletSelect !== "object") { |
| 63 | + throw new Error( |
| 64 | + `"modules" must have a "walletSelect" object, received: ${walletSelect}` |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + const { heading, description, wallets } = walletSelect |
| 69 | + |
| 70 | + if (!heading || typeof heading !== "string") { |
| 71 | + throw new Error( |
| 72 | + `"walletSelect" requires a "heading" parameter of type string, received: ${heading}` |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + if (!description || typeof description !== "string") { |
| 77 | + throw new Error( |
| 78 | + `"walletSelect" requires a "description" parameter of type string, received: ${description}` |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + if (!wallets || typeof wallets !== "object") { |
| 83 | + throw new Error( |
| 84 | + `"walletSelect" must have a "wallets" object, received: ${wallets}` |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + const { mobile, desktop } = wallets |
| 89 | + |
| 90 | + if (mobile) { |
| 91 | + if (!Array.isArray(mobile) || mobile.length < 1) { |
| 92 | + throw new Error( |
| 93 | + `"mobile" must be an array of at least one wallet object, received: ${mobile}` |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + mobile.forEach(module => { |
| 98 | + validateWalletModule(module) |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + if (desktop) { |
| 103 | + if (!Array.isArray(desktop) || desktop.length < 1) { |
| 104 | + throw new Error( |
| 105 | + `"desktop" must be an array of at least one wallet object, received: ${desktop}` |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + desktop.forEach(module => { |
| 110 | + validateWalletModule(module) |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +function validateWalletModule(module) { |
| 116 | + const { |
| 117 | + name, |
| 118 | + iconSrc, |
| 119 | + iconSrcSet, |
| 120 | + svg, |
| 121 | + wallet, |
| 122 | + link, |
| 123 | + installMessage, |
| 124 | + preferred |
| 125 | + } = module |
| 126 | + |
| 127 | + if (!name || typeof name !== "string") { |
| 128 | + throw new Error( |
| 129 | + `wallet module must have a name property of type string, received: ${name}` |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + if (!wallet || typeof wallet !== "string") { |
| 134 | + throw new Error( |
| 135 | + `wallet module must have a "wallet" property of type string, received: ${wallet}` |
| 136 | + ) |
| 137 | + } |
| 138 | + |
| 139 | + if (iconSrc && typeof iconSrc !== "string") { |
| 140 | + throw new Error(`"iconSrc" must be of type string, received: ${iconSrc}`) |
| 141 | + } |
| 142 | + |
| 143 | + if (iconSrcSet && typeof iconSrcSet !== "string") { |
| 144 | + throw new Error( |
| 145 | + `"iconSrcSet" must be of type string, received: ${iconSrcSet}` |
| 146 | + ) |
| 147 | + } |
| 148 | + |
| 149 | + if (svg && typeof svg !== "string") { |
| 150 | + throw new Error(`"svg" must be of type string, received: ${svg}`) |
| 151 | + } |
| 152 | + |
| 153 | + if (link && typeof link !== "string") { |
| 154 | + throw new Error(`"link" must be of type string, received: ${link}`) |
| 155 | + } |
| 156 | + |
| 157 | + if (installMessage && typeof installMessage !== "function") { |
| 158 | + throw new Error( |
| 159 | + `"installMessage" must be of type function, received: ${installMessage}` |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + if (preferred && typeof preferred !== "boolean") { |
| 164 | + throw new Error( |
| 165 | + `"preferred" must be of type boolean, received: ${preferred}` |
| 166 | + ) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +function validateWalletReady(walletReady) { |
| 171 | + if (!walletReady || typeof walletReady !== "object") { |
| 172 | + throw new Error( |
| 173 | + `"modules" must have a "walletReady" object, received: ${walletReady}` |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + if (!Array.isArray(walletReady) || walletReady.length < 1) { |
| 178 | + throw new Error( |
| 179 | + `"walletReady" must be an array of at least one readiness object, received: ${walletReady}` |
| 180 | + ) |
| 181 | + } |
| 182 | + |
| 183 | + walletReady.forEach(module => { |
| 184 | + if (typeof module !== "function") { |
| 185 | + throw new Error( |
| 186 | + `"walletReady" module must be of type function, received: ${module}` |
| 187 | + ) |
| 188 | + } |
| 189 | + }) |
| 190 | +} |
| 191 | + |
| 192 | +function validSubscriptionKey(key) { |
| 193 | + switch (key) { |
| 194 | + case "address": |
| 195 | + case "network": |
| 196 | + case "balance": |
| 197 | + case "wallet": |
| 198 | + return true |
| 199 | + default: |
| 200 | + return false |
| 201 | + } |
51 | 202 | }
|
52 | 203 |
|
53 | 204 | export function validateConfig(configuration) {
|
|
0 commit comments