Skip to content

Commit fd6f6e5

Browse files
committed
updated NodeId to use Id type
1 parent e2e1cc8 commit fd6f6e5

File tree

102 files changed

+1594
-1234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1594
-1234
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"@grpc/grpc-js": "1.3.7",
7474
"@matrixai/async-init": "^1.6.0",
7575
"@matrixai/db": "^1.1.2",
76-
"@matrixai/id": "^2.1.0",
76+
"@matrixai/id": "^3.3.0",
7777
"@matrixai/logger": "^2.1.0",
7878
"@matrixai/workers": "^1.2.5",
7979
"ajv": "^7.0.4",

src/acl/ACL.ts

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Ref } from '../types';
77

88
import { Mutex } from 'async-mutex';
99
import Logger from '@matrixai/logger';
10-
import { utils as idUtils } from '@matrixai/id';
10+
import { IdInternal, utils as idUtils } from '@matrixai/id';
1111
import {
1212
CreateDestroyStartStop,
1313
ready,
@@ -130,8 +130,16 @@ class ACL {
130130
nodeId2: NodeId,
131131
): Promise<boolean> {
132132
return await this._transaction(async () => {
133-
const permId1 = await this.db.get(this.aclNodesDbDomain, nodeId1, true);
134-
const permId2 = await this.db.get(this.aclNodesDbDomain, nodeId2, true);
133+
const permId1 = await this.db.get(
134+
this.aclNodesDbDomain,
135+
nodeId1.toBuffer(),
136+
true,
137+
);
138+
const permId2 = await this.db.get(
139+
this.aclNodesDbDomain,
140+
nodeId2.toBuffer(),
141+
true,
142+
);
135143
if (permId1 != null && permId2 != null && permId1 === permId2) {
136144
return true;
137145
}
@@ -147,7 +155,7 @@ class ACL {
147155
Record<NodeId, Permission>
148156
> = {};
149157
for await (const o of this.aclNodesDb.createReadStream()) {
150-
const nodeId = (o as any).key as NodeId;
158+
const nodeId = IdInternal.create<NodeId>((o as any).key);
151159
const data = (o as any).value as Buffer;
152160
const permId = makePermissionId(
153161
await this.db.deserializeDecrypt(data, true),
@@ -196,15 +204,16 @@ class ACL {
196204
);
197205
const nodePerm: Record<NodeId, Permission> = {};
198206
const nodeIdsGc: Set<NodeId> = new Set();
199-
for (const nodeId in nodeIds) {
207+
for (const nodeIdString in nodeIds) {
208+
const nodeId: NodeId = IdInternal.fromString(nodeIdString);
200209
const permId = await this.db.get(
201210
this.aclNodesDbDomain,
202-
nodeId as NodeId,
211+
nodeId.toBuffer(),
203212
true,
204213
);
205214
if (permId == null) {
206215
// Invalid node id
207-
nodeIdsGc.add(nodeId as NodeId);
216+
nodeIdsGc.add(nodeId);
208217
continue;
209218
}
210219
const permRef = (await this.db.get(
@@ -213,7 +222,7 @@ class ACL {
213222
)) as Ref<Permission>;
214223
if (!(vaultId in permRef.object.vaults)) {
215224
// Vault id is missing from the perm
216-
nodeIdsGc.add(nodeId as NodeId);
225+
nodeIdsGc.add(nodeId);
217226
continue;
218227
}
219228
nodePerm[nodeId] = permRef.object;
@@ -244,7 +253,11 @@ class ACL {
244253
@ready(new aclErrors.ErrorACLNotRunning())
245254
public async getNodePerm(nodeId: NodeId): Promise<Permission | undefined> {
246255
return await this._transaction(async () => {
247-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
256+
const permId = await this.db.get(
257+
this.aclNodesDbDomain,
258+
nodeId.toBuffer(),
259+
true,
260+
);
248261
if (permId == null) {
249262
return;
250263
}
@@ -275,15 +288,16 @@ class ACL {
275288
}
276289
const perms: Record<NodeId, Permission> = {};
277290
const nodeIdsGc: Set<NodeId> = new Set();
278-
for (const nodeId in nodeIds) {
291+
for (const nodeIdString in nodeIds) {
292+
const nodeId: NodeId = IdInternal.fromString(nodeIdString);
279293
const permId = await this.db.get(
280294
this.aclNodesDbDomain,
281-
nodeId as NodeId,
295+
nodeId.toBuffer(),
282296
true,
283297
);
284298
if (permId == null) {
285299
// Invalid node id
286-
nodeIdsGc.add(nodeId as NodeId);
300+
nodeIdsGc.add(nodeId);
287301
continue;
288302
}
289303
const permRef = (await this.db.get(
@@ -292,7 +306,7 @@ class ACL {
292306
)) as Ref<Permission>;
293307
if (!(vaultId in permRef.object.vaults)) {
294308
// Vault id is missing from the perm
295-
nodeIdsGc.add(nodeId as NodeId);
309+
nodeIdsGc.add(nodeId);
296310
continue;
297311
}
298312
perms[nodeId] = permRef.object;
@@ -318,7 +332,11 @@ class ACL {
318332
action: GestaltAction,
319333
): Promise<void> {
320334
return await this._transaction(async () => {
321-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
335+
const permId = await this.db.get(
336+
this.aclNodesDbDomain,
337+
nodeId.toBuffer(),
338+
true,
339+
);
322340
const ops: Array<DBOp> = [];
323341
if (permId == null) {
324342
const permId = await aclUtils.generatePermId();
@@ -341,7 +359,7 @@ class ACL {
341359
{
342360
type: 'put',
343361
domain: this.aclNodesDbDomain,
344-
key: nodeId,
362+
key: nodeId.toBuffer(),
345363
value: idUtils.toBuffer(permId),
346364
raw: true,
347365
},
@@ -369,7 +387,11 @@ class ACL {
369387
action: GestaltAction,
370388
): Promise<void> {
371389
return await this._transaction(async () => {
372-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
390+
const permId = await this.db.get(
391+
this.aclNodesDbDomain,
392+
nodeId.toBuffer(),
393+
true,
394+
);
373395
if (permId == null) {
374396
return;
375397
}
@@ -394,7 +416,11 @@ class ACL {
394416
this.aclVaultsDbDomain,
395417
idUtils.toBuffer(vaultId),
396418
)) ?? {};
397-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
419+
const permId = await this.db.get(
420+
this.aclNodesDbDomain,
421+
nodeId.toBuffer(),
422+
true,
423+
);
398424
if (permId == null) {
399425
throw new aclErrors.ErrorACLNodeIdMissing();
400426
}
@@ -419,7 +445,7 @@ class ACL {
419445
{
420446
type: 'put',
421447
domain: this.aclNodesDbDomain,
422-
key: nodeId,
448+
key: nodeId.toBuffer(),
423449
value: permId,
424450
raw: true,
425451
},
@@ -448,7 +474,11 @@ class ACL {
448474
if (nodeIds == null || !(nodeId in nodeIds)) {
449475
return;
450476
}
451-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
477+
const permId = await this.db.get(
478+
this.aclNodesDbDomain,
479+
nodeId.toBuffer(),
480+
true,
481+
);
452482
if (permId == null) {
453483
return;
454484
}
@@ -491,7 +521,7 @@ class ACL {
491521
for (const nodeId of nodeIds) {
492522
const permIdBuffer = await this.db.get(
493523
this.aclNodesDbDomain,
494-
nodeId,
524+
nodeId.toBuffer(),
495525
true,
496526
);
497527
if (permIdBuffer == null) {
@@ -537,7 +567,7 @@ class ACL {
537567
ops.push({
538568
domain: this.aclNodesDbDomain,
539569
type: 'put',
540-
key: nodeId,
570+
key: nodeId.toBuffer(),
541571
value: idUtils.toBuffer(permId),
542572
raw: true,
543573
});
@@ -558,7 +588,11 @@ class ACL {
558588
nodeId: NodeId,
559589
perm: Permission,
560590
): Promise<Array<DBOp>> {
561-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
591+
const permId = await this.db.get(
592+
this.aclNodesDbDomain,
593+
nodeId.toBuffer(),
594+
true,
595+
);
562596
const ops: Array<DBOp> = [];
563597
if (permId == null) {
564598
const permId = await aclUtils.generatePermId();
@@ -576,7 +610,7 @@ class ACL {
576610
{
577611
type: 'put',
578612
domain: this.aclNodesDbDomain,
579-
key: nodeId,
613+
key: nodeId.toBuffer(),
580614
value: idUtils.toBuffer(permId),
581615
raw: true,
582616
},
@@ -608,7 +642,11 @@ class ACL {
608642

609643
@ready(new aclErrors.ErrorACLNotRunning())
610644
public async unsetNodePermOps(nodeId: NodeId): Promise<Array<DBOp>> {
611-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
645+
const permId = await this.db.get(
646+
this.aclNodesDbDomain,
647+
nodeId.toBuffer(),
648+
true,
649+
);
612650
if (permId == null) {
613651
return [];
614652
}
@@ -635,7 +673,7 @@ class ACL {
635673
ops.push({
636674
type: 'del',
637675
domain: this.aclNodesDbDomain,
638-
key: nodeId,
676+
key: nodeId.toBuffer(),
639677
});
640678
// We do not remove the node id from the vaults
641679
// they can be removed later upon inspection
@@ -653,10 +691,11 @@ class ACL {
653691
return;
654692
}
655693
const ops: Array<DBOp> = [];
656-
for (const nodeId in nodeIds) {
694+
for (const nodeIdString in nodeIds) {
695+
const nodeId: NodeId = IdInternal.fromString(nodeIdString);
657696
const permId = await this.db.get(
658697
this.aclNodesDbDomain,
659-
nodeId as NodeId,
698+
nodeId.toBuffer(),
660699
true,
661700
);
662701
// Skip if the nodeId doesn't exist
@@ -703,7 +742,11 @@ class ACL {
703742
nodeIdsJoin: Array<NodeId>,
704743
perm?: Permission,
705744
): Promise<Array<DBOp>> {
706-
const permId = await this.db.get(this.aclNodesDbDomain, nodeId, true);
745+
const permId = await this.db.get(
746+
this.aclNodesDbDomain,
747+
nodeId.toBuffer(),
748+
true,
749+
);
707750
if (permId == null) {
708751
throw new aclErrors.ErrorACLNodeIdMissing();
709752
}
@@ -719,7 +762,7 @@ class ACL {
719762
for (const nodeIdJoin of nodeIdsJoin) {
720763
const permIdJoin = await this.db.get(
721764
this.aclNodesDbDomain,
722-
nodeIdJoin,
765+
nodeIdJoin.toBuffer(),
723766
true,
724767
);
725768
if (permIdJoin === permId) {
@@ -750,7 +793,7 @@ class ACL {
750793
ops.push({
751794
type: 'put',
752795
domain: this.aclNodesDbDomain,
753-
key: nodeIdJoin,
796+
key: nodeIdJoin.toBuffer(),
754797
value: permId,
755798
raw: true,
756799
});
@@ -789,15 +832,16 @@ class ACL {
789832
}
790833
const ops: Array<DBOp> = [];
791834
const nodeIdsGc: Set<NodeId> = new Set();
792-
for (const nodeId in nodeIds) {
835+
for (const nodeIdString in nodeIds) {
836+
const nodeId: NodeId = IdInternal.fromString(nodeIdString);
793837
const permId = await this.db.get(
794838
this.aclNodesDbDomain,
795-
nodeId as NodeId,
839+
nodeId.toBuffer(),
796840
true,
797841
);
798842
if (permId == null) {
799843
// Invalid node id
800-
nodeIdsGc.add(nodeId as NodeId);
844+
nodeIdsGc.add(nodeId);
801845
continue;
802846
}
803847
const permRef = (await this.db.get(
@@ -806,7 +850,7 @@ class ACL {
806850
)) as Ref<Permission>;
807851
if (!(vaultId in permRef.object.vaults)) {
808852
// Vault id is missing from the perm
809-
nodeIdsGc.add(nodeId as NodeId);
853+
nodeIdsGc.add(nodeId);
810854
continue;
811855
}
812856
const vaultActions: VaultActions | undefined =

src/agent/GRPCClientAgent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import type * as notificationsPB from '../proto/js/polykey/v1/notifications/noti
1313
import Logger from '@matrixai/logger';
1414
import { CreateDestroy, ready } from '@matrixai/async-init/dist/CreateDestroy';
1515
import * as agentErrors from './errors';
16-
import { GRPCClient, utils as grpcUtils } from '../grpc';
16+
import * as grpcUtils from '../grpc/utils';
17+
import GRPCClient from '../grpc/GRPCClient';
1718
import { AgentServiceClient } from '../proto/js/polykey/v1/agent_service_grpc_pb';
1819

1920
interface GRPCClientAgent extends CreateDestroy {}

src/agent/service/nodesClosestLocalNodesGet.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@ function nodesClosestLocalNodesGet({
1919
): Promise<void> => {
2020
const response = new nodesPB.NodeTable();
2121
try {
22-
const targetNodeId = nodesUtils.makeNodeId(call.request.getNodeId());
22+
const targetNodeId = nodesUtils.decodeNodeId(call.request.getNodeId());
2323
// Get all local nodes that are closest to the target node from the request
2424
const closestNodes = await nodeManager.getClosestLocalNodes(targetNodeId);
2525
for (const node of closestNodes) {
2626
const addressMessage = new nodesPB.Address();
2727
addressMessage.setHost(node.address.host);
2828
addressMessage.setPort(node.address.port);
2929
// Add the node to the response's map (mapping of node ID -> node address)
30-
response.getNodeTableMap().set(node.id, addressMessage);
30+
response
31+
.getNodeTableMap()
32+
.set(nodesUtils.encodeNodeId(node.id), addressMessage);
3133
}
3234
} catch (err) {
3335
callback(grpcUtils.fromError(err), response);

src/agent/service/nodesCrossSignClaim.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { KeyManager } from '../../keys';
66
import type * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb';
77
import { utils as grpcUtils } from '../../grpc';
88
import { utils as claimsUtils, errors as claimsErrors } from '../../claims';
9+
import { utils as nodesUtils } from '../../nodes';
910

1011
function nodesCrossSignClaim({
1112
keyManager,
@@ -66,7 +67,7 @@ function nodesCrossSignClaim({
6667
}
6768
// Verify the claim
6869
const senderPublicKey = await nodeManager.getPublicKey(
69-
payloadData.node1,
70+
nodesUtils.decodeNodeId(payloadData.node1),
7071
);
7172
const verified = await claimsUtils.verifyClaimSignature(
7273
constructedEncodedClaim,
@@ -79,12 +80,12 @@ function nodesCrossSignClaim({
7980
const doublySignedClaim = await claimsUtils.signIntermediaryClaim({
8081
claim: constructedIntermediaryClaim,
8182
privateKey: keyManager.getRootKeyPairPem().privateKey,
82-
signeeNodeId: nodeManager.getNodeId(),
83+
signeeNodeId: nodesUtils.encodeNodeId(nodeManager.getNodeId()),
8384
});
8485
// Then create your own intermediary node claim (from X -> Y)
8586
const singlySignedClaim = await sigchain.createIntermediaryClaim({
8687
type: 'node',
87-
node1: nodeManager.getNodeId(),
88+
node1: nodesUtils.encodeNodeId(nodeManager.getNodeId()),
8889
node2: payloadData.node1,
8990
});
9091
// Should never be reached, but just for type safety

0 commit comments

Comments
 (0)