@totemsdk/core 1.0.9 → 1.0.10
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/LICENSE +21 -0
- package/README.md +5 -5
- package/dist/Streamable.js +54 -31
- package/dist/adapters/index.js +18 -8
- package/dist/adapters/types.js +19 -8
- package/dist/binio.js +5 -1
- package/dist/bip39.js +24 -14
- package/dist/derive.js +11 -7
- package/dist/index.js +130 -22
- package/dist/javaStreamables.js +49 -30
- package/dist/lease/LeaseMonitor.js +7 -3
- package/dist/lease/LeaseStore.js +7 -3
- package/dist/lease/WatermarkStore.js +7 -3
- package/dist/lease/index.js +13 -4
- package/dist/lease-client.js +8 -3
- package/dist/minima32.js +22 -13
- package/dist/minimaWireSerializer.js +20 -14
- package/dist/mmr.js +35 -23
- package/dist/mx.js +10 -1
- package/dist/params.js +9 -5
- package/dist/script.js +12 -7
- package/dist/scripts/contract-helpers.js +37 -26
- package/dist/scripts/dapp-types.js +11 -6
- package/dist/scripts/index.js +38 -4
- package/dist/scripts/types.js +26 -12
- package/dist/scripts/witness-serializer.js +48 -30
- package/dist/transaction.js +55 -46
- package/dist/treekey.js +74 -58
- package/dist/tx/TransactionLifecycle.js +11 -5
- package/dist/tx/TransactionReceiptStore.js +7 -3
- package/dist/tx/TransactionService.js +14 -10
- package/dist/tx/index.js +11 -3
- package/dist/tx/types.js +2 -1
- package/dist/util.js +5 -1
- package/dist/utils.js +12 -5
- package/dist/verify.js +36 -25
- package/dist/version.js +5 -2
- package/dist/wots.js +94 -64
- package/package.json +31 -6
- package/src/Streamable.d.ts +0 -268
- package/src/Streamable.js +0 -416
- package/src/Streamable.ts +0 -567
- package/src/__tests__/Streamable.parity.test.ts +0 -422
- package/src/__tests__/perAddressDerivation.parity.test.ts +0 -152
- package/src/__tests__/roottree-prepare.test.ts +0 -101
- package/src/__tests__/wots-parity.test.ts +0 -289
- package/src/adapters/index.d.ts +0 -190
- package/src/adapters/index.js +0 -117
- package/src/adapters/index.ts +0 -301
- package/src/adapters/types.ts +0 -235
- package/src/addr.oracle.test.ts +0 -25
- package/src/address.test.ts +0 -55
- package/src/binio.ts +0 -21
- package/src/bip39.d.ts +0 -91
- package/src/bip39.js +0 -427
- package/src/bip39.ts +0 -432
- package/src/derive.d.ts +0 -2
- package/src/derive.js +0 -13
- package/src/derive.ts +0 -11
- package/src/index.d.ts +0 -26
- package/src/index.js +0 -128
- package/src/index.test.ts +0 -5
- package/src/index.ts +0 -221
- package/src/javaStreamables.d.ts +0 -233
- package/src/javaStreamables.js +0 -332
- package/src/javaStreamables.ts +0 -403
- package/src/lease/LeaseMonitor.d.ts +0 -44
- package/src/lease/LeaseMonitor.js +0 -111
- package/src/lease/LeaseMonitor.ts +0 -157
- package/src/lease/LeaseStore.d.ts +0 -57
- package/src/lease/LeaseStore.js +0 -148
- package/src/lease/LeaseStore.ts +0 -203
- package/src/lease/WatermarkStore.d.ts +0 -61
- package/src/lease/WatermarkStore.js +0 -206
- package/src/lease/WatermarkStore.ts +0 -270
- package/src/lease/index.d.ts +0 -14
- package/src/lease/index.js +0 -22
- package/src/lease/index.ts +0 -41
- package/src/lease-client.d.ts +0 -31
- package/src/lease-client.js +0 -44
- package/src/lease-client.ts +0 -69
- package/src/minima32.d.ts +0 -10
- package/src/minima32.js +0 -113
- package/src/minima32.test.ts +0 -11
- package/src/minima32.ts +0 -127
- package/src/minimaWireSerializer.d.ts +0 -189
- package/src/minimaWireSerializer.js +0 -311
- package/src/minimaWireSerializer.test.ts +0 -466
- package/src/minimaWireSerializer.ts +0 -117
- package/src/mmr.d.ts +0 -157
- package/src/mmr.js +0 -340
- package/src/mmr.oracle.test.ts +0 -28
- package/src/mmr.ts +0 -429
- package/src/mx.test.ts +0 -25
- package/src/mx.ts +0 -1
- package/src/params.d.ts +0 -29
- package/src/params.d.ts.map +0 -1
- package/src/params.js +0 -20
- package/src/params.js.map +0 -1
- package/src/params.ts +0 -44
- package/src/script.d.ts +0 -7
- package/src/script.js +0 -27
- package/src/script.ts +0 -24
- package/src/scripts/contract-helpers.ts +0 -609
- package/src/scripts/dapp-types.ts +0 -240
- package/src/scripts/index.ts +0 -22
- package/src/scripts/types.d.ts +0 -81
- package/src/scripts/types.js +0 -145
- package/src/scripts/types.ts +0 -247
- package/src/scripts/witness-serializer.ts +0 -384
- package/src/transaction.ts +0 -430
- package/src/treekey.d.ts +0 -383
- package/src/treekey.js +0 -774
- package/src/treekey.parity.test.ts +0 -459
- package/src/treekey.ts +0 -975
- package/src/tx/TransactionLifecycle.d.ts +0 -55
- package/src/tx/TransactionLifecycle.js +0 -188
- package/src/tx/TransactionLifecycle.ts +0 -280
- package/src/tx/TransactionReceiptStore.d.ts +0 -36
- package/src/tx/TransactionReceiptStore.js +0 -89
- package/src/tx/TransactionReceiptStore.ts +0 -112
- package/src/tx/TransactionService.d.ts +0 -31
- package/src/tx/TransactionService.js +0 -140
- package/src/tx/TransactionService.ts +0 -252
- package/src/tx/index.d.ts +0 -13
- package/src/tx/index.js +0 -20
- package/src/tx/index.ts +0 -44
- package/src/tx/types.d.ts +0 -80
- package/src/tx/types.js +0 -6
- package/src/tx/types.ts +0 -130
- package/src/util.d.ts +0 -1
- package/src/util.js +0 -5
- package/src/util.ts +0 -1
- package/src/utils.d.ts +0 -8
- package/src/utils.js +0 -42
- package/src/utils.ts +0 -38
- package/src/verify.ts +0 -210
- package/src/version.ts +0 -13
- package/src/wots.d.ts +0 -224
- package/src/wots.d.ts.map +0 -1
- package/src/wots.js +0 -565
- package/src/wots.js.map +0 -1
- package/src/wots.ts +0 -651
- package/test-vectors.json +0 -45
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @module WatermarkStore
|
|
4
|
-
* WOTS index tracking with server synchronization
|
|
5
|
-
*
|
|
6
|
-
* Uses in-memory cache backed by persistent storage.
|
|
7
|
-
* Pattern:
|
|
8
|
-
* - initialize() loads from storage into memory cache (or creates default)
|
|
9
|
-
* - Sync getters read from memory cache (fast, no I/O)
|
|
10
|
-
* - Mutators update memory cache AND persist to storage
|
|
11
|
-
* - Caller MUST call initialize() before using sync getters
|
|
12
|
-
*/
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.WatermarkStore = void 0;
|
|
15
|
-
const adapters_1 = require("../adapters");
|
|
16
|
-
const DEFAULT_STORAGE_KEY = 'totem_wots_watermark';
|
|
17
|
-
const MAX_INDEX_PER_LEVEL = 64;
|
|
18
|
-
const TOTAL_INDICES = 64 * 64 * 64;
|
|
19
|
-
class WatermarkStore {
|
|
20
|
-
constructor(storage, logger = new adapters_1.NoopLogger(), config = {}) {
|
|
21
|
-
this.state = null;
|
|
22
|
-
this._initialized = false;
|
|
23
|
-
this.storage = storage;
|
|
24
|
-
this.logger = logger;
|
|
25
|
-
this.storageKey = config.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
26
|
-
}
|
|
27
|
-
async initialize() {
|
|
28
|
-
if (this._initialized && this.state) {
|
|
29
|
-
return this.state;
|
|
30
|
-
}
|
|
31
|
-
const existing = await this.load();
|
|
32
|
-
if (existing) {
|
|
33
|
-
this._initialized = true;
|
|
34
|
-
return existing;
|
|
35
|
-
}
|
|
36
|
-
const initialState = {
|
|
37
|
-
next_addressIndex: 0,
|
|
38
|
-
next_l1: 0,
|
|
39
|
-
next_l2: 0,
|
|
40
|
-
usedIndices: []
|
|
41
|
-
};
|
|
42
|
-
await this.save(initialState);
|
|
43
|
-
this._initialized = true;
|
|
44
|
-
return initialState;
|
|
45
|
-
}
|
|
46
|
-
isInitialized() {
|
|
47
|
-
return this._initialized;
|
|
48
|
-
}
|
|
49
|
-
async load() {
|
|
50
|
-
try {
|
|
51
|
-
const watermark = await this.storage.get(this.storageKey);
|
|
52
|
-
if (watermark) {
|
|
53
|
-
this.state = watermark;
|
|
54
|
-
this.logger.debug('Loaded watermark from storage');
|
|
55
|
-
return this.state;
|
|
56
|
-
}
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
this.logger.error('Failed to load watermark:', error);
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
async save(watermark) {
|
|
65
|
-
try {
|
|
66
|
-
this.state = watermark;
|
|
67
|
-
await this.storage.set(this.storageKey, watermark);
|
|
68
|
-
}
|
|
69
|
-
catch (error) {
|
|
70
|
-
this.logger.error('Failed to save watermark:', error);
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
async clear() {
|
|
75
|
-
try {
|
|
76
|
-
this.state = null;
|
|
77
|
-
this._initialized = false;
|
|
78
|
-
await this.storage.remove(this.storageKey);
|
|
79
|
-
this.logger.debug('Cleared watermark');
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
this.logger.error('Failed to clear watermark:', error);
|
|
83
|
-
throw error;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
getCurrent() {
|
|
87
|
-
return this.state;
|
|
88
|
-
}
|
|
89
|
-
getNextIndices() {
|
|
90
|
-
if (!this.state || this.isExhausted()) {
|
|
91
|
-
return null;
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
addressIndex: this.state.next_addressIndex,
|
|
95
|
-
l1: this.state.next_l1,
|
|
96
|
-
l2: this.state.next_l2
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
isExhausted() {
|
|
100
|
-
if (!this.state)
|
|
101
|
-
return false;
|
|
102
|
-
return this.state.next_addressIndex >= MAX_INDEX_PER_LEVEL;
|
|
103
|
-
}
|
|
104
|
-
hasAvailableIndices() {
|
|
105
|
-
return !this.isExhausted();
|
|
106
|
-
}
|
|
107
|
-
async markUsed(indices) {
|
|
108
|
-
if (!this.state) {
|
|
109
|
-
await this.load();
|
|
110
|
-
}
|
|
111
|
-
if (!this.state) {
|
|
112
|
-
throw new Error('Watermark not initialized');
|
|
113
|
-
}
|
|
114
|
-
const indexTuple = [indices.addressIndex, indices.l1, indices.l2];
|
|
115
|
-
const flatIndex = this.flattenIndex(indices.addressIndex, indices.l1, indices.l2);
|
|
116
|
-
const alreadyUsed = this.state.usedIndices.some(([addressIndex, l1, l2]) => addressIndex === indices.addressIndex && l1 === indices.l1 && l2 === indices.l2);
|
|
117
|
-
if (!alreadyUsed) {
|
|
118
|
-
this.logger.debug(`Marking indices as USED: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2}) [${flatIndex}/${TOTAL_INDICES}]`);
|
|
119
|
-
this.logger.debug(` total used: ${this.state.usedIndices.length + 1} / ${TOTAL_INDICES}`);
|
|
120
|
-
this.state.usedIndices.push(indexTuple);
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
this.logger.warn(`Indices already marked as used: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2})`);
|
|
124
|
-
}
|
|
125
|
-
await this.save(this.state);
|
|
126
|
-
}
|
|
127
|
-
async advanceWatermark(indices) {
|
|
128
|
-
if (!this.state) {
|
|
129
|
-
await this.load();
|
|
130
|
-
}
|
|
131
|
-
if (!this.state) {
|
|
132
|
-
throw new Error('Watermark not initialized');
|
|
133
|
-
}
|
|
134
|
-
const currentFlat = this.flattenIndex(indices.addressIndex, indices.l1, indices.l2);
|
|
135
|
-
const nextIndices = this.calculateNextIndices(indices);
|
|
136
|
-
const nextFlat = this.flattenIndex(nextIndices.addressIndex, nextIndices.l1, nextIndices.l2);
|
|
137
|
-
this.logger.debug(`Advancing watermark...`);
|
|
138
|
-
this.logger.debug(` current: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2}) [${currentFlat}/${TOTAL_INDICES}]`);
|
|
139
|
-
this.logger.debug(` next: (addressIndex=${nextIndices.addressIndex}, l1=${nextIndices.l1}, l2=${nextIndices.l2}) [${nextFlat}/${TOTAL_INDICES}]`);
|
|
140
|
-
this.state.next_addressIndex = nextIndices.addressIndex;
|
|
141
|
-
this.state.next_l1 = nextIndices.l1;
|
|
142
|
-
this.state.next_l2 = nextIndices.l2;
|
|
143
|
-
await this.save(this.state);
|
|
144
|
-
this.logger.debug('Watermark advanced and persisted');
|
|
145
|
-
}
|
|
146
|
-
async updateFromServer(serverWatermark) {
|
|
147
|
-
if (!this.state) {
|
|
148
|
-
await this.load();
|
|
149
|
-
}
|
|
150
|
-
if (!this.state) {
|
|
151
|
-
await this.initialize();
|
|
152
|
-
}
|
|
153
|
-
const localIndexFlat = this.flattenIndex(this.state.next_addressIndex, this.state.next_l1, this.state.next_l2);
|
|
154
|
-
const serverIndexFlat = this.flattenIndex(serverWatermark.addressIndex, serverWatermark.l1, serverWatermark.l2);
|
|
155
|
-
const drift = serverIndexFlat - localIndexFlat;
|
|
156
|
-
const hasConflict = drift > 1;
|
|
157
|
-
const needsUpdate = this.shouldUseServerWatermark(this.state, serverWatermark);
|
|
158
|
-
if (needsUpdate) {
|
|
159
|
-
this.logger.debug(`Updating watermark from server (drift: ${drift})`);
|
|
160
|
-
this.state.next_addressIndex = serverWatermark.addressIndex;
|
|
161
|
-
this.state.next_l1 = serverWatermark.l1;
|
|
162
|
-
this.state.next_l2 = serverWatermark.l2;
|
|
163
|
-
this.state.serverWatermark = serverWatermark;
|
|
164
|
-
this.state.lastSyncTimestamp = Date.now();
|
|
165
|
-
await this.save(this.state);
|
|
166
|
-
return { updated: true, drift, hasConflict };
|
|
167
|
-
}
|
|
168
|
-
this.state.lastSyncTimestamp = Date.now();
|
|
169
|
-
await this.save(this.state);
|
|
170
|
-
return { updated: false, drift, hasConflict };
|
|
171
|
-
}
|
|
172
|
-
getUsageStats() {
|
|
173
|
-
const used = this.state?.usedIndices.length ?? 0;
|
|
174
|
-
return {
|
|
175
|
-
used,
|
|
176
|
-
total: TOTAL_INDICES,
|
|
177
|
-
percentage: (used / TOTAL_INDICES) * 100
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
calculateNextIndices(current) {
|
|
181
|
-
let { addressIndex, l1, l2 } = current;
|
|
182
|
-
l2++;
|
|
183
|
-
if (l2 >= MAX_INDEX_PER_LEVEL) {
|
|
184
|
-
l2 = 0;
|
|
185
|
-
l1++;
|
|
186
|
-
if (l1 >= MAX_INDEX_PER_LEVEL) {
|
|
187
|
-
l1 = 0;
|
|
188
|
-
addressIndex++;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return { addressIndex, l1, l2 };
|
|
192
|
-
}
|
|
193
|
-
shouldUseServerWatermark(local, server) {
|
|
194
|
-
if (server.addressIndex > local.next_addressIndex)
|
|
195
|
-
return true;
|
|
196
|
-
if (server.addressIndex === local.next_addressIndex && server.l1 > local.next_l1)
|
|
197
|
-
return true;
|
|
198
|
-
if (server.addressIndex === local.next_addressIndex && server.l1 === local.next_l1 && server.l2 > local.next_l2)
|
|
199
|
-
return true;
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
flattenIndex(addressIndex, l1, l2) {
|
|
203
|
-
return (addressIndex * MAX_INDEX_PER_LEVEL * MAX_INDEX_PER_LEVEL) + (l1 * MAX_INDEX_PER_LEVEL) + l2;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
exports.WatermarkStore = WatermarkStore;
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module WatermarkStore
|
|
3
|
-
* WOTS index tracking with server synchronization
|
|
4
|
-
*
|
|
5
|
-
* Uses in-memory cache backed by persistent storage.
|
|
6
|
-
* Pattern:
|
|
7
|
-
* - initialize() loads from storage into memory cache (or creates default)
|
|
8
|
-
* - Sync getters read from memory cache (fast, no I/O)
|
|
9
|
-
* - Mutators update memory cache AND persist to storage
|
|
10
|
-
* - Caller MUST call initialize() before using sync getters
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { StorageAdapter, LoggerAdapter } from '../adapters/index.js';
|
|
14
|
-
import { NoopLogger } from '../adapters/index.js';
|
|
15
|
-
|
|
16
|
-
export interface WotsIndices {
|
|
17
|
-
addressIndex: number;
|
|
18
|
-
l1: number;
|
|
19
|
-
l2: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface WatermarkState {
|
|
23
|
-
next_addressIndex: number;
|
|
24
|
-
next_l1: number;
|
|
25
|
-
next_l2: number;
|
|
26
|
-
usedIndices: Array<[number, number, number]>;
|
|
27
|
-
lastSyncTimestamp?: number;
|
|
28
|
-
serverWatermark?: WotsIndices;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface SyncResult {
|
|
32
|
-
updated: boolean;
|
|
33
|
-
drift: number;
|
|
34
|
-
hasConflict: boolean;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface WatermarkStoreConfig {
|
|
38
|
-
storageKey?: string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const DEFAULT_STORAGE_KEY = 'totem_wots_watermark';
|
|
42
|
-
const MAX_INDEX_PER_LEVEL = 64;
|
|
43
|
-
const TOTAL_INDICES = 64 * 64 * 64;
|
|
44
|
-
|
|
45
|
-
export class WatermarkStore {
|
|
46
|
-
private state: WatermarkState | null = null;
|
|
47
|
-
private _initialized = false;
|
|
48
|
-
private readonly storageKey: string;
|
|
49
|
-
private readonly storage: StorageAdapter;
|
|
50
|
-
private readonly logger: LoggerAdapter;
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
storage: StorageAdapter,
|
|
54
|
-
logger: LoggerAdapter = new NoopLogger(),
|
|
55
|
-
config: WatermarkStoreConfig = {}
|
|
56
|
-
) {
|
|
57
|
-
this.storage = storage;
|
|
58
|
-
this.logger = logger;
|
|
59
|
-
this.storageKey = config.storageKey ?? DEFAULT_STORAGE_KEY;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async initialize(): Promise<WatermarkState> {
|
|
63
|
-
if (this._initialized && this.state) {
|
|
64
|
-
return this.state;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const existing = await this.load();
|
|
68
|
-
if (existing) {
|
|
69
|
-
this._initialized = true;
|
|
70
|
-
return existing;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const initialState: WatermarkState = {
|
|
74
|
-
next_addressIndex: 0,
|
|
75
|
-
next_l1: 0,
|
|
76
|
-
next_l2: 0,
|
|
77
|
-
usedIndices: []
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
await this.save(initialState);
|
|
81
|
-
this._initialized = true;
|
|
82
|
-
return initialState;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
isInitialized(): boolean {
|
|
86
|
-
return this._initialized;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async load(): Promise<WatermarkState | null> {
|
|
90
|
-
try {
|
|
91
|
-
const watermark = await this.storage.get<WatermarkState>(this.storageKey);
|
|
92
|
-
if (watermark) {
|
|
93
|
-
this.state = watermark;
|
|
94
|
-
this.logger.debug('Loaded watermark from storage');
|
|
95
|
-
return this.state;
|
|
96
|
-
}
|
|
97
|
-
return null;
|
|
98
|
-
} catch (error) {
|
|
99
|
-
this.logger.error('Failed to load watermark:', error);
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async save(watermark: WatermarkState): Promise<void> {
|
|
105
|
-
try {
|
|
106
|
-
this.state = watermark;
|
|
107
|
-
await this.storage.set(this.storageKey, watermark);
|
|
108
|
-
} catch (error) {
|
|
109
|
-
this.logger.error('Failed to save watermark:', error);
|
|
110
|
-
throw error;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async clear(): Promise<void> {
|
|
115
|
-
try {
|
|
116
|
-
this.state = null;
|
|
117
|
-
this._initialized = false;
|
|
118
|
-
await this.storage.remove(this.storageKey);
|
|
119
|
-
this.logger.debug('Cleared watermark');
|
|
120
|
-
} catch (error) {
|
|
121
|
-
this.logger.error('Failed to clear watermark:', error);
|
|
122
|
-
throw error;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
getCurrent(): WatermarkState | null {
|
|
127
|
-
return this.state;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
getNextIndices(): WotsIndices | null {
|
|
131
|
-
if (!this.state || this.isExhausted()) {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return {
|
|
136
|
-
addressIndex: this.state.next_addressIndex,
|
|
137
|
-
l1: this.state.next_l1,
|
|
138
|
-
l2: this.state.next_l2
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
isExhausted(): boolean {
|
|
143
|
-
if (!this.state) return false;
|
|
144
|
-
return this.state.next_addressIndex >= MAX_INDEX_PER_LEVEL;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
hasAvailableIndices(): boolean {
|
|
148
|
-
return !this.isExhausted();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
async markUsed(indices: WotsIndices): Promise<void> {
|
|
152
|
-
if (!this.state) {
|
|
153
|
-
await this.load();
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
if (!this.state) {
|
|
157
|
-
throw new Error('Watermark not initialized');
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const indexTuple: [number, number, number] = [indices.addressIndex, indices.l1, indices.l2];
|
|
161
|
-
const flatIndex = this.flattenIndex(indices.addressIndex, indices.l1, indices.l2);
|
|
162
|
-
|
|
163
|
-
const alreadyUsed = this.state.usedIndices.some(([addressIndex, l1, l2]) =>
|
|
164
|
-
addressIndex === indices.addressIndex && l1 === indices.l1 && l2 === indices.l2
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
if (!alreadyUsed) {
|
|
168
|
-
this.logger.debug(`Marking indices as USED: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2}) [${flatIndex}/${TOTAL_INDICES}]`);
|
|
169
|
-
this.logger.debug(` total used: ${this.state.usedIndices.length + 1} / ${TOTAL_INDICES}`);
|
|
170
|
-
this.state.usedIndices.push(indexTuple);
|
|
171
|
-
} else {
|
|
172
|
-
this.logger.warn(`Indices already marked as used: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2})`);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
await this.save(this.state);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async advanceWatermark(indices: WotsIndices): Promise<void> {
|
|
179
|
-
if (!this.state) {
|
|
180
|
-
await this.load();
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (!this.state) {
|
|
184
|
-
throw new Error('Watermark not initialized');
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const currentFlat = this.flattenIndex(indices.addressIndex, indices.l1, indices.l2);
|
|
188
|
-
const nextIndices = this.calculateNextIndices(indices);
|
|
189
|
-
const nextFlat = this.flattenIndex(nextIndices.addressIndex, nextIndices.l1, nextIndices.l2);
|
|
190
|
-
|
|
191
|
-
this.logger.debug(`Advancing watermark...`);
|
|
192
|
-
this.logger.debug(` current: (addressIndex=${indices.addressIndex}, l1=${indices.l1}, l2=${indices.l2}) [${currentFlat}/${TOTAL_INDICES}]`);
|
|
193
|
-
this.logger.debug(` next: (addressIndex=${nextIndices.addressIndex}, l1=${nextIndices.l1}, l2=${nextIndices.l2}) [${nextFlat}/${TOTAL_INDICES}]`);
|
|
194
|
-
|
|
195
|
-
this.state.next_addressIndex = nextIndices.addressIndex;
|
|
196
|
-
this.state.next_l1 = nextIndices.l1;
|
|
197
|
-
this.state.next_l2 = nextIndices.l2;
|
|
198
|
-
|
|
199
|
-
await this.save(this.state);
|
|
200
|
-
this.logger.debug('Watermark advanced and persisted');
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async updateFromServer(serverWatermark: WotsIndices): Promise<SyncResult> {
|
|
204
|
-
if (!this.state) {
|
|
205
|
-
await this.load();
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (!this.state) {
|
|
209
|
-
await this.initialize();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
const localIndexFlat = this.flattenIndex(this.state!.next_addressIndex, this.state!.next_l1, this.state!.next_l2);
|
|
213
|
-
const serverIndexFlat = this.flattenIndex(serverWatermark.addressIndex, serverWatermark.l1, serverWatermark.l2);
|
|
214
|
-
const drift = serverIndexFlat - localIndexFlat;
|
|
215
|
-
const hasConflict = drift > 1;
|
|
216
|
-
|
|
217
|
-
const needsUpdate = this.shouldUseServerWatermark(this.state!, serverWatermark);
|
|
218
|
-
|
|
219
|
-
if (needsUpdate) {
|
|
220
|
-
this.logger.debug(`Updating watermark from server (drift: ${drift})`);
|
|
221
|
-
this.state!.next_addressIndex = serverWatermark.addressIndex;
|
|
222
|
-
this.state!.next_l1 = serverWatermark.l1;
|
|
223
|
-
this.state!.next_l2 = serverWatermark.l2;
|
|
224
|
-
this.state!.serverWatermark = serverWatermark;
|
|
225
|
-
this.state!.lastSyncTimestamp = Date.now();
|
|
226
|
-
await this.save(this.state!);
|
|
227
|
-
return { updated: true, drift, hasConflict };
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
this.state!.lastSyncTimestamp = Date.now();
|
|
231
|
-
await this.save(this.state!);
|
|
232
|
-
return { updated: false, drift, hasConflict };
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
getUsageStats(): { used: number; total: number; percentage: number } {
|
|
236
|
-
const used = this.state?.usedIndices.length ?? 0;
|
|
237
|
-
return {
|
|
238
|
-
used,
|
|
239
|
-
total: TOTAL_INDICES,
|
|
240
|
-
percentage: (used / TOTAL_INDICES) * 100
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
private calculateNextIndices(current: WotsIndices): WotsIndices {
|
|
245
|
-
let { addressIndex, l1, l2 } = current;
|
|
246
|
-
|
|
247
|
-
l2++;
|
|
248
|
-
if (l2 >= MAX_INDEX_PER_LEVEL) {
|
|
249
|
-
l2 = 0;
|
|
250
|
-
l1++;
|
|
251
|
-
if (l1 >= MAX_INDEX_PER_LEVEL) {
|
|
252
|
-
l1 = 0;
|
|
253
|
-
addressIndex++;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return { addressIndex, l1, l2 };
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private shouldUseServerWatermark(local: WatermarkState, server: WotsIndices): boolean {
|
|
261
|
-
if (server.addressIndex > local.next_addressIndex) return true;
|
|
262
|
-
if (server.addressIndex === local.next_addressIndex && server.l1 > local.next_l1) return true;
|
|
263
|
-
if (server.addressIndex === local.next_addressIndex && server.l1 === local.next_l1 && server.l2 > local.next_l2) return true;
|
|
264
|
-
return false;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private flattenIndex(addressIndex: number, l1: number, l2: number): number {
|
|
268
|
-
return (addressIndex * MAX_INDEX_PER_LEVEL * MAX_INDEX_PER_LEVEL) + (l1 * MAX_INDEX_PER_LEVEL) + l2;
|
|
269
|
-
}
|
|
270
|
-
}
|
package/src/lease/index.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @totemsdk/core/lease
|
|
3
|
-
* WOTS lease lifecycle management
|
|
4
|
-
*
|
|
5
|
-
* This module provides:
|
|
6
|
-
* - LeaseStore: Persistent storage for lease records with in-memory cache
|
|
7
|
-
* - WatermarkStore: WOTS index tracking with server sync
|
|
8
|
-
* - LeaseMonitor: Expiry monitoring and renewal
|
|
9
|
-
*/
|
|
10
|
-
export { LeaseStore, type StoredLease, type LeaseStatus, type LeaseStoreConfig, type WotsIndices as LeaseWotsIndices, } from './LeaseStore';
|
|
11
|
-
export { WatermarkStore, type WatermarkState, type SyncResult, type WatermarkStoreConfig, } from './WatermarkStore';
|
|
12
|
-
export type { WotsIndices } from './WatermarkStore';
|
|
13
|
-
export { LeaseMonitor, type LeaseExpiryEvent, type LeaseExpiryCallback, type LeaseMonitorConfig, } from './LeaseMonitor';
|
|
14
|
-
export { prepareLease, finalizeLease, flatIndexFromLanes, type PrepareArgs, type PrepareResp } from '../lease-client';
|
package/src/lease/index.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @module @totemsdk/core/lease
|
|
4
|
-
* WOTS lease lifecycle management
|
|
5
|
-
*
|
|
6
|
-
* This module provides:
|
|
7
|
-
* - LeaseStore: Persistent storage for lease records with in-memory cache
|
|
8
|
-
* - WatermarkStore: WOTS index tracking with server sync
|
|
9
|
-
* - LeaseMonitor: Expiry monitoring and renewal
|
|
10
|
-
*/
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.flatIndexFromLanes = exports.finalizeLease = exports.prepareLease = exports.LeaseMonitor = exports.WatermarkStore = exports.LeaseStore = void 0;
|
|
13
|
-
var LeaseStore_1 = require("./LeaseStore");
|
|
14
|
-
Object.defineProperty(exports, "LeaseStore", { enumerable: true, get: function () { return LeaseStore_1.LeaseStore; } });
|
|
15
|
-
var WatermarkStore_1 = require("./WatermarkStore");
|
|
16
|
-
Object.defineProperty(exports, "WatermarkStore", { enumerable: true, get: function () { return WatermarkStore_1.WatermarkStore; } });
|
|
17
|
-
var LeaseMonitor_1 = require("./LeaseMonitor");
|
|
18
|
-
Object.defineProperty(exports, "LeaseMonitor", { enumerable: true, get: function () { return LeaseMonitor_1.LeaseMonitor; } });
|
|
19
|
-
var lease_client_1 = require("../lease-client");
|
|
20
|
-
Object.defineProperty(exports, "prepareLease", { enumerable: true, get: function () { return lease_client_1.prepareLease; } });
|
|
21
|
-
Object.defineProperty(exports, "finalizeLease", { enumerable: true, get: function () { return lease_client_1.finalizeLease; } });
|
|
22
|
-
Object.defineProperty(exports, "flatIndexFromLanes", { enumerable: true, get: function () { return lease_client_1.flatIndexFromLanes; } });
|
package/src/lease/index.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @totemsdk/core/lease
|
|
3
|
-
* WOTS lease lifecycle management
|
|
4
|
-
*
|
|
5
|
-
* This module provides:
|
|
6
|
-
* - LeaseStore: Persistent storage for lease records with in-memory cache
|
|
7
|
-
* - WatermarkStore: WOTS index tracking with server sync
|
|
8
|
-
* - LeaseMonitor: Expiry monitoring and renewal
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export {
|
|
12
|
-
LeaseStore,
|
|
13
|
-
type StoredLease,
|
|
14
|
-
type LeaseStatus,
|
|
15
|
-
type LeaseStoreConfig,
|
|
16
|
-
type WotsIndices as LeaseWotsIndices,
|
|
17
|
-
} from './LeaseStore.js';
|
|
18
|
-
|
|
19
|
-
export {
|
|
20
|
-
WatermarkStore,
|
|
21
|
-
type WatermarkState,
|
|
22
|
-
type SyncResult,
|
|
23
|
-
type WatermarkStoreConfig,
|
|
24
|
-
} from './WatermarkStore.js';
|
|
25
|
-
|
|
26
|
-
export type { WotsIndices } from './WatermarkStore.js';
|
|
27
|
-
|
|
28
|
-
export {
|
|
29
|
-
LeaseMonitor,
|
|
30
|
-
type LeaseExpiryEvent,
|
|
31
|
-
type LeaseExpiryCallback,
|
|
32
|
-
type LeaseMonitorConfig,
|
|
33
|
-
} from './LeaseMonitor.js';
|
|
34
|
-
|
|
35
|
-
export {
|
|
36
|
-
prepareLease,
|
|
37
|
-
finalizeLease,
|
|
38
|
-
flatIndexFromLanes,
|
|
39
|
-
type PrepareArgs,
|
|
40
|
-
type PrepareResp
|
|
41
|
-
} from '../lease-client.js';
|
package/src/lease-client.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Totem <-> Axia hardened WOTS helpers (no deps on server internals).
|
|
3
|
-
*/
|
|
4
|
-
export type PrepareArgs = {
|
|
5
|
-
txId: string;
|
|
6
|
-
rootPublicKey: string;
|
|
7
|
-
to: string;
|
|
8
|
-
amount: string;
|
|
9
|
-
tokenId?: string;
|
|
10
|
-
burn?: string | null;
|
|
11
|
-
digestL2?: string | null;
|
|
12
|
-
digestL3?: string | null;
|
|
13
|
-
ttlMs?: number;
|
|
14
|
-
};
|
|
15
|
-
export type PrepareResp = {
|
|
16
|
-
leaseToken: string;
|
|
17
|
-
lease: {
|
|
18
|
-
addressIndex: number;
|
|
19
|
-
l1: number;
|
|
20
|
-
l2: number;
|
|
21
|
-
};
|
|
22
|
-
txId: string;
|
|
23
|
-
digestTx?: string | null;
|
|
24
|
-
};
|
|
25
|
-
export declare function prepareLease(apiUrl: string, apiKey: string, args: PrepareArgs): Promise<PrepareResp>;
|
|
26
|
-
export declare function finalizeLease(apiUrl: string, apiKey: string, leaseToken: string, signedHex: string): Promise<{
|
|
27
|
-
status: number;
|
|
28
|
-
body: string;
|
|
29
|
-
}>;
|
|
30
|
-
/** lane tuple -> flat WOTS index (64^3 space) */
|
|
31
|
-
export declare function flatIndexFromLanes(addressIndex: number, l1: number, l2: number): number;
|
package/src/lease-client.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.prepareLease = prepareLease;
|
|
4
|
-
exports.finalizeLease = finalizeLease;
|
|
5
|
-
exports.flatIndexFromLanes = flatIndexFromLanes;
|
|
6
|
-
async function prepareLease(apiUrl, apiKey, args) {
|
|
7
|
-
const res = await fetch(`${apiUrl}/v1/wots-hardened/prepare`, {
|
|
8
|
-
method: 'POST',
|
|
9
|
-
headers: { 'content-type': 'application/json', 'x-api-key': apiKey },
|
|
10
|
-
body: JSON.stringify({
|
|
11
|
-
tokenId: "0x00",
|
|
12
|
-
burn: null,
|
|
13
|
-
digestL2: null,
|
|
14
|
-
digestL3: null,
|
|
15
|
-
ttlMs: 20000,
|
|
16
|
-
...args
|
|
17
|
-
})
|
|
18
|
-
});
|
|
19
|
-
if (!res.ok)
|
|
20
|
-
throw new Error(`prepareLease ${res.status}`);
|
|
21
|
-
const raw = await res.json();
|
|
22
|
-
const lease = raw.lease ?? { addressIndex: raw.addressIndex, l1: raw.l1, l2: raw.l2 };
|
|
23
|
-
return {
|
|
24
|
-
leaseToken: raw.leaseToken,
|
|
25
|
-
lease,
|
|
26
|
-
txId: raw.txId,
|
|
27
|
-
digestTx: raw.digestTx ?? null
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
async function finalizeLease(apiUrl, apiKey, leaseToken, signedHex) {
|
|
31
|
-
const res = await fetch(`${apiUrl}/v1/wots-hardened/finalize`, {
|
|
32
|
-
method: 'POST',
|
|
33
|
-
headers: { 'content-type': 'application/json', 'x-api-key': apiKey },
|
|
34
|
-
body: JSON.stringify({ leaseToken, signedHex })
|
|
35
|
-
});
|
|
36
|
-
const text = await res.text();
|
|
37
|
-
return { status: res.status, body: text };
|
|
38
|
-
}
|
|
39
|
-
/** lane tuple -> flat WOTS index (64^3 space) */
|
|
40
|
-
function flatIndexFromLanes(addressIndex, l1, l2) {
|
|
41
|
-
if (addressIndex | l1 | l2 & ~63)
|
|
42
|
-
throw new Error('lane out of range');
|
|
43
|
-
return (addressIndex * 64 * 64) + (l1 * 64) + l2;
|
|
44
|
-
}
|