Compare commits
3 Commits
v0.1.0-dev
...
v0.1.0-dev
Author | SHA1 | Date |
---|---|---|
semantic-release-bot | 2148ffaf58 | |
Derrick Hammer | a1a5912166 | |
Derrick Hammer | 4a157d9eca |
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@lumeweb/libs5",
|
||||
"version": "0.1.0-develop.49",
|
||||
"version": "0.1.0-develop.50",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@lumeweb/libs5",
|
||||
"version": "0.1.0-develop.49",
|
||||
"version": "0.1.0-develop.50",
|
||||
"dependencies": {
|
||||
"@noble/curves": "^1.1.0",
|
||||
"@noble/hashes": "^1.3.1",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@lumeweb/libs5",
|
||||
"version": "0.1.0-develop.49",
|
||||
"version": "0.1.0-develop.50",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
"repository": {
|
||||
|
|
17
src/cid.ts
17
src/cid.ts
|
@ -3,6 +3,7 @@ import { Multihash } from "#multihash.js";
|
|||
import { CID_TYPES, REGISTRY_TYPES } from "#constants.js";
|
||||
import { decodeEndian, encodeEndian } from "#util.js";
|
||||
import { concatBytes, equalBytes } from "@noble/curves/abstract/utils";
|
||||
import { hexToBytes } from "@noble/hashes/utils";
|
||||
|
||||
export default class CID extends Multibase {
|
||||
type: number;
|
||||
|
@ -25,6 +26,22 @@ export default class CID extends Multibase {
|
|||
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 {
|
||||
const type = bytes[0];
|
||||
if (type === CID_TYPES.BRIDGE) {
|
||||
|
|
Loading…
Reference in New Issue