Skip to content

Commit

Permalink
feat: add compute hash utils (#175)
Browse files Browse the repository at this point in the history
<!--
  For Work In Progress Pull Requests, please use the Draft PR feature,
see https://github.blog/2019-02-14-introducing-draft-pull-requests/ for
further details.
  
  For a timely review/response, please avoid force-pushing additional
  commits if your PR already received reviews or comments.
  
Before submitting a Pull Request, please ensure you've done the
following:
- 📖 Read the [Contributing
Guide](https://github.com/uncefact/project-vckit/blob/main/CONTRIBUTING.md).
- 📖 Read the [Code of
Conduct](https://github.com/uncefact/project-vckit/blob/main/CODE_OF_CONDUCT.md).
  - 👷‍♀️ Create small PRs. In most cases, this will be possible.
  - ✅ Provide tests for your changes.
- 📝 Use descriptive commit messages following [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/).
- 📗 Update any related documentation and include any relevant
screenshots.
-->

## What type of PR is this? (check all applicable)

- [x] 🍕 Feature
- [ ] 🐛 Bug Fix
- [ ] 📝 Documentation Update
- [ ] 🎨 Style
- [ ] 🧑‍💻 Code Refactor
- [ ] 🔥 Performance Improvements
- [ ] ✅ Test
- [ ] 🤖 Build
- [ ] 🔁 CI
- [ ] 📦 Chore (Release)
- [ ] ⏩ Revert

## Description
Some reference links about the compute hash:
https://w3c-ccg.github.io/vc-render-method/#svgrenderingtemplate2023
w3c-ccg/vc-render-method#9
https://w3c-ccg.github.io/multibase/#tv-base58btc
https://www.ietf.org/archive/id/draft-multiformats-multibase-08.html
https://www.npmjs.com/package/multihashes

<!-- 
Please do not leave this blank 
This PR [adds/removes/fixes/replaces] the [feature/bug/etc]. 
-->
## Related Tickets & Documents
<!-- 
Please use this format link issue numbers: Fixes #123

https://docs.github.com/en/free-pro-team@latest/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
-->

## Mobile & Desktop Screenshots/Recordings

<!-- Visual changes require screenshots -->


## Added tests?

- [x] 👍 yes
- [ ] 🙅 no, because they aren't needed
- [ ] 🙋 no, because I need help

## Added to documentation?

- [ ] 📜 README.md
- [ ] 📓 [vc-kit doc site](https://uncefact.github.io/vckit/)
- [ ] 📕 storybook
- [x] 🙅 no documentation needed

## [optional] Are there any post-deployment tasks we need to perform?


<!-- note: PRs with deleted sections will be marked invalid -->
  • Loading branch information
ldhyen99 authored May 6, 2024
1 parent 2c947ce commit 6eb5802
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/cli/default/agent.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ constants:
- checkStatus
- routeCreationVerifiableCredential
- routeVerificationCredential
- computeHash

# Data base
dbConnection:
Expand Down Expand Up @@ -127,7 +128,7 @@ dbConnectionRevocationList:
revocationList:
$require: '@vckit/revocationlist#RevocationStatus2020'
$args:
- revocationListPath:
- revocationListPath:
$ref: /constants/baseUrl
bitStringLength: 8
dbConnection:
Expand Down Expand Up @@ -424,3 +425,4 @@ agent:
$args:
- RevocationList2020Status:
$require: '@vckit/revocationlist?t=object#checkStatus'
- $require: '@vckit/tools#MultibaseEncodedSHA256'
2 changes: 2 additions & 0 deletions packages/cli/default/default-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ constants:
- checkStatus
- routeCreationVerifiableCredential
- routeVerificationCredential
- computeHash

# Data base
dbConnection:
Expand Down Expand Up @@ -417,3 +418,4 @@ agent:
$args:
- RevocationList2020Status:
$require: '@vckit/revocationlist?t=object#checkStatus'
- $require: '@vckit/tools#MultibaseEncodedSHA256'
2 changes: 2 additions & 0 deletions packages/cli/default/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ constants:
- checkStatus
- routeCreationVerifiableCredential
- routeVerificationCredential
- computeHash

# Data base
dbConnection:
Expand Down Expand Up @@ -417,3 +418,4 @@ agent:
$args:
- RevocationList2020Status:
$require: '@vckit/revocationlist?t=object#checkStatus'
- $require: '@vckit/tools#MultibaseEncodedSHA256'
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@vckit/revocationlist": "workspace:^",
"@vckit/utils": "workspace:^",
"@vckit/vc-api": "workspace:1.0.0-beta.7",
"@vckit/tools": "workspace:^",
"@veramo/core": "5.5.3",
"@veramo/credential-eip712": "5.5.3",
"@veramo/credential-ld": "5.5.3",
Expand Down
3 changes: 2 additions & 1 deletion packages/core-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"IRenderer": "./src/types/IRender.ts",
"IEncryptedStorage": "./src/types/IEncryptedStorage.ts",
"IRevocationList2020": "./src/types/IRevocationList2020.ts",
"ICredentialRouter": "./src/types/ICredentialRouter.ts"
"ICredentialRouter": "./src/types/ICredentialRouter.ts",
"ITools": "./src/types/ITools.ts"
}
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/core-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export * from './types/IRendererProvider.js';
export * from './types/IEncryptedStorage.js';
export * from './types/IRevocationList2020.js';
export * from './types/ICredentialRouter.js';
export * from './types/ITools.js';
16 changes: 16 additions & 0 deletions packages/core-types/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6739,5 +6739,21 @@
}
}
}
},
"ITools": {
"components": {
"schemas": {},
"methods": {
"computeHash": {
"description": "",
"arguments": {
"type": "string"
},
"returnType": {
"type": "string"
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions packages/core-types/src/types/ITools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IPluginMethodMap } from './IAgent.js';

/**
* @public
*/
export interface ITools extends IPluginMethodMap {
computeHash(args: string): Promise<string>;
}
3 changes: 3 additions & 0 deletions packages/tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tools

This directory contains tools that are used to expose as library or API.
36 changes: 36 additions & 0 deletions packages/tools/__tests__/computeHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { jest } from '@jest/globals';
import { MultibaseEncodedSHA256 } from '../src/hash/multibase-encoded-256';

describe('compute hash', () => {
let computeHash: any;

beforeEach(() => {
const plugin = new MultibaseEncodedSHA256();
computeHash = plugin.methods.computeHash;
});

it('should compute hash', async () => {
const hash = await computeHash('hello world');
expect(hash).toBe('zQmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4');
});

it('should throw error when value is empty', async () => {
await expect(computeHash('')).rejects.toThrow('Value is invalid');
});

it('should throw error when value is not a string', async () => {
await expect(computeHash(123)).rejects.toThrow('Value is invalid');

await expect(computeHash(true)).rejects.toThrow('Value is invalid');

await expect(computeHash(NaN)).rejects.toThrow('Value is invalid');

await expect(computeHash(null)).rejects.toThrow('Value is invalid');

await expect(computeHash(undefined)).rejects.toThrow('Value is invalid');

await expect(computeHash({})).rejects.toThrow('Value is invalid');

await expect(computeHash([])).rejects.toThrow('Value is invalid');
});
});
29 changes: 29 additions & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@vckit/tools",
"version": "1.0.0",
"description": "",
"main": "build/index.js",
"types": "build/index.d.ts",
"exports": {
".": "./build/index.js"
},
"files": [
"build/**/*",
"src/**/*",
"plugin.schema.json",
"README.md",
"LICENSE"
],
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc -b --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@vckit/core-types": "workspace:1.0.0-beta.7",
"multiformats": "^13.1.0"
}
}
26 changes: 26 additions & 0 deletions packages/tools/src/hash/multibase-encoded-256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IAgentPlugin, ITools } from '@vckit/core-types';
import { sha256 } from 'multiformats/hashes/sha2';
import { base58btc } from 'multiformats/bases/base58';
import schema from '@vckit/core-types/build/plugin.schema.json' assert { type: 'json' };

export class MultibaseEncodedSHA256 implements IAgentPlugin {
readonly methods: ITools;
readonly schema = schema.ITools;

constructor() {
this.methods = {
computeHash: this.computeHash.bind(this),
};
}

async computeHash(value: string): Promise<string> {
if (!value || typeof value !== 'string') {
throw new Error('Value is invalid');
}

const bytes = new TextEncoder().encode(value);
const hash = await sha256.digest(bytes);
const multibasedHash = base58btc.encode(hash.bytes);
return multibasedHash;
}
}
1 change: 1 addition & 0 deletions packages/tools/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MultibaseEncodedSHA256 } from './hash/multibase-encoded-256.js';
11 changes: 11 additions & 0 deletions packages/tools/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.settings.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"declarationDir": "build",
"skipLibCheck": true
},
"references": [],
"include": ["./**/*.ts", "./src/plugin.schema.json"]
}
21 changes: 17 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6eb5802

Please sign in to comment.