@weave_protocol/domere 1.0.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/PLANNING.md +231 -0
- package/README.md +50 -0
- package/dist/anchoring/ethereum.d.ts +135 -0
- package/dist/anchoring/ethereum.d.ts.map +1 -0
- package/dist/anchoring/ethereum.js +474 -0
- package/dist/anchoring/ethereum.js.map +1 -0
- package/dist/anchoring/index.d.ts +93 -0
- package/dist/anchoring/index.d.ts.map +1 -0
- package/dist/anchoring/index.js +184 -0
- package/dist/anchoring/index.js.map +1 -0
- package/dist/anchoring/merkle.d.ts +91 -0
- package/dist/anchoring/merkle.d.ts.map +1 -0
- package/dist/anchoring/merkle.js +203 -0
- package/dist/anchoring/merkle.js.map +1 -0
- package/dist/anchoring/solana.d.ts +85 -0
- package/dist/anchoring/solana.d.ts.map +1 -0
- package/dist/anchoring/solana.js +301 -0
- package/dist/anchoring/solana.js.map +1 -0
- package/dist/constants.d.ts +130 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +536 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/language/code-analyzer.d.ts +80 -0
- package/dist/language/code-analyzer.d.ts.map +1 -0
- package/dist/language/code-analyzer.js +489 -0
- package/dist/language/code-analyzer.js.map +1 -0
- package/dist/language/detector.d.ts +53 -0
- package/dist/language/detector.d.ts.map +1 -0
- package/dist/language/detector.js +248 -0
- package/dist/language/detector.js.map +1 -0
- package/dist/language/index.d.ts +61 -0
- package/dist/language/index.d.ts.map +1 -0
- package/dist/language/index.js +109 -0
- package/dist/language/index.js.map +1 -0
- package/dist/language/nl-analyzer.d.ts +59 -0
- package/dist/language/nl-analyzer.d.ts.map +1 -0
- package/dist/language/nl-analyzer.js +350 -0
- package/dist/language/nl-analyzer.js.map +1 -0
- package/dist/language/semantic.d.ts +48 -0
- package/dist/language/semantic.d.ts.map +1 -0
- package/dist/language/semantic.js +329 -0
- package/dist/language/semantic.js.map +1 -0
- package/dist/storage/index.d.ts +6 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +6 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/memory.d.ts +48 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +211 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/thread/drift.d.ts +43 -0
- package/dist/thread/drift.d.ts.map +1 -0
- package/dist/thread/drift.js +248 -0
- package/dist/thread/drift.js.map +1 -0
- package/dist/thread/index.d.ts +9 -0
- package/dist/thread/index.d.ts.map +1 -0
- package/dist/thread/index.js +9 -0
- package/dist/thread/index.js.map +1 -0
- package/dist/thread/intent.d.ts +68 -0
- package/dist/thread/intent.d.ts.map +1 -0
- package/dist/thread/intent.js +333 -0
- package/dist/thread/intent.js.map +1 -0
- package/dist/thread/manager.d.ts +85 -0
- package/dist/thread/manager.d.ts.map +1 -0
- package/dist/thread/manager.js +305 -0
- package/dist/thread/manager.js.map +1 -0
- package/dist/thread/weave.d.ts +61 -0
- package/dist/thread/weave.d.ts.map +1 -0
- package/dist/thread/weave.js +158 -0
- package/dist/thread/weave.js.map +1 -0
- package/dist/tools/index.d.ts +18 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +102 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/types.d.ts +466 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +48 -0
- package/dist/types.js.map +1 -0
- package/package.json +24 -0
- package/src/anchoring/ethereum.ts +568 -0
- package/src/anchoring/index.ts +236 -0
- package/src/anchoring/merkle.ts +256 -0
- package/src/anchoring/solana.ts +370 -0
- package/src/constants.ts +566 -0
- package/src/index.ts +43 -0
- package/src/language/code-analyzer.ts +564 -0
- package/src/language/detector.ts +297 -0
- package/src/language/index.ts +129 -0
- package/src/language/nl-analyzer.ts +411 -0
- package/src/language/semantic.ts +385 -0
- package/src/storage/index.ts +6 -0
- package/src/storage/memory.ts +271 -0
- package/src/thread/drift.ts +319 -0
- package/src/thread/index.ts +9 -0
- package/src/thread/intent.ts +409 -0
- package/src/thread/manager.ts +414 -0
- package/src/thread/weave.ts +205 -0
- package/src/tools/index.ts +107 -0
- package/src/types.ts +736 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dōmere - The Judge Protocol
|
|
3
|
+
* Solana Anchoring Client
|
|
4
|
+
*
|
|
5
|
+
* Note: This is the client interface. The actual Solana program
|
|
6
|
+
* should be deployed separately using Anchor framework.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {
|
|
10
|
+
AnchorRequest,
|
|
11
|
+
AnchorResult,
|
|
12
|
+
AnchorVerification,
|
|
13
|
+
BlockchainNetwork
|
|
14
|
+
} from '../types.js';
|
|
15
|
+
import { AnchoringError } from '../types.js';
|
|
16
|
+
import { DEFAULT_CONFIG, PROTOCOL_FEES } from '../constants.js';
|
|
17
|
+
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// Solana Client Interface
|
|
20
|
+
// ============================================================================
|
|
21
|
+
|
|
22
|
+
export interface SolanaConfig {
|
|
23
|
+
rpc_url: string;
|
|
24
|
+
program_id: string;
|
|
25
|
+
treasury?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SolanaAnchorData {
|
|
29
|
+
thread_id: number[]; // [u8; 32]
|
|
30
|
+
merkle_root: number[]; // [u8; 32]
|
|
31
|
+
hop_count: number; // u64
|
|
32
|
+
intent_hash: number[]; // [u8; 32]
|
|
33
|
+
compliant: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// ============================================================================
|
|
37
|
+
// Solana Anchoring Client
|
|
38
|
+
// ============================================================================
|
|
39
|
+
|
|
40
|
+
export class SolanaAnchorClient {
|
|
41
|
+
private config: SolanaConfig;
|
|
42
|
+
private isDevnet: boolean;
|
|
43
|
+
|
|
44
|
+
constructor(config?: Partial<SolanaConfig>) {
|
|
45
|
+
this.config = {
|
|
46
|
+
rpc_url: config?.rpc_url ?? DEFAULT_CONFIG.anchoring.solana_rpc,
|
|
47
|
+
program_id: config?.program_id ?? DEFAULT_CONFIG.anchoring.solana_program_id,
|
|
48
|
+
treasury: config?.treasury,
|
|
49
|
+
};
|
|
50
|
+
this.isDevnet = this.config.rpc_url.includes('devnet');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Prepare anchor data for Solana
|
|
55
|
+
*/
|
|
56
|
+
prepareAnchorData(request: AnchorRequest): SolanaAnchorData {
|
|
57
|
+
return {
|
|
58
|
+
thread_id: this.stringToBytes32(request.thread_id),
|
|
59
|
+
merkle_root: this.hexToBytes32(request.merkle_root),
|
|
60
|
+
hop_count: request.hop_count,
|
|
61
|
+
intent_hash: this.hexToBytes32(request.intent_hash),
|
|
62
|
+
compliant: request.compliant,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Estimate transaction cost
|
|
68
|
+
*/
|
|
69
|
+
async estimateCost(): Promise<{
|
|
70
|
+
network_fee_lamports: number;
|
|
71
|
+
network_fee_sol: string;
|
|
72
|
+
protocol_fee_lamports: number;
|
|
73
|
+
protocol_fee_sol: string;
|
|
74
|
+
total_lamports: number;
|
|
75
|
+
total_sol: string;
|
|
76
|
+
}> {
|
|
77
|
+
// Base transaction fee on Solana is ~5000 lamports
|
|
78
|
+
// Account rent for storing anchor data is ~2039280 lamports (for first anchor)
|
|
79
|
+
// We assume account already exists for subsequent anchors
|
|
80
|
+
|
|
81
|
+
const baseFee = 5000; // lamports
|
|
82
|
+
const protocolFee = PROTOCOL_FEES.solana.base_lamports;
|
|
83
|
+
const total = baseFee + protocolFee;
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
network_fee_lamports: baseFee,
|
|
87
|
+
network_fee_sol: (baseFee / 1_000_000_000).toFixed(9),
|
|
88
|
+
protocol_fee_lamports: protocolFee,
|
|
89
|
+
protocol_fee_sol: (protocolFee / 1_000_000_000).toFixed(9),
|
|
90
|
+
total_lamports: total,
|
|
91
|
+
total_sol: (total / 1_000_000_000).toFixed(9),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Anchor thread to Solana
|
|
97
|
+
*
|
|
98
|
+
* Note: This returns the transaction data needed for signing.
|
|
99
|
+
* Actual signing must be done client-side with the user's wallet.
|
|
100
|
+
*/
|
|
101
|
+
async createAnchorTransaction(request: AnchorRequest): Promise<{
|
|
102
|
+
unsigned_transaction: string; // Base64 encoded transaction
|
|
103
|
+
message: string; // Message to sign
|
|
104
|
+
instructions: unknown[]; // Instruction data
|
|
105
|
+
estimated_cost: any;
|
|
106
|
+
}> {
|
|
107
|
+
const data = this.prepareAnchorData(request);
|
|
108
|
+
const cost = await this.estimateCost();
|
|
109
|
+
|
|
110
|
+
// In a real implementation, this would create the actual Solana transaction
|
|
111
|
+
// using @solana/web3.js. For now, we return a placeholder structure.
|
|
112
|
+
|
|
113
|
+
const instruction = {
|
|
114
|
+
program_id: this.config.program_id,
|
|
115
|
+
accounts: [
|
|
116
|
+
{ pubkey: 'payer', isSigner: true, isWritable: true },
|
|
117
|
+
{ pubkey: 'thread_anchor_pda', isSigner: false, isWritable: true },
|
|
118
|
+
{ pubkey: 'treasury', isSigner: false, isWritable: true },
|
|
119
|
+
{ pubkey: 'system_program', isSigner: false, isWritable: false },
|
|
120
|
+
],
|
|
121
|
+
data: {
|
|
122
|
+
instruction: 'anchor_thread',
|
|
123
|
+
...data,
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
unsigned_transaction: Buffer.from(JSON.stringify(instruction)).toString('base64'),
|
|
129
|
+
message: `Anchor thread ${request.thread_id} to Solana`,
|
|
130
|
+
instructions: [instruction],
|
|
131
|
+
estimated_cost: cost,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Submit signed transaction
|
|
137
|
+
*
|
|
138
|
+
* Note: In production, this would use @solana/web3.js to submit
|
|
139
|
+
* the signed transaction to the network.
|
|
140
|
+
*/
|
|
141
|
+
async submitSignedTransaction(signedTransaction: string): Promise<AnchorResult> {
|
|
142
|
+
// This is a placeholder - real implementation would:
|
|
143
|
+
// 1. Deserialize the signed transaction
|
|
144
|
+
// 2. Submit to Solana RPC
|
|
145
|
+
// 3. Wait for confirmation
|
|
146
|
+
// 4. Return result
|
|
147
|
+
|
|
148
|
+
const network: BlockchainNetwork = this.isDevnet ? 'solana-devnet' : 'solana';
|
|
149
|
+
|
|
150
|
+
// Simulate success for testing
|
|
151
|
+
const mockSignature = `${Date.now().toString(16)}${'0'.repeat(64)}`.slice(0, 88);
|
|
152
|
+
const mockSlot = Math.floor(Date.now() / 400); // Roughly Solana's slot timing
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
success: true,
|
|
156
|
+
network,
|
|
157
|
+
transaction_id: mockSignature,
|
|
158
|
+
slot: mockSlot,
|
|
159
|
+
timestamp: new Date(),
|
|
160
|
+
network_fee: '0.000005',
|
|
161
|
+
protocol_fee: '0.0001',
|
|
162
|
+
total_cost: '0.000105',
|
|
163
|
+
verification_url: this.getExplorerUrl(mockSignature),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Verify an anchor on-chain
|
|
169
|
+
*/
|
|
170
|
+
async verifyAnchor(
|
|
171
|
+
threadId: string,
|
|
172
|
+
expectedMerkleRoot: string
|
|
173
|
+
): Promise<AnchorVerification> {
|
|
174
|
+
// In production, this would:
|
|
175
|
+
// 1. Derive the PDA for the thread anchor
|
|
176
|
+
// 2. Fetch the account data
|
|
177
|
+
// 3. Verify the merkle root matches
|
|
178
|
+
|
|
179
|
+
// Placeholder implementation
|
|
180
|
+
const network: BlockchainNetwork = this.isDevnet ? 'solana-devnet' : 'solana';
|
|
181
|
+
|
|
182
|
+
return {
|
|
183
|
+
valid: true, // Would be determined by on-chain lookup
|
|
184
|
+
thread_id: threadId,
|
|
185
|
+
merkle_root: expectedMerkleRoot,
|
|
186
|
+
anchor: {
|
|
187
|
+
network,
|
|
188
|
+
transaction_id: 'verification_pending',
|
|
189
|
+
timestamp: new Date(),
|
|
190
|
+
verified: false,
|
|
191
|
+
},
|
|
192
|
+
verified_at: new Date(),
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get explorer URL for transaction
|
|
198
|
+
*/
|
|
199
|
+
getExplorerUrl(signature: string): string {
|
|
200
|
+
const cluster = this.isDevnet ? '?cluster=devnet' : '';
|
|
201
|
+
return `https://solscan.io/tx/${signature}${cluster}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get program address
|
|
206
|
+
*/
|
|
207
|
+
getProgramId(): string {
|
|
208
|
+
return this.config.program_id;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Derive PDA for thread anchor
|
|
213
|
+
*/
|
|
214
|
+
deriveThreadAnchorPda(threadId: string): string {
|
|
215
|
+
// In production, use @solana/web3.js PublicKey.findProgramAddressSync
|
|
216
|
+
// Seeds: ["thread_anchor", thread_id_bytes]
|
|
217
|
+
return `PDA_${threadId.slice(0, 20)}`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ============================================================================
|
|
221
|
+
// Utility Methods
|
|
222
|
+
// ============================================================================
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Convert string to 32-byte array
|
|
226
|
+
*/
|
|
227
|
+
private stringToBytes32(str: string): number[] {
|
|
228
|
+
const hash = require('crypto').createHash('sha256').update(str).digest();
|
|
229
|
+
return Array.from(hash);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Convert hex string to 32-byte array
|
|
234
|
+
*/
|
|
235
|
+
private hexToBytes32(hex: string): number[] {
|
|
236
|
+
// Remove 0x prefix if present
|
|
237
|
+
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
238
|
+
|
|
239
|
+
// Pad to 64 characters (32 bytes)
|
|
240
|
+
const padded = cleanHex.padStart(64, '0');
|
|
241
|
+
|
|
242
|
+
const bytes: number[] = [];
|
|
243
|
+
for (let i = 0; i < 64; i += 2) {
|
|
244
|
+
bytes.push(parseInt(padded.slice(i, i + 2), 16));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return bytes;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ============================================================================
|
|
252
|
+
// Solana Program IDL (for reference)
|
|
253
|
+
// ============================================================================
|
|
254
|
+
|
|
255
|
+
export const SOLANA_PROGRAM_IDL = `
|
|
256
|
+
// Anchor program for Dōmere anchoring
|
|
257
|
+
// Deploy using: anchor build && anchor deploy
|
|
258
|
+
|
|
259
|
+
use anchor_lang::prelude::*;
|
|
260
|
+
|
|
261
|
+
declare_id!("WeaveXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
|
|
262
|
+
|
|
263
|
+
#[program]
|
|
264
|
+
pub mod domere_anchor {
|
|
265
|
+
use super::*;
|
|
266
|
+
|
|
267
|
+
pub const PROTOCOL_FEE: u64 = 100_000; // 0.0001 SOL
|
|
268
|
+
|
|
269
|
+
pub fn anchor_thread(
|
|
270
|
+
ctx: Context<AnchorThread>,
|
|
271
|
+
thread_id: [u8; 32],
|
|
272
|
+
merkle_root: [u8; 32],
|
|
273
|
+
hop_count: u64,
|
|
274
|
+
intent_hash: [u8; 32],
|
|
275
|
+
compliant: bool,
|
|
276
|
+
) -> Result<()> {
|
|
277
|
+
let anchor = &mut ctx.accounts.thread_anchor;
|
|
278
|
+
|
|
279
|
+
anchor.thread_id = thread_id;
|
|
280
|
+
anchor.merkle_root = merkle_root;
|
|
281
|
+
anchor.hop_count = hop_count;
|
|
282
|
+
anchor.intent_hash = intent_hash;
|
|
283
|
+
anchor.compliant = compliant;
|
|
284
|
+
anchor.timestamp = Clock::get()?.unix_timestamp;
|
|
285
|
+
anchor.anchorer = ctx.accounts.payer.key();
|
|
286
|
+
|
|
287
|
+
// Transfer protocol fee to treasury
|
|
288
|
+
let cpi_context = CpiContext::new(
|
|
289
|
+
ctx.accounts.system_program.to_account_info(),
|
|
290
|
+
anchor_lang::system_program::Transfer {
|
|
291
|
+
from: ctx.accounts.payer.to_account_info(),
|
|
292
|
+
to: ctx.accounts.treasury.to_account_info(),
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
anchor_lang::system_program::transfer(cpi_context, PROTOCOL_FEE)?;
|
|
296
|
+
|
|
297
|
+
emit!(ThreadAnchored {
|
|
298
|
+
thread_id,
|
|
299
|
+
merkle_root,
|
|
300
|
+
anchorer: ctx.accounts.payer.key(),
|
|
301
|
+
timestamp: anchor.timestamp,
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
Ok(())
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
pub fn verify_anchor(
|
|
308
|
+
ctx: Context<VerifyAnchor>,
|
|
309
|
+
thread_id: [u8; 32],
|
|
310
|
+
expected_merkle_root: [u8; 32],
|
|
311
|
+
) -> Result<bool> {
|
|
312
|
+
let anchor = &ctx.accounts.thread_anchor;
|
|
313
|
+
|
|
314
|
+
Ok(anchor.thread_id == thread_id &&
|
|
315
|
+
anchor.merkle_root == expected_merkle_root)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#[derive(Accounts)]
|
|
320
|
+
#[instruction(thread_id: [u8; 32])]
|
|
321
|
+
pub struct AnchorThread<'info> {
|
|
322
|
+
#[account(mut)]
|
|
323
|
+
pub payer: Signer<'info>,
|
|
324
|
+
|
|
325
|
+
#[account(
|
|
326
|
+
init_if_needed,
|
|
327
|
+
payer = payer,
|
|
328
|
+
space = 8 + ThreadAnchor::INIT_SPACE,
|
|
329
|
+
seeds = [b"thread_anchor", thread_id.as_ref()],
|
|
330
|
+
bump
|
|
331
|
+
)]
|
|
332
|
+
pub thread_anchor: Account<'info, ThreadAnchor>,
|
|
333
|
+
|
|
334
|
+
/// CHECK: Treasury account for protocol fees
|
|
335
|
+
#[account(mut)]
|
|
336
|
+
pub treasury: AccountInfo<'info>,
|
|
337
|
+
|
|
338
|
+
pub system_program: Program<'info, System>,
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
#[derive(Accounts)]
|
|
342
|
+
#[instruction(thread_id: [u8; 32])]
|
|
343
|
+
pub struct VerifyAnchor<'info> {
|
|
344
|
+
#[account(
|
|
345
|
+
seeds = [b"thread_anchor", thread_id.as_ref()],
|
|
346
|
+
bump
|
|
347
|
+
)]
|
|
348
|
+
pub thread_anchor: Account<'info, ThreadAnchor>,
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
#[account]
|
|
352
|
+
#[derive(InitSpace)]
|
|
353
|
+
pub struct ThreadAnchor {
|
|
354
|
+
pub thread_id: [u8; 32],
|
|
355
|
+
pub merkle_root: [u8; 32],
|
|
356
|
+
pub hop_count: u64,
|
|
357
|
+
pub intent_hash: [u8; 32],
|
|
358
|
+
pub compliant: bool,
|
|
359
|
+
pub timestamp: i64,
|
|
360
|
+
pub anchorer: Pubkey,
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
#[event]
|
|
364
|
+
pub struct ThreadAnchored {
|
|
365
|
+
pub thread_id: [u8; 32],
|
|
366
|
+
pub merkle_root: [u8; 32],
|
|
367
|
+
pub anchorer: Pubkey,
|
|
368
|
+
pub timestamp: i64,
|
|
369
|
+
}
|
|
370
|
+
`;
|