cojson 0.8.12 → 0.8.17

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 (164) hide show
  1. package/CHANGELOG.md +95 -83
  2. package/dist/native/PeerKnownStates.js +6 -1
  3. package/dist/native/PeerKnownStates.js.map +1 -1
  4. package/dist/native/PeerState.js +4 -3
  5. package/dist/native/PeerState.js.map +1 -1
  6. package/dist/native/PriorityBasedMessageQueue.js +1 -10
  7. package/dist/native/PriorityBasedMessageQueue.js.map +1 -1
  8. package/dist/native/SyncStateSubscriptionManager.js +70 -0
  9. package/dist/native/SyncStateSubscriptionManager.js.map +1 -0
  10. package/dist/native/base64url.js.map +1 -1
  11. package/dist/native/base64url.test.js +1 -1
  12. package/dist/native/base64url.test.js.map +1 -1
  13. package/dist/native/coValue.js.map +1 -1
  14. package/dist/native/coValueCore.js +141 -149
  15. package/dist/native/coValueCore.js.map +1 -1
  16. package/dist/native/coValueState.js.map +1 -1
  17. package/dist/native/coValues/account.js +6 -6
  18. package/dist/native/coValues/account.js.map +1 -1
  19. package/dist/native/coValues/coList.js +2 -3
  20. package/dist/native/coValues/coList.js.map +1 -1
  21. package/dist/native/coValues/coMap.js +1 -1
  22. package/dist/native/coValues/coMap.js.map +1 -1
  23. package/dist/native/coValues/coStream.js +3 -5
  24. package/dist/native/coValues/coStream.js.map +1 -1
  25. package/dist/native/coValues/group.js +11 -11
  26. package/dist/native/coValues/group.js.map +1 -1
  27. package/dist/native/coreToCoValue.js +2 -2
  28. package/dist/native/coreToCoValue.js.map +1 -1
  29. package/dist/native/crypto/PureJSCrypto.js +4 -4
  30. package/dist/native/crypto/PureJSCrypto.js.map +1 -1
  31. package/dist/native/crypto/crypto.js.map +1 -1
  32. package/dist/native/exports.js +12 -12
  33. package/dist/native/exports.js.map +1 -1
  34. package/dist/native/ids.js.map +1 -1
  35. package/dist/native/jsonStringify.js.map +1 -1
  36. package/dist/native/localNode.js +5 -7
  37. package/dist/native/localNode.js.map +1 -1
  38. package/dist/native/permissions.js +4 -7
  39. package/dist/native/permissions.js.map +1 -1
  40. package/dist/native/priority.js.map +1 -1
  41. package/dist/native/storage/FileSystem.js.map +1 -1
  42. package/dist/native/storage/chunksAndKnownStates.js +2 -4
  43. package/dist/native/storage/chunksAndKnownStates.js.map +1 -1
  44. package/dist/native/storage/index.js +6 -15
  45. package/dist/native/storage/index.js.map +1 -1
  46. package/dist/native/streamUtils.js.map +1 -1
  47. package/dist/native/sync.js +57 -7
  48. package/dist/native/sync.js.map +1 -1
  49. package/dist/native/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  50. package/dist/native/typeUtils/expectGroup.js.map +1 -1
  51. package/dist/native/typeUtils/isAccountID.js.map +1 -1
  52. package/dist/native/typeUtils/isCoValue.js +1 -1
  53. package/dist/native/typeUtils/isCoValue.js.map +1 -1
  54. package/dist/web/PeerKnownStates.js +6 -1
  55. package/dist/web/PeerKnownStates.js.map +1 -1
  56. package/dist/web/PeerState.js +4 -3
  57. package/dist/web/PeerState.js.map +1 -1
  58. package/dist/web/PriorityBasedMessageQueue.js +1 -10
  59. package/dist/web/PriorityBasedMessageQueue.js.map +1 -1
  60. package/dist/web/SyncStateSubscriptionManager.js +70 -0
  61. package/dist/web/SyncStateSubscriptionManager.js.map +1 -0
  62. package/dist/web/base64url.js.map +1 -1
  63. package/dist/web/base64url.test.js +1 -1
  64. package/dist/web/base64url.test.js.map +1 -1
  65. package/dist/web/coValue.js.map +1 -1
  66. package/dist/web/coValueCore.js +141 -149
  67. package/dist/web/coValueCore.js.map +1 -1
  68. package/dist/web/coValueState.js.map +1 -1
  69. package/dist/web/coValues/account.js +6 -6
  70. package/dist/web/coValues/account.js.map +1 -1
  71. package/dist/web/coValues/coList.js +2 -3
  72. package/dist/web/coValues/coList.js.map +1 -1
  73. package/dist/web/coValues/coMap.js +1 -1
  74. package/dist/web/coValues/coMap.js.map +1 -1
  75. package/dist/web/coValues/coStream.js +3 -5
  76. package/dist/web/coValues/coStream.js.map +1 -1
  77. package/dist/web/coValues/group.js +11 -11
  78. package/dist/web/coValues/group.js.map +1 -1
  79. package/dist/web/coreToCoValue.js +2 -2
  80. package/dist/web/coreToCoValue.js.map +1 -1
  81. package/dist/web/crypto/PureJSCrypto.js +4 -4
  82. package/dist/web/crypto/PureJSCrypto.js.map +1 -1
  83. package/dist/web/crypto/WasmCrypto.js +5 -5
  84. package/dist/web/crypto/WasmCrypto.js.map +1 -1
  85. package/dist/web/crypto/crypto.js.map +1 -1
  86. package/dist/web/exports.js +12 -12
  87. package/dist/web/exports.js.map +1 -1
  88. package/dist/web/ids.js.map +1 -1
  89. package/dist/web/jsonStringify.js.map +1 -1
  90. package/dist/web/localNode.js +5 -7
  91. package/dist/web/localNode.js.map +1 -1
  92. package/dist/web/permissions.js +4 -7
  93. package/dist/web/permissions.js.map +1 -1
  94. package/dist/web/priority.js.map +1 -1
  95. package/dist/web/storage/FileSystem.js.map +1 -1
  96. package/dist/web/storage/chunksAndKnownStates.js +2 -4
  97. package/dist/web/storage/chunksAndKnownStates.js.map +1 -1
  98. package/dist/web/storage/index.js +6 -15
  99. package/dist/web/storage/index.js.map +1 -1
  100. package/dist/web/streamUtils.js.map +1 -1
  101. package/dist/web/sync.js +57 -7
  102. package/dist/web/sync.js.map +1 -1
  103. package/dist/web/typeUtils/accountOrAgentIDfromSessionID.js.map +1 -1
  104. package/dist/web/typeUtils/expectGroup.js.map +1 -1
  105. package/dist/web/typeUtils/isAccountID.js.map +1 -1
  106. package/dist/web/typeUtils/isCoValue.js +1 -1
  107. package/dist/web/typeUtils/isCoValue.js.map +1 -1
  108. package/package.json +4 -14
  109. package/src/PeerKnownStates.ts +98 -90
  110. package/src/PeerState.ts +92 -73
  111. package/src/PriorityBasedMessageQueue.ts +42 -49
  112. package/src/SyncStateSubscriptionManager.ts +124 -0
  113. package/src/base64url.test.ts +24 -24
  114. package/src/base64url.ts +44 -45
  115. package/src/coValue.ts +45 -45
  116. package/src/coValueCore.ts +746 -785
  117. package/src/coValueState.ts +82 -72
  118. package/src/coValues/account.ts +143 -150
  119. package/src/coValues/coList.ts +520 -522
  120. package/src/coValues/coMap.ts +283 -285
  121. package/src/coValues/coStream.ts +320 -324
  122. package/src/coValues/group.ts +306 -305
  123. package/src/coreToCoValue.ts +28 -31
  124. package/src/crypto/PureJSCrypto.ts +188 -194
  125. package/src/crypto/WasmCrypto.ts +236 -254
  126. package/src/crypto/crypto.ts +302 -309
  127. package/src/exports.ts +116 -116
  128. package/src/ids.ts +9 -9
  129. package/src/jsonStringify.ts +46 -46
  130. package/src/jsonValue.ts +24 -10
  131. package/src/localNode.ts +635 -660
  132. package/src/media.ts +3 -3
  133. package/src/permissions.ts +272 -278
  134. package/src/priority.ts +21 -19
  135. package/src/storage/FileSystem.ts +91 -99
  136. package/src/storage/chunksAndKnownStates.ts +110 -115
  137. package/src/storage/index.ts +466 -497
  138. package/src/streamUtils.ts +60 -60
  139. package/src/sync.ts +656 -608
  140. package/src/tests/PeerKnownStates.test.ts +38 -34
  141. package/src/tests/PeerState.test.ts +101 -64
  142. package/src/tests/PriorityBasedMessageQueue.test.ts +91 -91
  143. package/src/tests/SyncStateSubscriptionManager.test.ts +232 -0
  144. package/src/tests/account.test.ts +59 -59
  145. package/src/tests/coList.test.ts +65 -65
  146. package/src/tests/coMap.test.ts +137 -137
  147. package/src/tests/coStream.test.ts +254 -257
  148. package/src/tests/coValueCore.test.ts +153 -156
  149. package/src/tests/crypto.test.ts +136 -144
  150. package/src/tests/cryptoImpl.test.ts +205 -197
  151. package/src/tests/group.test.ts +24 -24
  152. package/src/tests/permissions.test.ts +1306 -1371
  153. package/src/tests/priority.test.ts +65 -82
  154. package/src/tests/sync.test.ts +1573 -1263
  155. package/src/tests/testUtils.ts +85 -53
  156. package/src/typeUtils/accountOrAgentIDfromSessionID.ts +4 -4
  157. package/src/typeUtils/expectGroup.ts +9 -9
  158. package/src/typeUtils/isAccountID.ts +1 -1
  159. package/src/typeUtils/isCoValue.ts +9 -9
  160. package/tsconfig.json +4 -6
  161. package/tsconfig.native.json +9 -11
  162. package/tsconfig.web.json +4 -10
  163. package/.eslintrc.cjs +0 -25
  164. package/.prettierrc.js +0 -9
@@ -2,106 +2,116 @@ import { CoValueCore } from "./coValueCore.js";
2
2
  import { PeerID } from "./sync.js";
3
3
 
4
4
  function createResolvablePromise<T>() {
5
- let resolve!: (value: T) => void;
5
+ let resolve!: (value: T) => void;
6
6
 
7
- const promise = new Promise<T>((res) => {
8
- resolve = res;
9
- });
7
+ const promise = new Promise<T>((res) => {
8
+ resolve = res;
9
+ });
10
10
 
11
- return { promise, resolve };
11
+ return { promise, resolve };
12
12
  }
13
13
 
14
14
  class CoValueUnknownState {
15
- type = "unknown" as const;
16
- private peers: Map<PeerID, ReturnType<typeof createResolvablePromise<"available" | "unavailable">>>;
17
- private resolve: (value: "available" | "unavailable") => void;
18
-
19
- ready: Promise<"available" | "unavailable">;
15
+ type = "unknown" as const;
16
+ private peers: Map<
17
+ PeerID,
18
+ ReturnType<typeof createResolvablePromise<"available" | "unavailable">>
19
+ >;
20
+ private resolve: (value: "available" | "unavailable") => void;
21
+
22
+ ready: Promise<"available" | "unavailable">;
23
+
24
+ constructor(peersIds: Iterable<PeerID>) {
25
+ this.peers = new Map();
26
+
27
+ for (const peerId of peersIds) {
28
+ this.peers.set(
29
+ peerId,
30
+ createResolvablePromise<"available" | "unavailable">(),
31
+ );
32
+ }
20
33
 
21
- constructor(peersIds: Iterable<PeerID>) {
22
- this.peers = new Map();
34
+ const { resolve, promise } = createResolvablePromise<
35
+ "available" | "unavailable"
36
+ >();
23
37
 
24
- for (const peerId of peersIds) {
25
- this.peers.set(peerId, createResolvablePromise<"available" | "unavailable">());
26
- }
38
+ this.ready = promise;
39
+ this.resolve = resolve;
40
+ }
27
41
 
28
- const { resolve, promise } = createResolvablePromise<"available" | "unavailable">();
42
+ update(peerId: PeerID, value: "available" | "unavailable") {
43
+ const entry = this.peers.get(peerId);
29
44
 
30
- this.ready = promise;
31
- this.resolve = resolve;
45
+ if (entry) {
46
+ entry.resolve(value);
32
47
  }
33
48
 
34
- update(peerId: PeerID, value: "available" | "unavailable") {
35
- const entry = this.peers.get(peerId);
36
-
37
- if (entry) {
38
- entry.resolve(value);
39
- }
40
-
41
- if (value === "available") {
42
- this.resolve("available");
43
- return;
44
- }
49
+ if (value === "available") {
50
+ this.resolve("available");
51
+ return;
52
+ }
45
53
 
46
- this.peers.delete(peerId);
54
+ this.peers.delete(peerId);
47
55
 
48
- // If none of the peers have the coValue, we resolve to unavailable
49
- if (this.peers.size === 0) {
50
- this.resolve("unavailable");
51
- }
56
+ // If none of the peers have the coValue, we resolve to unavailable
57
+ if (this.peers.size === 0) {
58
+ this.resolve("unavailable");
52
59
  }
60
+ }
53
61
 
54
- // Wait for a specific peer to have a known state
55
- waitForPeer(peerId: PeerID) {
56
- const entry = this.peers.get(peerId);
62
+ // Wait for a specific peer to have a known state
63
+ waitForPeer(peerId: PeerID) {
64
+ const entry = this.peers.get(peerId);
57
65
 
58
- if (!entry) {
59
- return Promise.resolve();
60
- }
61
-
62
- return entry.promise;
66
+ if (!entry) {
67
+ return Promise.resolve();
63
68
  }
69
+
70
+ return entry.promise;
71
+ }
64
72
  }
65
73
 
66
74
  class CoValueAvailableState {
67
- type = "available" as const;
75
+ type = "available" as const;
68
76
 
69
- constructor(public coValue: CoValueCore) { }
77
+ constructor(public coValue: CoValueCore) {}
70
78
  }
71
79
 
72
- type CoValueStateAction = {
73
- type: "not-found";
74
- peerId: PeerID;
75
- } | {
76
- type: "found";
77
- peerId: PeerID;
78
- coValue: CoValueCore;
79
- };
80
+ type CoValueStateAction =
81
+ | {
82
+ type: "not-found";
83
+ peerId: PeerID;
84
+ }
85
+ | {
86
+ type: "found";
87
+ peerId: PeerID;
88
+ coValue: CoValueCore;
89
+ };
80
90
 
81
91
  export class CoValueState {
82
- constructor(public state: CoValueUnknownState | CoValueAvailableState) { }
92
+ constructor(public state: CoValueUnknownState | CoValueAvailableState) {}
83
93
 
84
- static Unknown(peersToWaitFor: Set<PeerID>) {
85
- return new CoValueState(new CoValueUnknownState(peersToWaitFor));
86
- }
94
+ static Unknown(peersToWaitFor: Set<PeerID>) {
95
+ return new CoValueState(new CoValueUnknownState(peersToWaitFor));
96
+ }
97
+
98
+ static Available(coValue: CoValueCore) {
99
+ return new CoValueState(new CoValueAvailableState(coValue));
100
+ }
87
101
 
88
- static Available(coValue: CoValueCore) {
89
- return new CoValueState(new CoValueAvailableState(coValue));
102
+ dispatch(action: CoValueStateAction) {
103
+ if (this.state.type === "available") {
104
+ return;
90
105
  }
91
106
 
92
- dispatch(action: CoValueStateAction) {
93
- if (this.state.type === "available") {
94
- return;
95
- }
96
-
97
- switch (action.type) {
98
- case "not-found":
99
- this.state.update(action.peerId, "unavailable");
100
- break;
101
- case "found":
102
- this.state.update(action.peerId, "available");
103
- this.state = new CoValueAvailableState(action.coValue);
104
- break;
105
- }
107
+ switch (action.type) {
108
+ case "not-found":
109
+ this.state.update(action.peerId, "unavailable");
110
+ break;
111
+ case "found":
112
+ this.state.update(action.peerId, "available");
113
+ this.state = new CoValueAvailableState(action.coValue);
114
+ break;
106
115
  }
116
+ }
107
117
  }
@@ -1,193 +1,186 @@
1
+ import { Result, ok } from "neverthrow";
2
+ import { CoID, RawCoValue } from "../coValue.js";
1
3
  import {
2
- CoValueCore,
3
- CoValueHeader,
4
- CoValueUniqueness,
4
+ CoValueCore,
5
+ CoValueHeader,
6
+ CoValueUniqueness,
5
7
  } from "../coValueCore.js";
6
- import { CoID, RawCoValue } from "../coValue.js";
7
8
  import {
8
- AgentSecret,
9
- CryptoProvider,
10
- SealerID,
11
- SealerSecret,
12
- SignerID,
13
- SignerSecret,
9
+ AgentSecret,
10
+ CryptoProvider,
11
+ SealerID,
12
+ SealerSecret,
13
+ SignerID,
14
+ SignerSecret,
14
15
  } from "../crypto/crypto.js";
15
16
  import { AgentID } from "../ids.js";
16
- import { RawCoMap } from "./coMap.js";
17
- import { RawGroup, InviteSecret } from "./group.js";
18
17
  import { JsonObject } from "../jsonValue.js";
19
- import { err, ok, Result } from "neverthrow";
20
18
  import { LocalNode } from "../localNode.js";
19
+ import { RawCoMap } from "./coMap.js";
20
+ import { InviteSecret, RawGroup } from "./group.js";
21
21
 
22
22
  export function accountHeaderForInitialAgentSecret(
23
- agentSecret: AgentSecret,
24
- crypto: CryptoProvider,
23
+ agentSecret: AgentSecret,
24
+ crypto: CryptoProvider,
25
25
  ): CoValueHeader {
26
- const agent = crypto.getAgentID(agentSecret);
27
- return {
28
- type: "comap",
29
- ruleset: { type: "group", initialAdmin: agent },
30
- meta: {
31
- type: "account",
32
- },
33
- createdAt: null,
34
- uniqueness: null,
35
- };
26
+ const agent = crypto.getAgentID(agentSecret);
27
+ return {
28
+ type: "comap",
29
+ ruleset: { type: "group", initialAdmin: agent },
30
+ meta: {
31
+ type: "account",
32
+ },
33
+ createdAt: null,
34
+ uniqueness: null,
35
+ };
36
36
  }
37
37
 
38
38
  export type InvalidAccountAgentIDError = {
39
- type: "InvalidAccountAgentID";
40
- reason: string;
39
+ type: "InvalidAccountAgentID";
40
+ reason: string;
41
41
  };
42
42
 
43
43
  export class RawAccount<
44
- Meta extends AccountMeta = AccountMeta,
44
+ Meta extends AccountMeta = AccountMeta,
45
45
  > extends RawGroup<Meta> {
46
- _cachedCurrentAgentID: AgentID | undefined;
47
-
48
- currentAgentID(): Result<AgentID, InvalidAccountAgentIDError> {
49
- if (this._cachedCurrentAgentID) {
50
- return ok(this._cachedCurrentAgentID);
51
- }
52
- const agents = this.keys().filter((k): k is AgentID =>
53
- k.startsWith("sealer_"),
54
- );
55
-
56
- if (agents.length !== 1) {
57
- return err({
58
- type: "InvalidAccountAgentID",
59
- reason: "Account has " + agents.length + " agents",
60
- });
61
- }
62
-
63
- this._cachedCurrentAgentID = agents[0];
64
-
65
- return ok(agents[0]!);
46
+ _cachedCurrentAgentID: AgentID | undefined;
47
+
48
+ currentAgentID(): Result<AgentID, InvalidAccountAgentIDError> {
49
+ if (this._cachedCurrentAgentID) {
50
+ return ok(this._cachedCurrentAgentID);
66
51
  }
52
+ const agents = this.keys()
53
+ .filter((k): k is AgentID => k.startsWith("sealer_"))
54
+ .sort(
55
+ (a, b) =>
56
+ (this.lastEditAt(a)?.at.getTime() || 0) -
57
+ (this.lastEditAt(b)?.at.getTime() || 0),
58
+ );
59
+
60
+ if (agents.length !== 1) {
61
+ console.warn("Account has " + agents.length + " agents", this.id);
62
+ }
63
+
64
+ this._cachedCurrentAgentID = agents[0];
65
+
66
+ return ok(agents[0]!);
67
+ }
67
68
  }
68
69
 
69
70
  export interface ControlledAccountOrAgent {
70
- id: RawAccountID | AgentID;
71
- agentSecret: AgentSecret;
72
-
73
- currentAgentID: () => Result<AgentID, InvalidAccountAgentIDError>;
74
- currentSignerID: () => Result<SignerID, InvalidAccountAgentIDError>;
75
- currentSignerSecret: () => SignerSecret;
76
- currentSealerID: () => Result<SealerID, InvalidAccountAgentIDError>;
77
- currentSealerSecret: () => SealerSecret;
71
+ id: RawAccountID | AgentID;
72
+ agentSecret: AgentSecret;
73
+
74
+ currentAgentID: () => Result<AgentID, InvalidAccountAgentIDError>;
75
+ currentSignerID: () => Result<SignerID, InvalidAccountAgentIDError>;
76
+ currentSignerSecret: () => SignerSecret;
77
+ currentSealerID: () => Result<SealerID, InvalidAccountAgentIDError>;
78
+ currentSealerSecret: () => SealerSecret;
78
79
  }
79
80
 
80
81
  /** @hidden */
81
82
  export class RawControlledAccount<Meta extends AccountMeta = AccountMeta>
82
- extends RawAccount<Meta>
83
- implements ControlledAccountOrAgent
83
+ extends RawAccount<Meta>
84
+ implements ControlledAccountOrAgent
84
85
  {
85
- agentSecret: AgentSecret;
86
- crypto: CryptoProvider;
87
-
88
- constructor(core: CoValueCore, agentSecret: AgentSecret) {
89
- super(core);
90
-
91
- this.agentSecret = agentSecret;
92
- this.crypto = core.node.crypto;
93
- }
94
-
95
- /**
96
- * Creates a new group (with the current account as the group's first admin).
97
- * @category 1. High-level
98
- */
99
- createGroup(
100
- uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique(),
101
- ) {
102
- return this.core.node.createGroup(uniqueness);
103
- }
104
-
105
- async acceptInvite<T extends RawCoValue>(
106
- groupOrOwnedValueID: CoID<T>,
107
- inviteSecret: InviteSecret,
108
- ): Promise<void> {
109
- return this.core.node.acceptInvite(groupOrOwnedValueID, inviteSecret);
110
- }
111
-
112
- currentAgentID(): Result<AgentID, InvalidAccountAgentIDError> {
113
- if (this._cachedCurrentAgentID) {
114
- return ok(this._cachedCurrentAgentID);
115
- }
116
- const agentID = this.crypto.getAgentID(this.agentSecret);
117
- this._cachedCurrentAgentID = agentID;
118
- return ok(agentID);
119
- }
120
-
121
- currentSignerID() {
122
- return this.currentAgentID().map((id) =>
123
- this.crypto.getAgentSignerID(id),
124
- );
125
- }
126
-
127
- currentSignerSecret(): SignerSecret {
128
- return this.crypto.getAgentSignerSecret(this.agentSecret);
129
- }
130
-
131
- currentSealerID() {
132
- return this.currentAgentID().map((id) =>
133
- this.crypto.getAgentSealerID(id),
134
- );
135
- }
136
-
137
- currentSealerSecret(): SealerSecret {
138
- return this.crypto.getAgentSealerSecret(this.agentSecret);
86
+ agentSecret: AgentSecret;
87
+ crypto: CryptoProvider;
88
+
89
+ constructor(core: CoValueCore, agentSecret: AgentSecret) {
90
+ super(core);
91
+
92
+ this.agentSecret = agentSecret;
93
+ this.crypto = core.node.crypto;
94
+ }
95
+
96
+ /**
97
+ * Creates a new group (with the current account as the group's first admin).
98
+ * @category 1. High-level
99
+ */
100
+ createGroup(
101
+ uniqueness: CoValueUniqueness = this.core.crypto.createdNowUnique(),
102
+ ) {
103
+ return this.core.node.createGroup(uniqueness);
104
+ }
105
+
106
+ async acceptInvite<T extends RawCoValue>(
107
+ groupOrOwnedValueID: CoID<T>,
108
+ inviteSecret: InviteSecret,
109
+ ): Promise<void> {
110
+ return this.core.node.acceptInvite(groupOrOwnedValueID, inviteSecret);
111
+ }
112
+
113
+ currentAgentID(): Result<AgentID, InvalidAccountAgentIDError> {
114
+ if (this._cachedCurrentAgentID) {
115
+ return ok(this._cachedCurrentAgentID);
139
116
  }
117
+ const agentID = this.crypto.getAgentID(this.agentSecret);
118
+ this._cachedCurrentAgentID = agentID;
119
+ return ok(agentID);
120
+ }
121
+
122
+ currentSignerID() {
123
+ return this.currentAgentID().map((id) => this.crypto.getAgentSignerID(id));
124
+ }
125
+
126
+ currentSignerSecret(): SignerSecret {
127
+ return this.crypto.getAgentSignerSecret(this.agentSecret);
128
+ }
129
+
130
+ currentSealerID() {
131
+ return this.currentAgentID().map((id) => this.crypto.getAgentSealerID(id));
132
+ }
133
+
134
+ currentSealerSecret(): SealerSecret {
135
+ return this.crypto.getAgentSealerSecret(this.agentSecret);
136
+ }
140
137
  }
141
138
 
142
139
  export class ControlledAgent implements ControlledAccountOrAgent {
143
- constructor(
144
- public agentSecret: AgentSecret,
145
- public crypto: CryptoProvider,
146
- ) {}
147
-
148
- get id(): AgentID {
149
- return this.crypto.getAgentID(this.agentSecret);
150
- }
151
-
152
- currentAgentID() {
153
- return ok(this.crypto.getAgentID(this.agentSecret));
154
- }
155
-
156
- currentSignerID() {
157
- return this.currentAgentID().map((id) =>
158
- this.crypto.getAgentSignerID(id),
159
- );
160
- }
161
-
162
- currentSignerSecret(): SignerSecret {
163
- return this.crypto.getAgentSignerSecret(this.agentSecret);
164
- }
165
-
166
- currentSealerID() {
167
- return this.currentAgentID().map((id) =>
168
- this.crypto.getAgentSealerID(id),
169
- );
170
- }
171
-
172
- currentSealerSecret(): SealerSecret {
173
- return this.crypto.getAgentSealerSecret(this.agentSecret);
174
- }
140
+ constructor(
141
+ public agentSecret: AgentSecret,
142
+ public crypto: CryptoProvider,
143
+ ) {}
144
+
145
+ get id(): AgentID {
146
+ return this.crypto.getAgentID(this.agentSecret);
147
+ }
148
+
149
+ currentAgentID() {
150
+ return ok(this.crypto.getAgentID(this.agentSecret));
151
+ }
152
+
153
+ currentSignerID() {
154
+ return this.currentAgentID().map((id) => this.crypto.getAgentSignerID(id));
155
+ }
156
+
157
+ currentSignerSecret(): SignerSecret {
158
+ return this.crypto.getAgentSignerSecret(this.agentSecret);
159
+ }
160
+
161
+ currentSealerID() {
162
+ return this.currentAgentID().map((id) => this.crypto.getAgentSealerID(id));
163
+ }
164
+
165
+ currentSealerSecret(): SealerSecret {
166
+ return this.crypto.getAgentSealerSecret(this.agentSecret);
167
+ }
175
168
  }
176
169
 
177
170
  export type AccountMeta = { type: "account" };
178
171
  export type RawAccountID = CoID<RawAccount>;
179
172
 
180
173
  export type ProfileShape = {
181
- name: string;
174
+ name: string;
182
175
  };
183
176
 
184
177
  export class RawProfile<
185
- Shape extends ProfileShape = ProfileShape,
186
- Meta extends JsonObject | null = JsonObject | null,
178
+ Shape extends ProfileShape = ProfileShape,
179
+ Meta extends JsonObject | null = JsonObject | null,
187
180
  > extends RawCoMap<Shape, Meta> {}
188
181
 
189
182
  export type RawAccountMigration<Meta extends AccountMeta = AccountMeta> = (
190
- account: RawControlledAccount<Meta>,
191
- localNode: LocalNode,
192
- creationProps?: { name: string },
183
+ account: RawControlledAccount<Meta>,
184
+ localNode: LocalNode,
185
+ creationProps?: { name: string },
193
186
  ) => void | Promise<void>;