Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(utils): synchronous version of sha256 digest #1253

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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