---
# System prepended metadata

title: HGF Workshop Tutorial - Cheatsheet

---

# HGF Workshop Tutorial - Cheatsheet

1. Prepare project
    - clone, 
    - build, 
    - explain, client, server, why docker
2. Initialize agent
    - copy env, 
    - create new agent instance, label, logger, wallet, endpoints, transports
    - check hello endpoint
3. Make a connection
    - start UI
    - show invitation with localhost
    - ngrok or IP
    - make a connection impl
    - config update: auto accept?
4. Issue a credential
    - config update: ledger
    - create a public DID
    - connect to the ledger
    - register definition
    - issue credential
5. Request proof

## Section 1: Agent initialization

```ts
// TODO Section 1: Agent Initialization (agent config and instance)
const agent = new Agent(
  {
    label: 'Jakub`s amazing cloud agent.',
    logger: new ConsoleLogger(LogLevel.debug),
    walletConfig: {
      id: 'jakubs-cloud-agent-wallet',
      key: process.env.WALLET_KEY || '',
    },
    endpoints: process.env.AGENT_ENDPOINTS?.split(','),
  },
  agentDependencies
)

const app = createApp(agent)

agent.registerInboundTransport(
  new HttpInboundTransport({ app, port: Number(process.env.PORT) })
)
agent.registerOutboundTransport(new HttpOutboundTransport())


await agent.initialize()
```

## Section 2: Create an invitation

Config Update
```ts
autoAcceptConnections: true,
```

Implementation
```ts
// TODO Section 2: Create an invitation
app.get(
  '/invitation',
  asyncHandler(async (req, res) => {
    const outOfBandRecord = await agent.oob.createInvitation()
    const { outOfBandInvitation } = outOfBandRecord
    res.send(outOfBandInvitation.toUrl({ domain: 'https://example.com/ssi' }))
  })
)
```

## Section 3: Issue a credential


Create public DID
Register public DID
[GreenLight Ledger Indy Network](http://greenlight.bcovrin.vonx.io/)

### Register a credential definition

Config Update
```ts
publicDidSeed: process.env.PUBLIC_DID_SEED,
indyLedgers: [ledgers.bcovrin_greenlight],
```

Implementation
```ts
app.get(
  '/register-definition',
  asyncHandler(async (req, res) => {
    // TODO 3. Register a credential definition
    const schemaId = repository.getSchemaId()
    if (!schemaId) throw new Error('Schema ID is missing.')
    const schema = await agent.ledger.getSchema(schemaId)
    const definitionTemplate: CredentialDefinitionTemplate = {
      schema,
      signatureType: 'CL',
      supportRevocation: false,
      tag: 'default',
    }

    const definition = await agent.ledger.registerCredentialDefinition(
      definitionTemplate
    )
    repository.saveCredentialDefinitionId(definition.id)
    res.status(200).json({ definition })
  })
)
```

### Issue a credential

```ts
app.get(
  '/issue-credential/:connectionId',
  asyncHandler(async (req, res) => {
    // TODO 4 Issue a credential
    const connectionId = req.params.connectionId
    console.log('connectionId', connectionId)

    const credentialDefinitionId = repository.getCredentialDefinitionId()
    if (!credentialDefinitionId) {
      throw new Error('Credential definition is missing.')
    }

    const credentialPreview = V1CredentialPreview.fromRecord({
      Name: 'John',
      Surname: 'Doe',
      'Date of Birth': '19911911',
      'Event Name': 'Hyperledger Global Forum',
      'Event Year': '2022',
    })
    const credentialExchangeRecord = await agent.credentials.offerCredential({
      comment:
        'This credentials allows the holder to enter the Hyperledger Global Forum conference.',
      connectionId,
      credentialFormats: {
        indy: {
          attributes: credentialPreview.attributes,
          credentialDefinitionId,
        },
      },
      protocolVersion: 'v1',
      autoAcceptCredential: AutoAcceptCredential.ContentApproved,
    })
    res.status(200).json({ credentialExchangeRecord })
  })
)
```

## Section 4: Request a proof

```ts
app.get(
  '/request-proof/:connectionId',
  asyncHandler(async (req, res) => {
    // TODO 5. Request a proof
    const connectionId = req.params.connectionId

    const credentialDefinitionId = repository.getCredentialDefinitionId()
    if (!credentialDefinitionId) {
      throw new Error('Credential definition is missing.')
    }

    const attributes = {
      Surname: new ProofAttributeInfo({
        name: 'Surname',
        restrictions: [
          new AttributeFilter({
            credentialDefinitionId,
          }),
        ],
      }),
      'Event Name': new ProofAttributeInfo({
        name: 'Event Name',
        restrictions: [
          new AttributeFilter({
            credentialDefinitionId,
          }),
        ],
      }),
    }

    const predicates = {
      'Date of Birth': new ProofPredicateInfo({
        name: 'Date of Birth',
        predicateType: PredicateType.LessThanOrEqualTo,
        predicateValue: 20000101,
        restrictions: [
          new AttributeFilter({
            credentialDefinitionId,
          }),
        ],
      }),
      'Event Year': new ProofPredicateInfo({
        name: 'Event Year',
        predicateType: PredicateType.GreaterThanOrEqualTo,
        predicateValue: 2022,
        restrictions: [
          new AttributeFilter({
            credentialDefinitionId,
          }),
        ],
      }),
    }

    const proofRecord = await agent.proofs.requestProof(
      connectionId,
      {
        name: 'Hyperledger Entrance Check',
        requestedAttributes: attributes,
        requestedPredicates: predicates,
      },
      {
        autoAcceptProof: AutoAcceptProof.ContentApproved,
      }
    )
    res.status(200).json({ proofRecord })
  })
)

```
