@o-lang/olang 1.2.16 → 1.2.18

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@o-lang/olang",
3
- "version": "1.2.16",
4
- "author": "Olalekan Ogundipe <info@workfily.com>",
3
+ "version": "1.2.18",
4
+ "author": "Olalekan Ogundipe <info@olang.cloud>",
5
5
  "description": "O-Lang: A governance language for user-directed, rule-enforced agent workflows",
6
6
  "main": "./src/runtime/index.js",
7
7
  "bin": {
@@ -1,5 +1,99 @@
1
1
  // src/runtime/index.js
2
- const { RuntimeAPI, execute } = require('./RuntimeAPI');
3
- const { parse} = require('../parser');
2
+ const { RuntimeAPI } = require('./RuntimeAPI');
3
+ const { parse } = require('../parser');
4
+ const crypto = require('crypto');
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+
8
+ // ✅ Load kernel private key (cached)
9
+ let KERNEL_PRIVATE_KEY = null;
10
+ function getKernelPrivateKey() {
11
+ if (KERNEL_PRIVATE_KEY) return KERNEL_PRIVATE_KEY;
12
+
13
+ const keyPath = process.env.KERNEL_PRIVATE_KEY_PATH || './kernel-keys/kernel-private.pem';
14
+ const absolutePath = path.isAbsolute(keyPath) ? keyPath : path.join(process.cwd(), keyPath);
15
+
16
+ if (!fs.existsSync(absolutePath)) {
17
+ console.warn('[kernel] Warning: Private key not found at', absolutePath);
18
+ console.warn('[kernel] Audit entries will NOT be signed');
19
+ return null;
20
+ }
21
+
22
+ KERNEL_PRIVATE_KEY = fs.readFileSync(absolutePath, 'utf8');
23
+ return KERNEL_PRIVATE_KEY;
24
+ }
25
+
26
+ // ✅ Sign audit data with kernel private key
27
+ function signAuditData(auditData, privateKey) {
28
+ if (!privateKey) return null;
29
+
30
+ try {
31
+ // Serialize audit data EXACTLY as it will be verified
32
+ // (sorted keys for deterministic serialization)
33
+ const serialized = JSON.stringify(auditData, Object.keys(auditData).sort());
34
+
35
+ // Create signature
36
+ const sign = crypto.createSign('SHA256');
37
+ sign.update(serialized);
38
+ sign.end();
39
+
40
+ const signature = sign.sign(privateKey, 'hex');
41
+ return signature;
42
+ } catch (err) {
43
+ console.error('[kernel] Signature error:', err.message);
44
+ return null;
45
+ }
46
+ }
47
+
48
+ async function execute(workflow, inputs, agentResolver, verbose = false) {
49
+ const rt = new RuntimeAPI({ verbose });
50
+
51
+ // run the workflow — result only contains Return values
52
+ const result = await rt.executeWorkflow(workflow, inputs, agentResolver);
53
+
54
+ // rt is still alive here — grab audit before it dies
55
+ const lastEntry = rt.auditLog.at(-1);
56
+ const firstEntry = rt.auditLog.at(0);
57
+
58
+ // Build audit object BEFORE signing
59
+ const auditData = {
60
+ execution_hash: lastEntry?.hash ?? null,
61
+ previous_hash: firstEntry?.hash ?? 'GENESIS',
62
+ merkle_root: rt._calculateMerkleRoot(),
63
+ kernel_version: lastEntry?.details?.kernel_version ?? null,
64
+ governance_profile_hash: lastEntry?.details?.governance_profile_hash ?? null,
65
+ integrity: rt.verifyAuditLogIntegrity(),
66
+ chain: rt.auditLog,
67
+ };
68
+
69
+ // ✅ SIGN the audit data
70
+ const privateKey = getKernelPrivateKey();
71
+ const signature = signAuditData(auditData, privateKey);
72
+
73
+ // Load public key for verification (optional but helpful)
74
+ let publicKey = null;
75
+ if (privateKey) {
76
+ try {
77
+ const pubKeyPath = process.env.KERNEL_PUBLIC_KEY_PATH || './kernel-keys/kernel-public.pem';
78
+ const absolutePubPath = path.isAbsolute(pubKeyPath) ? pubKeyPath : path.join(process.cwd(), pubKeyPath);
79
+ if (fs.existsSync(absolutePubPath)) {
80
+ publicKey = fs.readFileSync(absolutePubPath, 'utf8')
81
+ .replace('-----BEGIN PUBLIC KEY-----', '')
82
+ .replace('-----END PUBLIC KEY-----', '')
83
+ .replace(/\s/g, '');
84
+ }
85
+ } catch (err) {
86
+ console.warn('[kernel] Could not load public key:', err.message);
87
+ }
88
+ }
89
+
90
+ result.__audit = {
91
+ ...auditData,
92
+ signature, // ✅ Cryptographic signature
93
+ publicKey, // ✅ Public key for verification
94
+ };
95
+
96
+ return result;
97
+ }
4
98
 
5
99
  module.exports = { execute, parse };