@dxos/echo-db 2.33.9-dev.d9963f00 → 2.33.9-dev.eb69ac10

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/echo-db",
3
- "version": "2.33.9-dev.d9963f00",
3
+ "version": "2.33.9-dev.eb69ac10",
4
4
  "description": "ECHO database.",
5
5
  "license": "MIT",
6
6
  "main": "dist/src/index.js",
@@ -13,22 +13,22 @@
13
13
  "src"
14
14
  ],
15
15
  "dependencies": {
16
- "@dxos/async": "2.33.9-dev.d9963f00",
17
- "@dxos/codec-protobuf": "2.33.9-dev.d9963f00",
18
- "@dxos/credentials": "2.33.9-dev.d9963f00",
19
- "@dxos/crypto": "2.33.9-dev.d9963f00",
20
- "@dxos/debug": "2.33.9-dev.d9963f00",
21
- "@dxos/echo-protocol": "2.33.9-dev.d9963f00",
22
- "@dxos/feed-store": "2.33.9-dev.d9963f00",
23
- "@dxos/mesh-protocol": "2.33.9-dev.d9963f00",
24
- "@dxos/model-factory": "2.33.9-dev.d9963f00",
25
- "@dxos/network-manager": "2.33.9-dev.d9963f00",
26
- "@dxos/object-model": "2.33.9-dev.d9963f00",
27
- "@dxos/protocol-plugin-presence": "2.33.9-dev.d9963f00",
28
- "@dxos/protocol-plugin-replicator": "2.33.9-dev.d9963f00",
29
- "@dxos/protocols": "2.33.9-dev.d9963f00",
30
- "@dxos/random-access-multi-storage": "2.33.9-dev.d9963f00",
31
- "@dxos/util": "2.33.9-dev.d9963f00",
16
+ "@dxos/async": "2.33.9-dev.eb69ac10",
17
+ "@dxos/codec-protobuf": "2.33.9-dev.eb69ac10",
18
+ "@dxos/credentials": "2.33.9-dev.eb69ac10",
19
+ "@dxos/crypto": "2.33.9-dev.eb69ac10",
20
+ "@dxos/debug": "2.33.9-dev.eb69ac10",
21
+ "@dxos/echo-protocol": "2.33.9-dev.eb69ac10",
22
+ "@dxos/feed-store": "2.33.9-dev.eb69ac10",
23
+ "@dxos/mesh-protocol": "2.33.9-dev.eb69ac10",
24
+ "@dxos/model-factory": "2.33.9-dev.eb69ac10",
25
+ "@dxos/network-manager": "2.33.9-dev.eb69ac10",
26
+ "@dxos/object-model": "2.33.9-dev.eb69ac10",
27
+ "@dxos/protocol-plugin-presence": "2.33.9-dev.eb69ac10",
28
+ "@dxos/protocol-plugin-replicator": "2.33.9-dev.eb69ac10",
29
+ "@dxos/protocols": "2.33.9-dev.eb69ac10",
30
+ "@dxos/random-access-multi-storage": "2.33.9-dev.eb69ac10",
31
+ "@dxos/util": "2.33.9-dev.eb69ac10",
32
32
  "assert": "^2.0.0",
33
33
  "base-x": "~3.0.9",
34
34
  "buffer-json-encoding": "^1.0.2",
package/src/echo.test.ts CHANGED
@@ -374,7 +374,7 @@ describe('ECHO', () => {
374
374
 
375
375
  }).timeout(10_000);
376
376
 
377
- test.skip('3 devices', async () => {
377
+ test('3 devices', async () => {
378
378
  const a = await setup({ createProfile: true });
379
379
  const b = await setup();
380
380
 
@@ -128,7 +128,8 @@ describe('FeedMuxer', () => {
128
128
  await pipeline.outboundEchoStream!.write({
129
129
  itemId: '123',
130
130
  genesis: {
131
- itemType: 'foo'
131
+ itemType: 'foo',
132
+ modelType: 'bar'
132
133
  }
133
134
  });
134
135
 
@@ -138,7 +139,8 @@ describe('FeedMuxer', () => {
138
139
  expect((echoMessages[0] as any).data).toEqual({
139
140
  itemId: '123',
140
141
  genesis: {
141
- itemType: 'foo'
142
+ itemType: 'foo',
143
+ modelType: 'bar'
142
144
  }
143
145
  });
144
146
  });
@@ -25,13 +25,15 @@ export interface AddPartyOptions {
25
25
  genesisFeed: PublicKey
26
26
  }
27
27
 
28
+ const emptyEchoMetadata = (): EchoMetadata => ({
29
+ version: STORAGE_VERSION,
30
+ parties: [],
31
+ created: new Date(),
32
+ updated: new Date()
33
+ });
34
+
28
35
  export class MetadataStore {
29
- private _metadata: EchoMetadata = {
30
- version: STORAGE_VERSION,
31
- parties: [],
32
- created: new Date(),
33
- updated: new Date()
34
- };
36
+ private _metadata: EchoMetadata = emptyEchoMetadata();
35
37
 
36
38
  constructor (
37
39
  private readonly _directory: Directory
@@ -56,19 +58,26 @@ export class MetadataStore {
56
58
  async load (): Promise<void> {
57
59
  const file = this._directory.createOrOpen('EchoMetadata');
58
60
  try {
59
- const { size } = await file.stat();
60
- if (size === 0) {
61
+ const { size: fileLength } = await file.stat();
62
+ if (fileLength < 4) {
61
63
  return;
62
64
  }
65
+ // Loading file size from first 4 bytes.
66
+ const dataSize = fromBytesInt32(await file.read(0, 4));
67
+ log(`Load: data size ${dataSize}`);
68
+
69
+ // Sanity check.
70
+ {
71
+ if (fileLength < dataSize + 4) {
72
+ throw new Error('Metadata storage is corrupted');
73
+ }
74
+ }
63
75
 
64
- const data = await file.read(0, size);
76
+ const data = await file.read(4, dataSize);
65
77
  this._metadata = schema.getCodecForType('dxos.echo.metadata.EchoMetadata').decode(data);
66
78
  } catch (err: any) {
67
- if (err.code === 'ENOENT') {
68
- return;
69
- } else {
70
- throw err;
71
- }
79
+ log(`Error loading metadata: ${err}`);
80
+ this._metadata = emptyEchoMetadata();
72
81
  } finally {
73
82
  await file.close();
74
83
  }
@@ -87,22 +96,14 @@ export class MetadataStore {
87
96
 
88
97
  try {
89
98
  const encoded = Buffer.from(schema.getCodecForType('dxos.echo.metadata.EchoMetadata').encode(data));
90
- await file.write(0, encoded);
91
99
 
92
- // Truncate the rest of the file.
93
- {
94
- const { size } = await file.stat();
95
- if (size > encoded.length) {
96
- await file.truncate(encoded.length, size);
97
- }
98
- }
100
+ // Saving file size at first 4 bytes.
101
+ log(`Save: data size ${encoded.length}`);
102
+ await file.write(0, toBytesInt32(encoded.length));
103
+
104
+ // Saving data.
105
+ await file.write(4, encoded);
99
106
 
100
- // Sanity check.
101
- const { size } = await file.stat();
102
- if (size !== encoded.length) {
103
- console.log('SANITY!');
104
- }
105
- assert(size === encoded.length);
106
107
  } finally {
107
108
  await file.close();
108
109
  }
@@ -196,3 +197,11 @@ export class MetadataStore {
196
197
  await this._save();
197
198
  }
198
199
  }
200
+
201
+ const toBytesInt32 = (num: number) => {
202
+ const buf = Buffer.alloc(4);
203
+ buf.writeInt32LE(num, 0);
204
+ return buf;
205
+ };
206
+
207
+ const fromBytesInt32 = (buf: Buffer) => buf.readInt32LE(0);
@@ -5,21 +5,18 @@
5
5
  import expect from 'expect';
6
6
  import { it as test } from 'mocha';
7
7
 
8
- import { createAuthMessage, createKeyAdmitMessage, createPartyGenesisMessage, Keyring, KeyType } from '@dxos/credentials';
8
+ import { createAuthMessage, createEnvelopeMessage, createFeedAdmitMessage, createKeyAdmitMessage, createPartyGenesisMessage, Keyring, KeyType, wrapMessage } from '@dxos/credentials';
9
9
 
10
10
  import { PartyProcessor } from '../pipeline';
11
11
  import { createAuthenticator } from './authenticator';
12
- import { CredentialsSigner } from './credentials-signer';
12
+ import { createTestIdentityCredentials } from './identity-credentials';
13
13
 
14
14
  describe('authenticator', () => {
15
- // TODO(dmaretskyi): Figure out how credentials work and if this test makes sense.
16
- test.skip('authenticates admitted peer', async () => {
15
+ test('authenticates party creator', async () => {
17
16
  const keyring = new Keyring();
17
+ const identity = await createTestIdentityCredentials(keyring);
18
18
  const partyKey = await keyring.createKeyRecord({ type: KeyType.PARTY });
19
- const identityKey = await keyring.createKeyRecord({ type: KeyType.IDENTITY });
20
- const deviceKey = await keyring.createKeyRecord({ type: KeyType.DEVICE });
21
- const feedKey = await keyring.createKeyRecord({ type: KeyType.FEED });
22
- const signer = CredentialsSigner.createDirectDeviceSigner(keyring);
19
+ const feedKey = await keyring.createKeyRecord({ type: KeyType.PARTY });
23
20
 
24
21
  const partyProcessor = new PartyProcessor(partyKey.publicKey);
25
22
  await partyProcessor.processMessage({
@@ -27,16 +24,106 @@ describe('authenticator', () => {
27
24
  keyring,
28
25
  partyKey,
29
26
  feedKey.publicKey,
30
- identityKey
27
+ partyKey
31
28
  ),
32
29
  meta: {} as any
33
30
  });
31
+ await partyProcessor.processMessage({
32
+ data: createEnvelopeMessage(
33
+ identity.keyring,
34
+ partyKey.publicKey,
35
+ wrapMessage(identity.identityGenesis),
36
+ [partyKey]
37
+ ),
38
+ meta: {} as any
39
+ });
40
+ await partyProcessor.processMessage({
41
+ data: createFeedAdmitMessage(
42
+ keyring,
43
+ partyKey.publicKey,
44
+ feedKey.publicKey,
45
+ [identity.deviceKeyChain]
46
+ ),
47
+ meta: {} as any
48
+ });
49
+
50
+ const authenticator = createAuthenticator(partyProcessor, identity.createCredentialsSigner(), null as any);
51
+
52
+ //
53
+ // This test follows the same party creation routing as party factory.
54
+ // Oddly, it does not admit the device key to the party.
55
+ // This means that authentication is actually done using the signature created using the feed key.
56
+ //
57
+
58
+ // Does not authenticate without the feed key.
59
+ {
60
+ const credential = createAuthMessage(
61
+ keyring,
62
+ partyKey.publicKey,
63
+ identity.identityKey,
64
+ identity.deviceKey
65
+ );
66
+ expect(await authenticator.authenticate(credential.payload)).toEqual(false);
67
+ }
68
+
69
+ // Does authenticate with the feed key.
70
+ {
71
+ const credential = createAuthMessage(
72
+ keyring,
73
+ partyKey.publicKey,
74
+ identity.identityKey,
75
+ identity.deviceKey,
76
+ feedKey.publicKey
77
+ );
78
+ expect(await authenticator.authenticate(credential.payload)).toEqual(true);
79
+ }
80
+ });
81
+
82
+ test('authenticates another identity', async () => {
83
+ const keyring = new Keyring();
84
+ const identity = await createTestIdentityCredentials(keyring);
85
+ const partyKey = await keyring.createKeyRecord({ type: KeyType.PARTY });
86
+ const feedKey = await keyring.createKeyRecord({ type: KeyType.PARTY });
87
+
88
+ const partyProcessor = new PartyProcessor(partyKey.publicKey);
89
+ await partyProcessor.processMessage({
90
+ data: createPartyGenesisMessage(
91
+ keyring,
92
+ partyKey,
93
+ feedKey.publicKey,
94
+ partyKey
95
+ ),
96
+ meta: {} as any
97
+ });
98
+ await partyProcessor.processMessage({
99
+ data: createEnvelopeMessage(
100
+ identity.keyring,
101
+ partyKey.publicKey,
102
+ wrapMessage(identity.identityGenesis),
103
+ [partyKey]
104
+ ),
105
+ meta: {} as any
106
+ });
107
+ await partyProcessor.processMessage({
108
+ data: createFeedAdmitMessage(
109
+ keyring,
110
+ partyKey.publicKey,
111
+ feedKey.publicKey,
112
+ [identity.deviceKeyChain]
113
+ ),
114
+ meta: {} as any
115
+ });
116
+
117
+ const authenticator = createAuthenticator(partyProcessor, identity.createCredentialsSigner(), null as any);
118
+
119
+ const identity2 = await createTestIdentityCredentials(keyring);
120
+
34
121
  await partyProcessor.processMessage({
35
122
  data: createKeyAdmitMessage(
36
123
  keyring,
37
124
  partyKey.publicKey,
38
- identityKey,
39
- [partyKey, identityKey]
125
+ identity2.identityKey,
126
+ [identity.deviceKeyChain]
40
127
  ),
41
128
  meta: {} as any
42
129
  });
@@ -44,20 +131,18 @@ describe('authenticator', () => {
44
131
  data: createKeyAdmitMessage(
45
132
  keyring,
46
133
  partyKey.publicKey,
47
- deviceKey,
48
- [identityKey, deviceKey]
134
+ identity2.deviceKey,
135
+ [identity.deviceKeyChain]
49
136
  ),
50
137
  meta: {} as any
51
138
  });
52
139
 
53
- const authenticator = createAuthenticator(partyProcessor, signer, null as any);
54
140
  const credential = createAuthMessage(
55
141
  keyring,
56
142
  partyKey.publicKey,
57
- identityKey,
58
- deviceKey
143
+ identity2.identityKey,
144
+ identity2.deviceKey
59
145
  );
60
-
61
146
  expect(await authenticator.authenticate(credential.payload)).toEqual(true);
62
147
  });
63
148
  });
@@ -26,10 +26,14 @@ describe('SnapshotStore', () => {
26
26
 
27
27
  const snapshot: PartySnapshot = {
28
28
  partyKey: key1.asBuffer(),
29
+ halo: {
30
+ messages: []
31
+ },
29
32
  database: {
30
33
  items: [{
31
34
  itemId: createId(),
32
- itemType: 'example:test'
35
+ itemType: 'example:test',
36
+ modelType: 'example:model'
33
37
  }],
34
38
  links: []
35
39
  }