@peerbit/trusted-network 6.0.99 → 6.0.101
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 +37 -50
- package/dist/src/controller.d.ts +7 -0
- package/dist/src/controller.d.ts.map +1 -1
- package/dist/src/controller.js +47 -15
- package/dist/src/controller.js.map +1 -1
- package/package.json +6 -6
- package/src/controller.ts +58 -14
package/README.md
CHANGED
|
@@ -1,59 +1,46 @@
|
|
|
1
1
|
# Trusted network
|
|
2
|
-
## 🚧 Experimental state 🚧
|
|
3
|
-
|
|
4
|
-
A store that lets you build trusted networks of identities.
|
|
5
|
-
|
|
6
|
-
The store is defined by the "root trust" which has the responsibility in the beginning to trust additional identities. Later, these identities can add more identities to the network.
|
|
7
|
-
Trusted identities can also be revoked.
|
|
8
|
-
|
|
9
|
-
Distributing content among untrusted peers will be unreliable, and not resilient to malicious parties that start to participate in the replication process with large amount (>> min replicas) of nodes followed by shutting them down simultaneously (no way for the original peers recover all lost data). To mitigate this, you can launch your program in a "Network", which is basically a list of nodes that trust each other. Symbolically you could thing of this as a VPC.
|
|
10
|
-
|
|
11
|
-
To do this, you only have to implement the "Network" interface:
|
|
12
|
-
```typescript
|
|
13
|
-
import { Peerbit, Network } from 'peerbit'
|
|
14
|
-
import { Log } from '@peerbit/log'
|
|
15
|
-
import { Program } from '@peerbit/program'
|
|
16
|
-
import { TrustedNetwork } from '@peerbit/trusted-network'
|
|
17
|
-
import { field, variant } from '@dao-xyz/borst-ts'
|
|
18
|
-
|
|
19
|
-
@variant("string_store")
|
|
20
|
-
@network({property: 'network'})
|
|
21
|
-
class StringStore extends Program
|
|
22
|
-
{
|
|
23
|
-
@field({type: Store})
|
|
24
|
-
log: Log<Uint8Array>
|
|
25
|
-
|
|
26
|
-
@field({type: TrustedNetwork})
|
|
27
|
-
network: TrustedNetwork // this is a database storing all peers. Peers that are trusted can add new peers
|
|
28
|
-
|
|
29
|
-
constructor(properties:{ log: Store<any>, network: TrustedNetwork }) {
|
|
30
|
-
|
|
31
|
-
this.log = properties.store
|
|
32
|
-
this.network = properties.network;
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async setup()
|
|
37
|
-
{
|
|
38
|
-
await store.setup({ encoding: ... , canPerform: ..., index: {... canRead ...}})
|
|
39
|
-
await trustedNetwork.setup()
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
2
|
|
|
3
|
+
## 🚧 Experimental state 🚧
|
|
43
4
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
5
|
+
`@peerbit/trusted-network` stores a directed graph of trust between signing
|
|
6
|
+
identities. A configured root identity is always trusted. An identity is also
|
|
7
|
+
trusted when the replicated graph contains a path from the root to that
|
|
8
|
+
identity.
|
|
47
9
|
|
|
48
|
-
|
|
10
|
+
```ts
|
|
11
|
+
import { TrustedNetwork } from "@peerbit/trusted-network";
|
|
49
12
|
|
|
50
|
-
|
|
51
|
-
|
|
13
|
+
const network = await peer.open(
|
|
14
|
+
new TrustedNetwork({ rootTrust: rootPeer.identity.publicKey }),
|
|
15
|
+
);
|
|
52
16
|
|
|
17
|
+
// The local append identity owns the new local -> member edge.
|
|
18
|
+
await network.add(memberPeer.identity.publicKey);
|
|
53
19
|
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
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
|
|
20
|
+
// Only that same owner can remove the edge.
|
|
21
|
+
await network.revoke(memberPeer.identity.publicKey);
|
|
57
22
|
```
|
|
58
23
|
|
|
59
|
-
|
|
24
|
+
Trust relations are directional. A trusted identity may add an outgoing edge
|
|
25
|
+
of its own, but it cannot add an edge on behalf of another identity. Likewise,
|
|
26
|
+
`revoke()` removes only the caller's direct outgoing edge while that owner is
|
|
27
|
+
still trusted. For example, B may revoke B → C, and the root may revoke root →
|
|
28
|
+
B, but B cannot delete root → C. Calling `revoke()` for an absent local edge is
|
|
29
|
+
a no-op and returns `undefined`.
|
|
30
|
+
|
|
31
|
+
Revoking an edge immediately affects future `isTrusted()` checks after the
|
|
32
|
+
operation has replicated. Removing root → B also makes identities reachable
|
|
33
|
+
only through B untrusted. It does not retroactively revalidate application
|
|
34
|
+
writes that replicas accepted before the revocation.
|
|
35
|
+
|
|
36
|
+
The graph itself is currently readable by any peer (`canRead` allows all), and
|
|
37
|
+
replication is not a confidentiality boundary. Applications that require a
|
|
38
|
+
private membership graph or confidential content must add content-layer
|
|
39
|
+
encryption and key rotation.
|
|
40
|
+
|
|
41
|
+
## Upgrade note
|
|
42
|
+
|
|
43
|
+
Releases before the owner-authorized revoke change accepted a relation delete
|
|
44
|
+
signed by any currently trusted identity. Deploy the security update across all
|
|
45
|
+
writers and validating replicas before relying on the stronger rule; an old
|
|
46
|
+
replica can still accept an unauthorized delete that an updated replica rejects.
|
package/dist/src/controller.d.ts
CHANGED
|
@@ -37,6 +37,13 @@ export declare class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
37
37
|
canPerform(properties: CanPerformOperations<IdentityRelation>): Promise<boolean>;
|
|
38
38
|
canRead(relation: any, publicKey?: PublicSignKey): Promise<boolean>;
|
|
39
39
|
add(trustee: PublicSignKey | PeerId, options?: AppendOptions<Operation>): Promise<IdentityRelation>;
|
|
40
|
+
/**
|
|
41
|
+
* Revoke this identity's direct trust in `trustee`.
|
|
42
|
+
*
|
|
43
|
+
* A caller can only remove the outgoing edge signed by the append identity.
|
|
44
|
+
* The operation is idempotent and returns `undefined` when that edge is absent.
|
|
45
|
+
*/
|
|
46
|
+
revoke(trustee: PublicSignKey | PeerId, options?: AppendOptions<Operation>): Promise<IdentityRelation | undefined>;
|
|
40
47
|
hasRelation(trustee: PublicSignKey | PeerId, truster?: PublicSignKey | PeerId): Promise<boolean>;
|
|
41
48
|
getRelation(trustee: PublicSignKey | PeerId, truster?: PublicSignKey | PeerId): Promise<IdentityRelation>;
|
|
42
49
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACN,aAAa,EAGb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,oBAAoB,EACzB,KAAK,OAAO,EAEZ,KAAK,aAAa,EAClB,KAAK,SAAS,EAGd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EACN,MAAM,EACN,gBAAgB,EAOhB,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACN,aAAa,EAGb,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,oBAAoB,EACzB,KAAK,OAAO,EAEZ,KAAK,aAAa,EAClB,KAAK,SAAS,EAGd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EACN,MAAM,EACN,gBAAgB,EAOhB,MAAM,qBAAqB,CAAC;AA+E7B,KAAK,iBAAiB,GAAG;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAC1B,SAAS,CAAC,EAAE,kBAAkB,CAAC;CAC/B,CAAC;AAEF,qBACa,aAAc,SAAQ,OAAO,CAAC,i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AAC,EAAE,aAAa,CAAC,SAAS,CAAC,GAChC,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC;IAalC,WAAW,CAChB,OAAO,EAAE,aAAa,GAAG,MAAM,EAC/B,OAAO,GAAE,aAAa,GAAG,MAAuB;IAIjD,WAAW,CACV,OAAO,EAAE,aAAa,GAAG,MAAM,EAC/B,OAAO,GAAE,aAAa,GAAG,MAAuB;IASjD;;;;;;;;;OASG;IACG,SAAS,CACd,OAAO,EAAE,aAAa,GAAG,MAAM,EAC/B,OAAO,GAAE,aAAa,GAAG,MAAuB,GAC9C,OAAO,CAAC,OAAO,CAAC;IA4Bb,eAAe,CACpB,OAAO,EAAE,aAAa,EACtB,OAAO,GAAE,aAA8B,GACrC,OAAO,CAAC,OAAO,CAAC;IAUb,UAAU,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC;IAU5C,QAAQ,IAAI,MAAM;CAGlB"}
|
package/dist/src/controller.js
CHANGED
|
@@ -65,21 +65,27 @@ const coercePublicKey = (publicKey) => {
|
|
|
65
65
|
return getPublicKeyFromPeerId(publicKey);
|
|
66
66
|
};
|
|
67
67
|
const canPerformByRelation = async (properties, isTrusted) => {
|
|
68
|
-
|
|
68
|
+
let owner;
|
|
69
|
+
try {
|
|
70
|
+
if (!properties.value) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
owner = coercePublicKey(properties.value.from);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
69
78
|
const keys = await properties.entry.getPublicKeys();
|
|
70
79
|
const checkKey = async (key) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const signer = coercePublicKey(key);
|
|
75
|
-
if (!from.equals(signer)) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
+
try {
|
|
81
|
+
const signer = coercePublicKey(key);
|
|
82
|
+
if (!owner.equals(signer)) {
|
|
80
83
|
return false;
|
|
81
84
|
}
|
|
82
85
|
}
|
|
86
|
+
catch {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
83
89
|
if (isTrusted) {
|
|
84
90
|
const trusted = await isTrusted(key);
|
|
85
91
|
return trusted;
|
|
@@ -96,7 +102,7 @@ const canPerformByRelation = async (properties, isTrusted) => {
|
|
|
96
102
|
}
|
|
97
103
|
return false;
|
|
98
104
|
};
|
|
99
|
-
const defaultIdentityGraphCanPerform = policy.or(policy.put(policy.signedByField("_from")), policy.
|
|
105
|
+
const defaultIdentityGraphCanPerform = policy.or(policy.put(policy.signedByField("_from")), policy.deleteSignedByExistingField("_from"));
|
|
100
106
|
let IdentityGraph = (() => {
|
|
101
107
|
let _classDecorators = [variant("relations")];
|
|
102
108
|
let _classDescriptor;
|
|
@@ -204,25 +210,51 @@ let TrustedNetwork = (() => {
|
|
|
204
210
|
}); // self referencing access controller
|
|
205
211
|
}
|
|
206
212
|
async canPerform(properties) {
|
|
207
|
-
|
|
213
|
+
if (properties.type === "put") {
|
|
214
|
+
return canPerformByRelation(properties, (key) => this.isTrusted(key));
|
|
215
|
+
}
|
|
216
|
+
// Arbitrary canPerform callbacks do not make Documents decode the removed
|
|
217
|
+
// value. Resolve it locally so the delete signature can be checked against
|
|
218
|
+
// the owner of the exact relation being removed.
|
|
219
|
+
const relation = await this.trustGraph.index.get(properties.operation.key, {
|
|
220
|
+
remote: false,
|
|
221
|
+
local: true,
|
|
222
|
+
});
|
|
223
|
+
return canPerformByRelation({ ...properties, value: relation }, (key) => this.isTrusted(key));
|
|
208
224
|
}
|
|
209
225
|
async canRead(relation, publicKey) {
|
|
210
226
|
return true; // TODO should we have read access control?
|
|
211
227
|
}
|
|
212
228
|
async add(trustee, options) {
|
|
213
229
|
const key = coercePublicKey(trustee);
|
|
214
|
-
const truster = coercePublicKey(this.node.identity.publicKey);
|
|
215
|
-
const existingRelation = await this.getRelation(
|
|
230
|
+
const truster = coercePublicKey(options?.identity?.publicKey || this.node.identity.publicKey);
|
|
231
|
+
const existingRelation = await this.getRelation(truster, key);
|
|
216
232
|
if (!existingRelation) {
|
|
217
233
|
const relation = new IdentityRelation({
|
|
218
234
|
to: key,
|
|
219
235
|
from: truster,
|
|
220
236
|
});
|
|
221
|
-
await this.trustGraph.put(relation);
|
|
237
|
+
await this.trustGraph.put(relation, options);
|
|
222
238
|
return relation;
|
|
223
239
|
}
|
|
224
240
|
return existingRelation;
|
|
225
241
|
}
|
|
242
|
+
/**
|
|
243
|
+
* Revoke this identity's direct trust in `trustee`.
|
|
244
|
+
*
|
|
245
|
+
* A caller can only remove the outgoing edge signed by the append identity.
|
|
246
|
+
* The operation is idempotent and returns `undefined` when that edge is absent.
|
|
247
|
+
*/
|
|
248
|
+
async revoke(trustee, options) {
|
|
249
|
+
const key = coercePublicKey(trustee);
|
|
250
|
+
const truster = coercePublicKey(options?.identity?.publicKey || this.node.identity.publicKey);
|
|
251
|
+
const relation = await this.getRelation(truster, key);
|
|
252
|
+
if (!relation) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
await this.trustGraph.del(relation.id, options);
|
|
256
|
+
return relation;
|
|
257
|
+
}
|
|
226
258
|
async hasRelation(trustee, truster = this.rootTrust) {
|
|
227
259
|
return !!(await this.getRelation(trustee, truster));
|
|
228
260
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACxE,OAAO,EAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACN,aAAa,EACb,sBAAsB,EACtB,gBAAgB,GAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAGN,SAAS,EAGT,aAAa,EACb,MAAM,GACN,MAAM,mBAAmB,CAAC;AAC3B,OAAO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,EACP,IAAI,CAAC,UAAU,EACf,oBAAoB,CACpB,CAAC;YACF,OAAO,CAAC,CAAC,SAAS,CAAC;QACpB,CAAC;QAED,KAAK,CAAC,UAAU;YACf,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC;YAC/B,MAAM,YAAY,GAAoB,CAAC,OAAO,CAAC,CAAC;YAChD,MAAM,SAAS,GAAG,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;YAC1E,IAAI,KAAK,EAAE,MAAM,IAAI,IAAI,SAAS,EAAE,CAAC;gBACpC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAC5B,CAAC;YACD,OAAO,YAAY,CAAC;QACrB,CAAC;QAED,QAAQ;YACP,OAAO,gBAAgB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QAC1C,CAAC;;;;SArLW,cAAc;AAuL3B;;;;;;;;;;;;;EAaE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/trusted-network",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.101",
|
|
4
4
|
"description": "Access controller that operates on a DB",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -56,14 +56,14 @@
|
|
|
56
56
|
"@libp2p/interface": "^3.2.5",
|
|
57
57
|
"uint8arrays": "^5.1.0",
|
|
58
58
|
"@peerbit/crypto": "3.1.6",
|
|
59
|
-
"@peerbit/log": "6.2.
|
|
60
|
-
"@peerbit/document": "15.0.
|
|
61
|
-
"@peerbit/
|
|
62
|
-
"@peerbit/
|
|
59
|
+
"@peerbit/log": "6.2.24",
|
|
60
|
+
"@peerbit/document": "15.0.15",
|
|
61
|
+
"@peerbit/program": "6.0.55",
|
|
62
|
+
"@peerbit/shared-log": "16.0.14"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"ethers": "^6.17.0",
|
|
66
|
-
"@peerbit/test-utils": "3.1.
|
|
66
|
+
"@peerbit/test-utils": "3.1.34",
|
|
67
67
|
"@peerbit/time": "3.0.1"
|
|
68
68
|
},
|
|
69
69
|
"repository": {
|
package/src/controller.ts
CHANGED
|
@@ -63,20 +63,27 @@ const canPerformByRelation = async (
|
|
|
63
63
|
properties: CanPerformOperations<IdentityRelation>,
|
|
64
64
|
isTrusted?: (key: PublicSignKey) => Promise<boolean>,
|
|
65
65
|
): Promise<boolean> => {
|
|
66
|
-
|
|
66
|
+
let owner: PublicSignKey;
|
|
67
|
+
try {
|
|
68
|
+
if (!properties.value) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
owner = coercePublicKey(properties.value.from);
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
67
76
|
const keys = await properties.entry.getPublicKeys();
|
|
68
77
|
const checkKey = async (key: PublicSignKey): Promise<boolean> => {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const signer = coercePublicKey(key);
|
|
73
|
-
if (!from.equals(signer)) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
} catch {
|
|
78
|
+
try {
|
|
79
|
+
const signer = coercePublicKey(key);
|
|
80
|
+
if (!owner.equals(signer)) {
|
|
77
81
|
return false;
|
|
78
82
|
}
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
79
85
|
}
|
|
86
|
+
|
|
80
87
|
if (isTrusted) {
|
|
81
88
|
const trusted = await isTrusted(key);
|
|
82
89
|
return trusted;
|
|
@@ -95,7 +102,7 @@ const canPerformByRelation = async (
|
|
|
95
102
|
|
|
96
103
|
const defaultIdentityGraphCanPerform = policy.or<IdentityRelation>(
|
|
97
104
|
policy.put(policy.signedByField<IdentityRelation>("_from")),
|
|
98
|
-
policy.
|
|
105
|
+
policy.deleteSignedByExistingField<IdentityRelation>("_from"),
|
|
99
106
|
);
|
|
100
107
|
|
|
101
108
|
type IdentityGraphArgs = {
|
|
@@ -200,7 +207,20 @@ export class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
200
207
|
async canPerform(
|
|
201
208
|
properties: CanPerformOperations<IdentityRelation>,
|
|
202
209
|
): Promise<boolean> {
|
|
203
|
-
|
|
210
|
+
if (properties.type === "put") {
|
|
211
|
+
return canPerformByRelation(properties, (key) => this.isTrusted(key));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Arbitrary canPerform callbacks do not make Documents decode the removed
|
|
215
|
+
// value. Resolve it locally so the delete signature can be checked against
|
|
216
|
+
// the owner of the exact relation being removed.
|
|
217
|
+
const relation = await this.trustGraph.index.get(properties.operation.key, {
|
|
218
|
+
remote: false,
|
|
219
|
+
local: true,
|
|
220
|
+
});
|
|
221
|
+
return canPerformByRelation({ ...properties, value: relation }, (key) =>
|
|
222
|
+
this.isTrusted(key),
|
|
223
|
+
);
|
|
204
224
|
}
|
|
205
225
|
|
|
206
226
|
async canRead(relation: any, publicKey?: PublicSignKey): Promise<boolean> {
|
|
@@ -212,20 +232,44 @@ export class TrustedNetwork extends Program<TrustedNetworkArgs> {
|
|
|
212
232
|
options?: AppendOptions<Operation>,
|
|
213
233
|
) {
|
|
214
234
|
const key = coercePublicKey(trustee);
|
|
215
|
-
const truster = coercePublicKey(
|
|
235
|
+
const truster = coercePublicKey(
|
|
236
|
+
options?.identity?.publicKey || this.node.identity.publicKey,
|
|
237
|
+
);
|
|
216
238
|
|
|
217
|
-
const existingRelation = await this.getRelation(
|
|
239
|
+
const existingRelation = await this.getRelation(truster, key);
|
|
218
240
|
if (!existingRelation) {
|
|
219
241
|
const relation = new IdentityRelation({
|
|
220
242
|
to: key,
|
|
221
243
|
from: truster,
|
|
222
244
|
});
|
|
223
|
-
await this.trustGraph!.put(relation);
|
|
245
|
+
await this.trustGraph!.put(relation, options);
|
|
224
246
|
return relation;
|
|
225
247
|
}
|
|
226
248
|
return existingRelation;
|
|
227
249
|
}
|
|
228
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Revoke this identity's direct trust in `trustee`.
|
|
253
|
+
*
|
|
254
|
+
* A caller can only remove the outgoing edge signed by the append identity.
|
|
255
|
+
* The operation is idempotent and returns `undefined` when that edge is absent.
|
|
256
|
+
*/
|
|
257
|
+
async revoke(
|
|
258
|
+
trustee: PublicSignKey | PeerId,
|
|
259
|
+
options?: AppendOptions<Operation>,
|
|
260
|
+
): Promise<IdentityRelation | undefined> {
|
|
261
|
+
const key = coercePublicKey(trustee);
|
|
262
|
+
const truster = coercePublicKey(
|
|
263
|
+
options?.identity?.publicKey || this.node.identity.publicKey,
|
|
264
|
+
);
|
|
265
|
+
const relation = await this.getRelation(truster, key);
|
|
266
|
+
if (!relation) {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
await this.trustGraph.del(relation.id, options);
|
|
270
|
+
return relation;
|
|
271
|
+
}
|
|
272
|
+
|
|
229
273
|
async hasRelation(
|
|
230
274
|
trustee: PublicSignKey | PeerId,
|
|
231
275
|
truster: PublicSignKey | PeerId = this.rootTrust,
|