@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.
- package/README.md +104 -0
- package/dist/agent/agent-adapter.d.ts +3 -0
- package/dist/agent/agent-adapter.d.ts.map +1 -0
- package/dist/agent/agent-adapter.js +3 -0
- package/dist/agent/agent-adapter.js.map +1 -0
- package/dist/agent/client.d.ts +5 -0
- package/dist/agent/client.d.ts.map +1 -0
- package/dist/agent/client.js +146 -0
- package/dist/agent/client.js.map +1 -0
- package/dist/agent/index.d.ts +2 -0
- package/dist/agent/index.d.ts.map +1 -0
- package/dist/agent/index.js +2 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +118 -0
- package/dist/client.js.map +1 -0
- package/dist/contracts.d.ts +19 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +28 -0
- package/dist/contracts.js.map +1 -0
- package/dist/data/index.d.ts +5 -0
- package/dist/data/index.d.ts.map +1 -0
- package/dist/data/index.js +5 -0
- package/dist/data/index.js.map +1 -0
- package/dist/data/orbitdb.d.ts +10 -0
- package/dist/data/orbitdb.d.ts.map +1 -0
- package/dist/data/orbitdb.js +39 -0
- package/dist/data/orbitdb.js.map +1 -0
- package/dist/data/pricing.d.ts +7 -0
- package/dist/data/pricing.d.ts.map +1 -0
- package/dist/data/pricing.js +55 -0
- package/dist/data/pricing.js.map +1 -0
- package/dist/data/serialization.d.ts +28 -0
- package/dist/data/serialization.d.ts.map +1 -0
- package/dist/data/serialization.js +76 -0
- package/dist/data/serialization.js.map +1 -0
- package/dist/data/vault.d.ts +21 -0
- package/dist/data/vault.d.ts.map +1 -0
- package/dist/data/vault.js +284 -0
- package/dist/data/vault.js.map +1 -0
- package/dist/discovery/discovery-layer.d.ts +3 -0
- package/dist/discovery/discovery-layer.d.ts.map +1 -0
- package/dist/discovery/discovery-layer.js +205 -0
- package/dist/discovery/discovery-layer.js.map +1 -0
- package/dist/discovery/index.d.ts +4 -0
- package/dist/discovery/index.d.ts.map +1 -0
- package/dist/discovery/index.js +4 -0
- package/dist/discovery/index.js.map +1 -0
- package/dist/discovery/mock-registry.d.ts +30 -0
- package/dist/discovery/mock-registry.d.ts.map +1 -0
- package/dist/discovery/mock-registry.js +71 -0
- package/dist/discovery/mock-registry.js.map +1 -0
- package/dist/discovery/on-chain-registry.d.ts +35 -0
- package/dist/discovery/on-chain-registry.d.ts.map +1 -0
- package/dist/discovery/on-chain-registry.js +199 -0
- package/dist/discovery/on-chain-registry.js.map +1 -0
- package/dist/encryption/aes.d.ts +15 -0
- package/dist/encryption/aes.d.ts.map +1 -0
- package/dist/encryption/aes.js +63 -0
- package/dist/encryption/aes.js.map +1 -0
- package/dist/encryption/encryption-layer.d.ts +8 -0
- package/dist/encryption/encryption-layer.d.ts.map +1 -0
- package/dist/encryption/encryption-layer.js +82 -0
- package/dist/encryption/encryption-layer.js.map +1 -0
- package/dist/encryption/index.d.ts +6 -0
- package/dist/encryption/index.d.ts.map +1 -0
- package/dist/encryption/index.js +4 -0
- package/dist/encryption/index.js.map +1 -0
- package/dist/encryption/lit.d.ts +23 -0
- package/dist/encryption/lit.d.ts.map +1 -0
- package/dist/encryption/lit.js +113 -0
- package/dist/encryption/lit.js.map +1 -0
- package/dist/encryption/vault-key.d.ts +37 -0
- package/dist/encryption/vault-key.d.ts.map +1 -0
- package/dist/encryption/vault-key.js +43 -0
- package/dist/encryption/vault-key.js.map +1 -0
- package/dist/identity/identity-layer.d.ts +3 -0
- package/dist/identity/identity-layer.d.ts.map +1 -0
- package/dist/identity/identity-layer.js +99 -0
- package/dist/identity/identity-layer.js.map +1 -0
- package/dist/identity/index.d.ts +4 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +4 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/ows-adapter.d.ts +15 -0
- package/dist/identity/ows-adapter.d.ts.map +1 -0
- package/dist/identity/ows-adapter.js +67 -0
- package/dist/identity/ows-adapter.js.map +1 -0
- package/dist/identity/session.d.ts +10 -0
- package/dist/identity/session.d.ts.map +1 -0
- package/dist/identity/session.js +36 -0
- package/dist/identity/session.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/persistence/create-agent.d.ts +11 -0
- package/dist/persistence/create-agent.d.ts.map +1 -0
- package/dist/persistence/create-agent.js +47 -0
- package/dist/persistence/create-agent.js.map +1 -0
- package/dist/persistence/index.d.ts +3 -0
- package/dist/persistence/index.d.ts.map +1 -0
- package/dist/persistence/index.js +3 -0
- package/dist/persistence/index.js.map +1 -0
- package/dist/persistence/persistence-layer.d.ts +12 -0
- package/dist/persistence/persistence-layer.d.ts.map +1 -0
- package/dist/persistence/persistence-layer.js +194 -0
- package/dist/persistence/persistence-layer.js.map +1 -0
- package/dist/transport/index.d.ts +3 -0
- package/dist/transport/index.d.ts.map +1 -0
- package/dist/transport/index.js +3 -0
- package/dist/transport/index.js.map +1 -0
- package/dist/transport/relay-session.d.ts +41 -0
- package/dist/transport/relay-session.d.ts.map +1 -0
- package/dist/transport/relay-session.js +86 -0
- package/dist/transport/relay-session.js.map +1 -0
- package/dist/transport/transport-layer.d.ts +32 -0
- package/dist/transport/transport-layer.d.ts.map +1 -0
- package/dist/transport/transport-layer.js +110 -0
- package/dist/transport/transport-layer.js.map +1 -0
- package/dist/types.d.ts +1319 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +91 -0
- package/src/__tests__/client.test.ts +30 -0
- package/src/__tests__/orbitdb-availability.ts +8 -0
- package/src/agent/__tests__/agent-adapter.test.ts +50 -0
- package/src/agent/__tests__/client.test.ts +50 -0
- package/src/agent/agent-adapter.ts +2 -0
- package/src/agent/client.ts +158 -0
- package/src/agent/index.ts +1 -0
- package/src/client.ts +134 -0
- package/src/contracts.ts +44 -0
- package/src/data/__tests__/pricing.test.ts +73 -0
- package/src/data/__tests__/vault-encryption.test.ts +346 -0
- package/src/data/__tests__/vault.test.ts +75 -0
- package/src/data/index.ts +8 -0
- package/src/data/orbitdb.ts +47 -0
- package/src/data/pricing.ts +63 -0
- package/src/data/serialization.ts +108 -0
- package/src/data/vault.ts +382 -0
- package/src/discovery/__tests__/discovery.test.ts +49 -0
- package/src/discovery/__tests__/on-chain-registry.test.ts +176 -0
- package/src/discovery/discovery-layer.ts +244 -0
- package/src/discovery/index.ts +3 -0
- package/src/discovery/mock-registry.ts +96 -0
- package/src/discovery/on-chain-registry.ts +237 -0
- package/src/encryption/__tests__/aes.test.ts +64 -0
- package/src/encryption/__tests__/encryption-layer.test.ts +80 -0
- package/src/encryption/__tests__/lit.test.ts +97 -0
- package/src/encryption/aes.ts +109 -0
- package/src/encryption/encryption-layer.ts +100 -0
- package/src/encryption/index.ts +5 -0
- package/src/encryption/lit.ts +161 -0
- package/src/encryption/vault-key.ts +63 -0
- package/src/identity/__tests__/identity.test.ts +31 -0
- package/src/identity/__tests__/ows-adapter.test.ts +47 -0
- package/src/identity/identity-layer.ts +123 -0
- package/src/identity/index.ts +3 -0
- package/src/identity/ows-adapter.ts +80 -0
- package/src/identity/session.ts +57 -0
- package/src/index.ts +12 -0
- package/src/persistence/__tests__/create-agent.test.ts +9 -0
- package/src/persistence/__tests__/persistence.test.ts +242 -0
- package/src/persistence/create-agent.ts +55 -0
- package/src/persistence/index.ts +2 -0
- package/src/persistence/persistence-layer.ts +236 -0
- package/src/transport/__tests__/solana-transport.test.ts +112 -0
- package/src/transport/__tests__/transport.test.ts +84 -0
- package/src/transport/index.ts +2 -0
- package/src/transport/relay-session.ts +118 -0
- package/src/transport/transport-layer.ts +171 -0
- package/src/types/orbitdb.d.ts +9 -0
- 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
|
+
}
|