Compare commits

...

3 Commits

Author SHA1 Message Date
semantic-release-bot 07600cea13 chore(release): 0.1.0-develop.49 [skip ci]
# [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)

### Features

* add CID class ([cc5cbdb](cc5cbdb860))
2023-09-07 22:41:18 +00:00
Derrick Hammer 764685a111
Merge remote-tracking branch 'origin/develop' into develop 2023-09-07 18:40:35 -04:00
Derrick Hammer cc5cbdb860
feat: add CID class 2023-09-07 18:40:27 -04:00
5 changed files with 124 additions and 4 deletions

View File

@ -1,3 +1,10 @@
# [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)
### Features
* add CID class ([cc5cbdb](https://git.lumeweb.com/LumeWeb/libs5/commit/cc5cbdb8605fa3170eea964b2ad8765fc73f1caa))
# [0.1.0-develop.48](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.47...v0.1.0-develop.48) (2023-09-07)
# [0.1.0-develop.47](https://git.lumeweb.com/LumeWeb/libs5/compare/v0.1.0-develop.46...v0.1.0-develop.47) (2023-09-04)

4
npm-shrinkwrap.json generated
View File

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

View File

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

112
src/cid.ts Normal file
View File

@ -0,0 +1,112 @@
import Multibase from "#multibase.js";
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";
export default class CID extends Multibase {
type: number;
hash: Multihash;
size?: number;
constructor(type: number, hash: Multihash, size?: number) {
super();
this.type = type;
this.hash = hash;
this.size = size;
}
static decode(cid: string): CID {
const decodedBytes = Multibase.decodeString(cid);
return CID._init(decodedBytes);
}
static fromBytes(bytes: Uint8Array): CID {
return CID._init(bytes);
}
private static _init(bytes: Uint8Array): CID {
const type = bytes[0];
if (type === CID_TYPES.BRIDGE) {
return new CID(type, new Multihash(bytes));
}
const hash = new Multihash(bytes.subarray(1, 34));
let size: number | undefined = undefined;
const sizeBytes = bytes.subarray(34);
if (sizeBytes.length > 0) {
size = decodeEndian(sizeBytes);
}
if (!Object.values(CID_TYPES).includes(type)) {
throw new Error(`invalid cid type ${type}`);
}
return new CID(type, hash, size);
}
copyWith({ size, type }: { type?: number; size?: number }): CID {
type = type || this.type;
if (!Object.values(CID_TYPES).includes(type)) {
throw new Error(`invalid cid type ${type}`);
}
return new CID(type, this.hash, size || this.size);
}
toBytes(): Uint8Array {
if (this.type === CID_TYPES.BRIDGE) {
return this.hash.fullBytes;
} else if (this.type === CID_TYPES.RAW) {
let sizeBytes = encodeEndian(this.size as number, 8);
while (sizeBytes.length > 0 && sizeBytes[sizeBytes.length - 1] === 0) {
sizeBytes = sizeBytes.slice(0, -1);
}
if (sizeBytes.length === 0) {
sizeBytes = new Uint8Array(1);
}
return concatBytes(
this._getPrefixBytes(),
this.hash.fullBytes,
sizeBytes,
);
}
return concatBytes(this._getPrefixBytes(), this.hash.fullBytes);
}
private _getPrefixBytes(): Uint8Array {
return Uint8Array.from([this.type]);
}
toRegistryEntry(): Uint8Array {
return concatBytes(Uint8Array.from([REGISTRY_TYPES.CID]), this.toBytes());
}
toRegistryCID(): Uint8Array {
return this.copyWith({ type: CID_TYPES.RESOLVER }).toBytes();
}
toString(): string {
return this.type === CID_TYPES.BRIDGE
? Buffer.from(this.hash.fullBytes).toString("utf8")
: this.toBase58();
}
equals(other: CID): boolean {
return equalBytes(this.toBytes(), other.toBytes());
}
hashCode(): number {
const fullBytes = this.toBytes();
return (
fullBytes[0] +
fullBytes[1] * 256 +
fullBytes[2] * 256 * 256 +
fullBytes[3] * 256 * 256 * 256
);
}
}

View File

@ -4,6 +4,7 @@ import { ed25519 } from "@noble/curves/ed25519";
import KeyPairEd25519 from "#ed25519.js";
import { S5NodeConfig } from "./types.js";
import NodeId from "#nodeId.js";
import CID from "#cid.js";
export * from "./types.js";
export * from "./constants.js";
@ -16,7 +17,7 @@ export {
BasePeer,
} from "./transports/index.js";
export type { SignedRegistryEntry };
export { NodeId };
export { NodeId, CID };
export function createNode(config: S5NodeConfig) {
return new S5Node(config);