Compare commits

...

3 Commits

Author SHA1 Message Date
semantic-release-bot 2148ffaf58 chore(release): 0.1.0-develop.50 [skip ci]
# [0.1.0-develop.50](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.49...v0.1.0-develop.50) (2023-09-07)

### Features

* add CID.fromHash ([4a157d9](4a157d9eca))
2023-09-07 22:52:49 +00:00
Derrick Hammer a1a5912166
Merge remote-tracking branch 'origin/develop' into develop 2023-09-07 18:51:44 -04:00
Derrick Hammer 4a157d9eca
feat: add CID.fromHash 2023-09-07 18:51:40 -04:00
4 changed files with 27 additions and 3 deletions

View File

@ -1,3 +1,10 @@
# [0.1.0-develop.50](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.49...v0.1.0-develop.50) (2023-09-07)
### Features
* add CID.fromHash ([4a157d9](https://git.lumeweb.com/LumeWeb/libs5/commit/4a157d9ecaa74eb9ff17d33398aa06ecf2b07607))
# [0.1.0-develop.49](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.48...v0.1.0-develop.49) (2023-09-07) # [0.1.0-develop.49](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.48...v0.1.0-develop.49) (2023-09-07)

4
npm-shrinkwrap.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.49", "version": "0.1.0-develop.50",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.49", "version": "0.1.0-develop.50",
"dependencies": { "dependencies": {
"@noble/curves": "^1.1.0", "@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1", "@noble/hashes": "^1.3.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@lumeweb/libs5", "name": "@lumeweb/libs5",
"version": "0.1.0-develop.49", "version": "0.1.0-develop.50",
"type": "module", "type": "module",
"main": "lib/index.js", "main": "lib/index.js",
"repository": { "repository": {

View File

@ -3,6 +3,7 @@ import { Multihash } from "#multihash.js";
import { CID_TYPES, REGISTRY_TYPES } from "#constants.js"; import { CID_TYPES, REGISTRY_TYPES } from "#constants.js";
import { decodeEndian, encodeEndian } from "#util.js"; import { decodeEndian, encodeEndian } from "#util.js";
import { concatBytes, equalBytes } from "@noble/curves/abstract/utils"; import { concatBytes, equalBytes } from "@noble/curves/abstract/utils";
import { hexToBytes } from "@noble/hashes/utils";
export default class CID extends Multibase { export default class CID extends Multibase {
type: number; type: number;
@ -25,6 +26,22 @@ export default class CID extends Multibase {
return CID._init(bytes); return CID._init(bytes);
} }
static fromHash(
bytes: string | Uint8Array,
size: number,
type = CID_TYPES.RAW,
): CID {
if (typeof bytes === "string") {
bytes = hexToBytes(bytes);
}
if (!Object.values(CID_TYPES).includes(type)) {
throw new Error(`invalid cid type ${type}`);
}
return new CID(type, new Multihash(bytes), size);
}
private static _init(bytes: Uint8Array): CID { private static _init(bytes: Uint8Array): CID {
const type = bytes[0]; const type = bytes[0];
if (type === CID_TYPES.BRIDGE) { if (type === CID_TYPES.BRIDGE) {