@mblaney/holster-browser 1.0.0 → 1.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 CHANGED
@@ -23,6 +23,7 @@ import {
23
23
  EditCache,
24
24
  SearchAppBar,
25
25
  registerServiceWorker,
26
+ useAccountSync,
26
27
  } from "@mblaney/holster-browser"
27
28
  ```
28
29
 
@@ -77,3 +78,13 @@ Displays available login codes for sharing with new users. Returns null when no
77
78
  #### EditCache
78
79
 
79
80
  Displays audio and video cache contents with options to remove individual items or clear a cache entirely.
81
+
82
+ #### useAccountSync
83
+
84
+ A hook that listens to the host accounts list and keeps the user's contacts in sync. When a contact's public key changes it re-shares any encrypted data under the new key, and adds new contacts automatically when they were referred by the current user.
85
+
86
+ ```jsx
87
+ const accountsReady = useAccountSync(holster, user, host, code)
88
+ ```
89
+
90
+ Returns a boolean that becomes `true` once the accounts list has been received for the first time.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mblaney/holster-browser",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "React components for holster applications",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,110 @@
1
+ import {useEffect, useState} from "react"
2
+
3
+ export function useAccountSync(holster, user, host, code) {
4
+ const [accountsReady, setAccountsReady] = useState(false)
5
+
6
+ useEffect(() => {
7
+ if (!host || !user.is) return
8
+
9
+ const updateAccounts = async accounts => {
10
+ if (!accounts) return
11
+
12
+ setAccountsReady(true)
13
+
14
+ // Check accounts against the users list of contacts.
15
+ const c = await new Promise(res => {
16
+ user.get("public").next("contacts", res)
17
+ })
18
+ if (!c) return
19
+
20
+ for (const [accountCode, account] of Object.entries(accounts)) {
21
+ if (!account) continue
22
+
23
+ // Only include properties that are present in the account
24
+ let check = {}
25
+ if (account.pub !== undefined) check.pub = account.pub
26
+ if (account.username !== undefined) check.username = account.username
27
+ if (account.name !== undefined) check.name = account.name
28
+ if (account.ref !== undefined) check.ref = account.ref
29
+ if (account.host !== undefined) check.host = account.host
30
+
31
+ let found = false
32
+ for (const [contactCode, contact] of Object.entries(c)) {
33
+ if (contactCode !== accountCode) continue
34
+
35
+ found = true
36
+ // The account we're checking hasn't changed.
37
+ if (contact.pub === check.pub) break
38
+
39
+ // If the public key has changed for this contact then store their
40
+ // new account details and re-share encrypted data with them to
41
+ // help restore their account.
42
+ const err = await new Promise(res => {
43
+ user
44
+ .get("public")
45
+ .next("contacts")
46
+ .next(contactCode)
47
+ .put(check, res)
48
+ })
49
+ if (err) console.error(err)
50
+
51
+ const oldEpub = await new Promise(res => {
52
+ user.get([contact.pub, "epub"], res)
53
+ })
54
+ if (!oldEpub) {
55
+ console.error("User not found for old public key")
56
+ break
57
+ }
58
+
59
+ const epub = await new Promise(res => {
60
+ user.get([check.pub, "epub"], res)
61
+ })
62
+ if (!epub) {
63
+ console.error("User not found for new public key")
64
+ break
65
+ }
66
+
67
+ const shared = await new Promise(res => {
68
+ user.get("shared").next(contactCode, res)
69
+ })
70
+ if (!shared) break
71
+
72
+ const oldSecret = await holster.SEA.secret(oldEpub, user.is)
73
+ const secret = await holster.SEA.secret(epub, user.is)
74
+ const update = {}
75
+ for (const [key, oldEnc] of Object.entries(shared)) {
76
+ if (!key) continue
77
+
78
+ const data = await holster.SEA.decrypt(oldEnc, oldSecret)
79
+ const enc = await holster.SEA.encrypt(data, secret)
80
+ update[key] = enc
81
+ }
82
+ if (Object.keys(update).length !== 0) {
83
+ const err = await new Promise(res => {
84
+ user.get("shared").next(contactCode).put(update, res)
85
+ })
86
+ if (err) console.error(err)
87
+ }
88
+ }
89
+ // Add the new contact if we referred them.
90
+ if (!found && account.ref === code) {
91
+ user
92
+ .get("public")
93
+ .next("contacts")
94
+ .next(accountCode)
95
+ .put(check, err => {
96
+ if (err) console.error(err)
97
+ })
98
+ }
99
+ }
100
+ }
101
+
102
+ user.get([host, "accounts"]).on(updateAccounts, true)
103
+
104
+ return () => {
105
+ user.get([host, "accounts"]).off(updateAccounts)
106
+ }
107
+ }, [host, code])
108
+
109
+ return accountsReady
110
+ }
package/src/index.js CHANGED
@@ -9,3 +9,4 @@ export {default as Settings} from "./components/Settings.jsx"
9
9
  export {default as UpdatePassword} from "./components/UpdatePassword.jsx"
10
10
  export {default as ValidateEmail} from "./components/ValidateEmail.jsx"
11
11
  export {register as registerServiceWorker} from "./utils/serviceWorkerRegistration.js"
12
+ export {useAccountSync} from "./hooks/useAccountSync.js"