| … | … | @@ -24,6 +24,7 @@ |
| 24 | 24 | |
| 25 | 25 | import IdentityKey from 'keet-identity-key' |
| 26 | 26 | import { IdentityManager as HyperidentVault } from 'hyperident/lib/identity-manager.js' |
| 27 | import { DeviceLinkingManager } from 'hyperident/lib/device-linking-manager.js' |
| 27 | 28 | import crypto from 'hypercore-crypto' |
| 28 | 29 | import b4a from 'b4a' |
| 29 | 30 | |
| … | … | @@ -147,6 +148,60 @@ export async function importMasterBundle ({ bundle, deviceName, passphrase = nul |
| 147 | 148 | return HyperidentVault.importDeviceBundle(bundle, deviceName, passphrase) |
| 148 | 149 | } |
| 149 | 150 | |
| 151 | /** |
| 152 | * Start sharing the local master vault over the network so another |
| 153 | * machine can join via `joinSharedDevice` below. |
| 154 | * |
| 155 | * Returns the identity public key (hex) the joining device needs. |
| 156 | * Uses hyperident's DeviceLinkingManager — network transport over |
| 157 | * Hyperswarm, same trust model as exportMasterBundle (the bundle is |
| 158 | * sent over the wire; the joining peer ends up with the mnemonic). |
| 159 | * |
| 160 | * Caller is responsible for stopping the share via the returned |
| 161 | * `stop()` callback (or letting the timeout expire). |
| 162 | * |
| 163 | * @param {object} [opts] |
| 164 | * @param {number} [opts.timeoutMinutes=5] |
| 165 | * @returns {Promise<{ |
| 166 | * identityPublicKey: string, |
| 167 | * stop: () => Promise<void> |
| 168 | * }>} |
| 169 | */ |
| 170 | export async function shareMasterVault ({ timeoutMinutes = 5 } = {}) { |
| 171 | const linker = new DeviceLinkingManager() |
| 172 | const identityPublicKey = await linker.startSharing(timeoutMinutes) |
| 173 | return { |
| 174 | identityPublicKey, |
| 175 | stop: async () => { await linker.stopSharing() } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Join a master vault that another machine is currently sharing. |
| 181 | * |
| 182 | * Connects to the sharing peer over Hyperswarm, receives the bundle, |
| 183 | * imports it into the local ~/.hyperident/. Same trust model as |
| 184 | * importMasterBundle — receiver ends up with the mnemonic. |
| 185 | * |
| 186 | * Refuses to run if a local vault already exists. Caller (pgh) is |
| 187 | * responsible for backing up + clearing the existing vault before |
| 188 | * invoking if --force is set. |
| 189 | * |
| 190 | * @param {object} opts |
| 191 | * @param {string} opts.identityPublicKey - hex pubkey returned by |
| 192 | * shareMasterVault on the source |
| 193 | * @param {string} opts.deviceName - name for the local device |
| 194 | * @param {string|null} [opts.passphrase] - if the local vault should be |
| 195 | * encrypted after import |
| 196 | * @returns {Promise<boolean>} success |
| 197 | */ |
| 198 | export async function joinSharedDevice ({ identityPublicKey, deviceName, passphrase = null }) { |
| 199 | if (!identityPublicKey) throw new Error('joinSharedDevice requires identityPublicKey') |
| 200 | if (!deviceName) throw new Error('joinSharedDevice requires deviceName') |
| 201 | const linker = new DeviceLinkingManager() |
| 202 | return linker.joinDevice(identityPublicKey, deviceName, passphrase) |
| 203 | } |
| 204 | |
| 150 | 205 | /** |
| 151 | 206 | * Verify a chain proof resolves to the local hyperident master root. |
| 152 | 207 | * |