Skip to content

Commit

Permalink
update tkey (#2009)
Browse files Browse the repository at this point in the history
* update tkey and wire up - wip

* fix some of the glitches
  • Loading branch information
FSM1 authored Mar 9, 2022
1 parent 9478095 commit 03f67c5
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 214 deletions.
8 changes: 4 additions & 4 deletions packages/files-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"@sentry/react": "^5.28.0",
"@stripe/react-stripe-js": "^1.4.1",
"@stripe/stripe-js": "^1.18.0",
"@tkey/default": "3.14.2",
"@tkey/security-questions": "3.14.2",
"@tkey/web-storage": "3.14.2",
"@toruslabs/torus-direct-web-sdk": "4.15.1",
"@tkey/default": "5.1.0",
"@tkey/security-questions": "5.1.0",
"@tkey/web-storage": "5.1.0",
"@toruslabs/customauth":"7.0.2",
"@types/filesystem": "^0.0.32",
"@types/uuid": "^8.3.0",
"axios": "0.21.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { t, Trans } from "@lingui/macro"
import { useFilesApi } from "../../../Contexts/FilesApiContext"
import { useWeb3 } from "@chainsafe/web3-context"
import { useThresholdKey } from "../../../Contexts/ThresholdKeyContext"
import { LOGIN_TYPE } from "@toruslabs/torus-direct-web-sdk"
import { LOGIN_TYPE } from "@toruslabs/customauth"
import { LINK_SHARING_BASE, ROUTE_LINKS } from "../../FilesRoutes"
import clsx from "clsx"
import { IdentityProvider } from "@chainsafe/files-api-client"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const SaveNewDevice = ({ className }: {className: string}) => {
const onSave = useCallback(() => {
setIsAccepted(true)
addNewDeviceShareAndSave()
.catch(console.error)
.catch(() => setIsAccepted(false))
}, [addNewDeviceShareAndSave])

const onDeny = useCallback(() => {
Expand Down
25 changes: 13 additions & 12 deletions packages/files-ui/src/Contexts/ThresholdKeyContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect, useMemo, useCallback } from "react"
import DirectAuthSdk, { createHandler, ILoginHandler, LOGIN_TYPE, TorusLoginResponse } from "@toruslabs/torus-direct-web-sdk"
import DirectAuthSdk, { createHandler, ILoginHandler, LOGIN_TYPE, TorusLoginResponse } from "@toruslabs/customauth"
import ThresholdKey from "@tkey/default"
import WebStorageModule, { WEB_STORAGE_MODULE_NAME } from "@tkey/web-storage"
import SecurityQuestionsModule, { SECURITY_QUESTIONS_MODULE_NAME } from "@tkey/security-questions"
Expand Down Expand Up @@ -181,7 +181,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
setStatus("logging in")
const tKeyJson = JSON.parse(tkeySerialized)
const serviceProvider = new ServiceProviderBase({ enableLogging, postboxKey })
const storageLayer = new TorusStorageLayer({ serviceProvider, enableLogging, hostUrl: "https://metadata.tor.us" })
const storageLayer = new TorusStorageLayer({ enableLogging, hostUrl: "https://metadata.tor.us" })
tkey = await ThresholdKey.fromJSON(tKeyJson, { modules, serviceProvider, storageLayer })

if (tKeyJson.modules) {
Expand Down Expand Up @@ -254,7 +254,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
// cached may be stale, resulting in a failure to reconstruct the key. This is
// identified through the nonce. Manually refreshing the metadata cache solves this problem
if (error.message.includes("nonce")) {
await TKeySdk.updateMetadata()
await TKeySdk._syncShareMetadata()
const { privKey } = await TKeySdk.reconstructKey(false)
const privKeyString = privKey.toString("hex")
if (privKeyString.length < 64) {
Expand Down Expand Up @@ -361,6 +361,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
shareEncPubKeyX = currentEncPubKeyX
await shareTransferModule.startRequestStatusCheck(currentEncPubKeyX, true)
const resultKey = await TKeySdk?.getKeyDetails()
await TKeySdk.syncLocalMetadataTransitions()
setKeyDetails(resultKey)
shareEncPubKeyX = undefined
} catch (error) {
Expand Down Expand Up @@ -442,6 +443,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
const loginResponse: TorusLoginResponse = {
privateKey: torusKey.privateKey,
publicAddress: torusKey.publicAddress,
typeOfUser: torusKey.typeOfUser,
metadataNonce: "",
userInfo: {
idToken: identityToken.token,
Expand Down Expand Up @@ -480,7 +482,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
console.log("privKey", privKey)
} else {
console.log("Existing key")
await TKeySdk.initialize({ input: metadata as ShareStore })
await TKeySdk.initialize({ withShare: metadata as ShareStore })
try {
console.log("Trying to load device share")
const storageModule = TKeySdk.modules[WEB_STORAGE_MODULE_NAME] as WebStorageModule
Expand Down Expand Up @@ -527,7 +529,6 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
if (!connected || !provider) throw new Error("Unable to connect to wallet.")
}


const signer = provider.getSigner()
if (!signer) throw new Error("Signer undefined")

Expand Down Expand Up @@ -663,7 +664,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f

try {
const storageModule = TKeySdk.modules[WEB_STORAGE_MODULE_NAME] as WebStorageModule
await TKeySdk.updateMetadata()
await TKeySdk._syncShareMetadata()
const newDeviceShare = await TKeySdk.generateNewShare()
const newDeviceShareStore = newDeviceShare.newShareStores[newDeviceShare.newShareIndex.toString("hex")]

Expand All @@ -674,6 +675,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
setKeyDetails(newKeyDetails)
} catch (e) {
console.error(e)
throw e
}
}

Expand All @@ -683,7 +685,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
try {
const shareTransferModule = TKeySdk.modules[SHARE_TRANSFER_MODULE_NAME] as ShareTransferModule
await shareTransferModule.approveRequest(encPubKeyX)
await TKeySdk.syncShareMetadata()
await TKeySdk._syncShareMetadata()
const newKeyDetails = await TKeySdk.getKeyDetails()
setKeyDetails(newKeyDetails)
} catch (e) {
Expand All @@ -697,7 +699,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
try {
const shareTransferModule = TKeySdk.modules[SHARE_TRANSFER_MODULE_NAME] as ShareTransferModule
await shareTransferModule.deleteShareTransferStore(encPubKey)
await TKeySdk.syncShareMetadata()
await TKeySdk._syncShareMetadata()
} catch (e) {
console.error(e)
}
Expand All @@ -709,7 +711,7 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
try {
const shareTransferModule = TKeySdk.modules[SHARE_TRANSFER_MODULE_NAME] as ShareTransferModule
await shareTransferModule.resetShareTransferStore()
await TKeySdk.syncShareMetadata()
await TKeySdk._syncShareMetadata()
} catch (e) {
console.error(e)
}
Expand Down Expand Up @@ -805,14 +807,13 @@ const ThresholdKeyProvider = ({ children, network = "mainnet", enableLogging = f
const refreshTKeyMeta = useCallback(async () => {
if (!TKeySdk) return
try {
await TKeySdk.syncShareMetadata()
await TKeySdk._syncShareMetadata()
const newKeyDetails = await TKeySdk.getKeyDetails()
setKeyDetails(newKeyDetails)
return
} catch (error: any) {
if (error.message.includes("nonce")) {
await TKeySdk.updateMetadata()
await TKeySdk.syncShareMetadata()
await TKeySdk._syncShareMetadata()
const newKeyDetails = await TKeySdk.getKeyDetails()
setKeyDetails(newKeyDetails)
} else {
Expand Down
Loading

0 comments on commit 03f67c5

Please sign in to comment.