cojson 0.0.1 → 0.0.2

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/.eslintrc.cjs ADDED
@@ -0,0 +1,18 @@
1
+ module.exports = {
2
+ extends: [
3
+ 'eslint:recommended',
4
+ 'plugin:@typescript-eslint/recommended',
5
+ ],
6
+ parser: '@typescript-eslint/parser',
7
+ plugins: ['@typescript-eslint'],
8
+ parserOptions: {
9
+ project: './tsconfig.json',
10
+ },
11
+ root: true,
12
+ rules: {
13
+ "no-unused-vars": "off",
14
+ "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
15
+ "@typescript-eslint/no-floating-promises": "error",
16
+ },
17
+
18
+ };
package/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2023, Garden Computing, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # CoJSON
2
+
3
+ CoJSON ("Collaborative JSON") will be a minimal protocol and implementation for collaborative values (CRDTs + public-key cryptography).
4
+
5
+ CoJSON is developed by [Garden Computing](https://gcmp.io) as the underpinnings of [Jazz](https://jazz.tools), a framework for building apps with telepathic data.
6
+
7
+ The protocol and implementation will cover:
8
+
9
+ - how to represent collaborative values internally
10
+ - the APIs collaborative values expose
11
+ - how to sync and query for collaborative values between peers
12
+ - how to enforce access rights within collaborative values locally and at sync boundaries
13
+
14
+ THIS IS WORK IN PROGRESS
15
+
16
+ ## Core Value Types
17
+
18
+ ### `Immutable` Values (JSON)
19
+ - null
20
+ - boolean
21
+ - number
22
+ - string
23
+ - stringly-encoded CoJSON identifiers & data (`CoValueID`, `AgentID`, `SessionID`, `SignatoryID`, `SignatorySecret`, `Signature`, `RecipientID`, `RecipientSecret`, `Sealed`, `Hash`, `ShortHash`, `KeySecret`, `KeyID`, `Encrypted`, `Role`)
24
+
25
+ - array
26
+ - object
27
+
28
+ ### `Collaborative` Values
29
+ - CoMap (`string` → `Immutable`, last-writer-wins per key)
30
+ - Team (`AgentID` → `Role`)
31
+ - CoList (`Immutable[]`, addressable positions, insertAfter semantics)
32
+ - Agent (`{signatoryID, recipientID}[]`)
33
+ - CoStream (independent per-session streams of `Immutable`s)
34
+ - Static (single addressable `Immutable`)
35
+
36
+ ## Implementation Abstractions
37
+ - CoValue
38
+ - Session Logs
39
+ - Transactions
40
+ - Private (encrypted) transactions
41
+ - Trusting (unencrypted) transactions
42
+ - Rulesets
43
+ - CoValue Content Types
44
+ - LocalNode
45
+ - Peers
46
+ - AgentCredentials
47
+ - Peer
48
+
49
+ ## Extensions & higher-level protocols
50
+
51
+ ### More complex datastructures
52
+ - CoText: a clean way to collaboratively mark up rich text with CoJSON
53
+ - CoJSON Tree: a clean way to represent collaborative tree structures with CoJSON
package/package.json CHANGED
@@ -1,8 +1,31 @@
1
1
  {
2
- "name": "cojson",
3
- "version": "0.0.1",
4
- "description": "Collaborative JSON",
5
- "main": "index.js",
6
- "author": "Garden Computing, Inc.",
7
- "license": "MIT"
2
+ "name": "cojson",
3
+ "module": "src/index.ts",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "version": "0.0.2",
7
+ "devDependencies": {
8
+ "@types/jest": "^29.5.3",
9
+ "@typescript-eslint/eslint-plugin": "^6.2.1",
10
+ "@typescript-eslint/parser": "^6.2.1",
11
+ "eslint": "^8.46.0",
12
+ "jest": "^29.6.2",
13
+ "ts-jest": "^29.1.1",
14
+ "typescript": "5.0.2"
15
+ },
16
+ "dependencies": {
17
+ "@noble/ciphers": "^0.1.3",
18
+ "@noble/curves": "^1.1.0",
19
+ "@noble/hashes": "^1.3.1",
20
+ "@scure/base": "^1.1.1",
21
+ "fast-json-stable-stringify": "^2.1.0",
22
+ "isomorphic-streams": "https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
23
+ },
24
+ "scripts": {
25
+ "test": "jest"
26
+ },
27
+ "jest": {
28
+ "preset": "ts-jest",
29
+ "testEnvironment": "node"
30
+ }
8
31
  }
@@ -0,0 +1,135 @@
1
+ import {
2
+ CoValue,
3
+ Transaction,
4
+ getAgent,
5
+ getAgentID,
6
+ newRandomAgentCredential,
7
+ newRandomSessionID,
8
+ } from "./coValue";
9
+ import { LocalNode } from "./node";
10
+ import { sign } from "./crypto";
11
+
12
+ test("Can create coValue with new agent credentials and add transaction to it", () => {
13
+ const agentCredential = newRandomAgentCredential("agent1");
14
+ const node = new LocalNode(
15
+ agentCredential,
16
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
17
+ );
18
+
19
+ const coValue = node.createCoValue({
20
+ type: "costream",
21
+ ruleset: { type: "unsafeAllowAll" },
22
+ meta: null,
23
+ });
24
+
25
+ const transaction: Transaction = {
26
+ privacy: "trusting",
27
+ madeAt: Date.now(),
28
+ changes: [
29
+ {
30
+ hello: "world",
31
+ },
32
+ ],
33
+ };
34
+
35
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
36
+ node.ownSessionID,
37
+ [transaction]
38
+ );
39
+
40
+ expect(
41
+ coValue.tryAddTransactions(
42
+ node.ownSessionID,
43
+ [transaction],
44
+ expectedNewHash,
45
+ sign(agentCredential.signatorySecret, expectedNewHash)
46
+ )
47
+ ).toBe(true);
48
+ });
49
+
50
+ test("transactions with wrong signature are rejected", () => {
51
+ const wrongAgent = newRandomAgentCredential("wrongAgent");
52
+ const agentCredential = newRandomAgentCredential("agent1");
53
+ const node = new LocalNode(
54
+ agentCredential,
55
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
56
+ );
57
+
58
+ const coValue = node.createCoValue({
59
+ type: "costream",
60
+ ruleset: { type: "unsafeAllowAll" },
61
+ meta: null,
62
+ });
63
+
64
+ const transaction: Transaction = {
65
+ privacy: "trusting",
66
+ madeAt: Date.now(),
67
+ changes: [
68
+ {
69
+ hello: "world",
70
+ },
71
+ ],
72
+ };
73
+
74
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
75
+ node.ownSessionID,
76
+ [transaction]
77
+ );
78
+
79
+ expect(
80
+ coValue.tryAddTransactions(
81
+ node.ownSessionID,
82
+ [transaction],
83
+ expectedNewHash,
84
+ sign(wrongAgent.signatorySecret, expectedNewHash)
85
+ )
86
+ ).toBe(false);
87
+ });
88
+
89
+ test("transactions with correctly signed, but wrong hash are rejected", () => {
90
+ const agentCredential = newRandomAgentCredential("agent1");
91
+ const node = new LocalNode(
92
+ agentCredential,
93
+ newRandomSessionID(getAgentID(getAgent(agentCredential)))
94
+ );
95
+
96
+ const coValue = node.createCoValue({
97
+ type: "costream",
98
+ ruleset: { type: "unsafeAllowAll" },
99
+ meta: null,
100
+ });
101
+
102
+ const transaction: Transaction = {
103
+ privacy: "trusting",
104
+ madeAt: Date.now(),
105
+ changes: [
106
+ {
107
+ hello: "world",
108
+ },
109
+ ],
110
+ };
111
+
112
+ const { expectedNewHash } = coValue.expectedNewHashAfter(
113
+ node.ownSessionID,
114
+ [
115
+ {
116
+ privacy: "trusting",
117
+ madeAt: Date.now(),
118
+ changes: [
119
+ {
120
+ hello: "wrong",
121
+ },
122
+ ],
123
+ },
124
+ ]
125
+ );
126
+
127
+ expect(
128
+ coValue.tryAddTransactions(
129
+ node.ownSessionID,
130
+ [transaction],
131
+ expectedNewHash,
132
+ sign(agentCredential.signatorySecret, expectedNewHash)
133
+ )
134
+ ).toBe(false);
135
+ });