Skip to content

Commit

Permalink
fix(utils): use synchronous version of sha256 digest for CID (#1253)
Browse files Browse the repository at this point in the history
relates to #1239
  • Loading branch information
simonas-notcat committed Sep 22, 2023
1 parent e2e794f commit 2e3972c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
35 changes: 32 additions & 3 deletions packages/test-react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import * as React from 'react'
import './App.css'
import { getAgent } from './veramo/setup'
import { DIDResolutionResult } from 'did-resolver'
import { VerifiableCredential } from '@veramo/core-types'

function App() {
const [didDoc, setDidDoc] = React.useState<DIDResolutionResult>(null)
const [invalidDidDoc, setInvalidDidDoc] = React.useState<DIDResolutionResult>(null)
const [didDoc, setDidDoc] = React.useState<DIDResolutionResult|undefined>(undefined)
const [invalidDidDoc, setInvalidDidDoc] = React.useState<DIDResolutionResult|undefined>(undefined)
const [credential, setCredential] = React.useState<VerifiableCredential|undefined>(undefined)

const agent = getAgent()

Expand All @@ -22,16 +24,43 @@ function App() {
})
setInvalidDidDoc(doc)
}

const issueCredential = async () => {
const identifier = await agent.didManagerGetOrCreate({
alias: 'default',
provider: 'did:ethr:goerli',
})
const credential = await agent.createVerifiableCredential({
credential: {
issuer: { id: identifier.did },
issuanceDate: new Date().toISOString(),
type: ['VerifiableCredential', 'UniversityDegreeCredential'],
credentialSubject: {
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
degree: {
type: 'BachelorDegree',
name: 'Bachelor of Science and Arts',
},
},
},
proofFormat: 'jwt',
})
const hash = await agent.dataStoreSaveVerifiableCredential({
verifiableCredential: credential,
})
console.log('Credential hash', hash)
setCredential(credential)
}
React.useEffect(() => {
resolve()
resolveInvalid()
issueCredential()
}, [])

return (
<div className="App">
<header className="App-header">
{didDoc && <pre id="result">{JSON.stringify(didDoc, null, 2)}</pre>}
{credential && <pre >{JSON.stringify(credential, null, 2)}</pre>}
{invalidDidDoc && <pre id="invalid-result">{JSON.stringify(invalidDidDoc, null, 2)}</pre>}
</header>
</div>
Expand Down
13 changes: 6 additions & 7 deletions packages/utils/src/credential-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
} from '@veramo/core-types'
import { decodeJWT } from 'did-jwt'
import { normalizeCredential, normalizePresentation } from 'did-jwt-vc'
import { sha256 } from 'multiformats/hashes/sha2'
import { code, encode, prepare } from '@ipld/dag-pb'
import * as Digest from 'multiformats/hashes/digest'
import { CID } from 'multiformats/cid'
import { UnixFS } from 'ipfs-unixfs'
import { sha256 } from '@noble/hashes/sha256'

/**
* Every Verifiable Credential `@context` property must contain this.
*
Expand Down Expand Up @@ -102,13 +104,10 @@ export function computeEntryHash(
type: 'file',
data: new TextEncoder().encode(hashable)
})

const bytes = encode(prepare({ Data: unixfs.marshal() }))
const multihash = sha256.digest(bytes)
//@ts-ignore
const cid = CID.create(0, code, multihash).toString()

return cid
const bytes = encode(prepare({ Data: unixfs.marshal() }))
const digest = Digest.create(18, sha256(bytes))
return CID.create(0, code, digest).toString()
}

/**
Expand Down

0 comments on commit 2e3972c

Please sign in to comment.