@peerbit/trusted-network 1.0.16 → 2.0.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/README.md +2 -2
- package/lib/esm/controller.d.ts +7 -7
- package/lib/esm/controller.js +18 -20
- package/lib/esm/controller.js.map +1 -1
- package/package.json +5 -5
- package/src/controller.ts +31 -23
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ class StringStore extends Program
|
|
|
34
34
|
|
|
35
35
|
async setup()
|
|
36
36
|
{
|
|
37
|
-
await store.setup({ encoding: ... ,
|
|
37
|
+
await store.setup({ encoding: ... , canPerform: ..., index: {... canRead ...}})
|
|
38
38
|
await trustedNetwork.setup()
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -52,7 +52,7 @@ await program.network.add(peer2.identity.publicKey)
|
|
|
52
52
|
|
|
53
53
|
// peer2 also has to "join" the network, in practice this means that peer2 adds a record telling that its Peer ID trusts its libp2p Id
|
|
54
54
|
const programPeer2 = await peer2.open(programPeer1.address, {... options ...})
|
|
55
|
-
await peer2.join(programPeer2) // This might fail with "AccessError" if you do this too quickly after "open", because it has not yet
|
|
55
|
+
await peer2.join(programPeer2) // This might fail with "AccessError" if you do this too quickly after "open", because it has not yet received the full trust graph from peer1 yet
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
See [this test(s)](./src/__tests__/network.test.ts) for working examples
|
package/lib/esm/controller.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Documents, Operation, Role } from "@peerbit/document";
|
|
2
|
-
import { AppendOptions
|
|
1
|
+
import { Documents, Operation, PutOperation, Role, CanRead, TransactionContext } from "@peerbit/document";
|
|
2
|
+
import { AppendOptions } from "@peerbit/log";
|
|
3
3
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
|
+
import { DeleteOperation } from "@peerbit/document";
|
|
4
5
|
import { IdentityRelation } from "./identity-graph.js";
|
|
5
6
|
import { Program } from "@peerbit/program";
|
|
6
|
-
import { CanRead } from "@peerbit/rpc";
|
|
7
7
|
import { PeerId } from "@libp2p/interface-peer-id";
|
|
8
8
|
type IdentityGraphArgs = {
|
|
9
|
-
canRead?: CanRead
|
|
9
|
+
canRead?: CanRead<IdentityRelation>;
|
|
10
10
|
role?: Role;
|
|
11
11
|
};
|
|
12
12
|
export declare class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
@@ -15,7 +15,7 @@ export declare class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
|
15
15
|
id?: Uint8Array;
|
|
16
16
|
relationGraph?: Documents<IdentityRelation>;
|
|
17
17
|
});
|
|
18
|
-
|
|
18
|
+
canPerform(operation: PutOperation<IdentityRelation> | DeleteOperation, context: TransactionContext<IdentityRelation>): Promise<boolean>;
|
|
19
19
|
open(options?: IdentityGraphArgs): Promise<void>;
|
|
20
20
|
addRelation(to: PublicSignKey | PeerId, options?: AppendOptions<Operation<IdentityRelation>>): Promise<void>;
|
|
21
21
|
}
|
|
@@ -33,8 +33,8 @@ export declare class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
33
33
|
rootTrust: PublicSignKey | PeerId;
|
|
34
34
|
});
|
|
35
35
|
open(options?: TrustedNetworkArgs): Promise<void>;
|
|
36
|
-
|
|
37
|
-
canRead(
|
|
36
|
+
canPerform(operation: PutOperation<IdentityRelation> | DeleteOperation, context: TransactionContext<IdentityRelation>): Promise<boolean>;
|
|
37
|
+
canRead(relation: any, publicKey?: PublicSignKey): Promise<boolean>;
|
|
38
38
|
add(trustee: PublicSignKey | PeerId): Promise<IdentityRelation | undefined>;
|
|
39
39
|
hasRelation(trustee: PublicSignKey | PeerId, truster?: PublicSignKey | PeerId): Promise<boolean>;
|
|
40
40
|
getRelation(trustee: PublicSignKey | PeerId, truster?: PublicSignKey | PeerId): Promise<IdentityRelation | undefined>;
|
package/lib/esm/controller.js
CHANGED
|
@@ -7,11 +7,11 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { field, serialize, variant } from "@dao-xyz/borsh";
|
|
11
11
|
import { SearchRequest, Documents, PutOperation, Replicator, } from "@peerbit/document";
|
|
12
12
|
import { PublicSignKey, getPublicKeyFromPeerId } from "@peerbit/crypto";
|
|
13
13
|
import { DeleteOperation } from "@peerbit/document";
|
|
14
|
-
import { IdentityRelation, createIdentityGraphStore, getPathGenerator, hasPath, getFromByTo, getToByFrom, getRelation,
|
|
14
|
+
import { IdentityRelation, createIdentityGraphStore, getPathGenerator, hasPath, getFromByTo, getToByFrom, getRelation, } from "./identity-graph.js";
|
|
15
15
|
import { Program } from "@peerbit/program";
|
|
16
16
|
import { sha256Base64Sync } from "@peerbit/crypto";
|
|
17
17
|
const coercePublicKey = (publicKey) => {
|
|
@@ -19,19 +19,17 @@ const coercePublicKey = (publicKey) => {
|
|
|
19
19
|
? publicKey
|
|
20
20
|
: getPublicKeyFromPeerId(publicKey);
|
|
21
21
|
};
|
|
22
|
-
const
|
|
22
|
+
const canPerformByRelation = async (operation, context, isTrusted) => {
|
|
23
23
|
// verify the payload
|
|
24
|
-
const operation = await entry.getPayloadValue();
|
|
25
24
|
if (operation instanceof PutOperation ||
|
|
26
25
|
operation instanceof DeleteOperation) {
|
|
27
26
|
/* const relation: Relation = operation.value || deserialize(operation.data, Relation); */
|
|
28
|
-
const keys = await entry.getPublicKeys();
|
|
27
|
+
const keys = await context.entry.getPublicKeys();
|
|
29
28
|
const checkKey = async (key) => {
|
|
30
29
|
if (operation instanceof PutOperation) {
|
|
31
|
-
// TODO, this clause is only applicable when we modify the identityGraph, but it does not make sense that the
|
|
32
|
-
// be, upon deserialization. There should be known in the `
|
|
33
|
-
const relation = operation.
|
|
34
|
-
operation._value = relation;
|
|
30
|
+
// TODO, this clause is only applicable when we modify the identityGraph, but it does not make sense that the canPerform method does not know what the payload will
|
|
31
|
+
// be, upon deserialization. There should be known in the `canPerform` method whether we are appending to the identityGraph.
|
|
32
|
+
const relation = operation.value;
|
|
35
33
|
if (relation instanceof IdentityRelation) {
|
|
36
34
|
if (!relation.from.equals(key)) {
|
|
37
35
|
return false;
|
|
@@ -68,15 +66,16 @@ export let IdentityGraph = class IdentityGraph extends Program {
|
|
|
68
66
|
props.relationGraph || createIdentityGraphStore(props?.id);
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
|
-
async
|
|
72
|
-
return
|
|
69
|
+
async canPerform(operation, context) {
|
|
70
|
+
return canPerformByRelation(operation, context);
|
|
73
71
|
}
|
|
74
72
|
async open(options) {
|
|
75
73
|
await this.relationGraph.open({
|
|
76
74
|
type: IdentityRelation,
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
canPerform: this.canPerform.bind(this),
|
|
76
|
+
role: options?.role,
|
|
79
77
|
index: {
|
|
78
|
+
canRead: options?.canRead,
|
|
80
79
|
fields: (obj, _entry) => {
|
|
81
80
|
return {
|
|
82
81
|
from: obj.from.hashcode(),
|
|
@@ -84,8 +83,7 @@ export let IdentityGraph = class IdentityGraph extends Program {
|
|
|
84
83
|
};
|
|
85
84
|
},
|
|
86
85
|
},
|
|
87
|
-
|
|
88
|
-
}); // self referencing access controller
|
|
86
|
+
});
|
|
89
87
|
}
|
|
90
88
|
async addRelation(to, options) {
|
|
91
89
|
/* trustee = PublicKey.from(trustee); */
|
|
@@ -114,10 +112,10 @@ export let TrustedNetwork = class TrustedNetwork extends Program {
|
|
|
114
112
|
async open(options) {
|
|
115
113
|
await this.trustGraph.open({
|
|
116
114
|
type: IdentityRelation,
|
|
117
|
-
|
|
118
|
-
canRead: this.canRead.bind(this),
|
|
115
|
+
canPerform: this.canPerform.bind(this),
|
|
119
116
|
role: options?.role,
|
|
120
117
|
index: {
|
|
118
|
+
canRead: this.canRead.bind(this),
|
|
121
119
|
fields: (obj, _entry) => {
|
|
122
120
|
return {
|
|
123
121
|
from: obj.from.hashcode(),
|
|
@@ -127,10 +125,10 @@ export let TrustedNetwork = class TrustedNetwork extends Program {
|
|
|
127
125
|
},
|
|
128
126
|
}); // self referencing access controller
|
|
129
127
|
}
|
|
130
|
-
async
|
|
131
|
-
return
|
|
128
|
+
async canPerform(operation, context) {
|
|
129
|
+
return canPerformByRelation(operation, context, (key) => this.isTrusted(key));
|
|
132
130
|
}
|
|
133
|
-
async canRead(
|
|
131
|
+
async canRead(relation, publicKey) {
|
|
134
132
|
return true; // TODO should we have read access control?
|
|
135
133
|
}
|
|
136
134
|
async add(trustee) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAe,KAAK,EAAE,SAAS,EAAE,OAAO,EAAO,MAAM,gBAAgB,CAAC;AAC7E,OAAO,EACN,aAAa,EACb,SAAS,EAET,YAAY,EACZ,UAAU,GAIV,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EACN,gBAAgB,EAChB,wBAAwB,EACxB,gBAAgB,EAChB,OAAO,EACP,WAAW,EACX,WAAW,EACX,WAAW,GAEX,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGnD,MAAM,eAAe,GAAG,CAAC,SAAiC,EAAE,EAAE;IAC7D,OAAO,SAAS,YAAY,aAAa;QACxC,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,sBAAsB,CAAC,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AE;gBAC9D,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE;aACtB,CAAC,CAAC;YACH,OAAO,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;SAC9C;IACF,CAAC;IAED,KAAK,CAAC,eAAe,CACpB,OAAsB,EACtB,UAAyB,IAAI,CAAC,SAAS;QAEvC,MAAM,SAAS,GAAG,MAAM,OAAO,CAC9B,OAAO,EACP,OAAO,EACP,IAAI,CAAC,UAAU,EACf,WAAW,CACX,CAAC;QACF,OAAO,CAAC,CAAC,SAAS,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,UAAU;QACf,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC;QAC/B,MAAM,YAAY,GAAoB,CAAC,OAAO,CAAC,CAAC;QAChD,MAAM,SAAS,GAAG,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QAC1E,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,SAAS,EAAE;YACnC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SAC3B;QACD,OAAO,YAAY,CAAC;IACrB,CAAC;IAED,QAAQ;QACP,OAAO,gBAAgB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;CACD,CAAA;AAtIA;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;8BACpB,aAAa;iDAAC;AAGzB;IADC,KAAK,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC;8BACf,SAAS;kDAAmB;AAL5B,cAAc;IAD1B,OAAO,CAAC,iBAAiB,CAAC;;GACd,cAAc,CAwI1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/trusted-network",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Access controller that operates on a DB",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -31,13 +31,13 @@
|
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@dao-xyz/borsh": "^5.1.5",
|
|
34
|
-
"@peerbit/crypto": "1.0.
|
|
35
|
-
"@peerbit/document": "
|
|
34
|
+
"@peerbit/crypto": "1.0.5",
|
|
35
|
+
"@peerbit/document": "3.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@ethersproject/wallet": "^5.7.0",
|
|
39
|
-
"@peerbit/test-utils": "^1.0.
|
|
39
|
+
"@peerbit/test-utils": "^1.0.16",
|
|
40
40
|
"@peerbit/time": "1.0.2"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "464e807d679e24b897b7811ac99d6f85fbd756f9"
|
|
43
43
|
}
|
package/src/controller.ts
CHANGED
|
@@ -6,6 +6,8 @@ import {
|
|
|
6
6
|
PutOperation,
|
|
7
7
|
Replicator,
|
|
8
8
|
Role,
|
|
9
|
+
CanRead,
|
|
10
|
+
TransactionContext,
|
|
9
11
|
} from "@peerbit/document";
|
|
10
12
|
import { AppendOptions, Entry } from "@peerbit/log";
|
|
11
13
|
import { PublicSignKey, getPublicKeyFromPeerId } from "@peerbit/crypto";
|
|
@@ -21,7 +23,6 @@ import {
|
|
|
21
23
|
AbstractRelation,
|
|
22
24
|
} from "./identity-graph.js";
|
|
23
25
|
import { Program } from "@peerbit/program";
|
|
24
|
-
import { CanRead } from "@peerbit/rpc";
|
|
25
26
|
import { sha256Base64Sync } from "@peerbit/crypto";
|
|
26
27
|
import { PeerId } from "@libp2p/interface-peer-id";
|
|
27
28
|
|
|
@@ -30,28 +31,25 @@ const coercePublicKey = (publicKey: PublicSignKey | PeerId) => {
|
|
|
30
31
|
? publicKey
|
|
31
32
|
: getPublicKeyFromPeerId(publicKey);
|
|
32
33
|
};
|
|
33
|
-
const
|
|
34
|
-
|
|
34
|
+
const canPerformByRelation = async (
|
|
35
|
+
operation: PutOperation<IdentityRelation> | DeleteOperation,
|
|
36
|
+
context: TransactionContext<IdentityRelation>,
|
|
35
37
|
isTrusted?: (key: PublicSignKey) => Promise<boolean>
|
|
36
38
|
): Promise<boolean> => {
|
|
37
39
|
// verify the payload
|
|
38
|
-
const operation = await entry.getPayloadValue();
|
|
39
40
|
if (
|
|
40
41
|
operation instanceof PutOperation ||
|
|
41
42
|
operation instanceof DeleteOperation
|
|
42
43
|
) {
|
|
43
44
|
/* const relation: Relation = operation.value || deserialize(operation.data, Relation); */
|
|
44
45
|
|
|
45
|
-
const keys = await entry.getPublicKeys();
|
|
46
|
+
const keys = await context.entry.getPublicKeys();
|
|
46
47
|
const checkKey = async (key: PublicSignKey): Promise<boolean> => {
|
|
47
48
|
if (operation instanceof PutOperation) {
|
|
48
|
-
// TODO, this clause is only applicable when we modify the identityGraph, but it does not make sense that the
|
|
49
|
-
// be, upon deserialization. There should be known in the `
|
|
50
|
-
|
|
51
|
-
const relation: AbstractRelation =
|
|
52
|
-
operation._value || deserialize(operation.data, AbstractRelation);
|
|
53
|
-
operation._value = relation;
|
|
49
|
+
// TODO, this clause is only applicable when we modify the identityGraph, but it does not make sense that the canPerform method does not know what the payload will
|
|
50
|
+
// be, upon deserialization. There should be known in the `canPerform` method whether we are appending to the identityGraph.
|
|
54
51
|
|
|
52
|
+
const relation = operation.value;
|
|
55
53
|
if (relation instanceof IdentityRelation) {
|
|
56
54
|
if (!relation.from.equals(key)) {
|
|
57
55
|
return false;
|
|
@@ -79,7 +77,8 @@ const canAppendByRelation = async (
|
|
|
79
77
|
}
|
|
80
78
|
};
|
|
81
79
|
|
|
82
|
-
type IdentityGraphArgs = { canRead?: CanRead
|
|
80
|
+
type IdentityGraphArgs = { canRead?: CanRead<IdentityRelation>; role?: Role };
|
|
81
|
+
|
|
83
82
|
@variant("relations")
|
|
84
83
|
export class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
85
84
|
@field({ type: Documents })
|
|
@@ -96,16 +95,20 @@ export class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
|
96
95
|
}
|
|
97
96
|
}
|
|
98
97
|
|
|
99
|
-
async
|
|
100
|
-
|
|
98
|
+
async canPerform(
|
|
99
|
+
operation: PutOperation<IdentityRelation> | DeleteOperation,
|
|
100
|
+
context: TransactionContext<IdentityRelation>
|
|
101
|
+
): Promise<boolean> {
|
|
102
|
+
return canPerformByRelation(operation, context);
|
|
101
103
|
}
|
|
102
104
|
|
|
103
105
|
async open(options?: IdentityGraphArgs) {
|
|
104
106
|
await this.relationGraph.open({
|
|
105
107
|
type: IdentityRelation,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
+
canPerform: this.canPerform.bind(this),
|
|
109
|
+
role: options?.role,
|
|
108
110
|
index: {
|
|
111
|
+
canRead: options?.canRead,
|
|
109
112
|
fields: (obj, _entry) => {
|
|
110
113
|
return {
|
|
111
114
|
from: obj.from.hashcode(),
|
|
@@ -113,8 +116,7 @@ export class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
|
113
116
|
};
|
|
114
117
|
},
|
|
115
118
|
},
|
|
116
|
-
|
|
117
|
-
}); // self referencing access controller
|
|
119
|
+
});
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
async addRelation(
|
|
@@ -137,6 +139,7 @@ export class IdentityGraph extends Program<IdentityGraphArgs> {
|
|
|
137
139
|
*/
|
|
138
140
|
|
|
139
141
|
type TrustedNetworkArgs = { role?: Role };
|
|
142
|
+
|
|
140
143
|
@variant("trusted_network")
|
|
141
144
|
export class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
142
145
|
@field({ type: PublicSignKey })
|
|
@@ -154,10 +157,10 @@ export class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
154
157
|
async open(options?: TrustedNetworkArgs) {
|
|
155
158
|
await this.trustGraph.open({
|
|
156
159
|
type: IdentityRelation,
|
|
157
|
-
|
|
158
|
-
canRead: this.canRead.bind(this),
|
|
160
|
+
canPerform: this.canPerform.bind(this),
|
|
159
161
|
role: options?.role,
|
|
160
162
|
index: {
|
|
163
|
+
canRead: this.canRead.bind(this),
|
|
161
164
|
fields: (obj, _entry) => {
|
|
162
165
|
return {
|
|
163
166
|
from: obj.from.hashcode(),
|
|
@@ -168,11 +171,16 @@ export class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
168
171
|
}); // self referencing access controller
|
|
169
172
|
}
|
|
170
173
|
|
|
171
|
-
async
|
|
172
|
-
|
|
174
|
+
async canPerform(
|
|
175
|
+
operation: PutOperation<IdentityRelation> | DeleteOperation,
|
|
176
|
+
context: TransactionContext<IdentityRelation>
|
|
177
|
+
): Promise<boolean> {
|
|
178
|
+
return canPerformByRelation(operation, context, (key) =>
|
|
179
|
+
this.isTrusted(key)
|
|
180
|
+
);
|
|
173
181
|
}
|
|
174
182
|
|
|
175
|
-
async canRead(
|
|
183
|
+
async canRead(relation: any, publicKey?: PublicSignKey): Promise<boolean> {
|
|
176
184
|
return true; // TODO should we have read access control?
|
|
177
185
|
}
|
|
178
186
|
|