@atcute/did-plc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.ts ADDED
@@ -0,0 +1,66 @@
1
+ import * as CBOR from '@atcute/cbor';
2
+ import { verifySigWithDidKey } from '@atcute/crypto';
3
+ import { fromBase64Url } from '@atcute/multibase';
4
+
5
+ import * as t from './types.js';
6
+
7
+ export const wrapHttpPrefix = (str: string): string => {
8
+ if (str.startsWith('http://') || str.startsWith('https://')) {
9
+ return str;
10
+ }
11
+
12
+ return `https://${str}`;
13
+ };
14
+
15
+ export const wrapAtprotoPrefix = (str: string): string => {
16
+ if (str.startsWith('at://')) {
17
+ return str;
18
+ }
19
+
20
+ const stripped = str.replace('http://', '').replace('https://', '');
21
+
22
+ return `at://${stripped}`;
23
+ };
24
+
25
+ export const normalizeOp = (op: t.CompatibleOperation): t.Operation => {
26
+ if (op.type === 'create') {
27
+ return {
28
+ type: 'plc_operation',
29
+ prev: op.prev,
30
+ sig: op.sig,
31
+ rotationKeys: [op.recoveryKey, op.signingKey],
32
+ verificationMethods: {
33
+ atproto: op.signingKey,
34
+ },
35
+ alsoKnownAs: [wrapAtprotoPrefix(op.handle)],
36
+ services: {
37
+ atproto_pds: {
38
+ type: 'AtprotoPersonalDataServer',
39
+ endpoint: wrapHttpPrefix(op.service),
40
+ },
41
+ },
42
+ };
43
+ }
44
+
45
+ return op;
46
+ };
47
+
48
+ export const isSignedOperationValid = async (
49
+ allowedKeys: t.DidKeyString[],
50
+ op: t.CompatibleOperationOrTombstone,
51
+ ): Promise<t.DidKeyString | null> => {
52
+ const { sig, ...unsignedOp } = op;
53
+
54
+ const sigBytes = fromBase64Url(sig);
55
+ const opBytes = CBOR.encode(unsignedOp);
56
+
57
+ for (const key of allowedKeys) {
58
+ const ok = await verifySigWithDidKey(key, sigBytes, opBytes);
59
+
60
+ if (ok) {
61
+ return key;
62
+ }
63
+ }
64
+
65
+ return null;
66
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "type": "module",
3
+ "name": "@atcute/did-plc",
4
+ "version": "0.1.0",
5
+ "description": "validations, type definitions and schemas for did:plc operations",
6
+ "keywords": [
7
+ "atproto",
8
+ "did",
9
+ "did-method-plc"
10
+ ],
11
+ "license": "MIT",
12
+ "repository": {
13
+ "url": "https://github.com/mary-ext/atcute",
14
+ "directory": "packages/identity/did-plc"
15
+ },
16
+ "files": [
17
+ "dist/",
18
+ "lib/",
19
+ "!lib/**/*.bench.ts",
20
+ "!lib/**/*.test.ts"
21
+ ],
22
+ "exports": {
23
+ ".": "./dist/index.js"
24
+ },
25
+ "sideEffects": false,
26
+ "devDependencies": {
27
+ "@types/bun": "^1.2.1"
28
+ },
29
+ "dependencies": {
30
+ "@badrap/valita": "^0.4.2",
31
+ "@atcute/cbor": "^2.1.3",
32
+ "@atcute/cid": "^2.1.0",
33
+ "@atcute/crypto": "^2.2.0",
34
+ "@atcute/multibase": "^1.1.2",
35
+ "@atcute/uint8array": "^1.0.1"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc --project tsconfig.build.json",
39
+ "test": "bun test --coverage",
40
+ "prepublish": "rm -rf dist; pnpm run build"
41
+ }
42
+ }