Skip to content

Commit

Permalink
Update for JSR
Browse files Browse the repository at this point in the history
  • Loading branch information
sgwilym committed May 18, 2024
1 parent 9c1d13c commit 0476c41
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 84 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 0 additions & 3 deletions deps.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/capabilities/encoding.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<Uint8Array> {
return {
Expand Down
82 changes: 41 additions & 41 deletions src/capabilities/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { getGrantedAreaCommunal, getGrantedAreaOwned } from "./semantics.ts";
import {
ANY_SUBSPACE,
Area,
concat,
decodeAreaInArea,
decodeCompactWidth,
decodeStreamAreaInArea,
Expand All @@ -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<
Expand Down Expand Up @@ -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],
);
}

Expand All @@ -98,9 +95,7 @@ export function handoverCommunal<
const newPubKey = opts.userScheme.encodings.publicKey.encode(newUser);

return concat(
areaInArea,
userSignature,
newPubKey,
[areaInArea, userSignature, newPubKey],
);
}

Expand Down Expand Up @@ -153,9 +148,7 @@ export function handoverOwned<
);

return concat(
areaInArea,
userSignature,
userPublicKey,
[areaInArea, userSignature, userPublicKey],
);
}

Expand All @@ -180,9 +173,7 @@ export function handoverOwned<
);

return concat(
areaInArea,
userSignature,
userPublicKey,
[areaInArea, userSignature, userPublicKey],
);
}

Expand Down Expand Up @@ -222,8 +213,7 @@ export function handoverSubspace<
);

return concat(
userSignature,
userPublicKey,
[userSignature, userPublicKey],
);
}

Expand All @@ -234,10 +224,12 @@ export function handoverSubspace<
);

return concat(
userSignature,
opts.userScheme.encodings.publicKey.encode(
newUser,
),
[
userSignature,
opts.userScheme.encodings.publicKey.encode(
newUser,
),
],
);
}

Expand Down Expand Up @@ -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,
],
);
}

Expand Down Expand Up @@ -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,
],
);
}

Expand Down Expand Up @@ -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,
],
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/semantics.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/capabilities/types.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
13 changes: 7 additions & 6 deletions src/capabilities/validity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
areaIsIncluded,
concat,
KeypairScheme,
PathScheme,
} from "../../deps.ts";
} from "@earthstar/willow-utils";
import { UserScheme } from "../meadowcap/types.ts";
import {
handoverCommunal,
Expand All @@ -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<
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -171,8 +170,10 @@ export async function isValidCapSubspace<
): Promise<boolean> {
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(
Expand Down
10 changes: 5 additions & 5 deletions src/meadowcap/meadowcap.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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,
getGrantedNamespace,
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);
Expand Down Expand Up @@ -93,7 +93,7 @@ const ecdsaScheme: KeypairScheme<ArrayBuffer, CryptoKey, ArrayBuffer> = {
hash: { name: "SHA-256" },
},
secretKey,
concat(new Uint8Array(publicKey), bytestring),
concat([new Uint8Array(publicKey), bytestring]),
);
},
verify: async (
Expand All @@ -119,7 +119,7 @@ const ecdsaScheme: KeypairScheme<ArrayBuffer, CryptoKey, ArrayBuffer> = {
},
publicKeyWeb,
signature,
concat(new Uint8Array(publicKey), bytestring),
concat([new Uint8Array(publicKey), bytestring]),
);
},
},
Expand Down
Loading

0 comments on commit 0476c41

Please sign in to comment.