-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- 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
Showing
15 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MultibaseEncodedSHA256 } from './hash/multibase-encoded-256.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.