@serfab/cadre-core 1.21.0 → 1.22.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.
Files changed (106) hide show
  1. package/README.md +294 -294
  2. package/dist/cadre-node.d.ts +329 -53
  3. package/dist/cadre-node.js +767 -85
  4. package/dist/cadre-node.js.map +1 -1
  5. package/dist/control-authorization.d.ts +32 -2
  6. package/dist/control-authorization.js +19 -0
  7. package/dist/control-authorization.js.map +1 -1
  8. package/dist/control-database.d.ts +236 -23
  9. package/dist/control-database.js +587 -173
  10. package/dist/control-database.js.map +1 -1
  11. package/dist/control-schema.d.ts +3 -3
  12. package/dist/control-schema.js +71 -7
  13. package/dist/control-schema.js.map +1 -1
  14. package/dist/ed25519-key.d.ts +8 -0
  15. package/dist/ed25519-key.js +11 -1
  16. package/dist/ed25519-key.js.map +1 -1
  17. package/dist/index.d.ts +5 -1
  18. package/dist/index.js +13 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/machine-removal.d.ts +51 -1
  21. package/dist/machine-removal.js +42 -1
  22. package/dist/machine-removal.js.map +1 -1
  23. package/dist/membership-connection-gater.d.ts +8 -0
  24. package/dist/membership-connection-gater.js +13 -1
  25. package/dist/membership-connection-gater.js.map +1 -1
  26. package/dist/owner-keys.js +2 -1
  27. package/dist/owner-keys.js.map +1 -1
  28. package/dist/owner-signer.js +3 -4
  29. package/dist/owner-signer.js.map +1 -1
  30. package/dist/peer-authorization.d.ts +14 -2
  31. package/dist/peer-authorization.js +24 -1
  32. package/dist/peer-authorization.js.map +1 -1
  33. package/dist/sealed-party-key.d.ts +32 -0
  34. package/dist/sealed-party-key.js +162 -0
  35. package/dist/sealed-party-key.js.map +1 -0
  36. package/dist/seed-bootstrap.d.ts +10 -3
  37. package/dist/seed-bootstrap.js +12 -0
  38. package/dist/seed-bootstrap.js.map +1 -1
  39. package/dist/strand-addr-protocol.d.ts +56 -6
  40. package/dist/strand-addr-protocol.js +57 -5
  41. package/dist/strand-addr-protocol.js.map +1 -1
  42. package/dist/strand-commit-durability.d.ts +33 -2
  43. package/dist/strand-commit-durability.js +44 -4
  44. package/dist/strand-commit-durability.js.map +1 -1
  45. package/dist/strand-database.d.ts +35 -5
  46. package/dist/strand-database.js +34 -10
  47. package/dist/strand-database.js.map +1 -1
  48. package/dist/strand-instance-manager.d.ts +133 -9
  49. package/dist/strand-instance-manager.js +266 -42
  50. package/dist/strand-instance-manager.js.map +1 -1
  51. package/dist/strand-membership-reconciler.d.ts +250 -51
  52. package/dist/strand-membership-reconciler.js +410 -66
  53. package/dist/strand-membership-reconciler.js.map +1 -1
  54. package/dist/strand-membership-writer.d.ts +7 -0
  55. package/dist/strand-membership-writer.js +9 -0
  56. package/dist/strand-membership-writer.js.map +1 -1
  57. package/dist/strand-party-key-rotation.d.ts +177 -0
  58. package/dist/strand-party-key-rotation.js +340 -0
  59. package/dist/strand-party-key-rotation.js.map +1 -0
  60. package/dist/strand-party-machines.d.ts +111 -0
  61. package/dist/strand-party-machines.js +258 -0
  62. package/dist/strand-party-machines.js.map +1 -0
  63. package/dist/strand-party-peers.d.ts +101 -0
  64. package/dist/strand-party-peers.js +49 -0
  65. package/dist/strand-party-peers.js.map +1 -0
  66. package/dist/strand-peer-redial.d.ts +34 -7
  67. package/dist/strand-peer-redial.js +46 -13
  68. package/dist/strand-peer-redial.js.map +1 -1
  69. package/dist/strand-revocation-enforcer.d.ts +15 -1
  70. package/dist/strand-revocation-enforcer.js +16 -9
  71. package/dist/strand-revocation-enforcer.js.map +1 -1
  72. package/dist/strand-transport-key.js +3 -0
  73. package/dist/strand-transport-key.js.map +1 -1
  74. package/dist/strand-watcher.d.ts +7 -4
  75. package/dist/strand-watcher.js +12 -7
  76. package/dist/strand-watcher.js.map +1 -1
  77. package/dist/types.d.ts +160 -13
  78. package/dist/types.js.map +1 -1
  79. package/package.json +9 -8
  80. package/src/cadre-node.ts +12252 -11528
  81. package/src/control-authorization.ts +41 -1
  82. package/src/control-database.ts +6123 -5603
  83. package/src/control-schema.ts +71 -7
  84. package/src/ed25519-key.ts +12 -1
  85. package/src/index.ts +957 -924
  86. package/src/machine-removal.ts +80 -1
  87. package/src/membership-connection-gater.ts +13 -1
  88. package/src/owner-keys.ts +2 -1
  89. package/src/owner-signer.ts +92 -94
  90. package/src/peer-authorization.ts +27 -3
  91. package/src/sealed-party-key.ts +190 -0
  92. package/src/seed-bootstrap.ts +17 -3
  93. package/src/strand-addr-protocol.ts +67 -6
  94. package/src/strand-commit-durability.ts +54 -5
  95. package/src/strand-database.ts +47 -11
  96. package/src/strand-instance-manager.ts +330 -42
  97. package/src/strand-membership-reconciler.ts +1310 -942
  98. package/src/strand-membership-writer.ts +10 -0
  99. package/src/strand-party-key-rotation.ts +436 -0
  100. package/src/strand-party-machines.ts +293 -0
  101. package/src/strand-party-peers.ts +129 -0
  102. package/src/strand-peer-redial.ts +61 -14
  103. package/src/strand-revocation-enforcer.ts +27 -9
  104. package/src/strand-transport-key.ts +3 -0
  105. package/src/strand-watcher.ts +12 -7
  106. package/src/types.ts +3112 -2963
@@ -10,7 +10,7 @@ import { Parser } from '@quereus/quereus';
10
10
  * docs/architecture.md → "Control schema versions and migrations", enforced by
11
11
  * `control-schema-versions.spec.ts`.
12
12
  */
13
- export const CONTROL_SCHEMA_VERSION = 3;
13
+ export const CONTROL_SCHEMA_VERSION = 4;
14
14
 
15
15
  /**
16
16
  * The first control schema version whose fixture is kept, and the oldest a store can be
@@ -48,9 +48,10 @@ export const CONTROL_SCHEMA_DECLARATION = `-- A Sereus party's cadre (its nodes)
48
48
  -- an owner key never accept each other's approvals. FormationUsage is the exception, its
49
49
  -- two digests scoped by the invitation token instead (FormationUsage.Authorized).
50
50
  --
51
- -- Rows of the guarded tables (OwnerKey, ValidationKey, Strand, StrandPartyKey, JoinedStrand,
52
- -- JoinRequest, CadrePeer, DeviceToken, CadreInvite, OwnerGrant) carry a one-off StampId bound
53
- -- into their add and remove digests. \`unique\` holds over live rows only, so a delete retires
51
+ -- Rows of the guarded tables (OwnerKey, ValidationKey, Strand, StrandPartyKey,
52
+ -- PendingStrandPartyKey, JoinedStrand, JoinRequest, CadrePeer, DeviceToken, CadreInvite,
53
+ -- OwnerGrant) carry a one-off StampId bound into their add and remove digests. \`unique\` holds
54
+ -- over live rows only, so a delete retires
54
55
  -- the stamp into Revocation in the same transaction (RevocationRecorded) and an insert refuses
55
56
  -- a retired stamp (NotRevoked): the never-expiring add approval cannot re-seat a removed row.
56
57
  -- A legitimate re-add mints a fresh stamp and a fresh signature. CadreInvite and OwnerGrant
@@ -272,6 +273,43 @@ declare schema CadreControl {
272
273
  )
273
274
  ) with context (OwnerKey text, Signature text);
274
275
 
276
+ -- The key this party's membership in one closed strand is moving to, while the move is under
277
+ -- way. It is written before the strand is touched and swapped into StrandPartyKey only after
278
+ -- the strand-side work is done: seating a fresh key and crashing before the control row changes
279
+ -- would leave a manager seat nobody holds, and changing the control row first would delete the
280
+ -- only copy of the old key, which the seating needs. A table of its own because StrandPartyKey
281
+ -- is keyed on the strand id alone and NoUpdate. Guarded as StrandPartyKey is, with no reap
282
+ -- branch for the same reason: the key is stored nowhere else.
283
+ -- NOTE: unlike a tombstone (Revocation.ReissuedAt), a pending row committed while the remover
284
+ -- was alone has no re-issue path, so a remover that removes alone and is lost before its
285
+ -- rotations finish loses the rows with it. If that ever matters, give this table that idiom.
286
+ table PendingStrandPartyKey (
287
+ Id text primary key, -- the strand id
288
+ PublicKey text, -- the key being moved to, public half (base64url): the Strand.Member.Key it is seated as.
289
+ -- In the clear so a second removal lists it in Superseded without opening the row.
290
+ SealedKey text, -- its private half (StrandPartyKey.PrivateKey encoding), sealed to the identity keys of the
291
+ -- machines that remain (sealed-party-key.ts): the row is written in the removed machine's last
292
+ -- control transaction and reaches it, so what it carries must open only for the others.
293
+ Superseded text null, -- comma-joined public keys (base64url) of earlier pending keys this row replaced.
294
+ -- A second removal replaces the row because the old key may have reached the
295
+ -- machine it removes, and a seat that key already holds must then be revoked.
296
+ StampId text unique,
297
+ constraint NotRevoked check on insert (
298
+ not exists (select 1 from Revocation R where R.TableName = 'PendingStrandPartyKey' and R.StampId = new.StampId)
299
+ ),
300
+ constraint RevocationRecorded check on delete (
301
+ exists (select 1 from Revocation R where R.TableName = 'PendingStrandPartyKey' and R.StampId = old.StampId and R.RowKey = old.Id)
302
+ ),
303
+ constraint NoUpdate check on update (false),
304
+ -- Binding Superseded as well as the key: a captured approval cannot drop a key from the list.
305
+ constraint AuthorizedInsert check on insert (
306
+ exists (select 1 from OwnerKey A where A.Key = context.OwnerKey and not exists (select 1 from Revocation R where R.TableName = 'OwnerKey' and R.StampId = A.StampId) and verify(digest('CadreControl.PendingStrandPartyKey', 'add', party_id(), new.Id, new.PublicKey, new.SealedKey, coalesce(new.Superseded, ''), new.StampId), context.Signature, A.Key, 'ed25519'))
307
+ ),
308
+ constraint AuthorizedDelete check on delete (
309
+ exists (select 1 from OwnerKey A where A.Key = context.OwnerKey and not exists (select 1 from Revocation R where R.TableName = 'OwnerKey' and R.StampId = A.StampId) and verify(digest('CadreControl.PendingStrandPartyKey', 'remove', party_id(), old.Id, old.StampId), context.Signature, A.Key, 'ed25519'))
310
+ )
311
+ ) with context (OwnerKey text, Signature text);
312
+
275
313
  -- A strand this party joined from another party, whose control database holds the Strand
276
314
  -- row. Every machine of the party reads it, so a replica host keeps a copy and the party's
277
315
  -- other machines can attach. A separate table because every reader of Strand takes it to
@@ -559,6 +597,30 @@ declare schema CadreControl {
559
597
  )
560
598
  ) with context (OwnerKey text null, Signature text);
561
599
 
600
+ -- Each machine's own signed statement of the peer id it uses in one strand. That id derives from
601
+ -- the machine's private identity seed (cadre-core strandTransportKey), so no other machine can
602
+ -- compute it, and a machine removal needs it to tell the removed machine's Strand.MemberPeer
603
+ -- binding from its siblings'. A row is a permanent fact: the derivation is deterministic, and a
604
+ -- removed machine's rows are how its orphaned bindings stay attributed to it. So no stamp, no
605
+ -- tombstone and no delete, and no Revocation.RowIsGone branch.
606
+ -- The check defers (it has a subquery), so a CadrePeer row written in the same transaction
607
+ -- counts. A node that does not hold the machine's CadrePeer row yet refuses the insert, and the
608
+ -- writer tries again later. The check runs only on the node that takes the insert, and it cannot
609
+ -- tell whether PublicKey is really the key behind PeerId, so a reader re-verifies Sig against the
610
+ -- key derived from PeerId (ControlDatabase.queryStrandPeers) and judges PeerId against the
611
+ -- membership views.
612
+ table StrandPeer (
613
+ StrandId text, -- a Strand.Id or JoinedStrand.Id
614
+ PeerId text, -- the machine's control peer id (CadrePeer.PeerId)
615
+ StrandPeerId text, -- that machine's peer id in the strand's own network
616
+ Sig text, -- the machine's signature over the 'add' digest
617
+ primary key (StrandId, PeerId),
618
+ constraint Immutable check on update, delete (false),
619
+ constraint Authorized check on insert (
620
+ exists (select 1 from CadrePeer P where P.PeerId = new.PeerId and verify(digest('CadreControl.StrandPeer', 'add', party_id(), new.StrandId, new.PeerId, new.StrandPeerId), new.Sig, P.PublicKey, 'ed25519'))
621
+ )
622
+ );
623
+
562
624
  -- An open invitation to form a strand with this party.
563
625
  table FormationInvite (
564
626
  Token text primary key, -- a random string
@@ -909,9 +971,10 @@ declare schema CadreControl {
909
971
  -- TableName, and queryRevocations skips it. A 'CadreInvite' or 'OwnerGrant' row withdraws
910
972
  -- an invitation or a grant whose row stays (the table comments).
911
973
  table Revocation (
912
- TableName text, -- 'OwnerKey' | 'ValidationKey' | 'Strand' | 'StrandPartyKey' | 'JoinedStrand' |
913
- -- 'JoinRequest' | 'CadrePeer' | 'DeviceToken' | 'CadreInvite' | 'OwnerGrant', or
914
- -- 'Revocation' for an older party's marker
974
+ TableName text, -- 'OwnerKey' | 'ValidationKey' | 'Strand' | 'StrandPartyKey' |
975
+ -- 'PendingStrandPartyKey' | 'JoinedStrand' | 'JoinRequest' | 'CadrePeer' |
976
+ -- 'DeviceToken' | 'CadreInvite' | 'OwnerGrant', or 'Revocation' for an older
977
+ -- party's marker
915
978
  RowKey text, -- primary key of the removed row
916
979
  StampId text, -- the retired stamp
917
980
  ReissuedAt integer default 0, -- bumped by an owner-signed re-issue so a tombstone committed while the
@@ -950,6 +1013,7 @@ declare schema CadreControl {
950
1013
  or (new.TableName = 'ValidationKey' and not exists (select 1 from ValidationKey V where V.StampId = new.StampId))
951
1014
  or (new.TableName = 'Strand' and not exists (select 1 from Strand S where S.StampId = new.StampId))
952
1015
  or (new.TableName = 'StrandPartyKey' and not exists (select 1 from StrandPartyKey K where K.StampId = new.StampId))
1016
+ or (new.TableName = 'PendingStrandPartyKey' and not exists (select 1 from PendingStrandPartyKey K where K.StampId = new.StampId))
953
1017
  or (new.TableName = 'JoinedStrand' and not exists (select 1 from JoinedStrand J where J.StampId = new.StampId))
954
1018
  or (new.TableName = 'JoinRequest' and not exists (select 1 from JoinRequest J where J.StampId = new.StampId))
955
1019
  or (new.TableName = 'DeviceToken' and not exists (select 1 from DeviceToken D where D.StampId = new.StampId))
@@ -1,5 +1,5 @@
1
1
  import { toString as uint8ArrayToString, fromString as uint8ArrayFromString } from 'uint8arrays';
2
- import { getPublicKey } from '@optimystic/quereus-plugin-crypto';
2
+ import { getPublicKey, sign } from '@optimystic/quereus-plugin-crypto';
3
3
  import { peerIdFromString } from '@libp2p/peer-id';
4
4
  import type { PrivateKey } from '@libp2p/interface';
5
5
 
@@ -69,6 +69,17 @@ export function ed25519PublicKeyFromPrivate(privateKeyB64: string): string {
69
69
  return getPublicKey(privateKeyB64, 'ed25519', 'base64url', 'base64url') as string;
70
70
  }
71
71
 
72
+ /**
73
+ * A signer over an in-memory Ed25519 seed, in the shape the control database's signing
74
+ * callbacks take (`ControlSign`, `OwnerSign`): raw message bytes in, a base64url signature out.
75
+ * The crypto plugin takes the bytes as they are (its input encoding applies to a string only).
76
+ *
77
+ * @param privateKeyB64 - The base64url-encoded 32-byte Ed25519 seed.
78
+ */
79
+ export function ed25519MessageSigner(privateKeyB64: string): (message: Uint8Array) => string {
80
+ return (message) => sign(message, privateKeyB64, 'ed25519', 'base64url', 'base64url', 'base64url') as string;
81
+ }
82
+
72
83
  /**
73
84
  * Reject a value that isn't a base64url-encoded 32-byte Ed25519 seed, the private-key form
74
85
  * {@link ed25519PublicKeyFromPrivate} and the crypto plugin's `sign` take. Unlike