Feat: use program.recoverFileSystem instead of doing it manually (#113)

This commit is contained in:
Andrew Vivash 2023-02-03 10:35:06 -08:00 committed by GitHub
parent 0a1c57f393
commit 88725d3d36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 41 deletions

14
package-lock.json generated
View File

@ -11,7 +11,7 @@
"clipboard-copy": "^4.0.1", "clipboard-copy": "^4.0.1",
"qrcode-svg": "^1.1.0", "qrcode-svg": "^1.1.0",
"uint8arrays": "^4.0.2", "uint8arrays": "^4.0.2",
"webnative": "^0.35.1" "webnative": "^0.36.0"
}, },
"devDependencies": { "devDependencies": {
"@playwright/test": "^1.29.2", "@playwright/test": "^1.29.2",
@ -6322,9 +6322,9 @@
} }
}, },
"node_modules/webnative": { "node_modules/webnative": {
"version": "0.35.2", "version": "0.36.0",
"resolved": "https://registry.npmjs.org/webnative/-/webnative-0.35.2.tgz", "resolved": "https://registry.npmjs.org/webnative/-/webnative-0.36.0.tgz",
"integrity": "sha512-2TIK8Amt2M0STz3DFpxSgbujzhHjrwqaDgyTvIghzg6qGW9eHS73KwAC4ErjshFNlOPT7w6z1mVoIB9x3AUSJA==", "integrity": "sha512-gU+a3EvyqcG8yifZW+5ioQcuq/fRX4K3PXDqr2cSYnQhWc3C6DW5r/g+N5uFFL16huCJbn1lg0iSPJAu9El5Ww==",
"dependencies": { "dependencies": {
"@ipld/dag-cbor": "^8.0.0", "@ipld/dag-cbor": "^8.0.0",
"@ipld/dag-pb": "^3.0.1", "@ipld/dag-pb": "^3.0.1",
@ -10971,9 +10971,9 @@
"requires": {} "requires": {}
}, },
"webnative": { "webnative": {
"version": "0.35.2", "version": "0.36.0",
"resolved": "https://registry.npmjs.org/webnative/-/webnative-0.35.2.tgz", "resolved": "https://registry.npmjs.org/webnative/-/webnative-0.36.0.tgz",
"integrity": "sha512-2TIK8Amt2M0STz3DFpxSgbujzhHjrwqaDgyTvIghzg6qGW9eHS73KwAC4ErjshFNlOPT7w6z1mVoIB9x3AUSJA==", "integrity": "sha512-gU+a3EvyqcG8yifZW+5ioQcuq/fRX4K3PXDqr2cSYnQhWc3C6DW5r/g+N5uFFL16huCJbn1lg0iSPJAu9El5Ww==",
"requires": { "requires": {
"@ipld/dag-cbor": "^8.0.0", "@ipld/dag-cbor": "^8.0.0",
"@ipld/dag-pb": "^3.0.1", "@ipld/dag-pb": "^3.0.1",

View File

@ -59,7 +59,7 @@
"clipboard-copy": "^4.0.1", "clipboard-copy": "^4.0.1",
"qrcode-svg": "^1.1.0", "qrcode-svg": "^1.1.0",
"uint8arrays": "^4.0.2", "uint8arrays": "^4.0.2",
"webnative": "^0.35.1" "webnative": "^0.36.0"
}, },
"engines": { "engines": {
"node": ">=16.14" "node": ">=16.14"

View File

@ -1,7 +1,4 @@
<script lang="ts"> <script lang="ts">
import * as RootKey from 'webnative/common/root-key'
import * as UCAN from 'webnative/ucan/index'
import * as DID from 'webnative/did/index'
import * as uint8arrays from 'uint8arrays' import * as uint8arrays from 'uint8arrays'
import { sessionStore } from '$src/stores' import { sessionStore } from '$src/stores'
@ -60,40 +57,16 @@
storage.setItem(USERNAME_STORAGE_KEY, newUsername) storage.setItem(USERNAME_STORAGE_KEY, newUsername)
// Register the user with the `hashedNewUsername` // Recover user's file system
const { success } = await authStrategy.register({ const { success } = await $sessionStore.program.fileSystem.recover({
username: hashedNewUsername newUsername: hashedNewUsername,
oldUsername: hashedOldUsername,
readKey
}) })
if (!success) { if (!success) {
throw new Error('Failed to register new user') throw new Error('Failed to recover file system')
} }
// Build an ephemeral UCAN to authorize the dataRoot.update call
const proof: string | null = await storage.getItem(
storage.KEYS.ACCOUNT_UCAN
)
const ucan = await UCAN.build({
dependencies: $sessionStore.program.components,
potency: 'APPEND',
resource: '*',
proof: proof ? proof : undefined,
lifetimeInSeconds: 60 * 3, // Three minutes
audience: newRootDID,
issuer: newRootDID
})
const oldRootCID = await reference.dataRoot.lookup(hashedOldUsername)
// Update the dataRoot of the new user
await reference.dataRoot.update(oldRootCID, ucan)
// Store the accountDID, which is used to namespace the readKey
await RootKey.store({
accountDID: newRootDID,
readKey,
crypto: crypto
})
// Load account data into sessionStore // Load account data into sessionStore
await loadAccount(hashedNewUsername, newUsername) await loadAccount(hashedNewUsername, newUsername)