diff --git a/README.md b/README.md index 84fdff0..7aa20b2 100644 --- a/README.md +++ b/README.md @@ -56,14 +56,6 @@ to think about those parameters anymore. Complete API documentation can be found [here](https://deno.land/x/meadowcap/mod.ts). -With ESM imports: - -``` -import { Meadowcap } from "https://deno.land/x/meadowcap/mod.ts" -``` - -NPM distribution coming very soon. - ## Development Deno is used as the development runtime. diff --git a/deno.json b/deno.json index ca6538e..15ceb9f 100644 --- a/deno.json +++ b/deno.json @@ -1,10 +1,16 @@ { + "name": "@earthstar/meadowcap", + "version": "0.6.2", + "exports": "./mod.ts", "imports": { - "$std/": "https://deno.land/std@0.198.0/" + "@earthstar/willow-utils": "jsr:@earthstar/willow-utils@^0.8.1", + "@std/assert": "jsr:@std/assert@^0.225.2", + "@std/async": "jsr:@std/async@^0.224.0", + "@std/bytes": "jsr:@std/bytes@^0.224.0", + "@std/collections": "jsr:@std/collections@^0.224.2" }, "tasks": { "test": "deno test src", "test-watch": "deno test src --watch" - }, - "lock": false + } } diff --git a/deps.ts b/deps.ts deleted file mode 100644 index 5a71464..0000000 --- a/deps.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from "https://deno.land/x/willow_utils@0.8.0/mod.ts"; -export { chunk } from "https://deno.land/std@0.198.0/collections/chunk.ts"; -export { concat } from "https://deno.land/std@0.198.0/bytes/concat.ts"; diff --git a/src/capabilities/encoding.test.ts b/src/capabilities/encoding.test.ts index 0ba8701..7083332 100644 --- a/src/capabilities/encoding.test.ts +++ b/src/capabilities/encoding.test.ts @@ -1,5 +1,5 @@ import FIFO from "https://deno.land/x/fifo@v0.2.2/mod.ts"; -import { delay } from "https://deno.land/std@0.202.0/async/delay.ts"; +import { delay } from "@std/async"; import { decodeMcCapability, decodeStreamMcCapability, @@ -9,14 +9,13 @@ import { encodeSubspaceCapability, } from "./encoding.ts"; import { McCapability, McSubspaceCapability } from "./types.ts"; - import { EncodingScheme, GrowingBytes, OPEN_END, orderBytes, -} from "../../deps.ts"; -import { assertEquals } from "$std/assert/assert_equals.ts"; +} from "@earthstar/willow-utils"; +import { assertEquals } from "@std/assert"; function makeEncodings(len: number): EncodingScheme { return { diff --git a/src/capabilities/encoding.ts b/src/capabilities/encoding.ts index 76588d1..9b38cf3 100644 --- a/src/capabilities/encoding.ts +++ b/src/capabilities/encoding.ts @@ -9,7 +9,6 @@ import { getGrantedAreaCommunal, getGrantedAreaOwned } from "./semantics.ts"; import { ANY_SUBSPACE, Area, - concat, decodeAreaInArea, decodeCompactWidth, decodeStreamAreaInArea, @@ -24,8 +23,9 @@ import { PathScheme, subspaceArea, TotalOrder, -} from "../../deps.ts"; +} from "@earthstar/willow-utils"; import { UserScheme } from "../meadowcap/types.ts"; +import { concat } from "@std/bytes"; /** Returns the handover message to be signed when issuing a delegation for a communal capability. */ export function handoverCommunal< @@ -70,10 +70,7 @@ export function handoverCommunal< const newPubKey = opts.userScheme.encodings.publicKey.encode(newUser); return concat( - accessModeByte, - namespace, - areaInArea, - newPubKey, + [accessModeByte, namespace, areaInArea, newPubKey], ); } @@ -98,9 +95,7 @@ export function handoverCommunal< const newPubKey = opts.userScheme.encodings.publicKey.encode(newUser); return concat( - areaInArea, - userSignature, - newPubKey, + [areaInArea, userSignature, newPubKey], ); } @@ -153,9 +148,7 @@ export function handoverOwned< ); return concat( - areaInArea, - userSignature, - userPublicKey, + [areaInArea, userSignature, userPublicKey], ); } @@ -180,9 +173,7 @@ export function handoverOwned< ); return concat( - areaInArea, - userSignature, - userPublicKey, + [areaInArea, userSignature, userPublicKey], ); } @@ -222,8 +213,7 @@ export function handoverSubspace< ); return concat( - userSignature, - userPublicKey, + [userSignature, userPublicKey], ); } @@ -234,10 +224,12 @@ export function handoverSubspace< ); return concat( - userSignature, - opts.userScheme.encodings.publicKey.encode( - newUser, - ), + [ + userSignature, + opts.userScheme.encodings.publicKey.encode( + newUser, + ), + ], ); } @@ -317,17 +309,21 @@ export function encodeMcCapability< const userPkEnc = opts.encodingUser.encode(pk); const sigEnc = opts.encodingUserSig.encode(sig); - encodedDelegationsAcc.push(concat(areaInAreaEncoded, userPkEnc, sigEnc)); + encodedDelegationsAcc.push( + concat([areaInAreaEncoded, userPkEnc, sigEnc]), + ); prevArea = area; } return concat( - new Uint8Array([header]), - encodedNamespace, - encodedUser, - compactWidthDelegationsLen, - ...encodedDelegationsAcc, + [ + new Uint8Array([header]), + encodedNamespace, + encodedUser, + compactWidthDelegationsLen, + ...encodedDelegationsAcc, + ], ); } @@ -374,18 +370,20 @@ export function encodeMcCapability< const userPkEnc = opts.encodingUser.encode(pk); const sigEnc = opts.encodingUserSig.encode(sig); - encodedDelegationsAcc.push(concat(areaInAreaEncoded, userPkEnc, sigEnc)); + encodedDelegationsAcc.push(concat([areaInAreaEncoded, userPkEnc, sigEnc])); prevArea = area; } return concat( - new Uint8Array([header]), - encodedNamespace, - encodedUser, - encodedNamespaceSig, - compactWidthDelegationsLen, - ...encodedDelegationsAcc, + [ + new Uint8Array([header]), + encodedNamespace, + encodedUser, + encodedNamespaceSig, + compactWidthDelegationsLen, + ...encodedDelegationsAcc, + ], ); } @@ -830,16 +828,18 @@ export function encodeSubspaceCapability< const userPkEnc = opts.encodingUser.encode(pk); const sigEnc = opts.encodingUserSig.encode(sig); - encodedDelegationsAcc.push(concat(userPkEnc, sigEnc)); + encodedDelegationsAcc.push(concat([userPkEnc, sigEnc])); } return concat( - new Uint8Array([header]), - namespaceEncoded, - userEncoded, - sigEncoded, - delLength, - ...encodedDelegationsAcc, + [ + new Uint8Array([header]), + namespaceEncoded, + userEncoded, + sigEncoded, + delLength, + ...encodedDelegationsAcc, + ], ); } diff --git a/src/capabilities/semantics.ts b/src/capabilities/semantics.ts index 8c636b3..db88bc5 100644 --- a/src/capabilities/semantics.ts +++ b/src/capabilities/semantics.ts @@ -1,4 +1,4 @@ -import { ANY_SUBSPACE, Area, OPEN_END } from "../../deps.ts"; +import { ANY_SUBSPACE, Area, OPEN_END } from "@earthstar/willow-utils"; import { CommunalCapability, McCapability, diff --git a/src/capabilities/types.ts b/src/capabilities/types.ts index 4f33ff0..32be1c6 100644 --- a/src/capabilities/types.ts +++ b/src/capabilities/types.ts @@ -1,4 +1,4 @@ -import { Area } from "../../deps.ts"; +import { Area } from "@earthstar/willow-utils"; /** Whether a capability grants read or write access. */ export type AccessMode = "read" | "write"; diff --git a/src/capabilities/validity.ts b/src/capabilities/validity.ts index f239c73..b7b8a29 100644 --- a/src/capabilities/validity.ts +++ b/src/capabilities/validity.ts @@ -1,9 +1,8 @@ import { areaIsIncluded, - concat, KeypairScheme, PathScheme, -} from "../../deps.ts"; +} from "@earthstar/willow-utils"; import { UserScheme } from "../meadowcap/types.ts"; import { handoverCommunal, @@ -23,6 +22,7 @@ import { McSubspaceCapability, OwnedCapability, } from "./types.ts"; +import { concat } from "@std/bytes"; /** Returns whether a communal capability is valid. */ export async function isValidCapCommunal< @@ -106,8 +106,7 @@ export async function isValidCapOwned< ]); const message = concat( - accessModeByte, - opts.userScheme.encodings.publicKey.encode(cap.userKey), + [accessModeByte, opts.userScheme.encodings.publicKey.encode(cap.userKey)], ); return opts.namespaceScheme.signatures.verify( @@ -171,8 +170,10 @@ export async function isValidCapSubspace< ): Promise { if (cap.delegations.length === 0) { const message = concat( - new Uint8Array([0x2]), - opts.userScheme.encodings.publicKey.encode(cap.userKey), + [ + new Uint8Array([0x2]), + opts.userScheme.encodings.publicKey.encode(cap.userKey), + ], ); return opts.namespaceScheme.signatures.verify( diff --git a/src/meadowcap/meadowcap.test.ts b/src/meadowcap/meadowcap.test.ts index 2d23528..63e768c 100644 --- a/src/meadowcap/meadowcap.test.ts +++ b/src/meadowcap/meadowcap.test.ts @@ -1,13 +1,11 @@ -import { assert, assertEquals, assertRejects } from "$std/assert/mod.ts"; import { ANY_SUBSPACE, - concat, encodeEntry, Entry, KeypairScheme, OPEN_END, orderBytes, -} from "../../deps.ts"; +} from "@earthstar/willow-utils"; import { getGrantedAreaCommunal, getGrantedAreaOwned, @@ -15,6 +13,8 @@ import { getReceiver, } from "../capabilities/semantics.ts"; import { Meadowcap } from "./meadowcap.ts"; +import { concat } from "@std/bytes"; +import { assert, assertEquals, assertRejects } from "@std/assert"; function isCommunal(key: ArrayBuffer): boolean { const ui8 = new Uint8Array(key); @@ -93,7 +93,7 @@ const ecdsaScheme: KeypairScheme = { hash: { name: "SHA-256" }, }, secretKey, - concat(new Uint8Array(publicKey), bytestring), + concat([new Uint8Array(publicKey), bytestring]), ); }, verify: async ( @@ -119,7 +119,7 @@ const ecdsaScheme: KeypairScheme = { }, publicKeyWeb, signature, - concat(new Uint8Array(publicKey), bytestring), + concat([new Uint8Array(publicKey), bytestring]), ); }, }, diff --git a/src/meadowcap/meadowcap.ts b/src/meadowcap/meadowcap.ts index 3c7409c..bb65cc9 100644 --- a/src/meadowcap/meadowcap.ts +++ b/src/meadowcap/meadowcap.ts @@ -2,13 +2,12 @@ import { ANY_SUBSPACE, Area, areaIsIncluded, - concat, encodeEntry, Entry, entryPosition, GrowingBytes, isIncludedArea, -} from "../../deps.ts"; +} from "@earthstar/willow-utils"; import { decodeMcCapability, decodeStreamMcCapability, @@ -45,6 +44,7 @@ import { import { InvalidCapError, MeadowcapError } from "./errors.ts"; import { MeadowcapAuthorisationToken, MeadowcapParams } from "./types.ts"; +import { concat } from "@std/bytes"; /** Represents a configured instantiation of [Meadowcap](https://willowprotocol.org/specs/meadowcap), used for the creation, delegation, and validation of capabilities, and more. * @@ -142,8 +142,7 @@ export class Meadowcap< const accessModeByte = new Uint8Array([accessMode === "read" ? 0x2 : 0x3]); const message = concat( - accessModeByte, - this.params.userScheme.encodings.publicKey.encode(user), + [accessModeByte, this.params.userScheme.encodings.publicKey.encode(user)], ); const signature = await this.params.namespaceKeypairScheme.signatures @@ -443,7 +442,14 @@ export class Meadowcap< } /** Decode a McCapability from bytes. */ - decodeCap(encoded: Uint8Array) { + decodeCap( + encoded: Uint8Array, + ): McCapability< + NamespacePublicKey, + UserPublicKey, + NamespaceSignature, + UserSignature + > { return decodeMcCapability({ pathScheme: this.params.pathScheme, encodingNamespace: this.params.namespaceKeypairScheme.encodings.publicKey, @@ -456,7 +462,14 @@ export class Meadowcap< } /** Decode a McCapability from an incoming stream of bytes. */ - decodeStreamingCap(bytes: GrowingBytes) { + decodeStreamingCap(bytes: GrowingBytes): Promise< + McCapability< + NamespacePublicKey, + UserPublicKey, + NamespaceSignature, + UserSignature + > + > { return decodeStreamMcCapability({ pathScheme: this.params.pathScheme, encodingNamespace: this.params.namespaceKeypairScheme.encodings.publicKey, @@ -510,8 +523,10 @@ export class Meadowcap< > > { const messageToSign = concat( - new Uint8Array([0x2]), - this.params.userScheme.encodings.publicKey.encode(userKey), + [ + new Uint8Array([0x2]), + this.params.userScheme.encodings.publicKey.encode(userKey), + ], ); const signature = await this.params.namespaceKeypairScheme.signatures.sign( @@ -598,7 +613,7 @@ export class Meadowcap< UserSignature >, omitNamespace?: boolean, - ) { + ): Uint8Array { return encodeSubspaceCapability({ omitNamespace, pathScheme: this.params.pathScheme, @@ -612,7 +627,15 @@ export class Meadowcap< } /** Decode a McSubspaceCapability from bytes. */ - decodeSubspaceCap(encoded: Uint8Array, knownNamespace?: NamespacePublicKey) { + decodeSubspaceCap( + encoded: Uint8Array, + knownNamespace?: NamespacePublicKey, + ): McSubspaceCapability< + NamespacePublicKey, + UserPublicKey, + NamespaceSignature, + UserSignature + > { return decodeSubspaceCapability({ knownNamespace, pathScheme: this.params.pathScheme, @@ -629,7 +652,14 @@ export class Meadowcap< decodeStreamingSubspaceCap( bytes: GrowingBytes, knownNamespace?: NamespacePublicKey, - ) { + ): Promise< + McSubspaceCapability< + NamespacePublicKey, + UserPublicKey, + NamespaceSignature, + UserSignature + > + > { return decodeStreamSubspaceCapability({ knownNamespace, pathScheme: this.params.pathScheme, diff --git a/src/meadowcap/types.ts b/src/meadowcap/types.ts index 80fb01f..6acce5c 100644 --- a/src/meadowcap/types.ts +++ b/src/meadowcap/types.ts @@ -3,7 +3,7 @@ import { KeypairScheme, PathScheme, TotalOrder, -} from "../../deps.ts"; +} from "@earthstar/willow-utils"; import { McCapability } from "../capabilities/types.ts"; // Yes. It's a lot.