Skip to content

Commit

Permalink
chore: moved functionality into functions
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht committed Oct 1, 2024
1 parent 4d3901f commit a50320e
Show file tree
Hide file tree
Showing 21 changed files with 1,077 additions and 63 deletions.
2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"@credo-ts/core": "^0.5.11",
"@credo-ts/indy-vdr": "^0.5.11",
"@credo-ts/node": "^0.5.11",
"@credo-ts/react-hooks": "^0.6.1",
"@credo-ts/react-native": "^0.5.11",
"@credo-ts/transport-ble": "^0.3.0",
"@hyperledger/anoncreds-nodejs": "^0.2.4",
"@hyperledger/anoncreds-react-native": "^0.2.4",
"@hyperledger/aries-askar-nodejs": "^0.2.3",
Expand Down
2 changes: 1 addition & 1 deletion example/scripts/issueCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void (async () => {
label: 'nodejs-register-agent',
walletConfig: { id: 'nodejs-register-agent', key: 'nodejs-register-key' },
logger: new ConsoleLogger(LogLevel.off),
endpoints: ['https://36ba-161-51-75-238.ngrok-free.app'],
endpoints: ['https://65a0-84-241-194-48.ngrok-free.app'],
},
modules,
dependencies: agentDependencies,
Expand Down
50 changes: 50 additions & 0 deletions example/src/credo/BleProver.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useCentral } from '@animo-id/react-native-ble-didcomm'
import type React from 'react'
import { useEffect, useState } from 'react'
import { Button, Text, View } from 'react-native'
import { Spacer } from '../Spacer'
import type { AppAgent } from './agent'
import { bleShareProof } from './bleShareProof'
import { closeBleTransports } from './utils/closeBleTransports'

type BleProverProps = {
agent: AppAgent
serviceUuid: string
}

export const BleProver: React.FC<BleProverProps> = ({ agent, serviceUuid }) => {
const [hasSharedProof, setHasSharedProof] = useState(false)
const { central } = useCentral()

const onFailure = () => console.error('[CENTRAL]: failure')
const onConnected = () => console.log('[CENTRAL]: connected')
const onDisconnected = () => console.log('[CENTRAL]: disconnected')

useEffect(() => {
return () => {
console.log('[CENTRAL]: shutting down')
closeBleTransports(agent)
void central.shutdown()
}
}, [central.shutdown, agent])

const shareProof = () =>
bleShareProof({
onFailure,
serviceUuid,
central,
agent,
onConnected,
onDisconnected,
}).then(() => setHasSharedProof(true))

return (
<View>
<Text>Ble Prover</Text>
<Spacer />
<Button title="Ready to share" onPress={shareProof} />
<Spacer />
<Text>Proof has{hasSharedProof ? ' ' : ' not '}been shared!</Text>
</View>
)
}
67 changes: 67 additions & 0 deletions example/src/credo/BleVerifier.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { usePeripheral } from '@animo-id/react-native-ble-didcomm'
import type React from 'react'
import { useEffect, useState } from 'react'
import { Button, Text, View } from 'react-native'
import { Spacer } from '../Spacer'
import type { AppAgent } from './agent'
import { bleRequestProof } from './bleRequestProof'
import { closeBleTransports } from './utils/closeBleTransports'
import type { ProofTemplate } from './utils/createBleProofMessage'

type BleVerifierProps = {
agent: AppAgent
serviceUuid: string
}

const presentationTemplate: ProofTemplate = {
id: 'test',
name: 'my-proof-request',
requestMessage: {
anoncreds: {
name: 'anon-request',
version: '2.0',
requested_attributes: { nameGroup: { name: 'name' } },
requested_predicates: {
ageGroup: { name: 'age', p_value: 20, p_type: '>' },
},
},
},
}

export const BleVerifier: React.FC<BleVerifierProps> = ({ agent, serviceUuid }) => {
const [hasReceivedProof, setHasReceivedProof] = useState(false)
const { peripheral } = usePeripheral()

const onFailure = () => console.error('[PERIPHEAL]: failure')
const onConnected = () => console.log('[PERIPHERAL]: connected')
const onDisconnected = () => console.log('[PERIPHERAL]: disconnected')

useEffect(() => {
return () => {
console.log('[PERIPHERAL]: shutting down')
closeBleTransports(agent)
void peripheral.shutdown()
}
}, [peripheral.shutdown, agent])

const requestProof = () =>
bleRequestProof({
onConnected,
onDisconnected,
onFailure,
peripheral,
agent,
serviceUuid,
presentationTemplate,
}).then(() => setHasReceivedProof(true))

return (
<View>
<Text>Ble Verifier</Text>
<Spacer />
<Button title="Request a proof" onPress={requestProof} />
<Spacer />
<Text>Proof has{hasReceivedProof ? ' ' : ' not '}been received!</Text>
</View>
)
}
28 changes: 26 additions & 2 deletions example/src/credo/CredoScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Central, CentralProvider, Peripheral, PeripheralProvider } from '@animo-id/react-native-ble-didcomm'
import AgentProvider from '@credo-ts/react-hooks'
import CredentialProvider from '@credo-ts/react-hooks/build/CredentialProvider'
import ProofProvider from '@credo-ts/react-hooks/build/ProofProvider'
import * as React from 'react'
import { type ReactElement, useEffect, useState } from 'react'
import { Button, Text } from 'react-native'
Expand All @@ -6,6 +10,8 @@ import { Prover } from './Prover'
import { Verifier } from './Verifier'
import { type AppAgent, setupAgent } from './agent'

const uuid = 'd5477fcb-6e8b-4091-ad39-a1e30386ef76'

export const CredoScreen = () => {
const [role, setRole] = useState<'prover' | 'verifier'>()
const [agent, setAgent] = useState<AppAgent>()
Expand All @@ -31,11 +37,29 @@ export const CredoScreen = () => {
}

if (role === 'prover' && agent) {
component = <Prover agent={agent} />
component = (
<AgentProvider agent={agent}>
<CredentialProvider agent={agent}>
<ProofProvider agent={agent}>
<CentralProvider central={new Central()}>
<Prover agent={agent} serviceUuid={uuid} />
</CentralProvider>
</ProofProvider>
</CredentialProvider>
</AgentProvider>
)
}

if (role === 'verifier' && agent) {
component = <Verifier agent={agent} />
component = (
<AgentProvider agent={agent}>
<ProofProvider agent={agent}>
<PeripheralProvider peripheral={new Peripheral()}>
<Verifier agent={agent} serviceUuid={uuid} />
</PeripheralProvider>
</ProofProvider>
</AgentProvider>
)
}

return component
Expand Down
42 changes: 27 additions & 15 deletions example/src/credo/Prover.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { type FunctionComponent, useEffect, useState } from 'react'
import { Button, Text, View } from 'react-native'
import { type FunctionComponent, type ReactElement, useState } from 'react'
import { Button } from 'react-native'
import { BleProver } from './BleProver'
import { Camera } from './Camera'
import type { AppAgent } from './agent'

type ProverProps = {
agent: AppAgent
serviceUuid: string
}

export const Prover: FunctionComponent<ProverProps> = ({ agent }) => {
export const Prover: FunctionComponent<ProverProps> = ({ agent, serviceUuid }) => {
const [showCamera, setShowCamera] = useState(false)
const [credentialCount, setCredentialCount] = useState(0)

useEffect(() => {
agent.credentials.getAll().then((a) => setCredentialCount(a.length))
}, [agent.credentials.getAll])
const [bleFlowInProgress, setBleFlowInProgress] = useState(false)

const onQrScanned = async (data: string) => {
console.log('scanned!')
setShowCamera(false)
await agent.oob.receiveInvitationFromUrl(data)
}
Expand All @@ -25,10 +22,25 @@ export const Prover: FunctionComponent<ProverProps> = ({ agent }) => {
return <Camera onQrScanned={onQrScanned} />
}

return (
<View>
<Button title="scan QR code" onPress={() => setShowCamera(true)} />
<Text>Prover with {credentialCount} credentials</Text>
</View>
)
let component: ReactElement

if (bleFlowInProgress) {
component = (
<>
<Button title="quit ble flow" onPress={() => setBleFlowInProgress(false)} />
<BleProver agent={agent} serviceUuid={serviceUuid} />
</>
)
}

if (!bleFlowInProgress) {
component = (
<>
<Button title="scan QR code" onPress={() => setShowCamera(true)} />
<Button title="start ble flow" onPress={() => setBleFlowInProgress(true)} />
</>
)
}

return component
}
36 changes: 28 additions & 8 deletions example/src/credo/Verifier.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
import type { FunctionComponent } from 'react'
import { Text, View } from 'react-native'
import { type FunctionComponent, type ReactElement, useState } from 'react'
import { Button, Text } from 'react-native'
import { BleVerifier } from './BleVerifier'
import type { AppAgent } from './agent'

type VerifierProps = {
agent: AppAgent
serviceUuid: string
}

export const Verifier: FunctionComponent<VerifierProps> = ({ agent }) => {
return (
<View>
<Text>Verifier</Text>
</View>
)
export const Verifier: FunctionComponent<VerifierProps> = ({ agent, serviceUuid }) => {
const [bleFlowInProgress, setBleFlowInProgress] = useState(false)

let component: ReactElement

if (bleFlowInProgress) {
component = (
<>
<Button title="quit ble flow" onPress={() => setBleFlowInProgress(false)} />
<BleVerifier agent={agent} serviceUuid={serviceUuid} />
</>
)
}

if (!bleFlowInProgress) {
component = (
<>
<Text>Verifier</Text>
<Button title="request proof" onPress={() => setBleFlowInProgress(true)} />
</>
)
}

return component
}
19 changes: 13 additions & 6 deletions example/src/credo/agent.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { AnonCredsCredentialFormatService, AnonCredsModule } from '@credo-ts/anoncreds'
import { AnonCredsCredentialFormatService, AnonCredsModule, AnonCredsProofFormatService } from '@credo-ts/anoncreds'
import { AskarModule } from '@credo-ts/askar'
import {
Agent,
AutoAcceptCredential,
AutoAcceptProof,
ConnectionsModule,
ConsoleLogger,
CredentialEventTypes,
CredentialState,
CredentialStateChangedEvent,
CredentialsModule,
DidsModule,
HttpOutboundTransport,
LogLevel,
MediationRecipientModule,
MediatorPickupStrategy,
ProofsModule,
V2CredentialProtocol,
V2ProofProtocol,
WsOutboundTransport,
} from '@credo-ts/core'
import {
Expand All @@ -37,7 +36,7 @@ export const setupAgent = async () => {
id: 'react-native-ble-didcomm-agent',
key: 'react-native-ble-didcomm-key',
},
logger: new ConsoleLogger(LogLevel.off),
logger: new ConsoleLogger(LogLevel.trace),
},
modules: {
askar: new AskarModule({ ariesAskar }),
Expand Down Expand Up @@ -68,6 +67,14 @@ export const setupAgent = async () => {
}),
],
}),
proofs: new ProofsModule({
autoAcceptProofs: AutoAcceptProof.ContentApproved,
proofProtocols: [
new V2ProofProtocol({
proofFormats: [new AnonCredsProofFormatService()],
}),
],
}),
mediationRecipient: new MediationRecipientModule({
mediatorInvitationUrl:
'https://mediator.dev.animo.id/invite?oob=eyJAdHlwZSI6Imh0dHBzOi8vZGlkY29tbS5vcmcvb3V0LW9mLWJhbmQvMS4xL2ludml0YXRpb24iLCJAaWQiOiIyMDc1MDM4YS05ZGU3LTRiODItYWUxYi1jNzBmNDg4MjYzYTciLCJsYWJlbCI6IkFuaW1vIE1lZGlhdG9yIiwiYWNjZXB0IjpbImRpZGNvbW0vYWlwMSIsImRpZGNvbW0vYWlwMjtlbnY9cmZjMTkiXSwiaGFuZHNoYWtlX3Byb3RvY29scyI6WyJodHRwczovL2RpZGNvbW0ub3JnL2RpZGV4Y2hhbmdlLzEuMCIsImh0dHBzOi8vZGlkY29tbS5vcmcvY29ubmVjdGlvbnMvMS4wIl0sInNlcnZpY2VzIjpbeyJpZCI6IiNpbmxpbmUtMCIsInNlcnZpY2VFbmRwb2ludCI6Imh0dHBzOi8vbWVkaWF0b3IuZGV2LmFuaW1vLmlkIiwidHlwZSI6ImRpZC1jb21tdW5pY2F0aW9uIiwicmVjaXBpZW50S2V5cyI6WyJkaWQ6a2V5Ono2TWtvSG9RTUphdU5VUE5OV1pQcEw3RGs1SzNtQ0NDMlBpNDJGY3FwR25iampMcSJdLCJyb3V0aW5nS2V5cyI6W119LHsiaWQiOiIjaW5saW5lLTEiLCJzZXJ2aWNlRW5kcG9pbnQiOiJ3c3M6Ly9tZWRpYXRvci5kZXYuYW5pbW8uaWQiLCJ0eXBlIjoiZGlkLWNvbW11bmljYXRpb24iLCJyZWNpcGllbnRLZXlzIjpbImRpZDprZXk6ejZNa29Ib1FNSmF1TlVQTk5XWlBwTDdEazVLM21DQ0MyUGk0MkZjcXBHbmJqakxxIl0sInJvdXRpbmdLZXlzIjpbXX1dfQ',
Expand Down
Loading

0 comments on commit a50320e

Please sign in to comment.