@orbitmem/sdk 0.1.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 (176) hide show
  1. package/README.md +104 -0
  2. package/dist/agent/agent-adapter.d.ts +3 -0
  3. package/dist/agent/agent-adapter.d.ts.map +1 -0
  4. package/dist/agent/agent-adapter.js +3 -0
  5. package/dist/agent/agent-adapter.js.map +1 -0
  6. package/dist/agent/client.d.ts +5 -0
  7. package/dist/agent/client.d.ts.map +1 -0
  8. package/dist/agent/client.js +146 -0
  9. package/dist/agent/client.js.map +1 -0
  10. package/dist/agent/index.d.ts +2 -0
  11. package/dist/agent/index.d.ts.map +1 -0
  12. package/dist/agent/index.js +2 -0
  13. package/dist/agent/index.js.map +1 -0
  14. package/dist/client.d.ts +3 -0
  15. package/dist/client.d.ts.map +1 -0
  16. package/dist/client.js +118 -0
  17. package/dist/client.js.map +1 -0
  18. package/dist/contracts.d.ts +19 -0
  19. package/dist/contracts.d.ts.map +1 -0
  20. package/dist/contracts.js +28 -0
  21. package/dist/contracts.js.map +1 -0
  22. package/dist/data/index.d.ts +5 -0
  23. package/dist/data/index.d.ts.map +1 -0
  24. package/dist/data/index.js +5 -0
  25. package/dist/data/index.js.map +1 -0
  26. package/dist/data/orbitdb.d.ts +10 -0
  27. package/dist/data/orbitdb.d.ts.map +1 -0
  28. package/dist/data/orbitdb.js +39 -0
  29. package/dist/data/orbitdb.js.map +1 -0
  30. package/dist/data/pricing.d.ts +7 -0
  31. package/dist/data/pricing.d.ts.map +1 -0
  32. package/dist/data/pricing.js +55 -0
  33. package/dist/data/pricing.js.map +1 -0
  34. package/dist/data/serialization.d.ts +28 -0
  35. package/dist/data/serialization.d.ts.map +1 -0
  36. package/dist/data/serialization.js +76 -0
  37. package/dist/data/serialization.js.map +1 -0
  38. package/dist/data/vault.d.ts +21 -0
  39. package/dist/data/vault.d.ts.map +1 -0
  40. package/dist/data/vault.js +284 -0
  41. package/dist/data/vault.js.map +1 -0
  42. package/dist/discovery/discovery-layer.d.ts +3 -0
  43. package/dist/discovery/discovery-layer.d.ts.map +1 -0
  44. package/dist/discovery/discovery-layer.js +205 -0
  45. package/dist/discovery/discovery-layer.js.map +1 -0
  46. package/dist/discovery/index.d.ts +4 -0
  47. package/dist/discovery/index.d.ts.map +1 -0
  48. package/dist/discovery/index.js +4 -0
  49. package/dist/discovery/index.js.map +1 -0
  50. package/dist/discovery/mock-registry.d.ts +30 -0
  51. package/dist/discovery/mock-registry.d.ts.map +1 -0
  52. package/dist/discovery/mock-registry.js +71 -0
  53. package/dist/discovery/mock-registry.js.map +1 -0
  54. package/dist/discovery/on-chain-registry.d.ts +35 -0
  55. package/dist/discovery/on-chain-registry.d.ts.map +1 -0
  56. package/dist/discovery/on-chain-registry.js +199 -0
  57. package/dist/discovery/on-chain-registry.js.map +1 -0
  58. package/dist/encryption/aes.d.ts +15 -0
  59. package/dist/encryption/aes.d.ts.map +1 -0
  60. package/dist/encryption/aes.js +63 -0
  61. package/dist/encryption/aes.js.map +1 -0
  62. package/dist/encryption/encryption-layer.d.ts +8 -0
  63. package/dist/encryption/encryption-layer.d.ts.map +1 -0
  64. package/dist/encryption/encryption-layer.js +82 -0
  65. package/dist/encryption/encryption-layer.js.map +1 -0
  66. package/dist/encryption/index.d.ts +6 -0
  67. package/dist/encryption/index.d.ts.map +1 -0
  68. package/dist/encryption/index.js +4 -0
  69. package/dist/encryption/index.js.map +1 -0
  70. package/dist/encryption/lit.d.ts +23 -0
  71. package/dist/encryption/lit.d.ts.map +1 -0
  72. package/dist/encryption/lit.js +113 -0
  73. package/dist/encryption/lit.js.map +1 -0
  74. package/dist/encryption/vault-key.d.ts +37 -0
  75. package/dist/encryption/vault-key.d.ts.map +1 -0
  76. package/dist/encryption/vault-key.js +43 -0
  77. package/dist/encryption/vault-key.js.map +1 -0
  78. package/dist/identity/identity-layer.d.ts +3 -0
  79. package/dist/identity/identity-layer.d.ts.map +1 -0
  80. package/dist/identity/identity-layer.js +99 -0
  81. package/dist/identity/identity-layer.js.map +1 -0
  82. package/dist/identity/index.d.ts +4 -0
  83. package/dist/identity/index.d.ts.map +1 -0
  84. package/dist/identity/index.js +4 -0
  85. package/dist/identity/index.js.map +1 -0
  86. package/dist/identity/ows-adapter.d.ts +15 -0
  87. package/dist/identity/ows-adapter.d.ts.map +1 -0
  88. package/dist/identity/ows-adapter.js +67 -0
  89. package/dist/identity/ows-adapter.js.map +1 -0
  90. package/dist/identity/session.d.ts +10 -0
  91. package/dist/identity/session.d.ts.map +1 -0
  92. package/dist/identity/session.js +36 -0
  93. package/dist/identity/session.js.map +1 -0
  94. package/dist/index.d.ts +12 -0
  95. package/dist/index.d.ts.map +1 -0
  96. package/dist/index.js +12 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/persistence/create-agent.d.ts +11 -0
  99. package/dist/persistence/create-agent.d.ts.map +1 -0
  100. package/dist/persistence/create-agent.js +47 -0
  101. package/dist/persistence/create-agent.js.map +1 -0
  102. package/dist/persistence/index.d.ts +3 -0
  103. package/dist/persistence/index.d.ts.map +1 -0
  104. package/dist/persistence/index.js +3 -0
  105. package/dist/persistence/index.js.map +1 -0
  106. package/dist/persistence/persistence-layer.d.ts +12 -0
  107. package/dist/persistence/persistence-layer.d.ts.map +1 -0
  108. package/dist/persistence/persistence-layer.js +194 -0
  109. package/dist/persistence/persistence-layer.js.map +1 -0
  110. package/dist/transport/index.d.ts +3 -0
  111. package/dist/transport/index.d.ts.map +1 -0
  112. package/dist/transport/index.js +3 -0
  113. package/dist/transport/index.js.map +1 -0
  114. package/dist/transport/relay-session.d.ts +41 -0
  115. package/dist/transport/relay-session.d.ts.map +1 -0
  116. package/dist/transport/relay-session.js +86 -0
  117. package/dist/transport/relay-session.js.map +1 -0
  118. package/dist/transport/transport-layer.d.ts +32 -0
  119. package/dist/transport/transport-layer.d.ts.map +1 -0
  120. package/dist/transport/transport-layer.js +110 -0
  121. package/dist/transport/transport-layer.js.map +1 -0
  122. package/dist/types.d.ts +1319 -0
  123. package/dist/types.d.ts.map +1 -0
  124. package/dist/types.js +7 -0
  125. package/dist/types.js.map +1 -0
  126. package/package.json +91 -0
  127. package/src/__tests__/client.test.ts +30 -0
  128. package/src/__tests__/orbitdb-availability.ts +8 -0
  129. package/src/agent/__tests__/agent-adapter.test.ts +50 -0
  130. package/src/agent/__tests__/client.test.ts +50 -0
  131. package/src/agent/agent-adapter.ts +2 -0
  132. package/src/agent/client.ts +158 -0
  133. package/src/agent/index.ts +1 -0
  134. package/src/client.ts +134 -0
  135. package/src/contracts.ts +44 -0
  136. package/src/data/__tests__/pricing.test.ts +73 -0
  137. package/src/data/__tests__/vault-encryption.test.ts +346 -0
  138. package/src/data/__tests__/vault.test.ts +75 -0
  139. package/src/data/index.ts +8 -0
  140. package/src/data/orbitdb.ts +47 -0
  141. package/src/data/pricing.ts +63 -0
  142. package/src/data/serialization.ts +108 -0
  143. package/src/data/vault.ts +382 -0
  144. package/src/discovery/__tests__/discovery.test.ts +49 -0
  145. package/src/discovery/__tests__/on-chain-registry.test.ts +176 -0
  146. package/src/discovery/discovery-layer.ts +244 -0
  147. package/src/discovery/index.ts +3 -0
  148. package/src/discovery/mock-registry.ts +96 -0
  149. package/src/discovery/on-chain-registry.ts +237 -0
  150. package/src/encryption/__tests__/aes.test.ts +64 -0
  151. package/src/encryption/__tests__/encryption-layer.test.ts +80 -0
  152. package/src/encryption/__tests__/lit.test.ts +97 -0
  153. package/src/encryption/aes.ts +109 -0
  154. package/src/encryption/encryption-layer.ts +100 -0
  155. package/src/encryption/index.ts +5 -0
  156. package/src/encryption/lit.ts +161 -0
  157. package/src/encryption/vault-key.ts +63 -0
  158. package/src/identity/__tests__/identity.test.ts +31 -0
  159. package/src/identity/__tests__/ows-adapter.test.ts +47 -0
  160. package/src/identity/identity-layer.ts +123 -0
  161. package/src/identity/index.ts +3 -0
  162. package/src/identity/ows-adapter.ts +80 -0
  163. package/src/identity/session.ts +57 -0
  164. package/src/index.ts +12 -0
  165. package/src/persistence/__tests__/create-agent.test.ts +9 -0
  166. package/src/persistence/__tests__/persistence.test.ts +242 -0
  167. package/src/persistence/create-agent.ts +55 -0
  168. package/src/persistence/index.ts +2 -0
  169. package/src/persistence/persistence-layer.ts +236 -0
  170. package/src/transport/__tests__/solana-transport.test.ts +112 -0
  171. package/src/transport/__tests__/transport.test.ts +84 -0
  172. package/src/transport/index.ts +2 -0
  173. package/src/transport/relay-session.ts +118 -0
  174. package/src/transport/transport-layer.ts +171 -0
  175. package/src/types/orbitdb.d.ts +9 -0
  176. package/src/types.ts +1496 -0
@@ -0,0 +1,171 @@
1
+ import { createSignerClient } from "@slicekit/erc8128";
2
+ import type { EthHttpSigner } from "@slicekit/erc8128";
3
+
4
+ import type { ChainFamily, ITransportLayer, SignatureAlgorithm, WalletAddress } from "../types.js";
5
+
6
+ interface TransportConfig {
7
+ signer: (
8
+ payload: Uint8Array,
9
+ ) => Promise<{ signature: Uint8Array; algorithm: SignatureAlgorithm }>;
10
+ verifier?: (
11
+ payload: Uint8Array,
12
+ signature: Uint8Array,
13
+ algorithm: SignatureAlgorithm,
14
+ ) => Promise<boolean>;
15
+ signerAddress: WalletAddress;
16
+ family: ChainFamily;
17
+ nonceTTL?: number; // ms, default 5 min
18
+ /** ERC-8128 chain ID (default 84532 = Base Sepolia) */
19
+ chainId?: number;
20
+ }
21
+
22
+ export function createTransportLayer(config: TransportConfig): ITransportLayer {
23
+ const nonceCache = new Map<string, number>(); // nonce -> timestamp
24
+ const NONCE_TTL = config.nonceTTL ?? 5 * 60 * 1000;
25
+ const TIMESTAMP_TOLERANCE = 30 * 1000; // ±30s
26
+
27
+ // Periodic cleanup
28
+ function cleanNonces() {
29
+ const now = Date.now();
30
+ for (const [nonce, ts] of nonceCache) {
31
+ if (now - ts > NONCE_TTL) nonceCache.delete(nonce);
32
+ }
33
+ }
34
+
35
+ async function computePayload(
36
+ method: string,
37
+ url: string,
38
+ timestamp: number,
39
+ nonce: string,
40
+ body?: unknown,
41
+ ): Promise<Uint8Array> {
42
+ const bodyHash = body
43
+ ? new Uint8Array(
44
+ await crypto.subtle.digest("SHA-256", new TextEncoder().encode(JSON.stringify(body))),
45
+ )
46
+ : new Uint8Array(0);
47
+ const payload = new TextEncoder().encode(
48
+ `${method}\n${url}\n${timestamp}\n${nonce}\n${Array.from(bodyHash)
49
+ .map((b) => b.toString(16).padStart(2, "0"))
50
+ .join("")}`,
51
+ );
52
+ return payload;
53
+ }
54
+
55
+ return {
56
+ async createSignedRequest(request) {
57
+ const timestamp = Date.now();
58
+ const nonce = crypto.randomUUID();
59
+ const payload = await computePayload(
60
+ request.method,
61
+ request.url,
62
+ timestamp,
63
+ nonce,
64
+ request.body,
65
+ );
66
+ const { signature, algorithm } = await config.signer(payload);
67
+
68
+ const headers: Record<string, string> = {
69
+ ...request.headers,
70
+ "X-OrbitMem-Signer": config.signerAddress as string,
71
+ "X-OrbitMem-Family": config.family,
72
+ "X-OrbitMem-Algorithm": algorithm,
73
+ "X-OrbitMem-Timestamp": String(timestamp),
74
+ "X-OrbitMem-Nonce": nonce,
75
+ "X-OrbitMem-Signature": Array.from(signature)
76
+ .map((b) => b.toString(16).padStart(2, "0"))
77
+ .join(""),
78
+ };
79
+
80
+ return {
81
+ url: request.url,
82
+ method: request.method ?? "GET",
83
+ headers,
84
+ body: request.body,
85
+ proof: {
86
+ signer: config.signerAddress,
87
+ family: config.family,
88
+ signature,
89
+ algorithm,
90
+ timestamp,
91
+ nonce,
92
+ },
93
+ };
94
+ },
95
+
96
+ async verifyRequest(request) {
97
+ cleanNonces();
98
+
99
+ const { proof } = request;
100
+
101
+ // Timestamp check
102
+ const now = Date.now();
103
+ if (Math.abs(now - proof.timestamp) > TIMESTAMP_TOLERANCE) {
104
+ return { valid: false, signer: proof.signer, family: proof.family, isReplay: false };
105
+ }
106
+
107
+ // Nonce replay check
108
+ if (nonceCache.has(proof.nonce)) {
109
+ return { valid: true, signer: proof.signer, family: proof.family, isReplay: true };
110
+ }
111
+
112
+ // Signature verification
113
+ if (config.verifier) {
114
+ const payload = await computePayload(
115
+ request.method,
116
+ request.url,
117
+ proof.timestamp,
118
+ proof.nonce,
119
+ request.body,
120
+ );
121
+ const valid = await config.verifier(payload, proof.signature, proof.algorithm);
122
+ if (valid) {
123
+ nonceCache.set(proof.nonce, Date.now());
124
+ }
125
+ return { valid, signer: proof.signer, family: proof.family, isReplay: false };
126
+ }
127
+
128
+ // No verifier — trust the signature (for client-side use)
129
+ nonceCache.set(proof.nonce, Date.now());
130
+ return { valid: true, signer: proof.signer, family: proof.family, isReplay: false };
131
+ },
132
+
133
+ async fetch(url, init) {
134
+ const signed = await this.createSignedRequest({
135
+ url,
136
+ method: (init?.method as any) ?? "GET",
137
+ headers: init?.headers,
138
+ body: init?.body,
139
+ });
140
+
141
+ return globalThis.fetch(signed.url, {
142
+ method: signed.method,
143
+ headers: signed.headers,
144
+ body: signed.body ? JSON.stringify(signed.body) : undefined,
145
+ });
146
+ },
147
+ };
148
+ }
149
+
150
+ /**
151
+ * Create an RFC 9421 / ERC-8128 compliant transport layer using @slicekit/erc8128.
152
+ * This is the recommended transport for new integrations.
153
+ */
154
+ export function createErc8128TransportLayer(config: {
155
+ signer: EthHttpSigner;
156
+ /** Whether to prefer replayable signatures (default false) */
157
+ preferReplayable?: boolean;
158
+ /** Signature TTL in seconds (default 60) */
159
+ ttlSeconds?: number;
160
+ }) {
161
+ const client = createSignerClient(config.signer, {
162
+ preferReplayable: config.preferReplayable ?? false,
163
+ ttlSeconds: config.ttlSeconds ?? 60,
164
+ });
165
+
166
+ return {
167
+ client,
168
+ signRequest: (input: RequestInfo, init?: RequestInit) => client.signRequest(input, init),
169
+ fetch: (input: RequestInfo, init?: RequestInit) => client.fetch(input, init),
170
+ };
171
+ }
@@ -0,0 +1,9 @@
1
+ declare module "@orbitdb/core" {
2
+ export function createOrbitDB(opts: any): Promise<any>;
3
+ export function useDatabaseType(type: any): void;
4
+ }
5
+
6
+ declare module "@orbitdb/nested-db" {
7
+ const Nested: any;
8
+ export { Nested };
9
+ }