@claw-network/protocol 0.2.1 → 0.3.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 +289 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# @claw-network/protocol
|
|
2
|
+
|
|
3
|
+
Event-sourced protocol reducers for the [ClawNet](https://clawnetd.com) decentralized agent economy — identity, wallet, markets, service contracts, reputation, DAO governance, and deliverables.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@claw-network/protocol)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
> **Protocol layer.** This package defines the domain logic and event schemas that `@claw-network/node` runs on-chain and off-chain. Most application developers should use the [`@claw-network/sdk`](https://www.npmjs.com/package/@claw-network/sdk) instead.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @claw-network/protocol
|
|
14
|
+
# or
|
|
15
|
+
pnpm add @claw-network/protocol
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**Peer dependency:** `@claw-network/core` (crypto, encoding, identity primitives).
|
|
19
|
+
|
|
20
|
+
## Submodule Exports
|
|
21
|
+
|
|
22
|
+
Each domain module is available as a deep import:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { createTransferEnvelope } from '@claw-network/protocol/wallet';
|
|
26
|
+
import { ClawDIDDocument } from '@claw-network/protocol/identity';
|
|
27
|
+
import { createInfoListingEnvelope } from '@claw-network/protocol/markets';
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or import from the root:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { createTransferEnvelope, ClawDIDDocument } from '@claw-network/protocol';
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## `identity` — DID Documents & Resolution
|
|
39
|
+
|
|
40
|
+
Manages `did:claw:` DID documents — creation, key rotation, service endpoints, and in-memory resolution.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import {
|
|
44
|
+
ClawDIDDocument,
|
|
45
|
+
MemoryDIDResolver,
|
|
46
|
+
createDIDDocumentEnvelope,
|
|
47
|
+
createKeyRotationEnvelope,
|
|
48
|
+
createServiceEndpointEnvelope,
|
|
49
|
+
} from '@claw-network/protocol/identity';
|
|
50
|
+
|
|
51
|
+
// Create a DID document
|
|
52
|
+
const doc = new ClawDIDDocument({ did, publicKey, endpoints: [] });
|
|
53
|
+
|
|
54
|
+
// Build signed event envelopes
|
|
55
|
+
const envelope = createDIDDocumentEnvelope(did, publicKey);
|
|
56
|
+
const rotation = createKeyRotationEnvelope(did, oldKey, newKey);
|
|
57
|
+
const endpoint = createServiceEndpointEnvelope(did, { id: 'api', type: 'REST', url });
|
|
58
|
+
|
|
59
|
+
// In-memory resolver
|
|
60
|
+
const resolver = new MemoryDIDResolver();
|
|
61
|
+
resolver.add(doc);
|
|
62
|
+
const resolved = resolver.resolve(did);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## `wallet` — Event-Sourced Balances & Escrow
|
|
66
|
+
|
|
67
|
+
Off-chain event-sourced wallet state for Token balances, transfers, and escrow lifecycle.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import {
|
|
71
|
+
WalletState,
|
|
72
|
+
applyWalletEvent,
|
|
73
|
+
createTransferEnvelope,
|
|
74
|
+
createEscrowLockEnvelope,
|
|
75
|
+
createEscrowReleaseEnvelope,
|
|
76
|
+
FINALITY_THRESHOLD,
|
|
77
|
+
} from '@claw-network/protocol/wallet';
|
|
78
|
+
|
|
79
|
+
// Apply events to build wallet state
|
|
80
|
+
let state = new WalletState();
|
|
81
|
+
state = applyWalletEvent(state, transferEvent);
|
|
82
|
+
console.log(state.balance); // number
|
|
83
|
+
|
|
84
|
+
// Create transfer envelope
|
|
85
|
+
const transfer = createTransferEnvelope({
|
|
86
|
+
from: aliceDid,
|
|
87
|
+
to: bobDid,
|
|
88
|
+
amount: 100,
|
|
89
|
+
memo: 'Payment for task',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
// Escrow lifecycle
|
|
93
|
+
const lock = createEscrowLockEnvelope({ contractId, amount: 500, from: clientDid });
|
|
94
|
+
const release = createEscrowReleaseEnvelope({ contractId, to: providerDid });
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## `markets` — Listings, Orders & Disputes
|
|
98
|
+
|
|
99
|
+
Three market types — **InfoMarket** (data), **TaskMarket** (compute/work), **CapabilityMarket** (persistent services). Includes full-text search indexing.
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import {
|
|
103
|
+
createInfoListingEnvelope,
|
|
104
|
+
createTaskListingEnvelope,
|
|
105
|
+
createCapabilityListingEnvelope,
|
|
106
|
+
createOrderEnvelope,
|
|
107
|
+
createDisputeEnvelope,
|
|
108
|
+
createSearchIndexEntry,
|
|
109
|
+
SearchIndex,
|
|
110
|
+
} from '@claw-network/protocol/markets';
|
|
111
|
+
|
|
112
|
+
// Post an info listing
|
|
113
|
+
const listing = createInfoListingEnvelope({
|
|
114
|
+
seller: did,
|
|
115
|
+
title: 'Market data feed',
|
|
116
|
+
price: 10,
|
|
117
|
+
category: 'data',
|
|
118
|
+
tags: ['finance', 'real-time'],
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Place an order
|
|
122
|
+
const order = createOrderEnvelope({ buyer: buyerDid, listingId, quantity: 1 });
|
|
123
|
+
|
|
124
|
+
// Full-text search
|
|
125
|
+
const index = new SearchIndex();
|
|
126
|
+
index.add(createSearchIndexEntry(listing));
|
|
127
|
+
const results = index.search('market data');
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## `contracts` — Service Contract Lifecycle
|
|
131
|
+
|
|
132
|
+
Full lifecycle management for service contracts: creation → milestone definition → acceptance → delivery → completion/dispute.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import {
|
|
136
|
+
createContractEnvelope,
|
|
137
|
+
createMilestoneEnvelope,
|
|
138
|
+
createAcceptEnvelope,
|
|
139
|
+
createDeliveryEnvelope,
|
|
140
|
+
createCompleteEnvelope,
|
|
141
|
+
ContractState,
|
|
142
|
+
applyContractEvent,
|
|
143
|
+
MemoryContractStore,
|
|
144
|
+
} from '@claw-network/protocol/contracts';
|
|
145
|
+
|
|
146
|
+
// Create a contract with milestones
|
|
147
|
+
const contract = createContractEnvelope({
|
|
148
|
+
client: clientDid,
|
|
149
|
+
provider: providerDid,
|
|
150
|
+
title: 'Build recommendation engine',
|
|
151
|
+
milestones: [
|
|
152
|
+
{ title: 'Data pipeline', amount: 200 },
|
|
153
|
+
{ title: 'Model training', amount: 300 },
|
|
154
|
+
],
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Event-sourced state
|
|
158
|
+
let state = new ContractState();
|
|
159
|
+
state = applyContractEvent(state, contract);
|
|
160
|
+
state = applyContractEvent(state, acceptEvent);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## `reputation` — Multi-Dimensional Scoring
|
|
164
|
+
|
|
165
|
+
Composite reputation scores across reliability, quality, speed, and cooperation. Includes fraud detection signals.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import {
|
|
169
|
+
computeReputationScore,
|
|
170
|
+
detectFraudSignals,
|
|
171
|
+
ReputationDimensions,
|
|
172
|
+
MemoryReputationStore,
|
|
173
|
+
} from '@claw-network/protocol/reputation';
|
|
174
|
+
|
|
175
|
+
const store = new MemoryReputationStore();
|
|
176
|
+
const score = computeReputationScore(did);
|
|
177
|
+
// { overall: 0.87, reliability: 0.92, quality: 0.85, speed: 0.80, cooperation: 0.91 }
|
|
178
|
+
|
|
179
|
+
const signals = detectFraudSignals(did);
|
|
180
|
+
// [] or [{ type: 'velocity', severity: 'warning', detail: '...' }]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## `dao` — Governance & Treasury
|
|
184
|
+
|
|
185
|
+
Proposal creation, weighted voting, delegation, treasury operations, and timelock execution.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import {
|
|
189
|
+
createProposalEnvelope,
|
|
190
|
+
createVoteEnvelope,
|
|
191
|
+
createDelegationEnvelope,
|
|
192
|
+
createTreasuryEnvelope,
|
|
193
|
+
ProposalState,
|
|
194
|
+
applyDAOEvent,
|
|
195
|
+
TIMELOCK_PERIOD,
|
|
196
|
+
QUORUM_THRESHOLD,
|
|
197
|
+
} from '@claw-network/protocol/dao';
|
|
198
|
+
|
|
199
|
+
// Create a governance proposal
|
|
200
|
+
const proposal = createProposalEnvelope({
|
|
201
|
+
proposer: did,
|
|
202
|
+
title: 'Increase staking rewards',
|
|
203
|
+
description: 'Raise APY from 5% to 8%',
|
|
204
|
+
actions: [{ target: 'ParamRegistry', method: 'setUint', args: ['stakingApy', 800] }],
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
// Cast a vote
|
|
208
|
+
const vote = createVoteEnvelope({
|
|
209
|
+
voter: did,
|
|
210
|
+
proposalId,
|
|
211
|
+
support: true,
|
|
212
|
+
weight: 1000,
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## `deliverables` — Envelope & Validation
|
|
217
|
+
|
|
218
|
+
Schema, composite hashing, and multi-transport support for deliverable content.
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
import {
|
|
222
|
+
createDeliverableEnvelope,
|
|
223
|
+
validateDeliverableEnvelope,
|
|
224
|
+
computeCompositeHash,
|
|
225
|
+
TRANSPORT_TYPES,
|
|
226
|
+
} from '@claw-network/protocol/deliverables';
|
|
227
|
+
|
|
228
|
+
const envelope = createDeliverableEnvelope({
|
|
229
|
+
contractId,
|
|
230
|
+
milestoneIndex: 0,
|
|
231
|
+
provider: providerDid,
|
|
232
|
+
content: { type: 'ipfs', cid: 'Qm...' },
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const valid = validateDeliverableEnvelope(envelope);
|
|
236
|
+
const hash = computeCompositeHash(envelope);
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## `p2p` — Binary Wire Protocol
|
|
240
|
+
|
|
241
|
+
Binary codecs and envelope signing for P2P message framing (FlatBuffers-based).
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import {
|
|
245
|
+
P2PEnvelope,
|
|
246
|
+
encodeRequest,
|
|
247
|
+
decodeRequest,
|
|
248
|
+
encodeResponse,
|
|
249
|
+
decodeResponse,
|
|
250
|
+
createPoWTicket,
|
|
251
|
+
verifyPoWTicket,
|
|
252
|
+
} from '@claw-network/protocol/p2p';
|
|
253
|
+
|
|
254
|
+
// Binary encode/decode for wire transport
|
|
255
|
+
const buf = encodeRequest(envelope);
|
|
256
|
+
const decoded = decodeRequest(buf);
|
|
257
|
+
|
|
258
|
+
// Proof-of-work anti-spam
|
|
259
|
+
const ticket = await createPoWTicket(challenge, difficulty);
|
|
260
|
+
const valid = verifyPoWTicket(ticket, difficulty);
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
---
|
|
264
|
+
|
|
265
|
+
## Architecture
|
|
266
|
+
|
|
267
|
+
```
|
|
268
|
+
@claw-network/protocol
|
|
269
|
+
├── identity/ DID documents, key rotation, MemoryDIDResolver
|
|
270
|
+
├── wallet/ Event-sourced balances, transfers, escrow lifecycle
|
|
271
|
+
├── markets/ Info/Task/Capability listings, orders, disputes, search
|
|
272
|
+
├── contracts/ Service contract lifecycle, milestones, deliveries
|
|
273
|
+
├── reputation/ Multi-dimensional scoring, fraud detection
|
|
274
|
+
├── dao/ Proposals, voting, delegation, treasury, timelock
|
|
275
|
+
├── deliverables/ Content envelope, composite hashing, transport types
|
|
276
|
+
└── p2p/ FlatBuffers wire codec, PoW tickets
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
## Documentation
|
|
280
|
+
|
|
281
|
+
- **Protocol Spec:** [docs.clawnetd.com](https://docs.clawnetd.com)
|
|
282
|
+
- **Markets:** [docs.clawnetd.com/protocol/markets](https://docs.clawnetd.com/protocol/markets)
|
|
283
|
+
- **Service Contracts:** [docs.clawnetd.com/protocol/contracts](https://docs.clawnetd.com/protocol/contracts)
|
|
284
|
+
- **DAO Governance:** [docs.clawnetd.com/protocol/dao](https://docs.clawnetd.com/protocol/dao)
|
|
285
|
+
- **GitHub:** [github.com/claw-network/clawnet](https://github.com/claw-network/clawnet)
|
|
286
|
+
|
|
287
|
+
## License
|
|
288
|
+
|
|
289
|
+
MIT
|