Skip to content

Commit

Permalink
update snap and identity interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukachi committed Nov 17, 2023
1 parent 0cb9969 commit ad3a464
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 155 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
#### Changed
- `@rarimo/rarime-connector` - bump version to 1.0.0-rc.0
- Getting identity id's

## [2.3.0] - 2023-11-17
#### Changed
- env files interaction, now .env files are able to bundle in ci
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@rarimo/auth-zkp-iden3": "^2.0.0-rc.24",
"@rarimo/client": "^2.0.0-rc.24",
"@rarimo/identity-gen-iden3": "^2.0.0-rc.24",
"@rarimo/rarime-connector": "0.8.0",
"@rarimo/rarime-connector": "1.0.0-rc.2",
"@rarimo/shared-zkp-iden3": "^2.0.0-rc.24",
"@rarimo/zkp-gen-iden3": "^2.0.0-rc.24",
"@sentry/react": "7.63.0",
Expand Down
45 changes: 26 additions & 19 deletions src/contexts/FormStepperContext/FormStepperContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {

createIdentity,
getIsIdentityProvedMsg,
parseDIDToIdentityBigIntString,
} = useZkpContext()
const { isVCRequestPending, isUserHasClaimHandled, handleWorldcoinRedirect } =
useKycContext()
Expand All @@ -185,14 +184,20 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {
const [searchParams] = useSearchParams()

const handleIfUserClaimExist = useCallback(async (): Promise<Steps> => {
const identityIdString = await createIdentity()
let identity

const identityBigIntString = identityIdString
? parseDIDToIdentityBigIntString(identityIdString)
: ''
try {
identity = await createIdentity()
} catch (e) {
identity = undefined
}

if (!identity) return Steps.IdentityCreationStep

const { identityIdBigIntString } = identity

const isIdentityProvedMsg = await getIsIdentityProvedMsg(
identityBigIntString,
identityIdBigIntString,
)

if (isIdentityProvedMsg) {
Expand All @@ -210,12 +215,7 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {
}

return Steps.KycProvidersStep
}, [
createIdentity,
getIsIdentityProvedMsg,
isUserHasClaimHandled,
parseDIDToIdentityBigIntString,
])
}, [createIdentity, getIsIdentityProvedMsg, isUserHasClaimHandled])

const detectStartStep = useCallback(async () => {
if (
Expand Down Expand Up @@ -261,11 +261,13 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {
* As createIdentity() method is return existing identity or create new,
* we can detect created one by checking verifiable credentials
*/
const identityIdString = await createIdentity()
try {
const identity = await createIdentity()

identityBigIntString = identityIdString
? parseDIDToIdentityBigIntString(identityIdString)
: ''
identityBigIntString = identity?.identityIdBigIntString ?? ''
} catch (error) {
identityBigIntString = ''
}
}

if (identityBigIntString) {
Expand Down Expand Up @@ -293,7 +295,6 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {
provider?.isConnected,
handleWorldcoinRedirect,
createIdentity,
parseDIDToIdentityBigIntString,
getIsIdentityProvedMsg,
])

Expand Down Expand Up @@ -534,13 +535,19 @@ const FormStepperContextProvider: FC<Props> = ({ children }) => {
useEffect(() => {
if (!isLoaded || isInitialized) return

const h = async () => {
let defineStep = async () => {
if (!currentStep) setCurrentStep(await detectStartStep())

setIsInitialized(true)
}

h()
defineStep()

return () => {
defineStep = async () => {
/* empty */
}
}
}, [currentStep, detectStartStep, isInitialized, isLoaded])

useEffect(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/contexts/KycContext/KycContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ const KycContextProvider: FC<HTMLAttributes<HTMLDivElement>> = ({
const socketEndpoint = `${String(config.ISSUER_API_URL).replace(
'http',
'ws',
)}/v1/credentials/did:iden3:${identityIdString}/${claimType}/subscribe`
)}/v1/credentials/${identityIdString}/${claimType}/subscribe`

const socket = new WebSocket(socketEndpoint)

Expand Down Expand Up @@ -543,7 +543,7 @@ const KycContextProvider: FC<HTMLAttributes<HTMLDivElement>> = ({
const isUserHasClaimHandled = useCallback(
async (_VCCreatedOrKycFinishedCb?: () => void) => {
const currentIdentityIdString =
identityIdString || (await createIdentity())
identityIdString || (await createIdentity())?.identityIdString

if (!currentIdentityIdString) throw new TypeError('identityId is empty')

Expand All @@ -562,7 +562,7 @@ const KycContextProvider: FC<HTMLAttributes<HTMLDivElement>> = ({
"You already have a verifiable credentials, let's check it out!",
)

const vc = await getVerifiableCredentials(identityIdString, claim)
const vc = await getVerifiableCredentials(currentIdentityIdString, claim)

detectProviderFromVC(vc)

Expand Down
10 changes: 8 additions & 2 deletions src/contexts/MetamaskZkpSnapContext/MetamaskZkpSnapContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ interface MetamaskZkpSnapContextValue {

isLocalSnap: (snapId: string) => boolean

createIdentity: () => Promise<string | undefined>
createIdentity: () => Promise<
| {
identityIdString: string
identityIdBigIntString: string
}
| undefined
>
getVerifiableCredentials: (
params: SaveCredentialsRequestParams,
) => Promise<W3CCredential[] | undefined>
Expand Down Expand Up @@ -91,7 +97,7 @@ const MetamaskZkpSnapContextProvider: FC<HTMLAttributes<HTMLDivElement>> = ({
const createIdentity = useCallback(async () => {
if (!connector) throw new TypeError('Connector is not defined')

return (await connector.createIdentity())?.split(':')?.[2]
return connector.createIdentity()
}, [connector])

/**
Expand Down
64 changes: 28 additions & 36 deletions src/contexts/ZkpContext/ZkpContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { config, SUPPORTED_CHAINS } from '@config'
import { type EthTransactionResponse } from '@distributedlab/w3p'
import { type TransactionRequest } from '@ethersproject/providers'
import { DID } from '@iden3/js-iden3-core'
import type {
SaveCredentialsRequestParams,
StateInfo,
Expand Down Expand Up @@ -37,7 +36,7 @@ export type StatesMerkleProof = {

interface ZkpContextValue {
identityIdString: string
identityBigIntString: string
identityIdBigIntString: string
isZKPRequestPending: boolean
isProveRequestPending: boolean
verifiableCredentials?: W3CCredential
Expand All @@ -49,20 +48,25 @@ interface ZkpContextValue {
getClaimOffer: (
_identityIdString?: string,
) => Promise<SaveCredentialsRequestParams | undefined>
createIdentity: () => Promise<string | undefined>
createIdentity: () => Promise<
| {
identityIdString: string
identityIdBigIntString: string
}
| undefined
>
getVerifiableCredentials: (
_identityIdString?: string,
claimOffer?: SaveCredentialsRequestParams,
) => Promise<W3CCredential | undefined>
getZkProof: () => Promise<ZKPProofResponse | undefined>
submitZkp: (selectedChain: SUPPORTED_CHAINS) => Promise<void>
getIsIdentityProvedMsg: (_identityBigIntString?: string) => Promise<string>
parseDIDToIdentityBigIntString: (identityIdString: string) => string
}

export const zkpContext = createContext<ZkpContextValue>({
identityIdString: '',
identityBigIntString: '',
identityIdBigIntString: '',

txSubmitExplorerLink: '',

Expand Down Expand Up @@ -91,9 +95,6 @@ export const zkpContext = createContext<ZkpContextValue>({
getIsIdentityProvedMsg: () => {
throw new TypeError(`getIsIdentityProvedString() not implemented`)
},
parseDIDToIdentityBigIntString: () => {
throw new TypeError(`parseDIDToIdentityBigIntString() not implemented`)
},
})

type Props = HTMLAttributes<HTMLDivElement>
Expand All @@ -111,6 +112,7 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
const [updateStateDetails, setUpdateStateDetails] = useState<UpdateStateDetails>()
const [verifiableCredentials, setVerifiableCredentials] = useLocalStorage<W3CCredential>('vc', undefined)
const [identityIdString, setIdentityIdString] = useState('')
const [identityIdBigIntString, setIdentityIdBigIntString] = useState('')
const [txSubmitHash, setTxSubmitHash] = useState('')
/* eslint-enable */
/* prettier-ignore-end */
Expand All @@ -121,23 +123,6 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
const { isIdentityProved, isSenderAddressProved, getProveIdentityTxBody } =
useSbtIdentityVerifier()

const parseDIDToIdentityBigIntString = useCallback(
(identityIdString: string) => {
const parsedDid = DID.parse(`did:iden3:${identityIdString}`)

if (!parsedDid?.id?.bigInt()?.toString()) throw new Error('Invalid DID')

return parsedDid.id.bigInt().toString()
},
[],
)

const identityBigIntString = useMemo(() => {
if (!identityIdString) return ''

return parseDIDToIdentityBigIntString(identityIdString)
}, [identityIdString, parseDIDToIdentityBigIntString])

const txSubmitExplorerLink = useMemo(() => {
if (!txSubmitHash || !provider?.getTxUrl) return ''

Expand All @@ -150,16 +135,24 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
}, [provider, txSubmitHash])

const createIdentity = useCallback(async () => {
if (identityIdString) return identityIdString
if (identityIdString && identityIdBigIntString)
return { identityIdString, identityIdBigIntString }

const _identityId = await zkpSnap.createIdentity()

const _identityIdString = await zkpSnap.createIdentity()
if (!_identityId) throw new Error('Identity has not created')

if (!_identityIdString) throw new Error('Identity has not created')
const { identityIdString: did, identityIdBigIntString: didBigInt } =
_identityId

setIdentityIdString(_identityIdString)
setIdentityIdString(did)
setIdentityIdBigIntString(didBigInt)

return _identityIdString
}, [identityIdString, setIdentityIdString, zkpSnap])
return {
identityIdString: did,
identityIdBigIntString: didBigInt,
}
}, [identityIdBigIntString, identityIdString, zkpSnap])

/**
* GETTING VERIFIABLE CREDENTIALS
Expand All @@ -171,7 +164,7 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {

// FIXME: remove
const { data } = await issuerApi.get<SaveCredentialsRequestParams>(
`/v1/credentials/did:iden3:${currIdentityIdString}/${config.CLAIM_TYPE}`,
`/v1/credentials/${currIdentityIdString}/${config.CLAIM_TYPE}`,
)

return data
Expand Down Expand Up @@ -352,7 +345,7 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
const getIsIdentityProvedMsg = useCallback(
async (_identityBigIntString?: string) => {
const currentIdentityBigIntString =
_identityBigIntString ?? identityBigIntString
_identityBigIntString ?? identityIdBigIntString

if (!currentIdentityBigIntString || !provider?.address)
throw new TypeError(`Identity or provider is not defined`)
Expand All @@ -376,7 +369,7 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
return provedMsg
},
[
identityBigIntString,
identityIdBigIntString,
isIdentityProved,
isSenderAddressProved,
provider?.address,
Expand All @@ -387,7 +380,7 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
<zkpContext.Provider
value={{
identityIdString,
identityBigIntString,
identityIdBigIntString,
verifiableCredentials,
isZKPRequestPending,
isProveRequestPending,
Expand All @@ -402,7 +395,6 @@ const ZkpContextProvider: FC<Props> = ({ children, ...rest }) => {
getZkProof,
submitZkp,
getIsIdentityProvedMsg,
parseDIDToIdentityBigIntString,
}}
{...rest}
>
Expand Down
18 changes: 0 additions & 18 deletions src/helpers/file.helpers.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export * from './device.helpers'
export * from './error-handler'
export * from './ethereum.helpers'
export * from './event-bus'
export * from './file.helpers'
export * from './ga.helpers'
export * from './kyc.helpers'
export * from './promise.helpers'
Expand Down
17 changes: 16 additions & 1 deletion src/pages/MainPage/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ const mainContentAnimationVariants: Variants = {
},
}

const mainContentFillAnimationVariants: Variants = {
open: {
width: '100%',
overflowX: 'hidden',
},
collapsed: {
width: '100%',
overflowX: 'hidden',
},
}

const sidebarAnimationVariants: Variants = {
open: {
width: '33.3%',
Expand Down Expand Up @@ -124,7 +135,11 @@ const MainPage: FC<Props> = ({ className, ...rest }) => {
<motion.div
className='main-page__content'
key={mainContentUuid}
variants={mainContentAnimationVariants}
variants={
isSidebarOpen && SidebarComponent
? mainContentAnimationVariants
: mainContentFillAnimationVariants
}
initial='open'
animate='collapsed'
exit='open'
Expand Down
Loading

0 comments on commit ad3a464

Please sign in to comment.