-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.js
More file actions
260 lines (223 loc) · 6.64 KB
/
Copy pathStateManager.js
File metadata and controls
260 lines (223 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* StateManager - JSON state management with optional Bitcoin anchoring
*
* Manages a JSON state file from a synced git repository.
* Optionally anchors state changes to Bitcoin via blocktrails.
*/
export class StateManager {
/**
* Create a StateManager
* @param {Object} options
* @param {NostrGitClient} options.sync - NostrGitClient instance
* @param {string} options.file - Path to JSON state file (default: 'state.json')
* @param {Object} options.anchor - Bitcoin anchoring options (optional)
* @param {string} options.anchor.privkey - Private key hex for anchoring
* @param {string} options.anchor.network - 'testnet4' or 'mainnet' (default: 'testnet4')
* @param {Function} options.onChange - Callback when state changes
*/
constructor(options) {
this.sync = options.sync;
this.file = options.file || 'state.json';
this.anchorConfig = options.anchor || null;
this.onChange = options.onChange || null;
this.state = null;
this.stateHash = null;
this.anchors = [];
this.blocktrails = null;
// Listen for sync events
this.sync.on('sync', async (event) => {
if (event.status === 'complete') {
await this.loadState();
}
});
}
/**
* Initialize the state manager
* Loads blocktrails if anchoring is enabled
*/
async init() {
// Try to load blocktrails for anchoring
if (this.anchorConfig) {
try {
const bt = await import('blocktrails');
this.blocktrails = bt;
} catch {
console.warn('blocktrails not available - anchoring disabled');
this.anchorConfig = null;
}
}
// Load initial state if synced
if (this.sync.synced) {
await this.loadState();
}
return this;
}
/**
* Load state from the synced file
*/
async loadState() {
try {
const content = await this.sync.readFile(this.file);
const newState = JSON.parse(content);
const newHash = await this.hashState(newState);
if (newHash !== this.stateHash) {
this.state = newState;
this.stateHash = newHash;
if (this.onChange) {
this.onChange(this.state);
}
}
return this.state;
} catch (err) {
// File might not exist yet
if (err.code === 'ENOENT') {
this.state = {};
this.stateHash = null;
return this.state;
}
throw err;
}
}
/**
* Get current state
* @returns {Object} Current state
*/
get() {
return this.state;
}
/**
* Hash state for anchoring/comparison
* @param {Object} state - State to hash
* @returns {string} SHA-256 hash hex
*/
async hashState(state) {
const json = JSON.stringify(state, null, 2);
const bytes = new TextEncoder().encode(json);
const hashBuffer = await crypto.subtle.digest('SHA-256', bytes);
return Array.from(new Uint8Array(hashBuffer))
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}
/**
* Anchor current state to Bitcoin
* Creates a P2TR output committing to the state hash
* @returns {Object} Anchor result with txid
*/
async anchor() {
if (!this.blocktrails || !this.anchorConfig) {
throw new Error('Anchoring not configured');
}
if (!this.state || !this.stateHash) {
throw new Error('No state to anchor');
}
const bt = this.blocktrails;
const privkey = this.anchorConfig.privkey;
const network = this.anchorConfig.network || 'testnet4';
// Get previous anchor states for chaining
const prevStates = this.anchors.map(a => a.hash);
const allStates = [...prevStates, this.stateHash];
// Derive pubkey from privkey
const pubkeyBase = bt.getPublicKey(privkey);
// For first anchor, derive simple tweaked address
// For subsequent anchors, use chained derivation
let fromPubkey, toPubkey;
if (prevStates.length === 0) {
// First anchor - spend from base key to tweaked key
fromPubkey = pubkeyBase;
toPubkey = bt.derivePubkey(pubkeyBase, this.stateHash);
} else {
// Chained anchor - spend from previous tweaked to new tweaked
fromPubkey = bt.deriveChainedPubkey(pubkeyBase, prevStates);
toPubkey = bt.deriveChainedPubkey(pubkeyBase, allStates);
}
// Get UTXOs from previous address
const fromAddress = bt.pubkeyToAddress(fromPubkey, network);
const utxos = await bt.fetchUTXOs(fromAddress, network);
if (utxos.length === 0) {
throw new Error(`No funds at ${fromAddress}`);
}
// Use largest UTXO
const utxo = utxos.reduce((a, b) => a.value > b.value ? a : b);
// Create and broadcast transaction
const toAddress = bt.pubkeyToAddress(toPubkey, network);
const fee = 300; // Minimal fee for P2TR
const tx = bt.createAnchorTx({
utxo,
privkey,
prevStates,
newState: this.stateHash,
toAddress,
fee,
network
});
const txid = await bt.broadcastTx(tx, network);
// Record anchor
const anchor = {
txid,
hash: this.stateHash,
address: toAddress,
timestamp: Date.now(),
state: JSON.parse(JSON.stringify(this.state))
};
this.anchors.push(anchor);
return anchor;
}
/**
* Get all anchors
* @returns {Array} List of anchors
*/
getAnchors() {
return this.anchors;
}
/**
* Verify anchor chain
* Checks that all anchors form a valid chain
* @returns {boolean} True if valid
*/
async verifyChain() {
if (!this.blocktrails || this.anchors.length === 0) {
return true;
}
const bt = this.blocktrails;
const network = this.anchorConfig?.network || 'testnet4';
const pubkeyBase = bt.getPublicKey(this.anchorConfig.privkey);
// Verify each anchor has the expected address
const states = [];
for (const anchor of this.anchors) {
states.push(anchor.hash);
const expectedPubkey = bt.deriveChainedPubkey(pubkeyBase, states);
const expectedAddress = bt.pubkeyToAddress(expectedPubkey, network);
if (anchor.address !== expectedAddress) {
return false;
}
}
return true;
}
/**
* Get anchor chain storage key
*/
get storageKey() {
return `state-anchors-${this.sync.repoId}-${this.file}`;
}
/**
* Save anchors to localStorage
*/
saveAnchors() {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(this.storageKey, JSON.stringify(this.anchors));
}
}
/**
* Load anchors from localStorage
*/
loadAnchors() {
if (typeof localStorage !== 'undefined') {
try {
const saved = localStorage.getItem(this.storageKey);
if (saved) {
this.anchors = JSON.parse(saved);
}
} catch {}
}
}
}