@totemsdk/core 1.2.5 → 1.2.7
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/dist/MiniNumber.js +19 -21
- package/dist/Streamable.js +31 -54
- package/dist/adapters/index.js +9 -18
- package/dist/adapters/types.js +11 -19
- package/dist/binio.js +7 -20
- package/dist/bip39.js +14 -24
- package/dist/canonical.d.ts +3 -0
- package/dist/canonical.js +22 -0
- package/dist/cjs/MiniNumber.d.ts +47 -0
- package/dist/cjs/MiniNumber.js +302 -0
- package/dist/cjs/Streamable.d.ts +268 -0
- package/dist/cjs/Streamable.js +439 -0
- package/dist/cjs/adapters/index.d.ts +190 -0
- package/dist/cjs/adapters/index.js +118 -0
- package/dist/cjs/adapters/types.d.ts +150 -0
- package/dist/cjs/adapters/types.js +81 -0
- package/dist/cjs/binio.d.ts +9 -0
- package/dist/cjs/binio.js +25 -0
- package/dist/cjs/bip39.d.ts +91 -0
- package/dist/cjs/bip39.js +434 -0
- package/dist/cjs/canonical.d.ts +3 -0
- package/dist/cjs/canonical.js +27 -0
- package/dist/cjs/derive.d.ts +2 -0
- package/dist/cjs/derive.js +13 -0
- package/dist/cjs/index.d.ts +49 -0
- package/dist/cjs/index.js +231 -0
- package/dist/cjs/javaStreamables.d.ts +254 -0
- package/dist/cjs/javaStreamables.js +391 -0
- package/dist/cjs/lease/LeaseMonitor.d.ts +44 -0
- package/dist/cjs/lease/LeaseMonitor.js +115 -0
- package/dist/cjs/lease/LeaseStore.d.ts +58 -0
- package/dist/cjs/lease/LeaseStore.js +151 -0
- package/dist/cjs/lease/WatermarkStore.d.ts +61 -0
- package/dist/cjs/lease/WatermarkStore.js +209 -0
- package/dist/cjs/lease/index.d.ts +14 -0
- package/dist/cjs/lease/index.js +22 -0
- package/dist/cjs/lease-client.d.ts +31 -0
- package/dist/cjs/lease-client.js +44 -0
- package/dist/cjs/legacy.d.ts +23 -0
- package/dist/cjs/legacy.js +163 -0
- package/dist/cjs/minima32.d.ts +18 -0
- package/dist/cjs/minima32.js +121 -0
- package/dist/cjs/minimaWireSerializer.d.ts +60 -0
- package/dist/cjs/minimaWireSerializer.js +77 -0
- package/dist/cjs/mmr.d.ts +157 -0
- package/dist/cjs/mmr.js +352 -0
- package/dist/cjs/mx.d.ts +1 -0
- package/dist/cjs/mx.js +10 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/params.d.ts +29 -0
- package/dist/cjs/params.js +20 -0
- package/dist/cjs/script.d.ts +7 -0
- package/dist/cjs/script.js +27 -0
- package/dist/cjs/scripts/contract-helpers.d.ts +252 -0
- package/dist/cjs/scripts/contract-helpers.js +407 -0
- package/dist/cjs/scripts/dapp-types.d.ts +119 -0
- package/dist/cjs/scripts/dapp-types.js +77 -0
- package/dist/cjs/scripts/index.d.ts +4 -0
- package/dist/cjs/scripts/index.js +38 -0
- package/dist/cjs/scripts/types.d.ts +81 -0
- package/dist/cjs/scripts/types.js +159 -0
- package/dist/cjs/scripts/witness-serializer.d.ts +29 -0
- package/dist/cjs/scripts/witness-serializer.js +306 -0
- package/dist/cjs/transaction.d.ts +106 -0
- package/dist/cjs/transaction.js +293 -0
- package/dist/cjs/treekey.d.ts +399 -0
- package/dist/cjs/treekey.js +827 -0
- package/dist/cjs/tx/TransactionLifecycle.d.ts +55 -0
- package/dist/cjs/tx/TransactionLifecycle.js +205 -0
- package/dist/cjs/tx/TransactionReceiptStore.d.ts +36 -0
- package/dist/cjs/tx/TransactionReceiptStore.js +93 -0
- package/dist/cjs/tx/TransactionService.d.ts +50 -0
- package/dist/cjs/tx/TransactionService.js +186 -0
- package/dist/cjs/tx/index.d.ts +13 -0
- package/dist/cjs/tx/index.js +20 -0
- package/dist/cjs/tx/types.d.ts +115 -0
- package/dist/cjs/tx/types.js +6 -0
- package/dist/cjs/util.d.ts +1 -0
- package/dist/cjs/util.js +5 -0
- package/dist/cjs/utils.d.ts +8 -0
- package/dist/cjs/utils.js +42 -0
- package/dist/cjs/verify.d.ts +18 -0
- package/dist/cjs/verify.js +174 -0
- package/dist/cjs/version.d.ts +12 -0
- package/dist/cjs/version.js +15 -0
- package/dist/cjs/wasm-sync.d.ts +71 -0
- package/dist/cjs/wasm-sync.js +97 -0
- package/dist/cjs/wots.d.ts +232 -0
- package/dist/cjs/wots.js +589 -0
- package/dist/derive.js +7 -11
- package/dist/index.d.ts +8 -2
- package/dist/index.js +50 -143
- package/dist/javaStreamables.js +34 -55
- package/dist/lease/LeaseMonitor.js +10 -10
- package/dist/lease/LeaseStore.js +8 -9
- package/dist/lease/WatermarkStore.js +8 -9
- package/dist/lease/index.js +4 -13
- package/dist/lease-client.js +3 -8
- package/dist/legacy.js +13 -140
- package/dist/minima32.js +13 -22
- package/dist/minimaWireSerializer.js +14 -20
- package/dist/mmr.js +27 -41
- package/dist/mx.js +1 -10
- package/dist/params.js +5 -9
- package/dist/script.js +7 -12
- package/dist/scripts/contract-helpers.js +25 -36
- package/dist/scripts/dapp-types.js +6 -11
- package/dist/scripts/index.js +4 -38
- package/dist/scripts/types.js +12 -26
- package/dist/scripts/witness-serializer.js +30 -48
- package/dist/transaction.js +46 -55
- package/dist/treekey.js +96 -89
- package/dist/tx/TransactionLifecycle.js +15 -11
- package/dist/tx/TransactionReceiptStore.js +9 -9
- package/dist/tx/TransactionService.js +14 -14
- package/dist/tx/index.js +3 -11
- package/dist/tx/types.js +1 -2
- package/dist/util.js +1 -5
- package/dist/utils.js +5 -12
- package/dist/verify.js +25 -36
- package/dist/version.js +2 -5
- package/dist/wasm-sync.d.ts +2 -2
- package/dist/wasm-sync.js +58 -61
- package/dist/wots.js +64 -94
- package/package.json +84 -45
- package/dist/wasm-bridge.d.ts +0 -54
- package/dist/wasm-bridge.js +0 -303
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Java-Compatible Serialization Helpers
|
|
4
|
+
*
|
|
5
|
+
* Matches Minima Java serialization exactly:
|
|
6
|
+
* - MiniNumber.java: writeDataStream / readDataStream
|
|
7
|
+
* - MiniData.java: writeDataStream / readDataStream
|
|
8
|
+
* - Crypto.java: hashAllObjects - serializes Streamables then SHA3-256
|
|
9
|
+
*
|
|
10
|
+
* These helpers are used for seed derivation to match TreeKeyNode.java:
|
|
11
|
+
* MiniData seed = Crypto.getInstance().hashAllObjects(new MiniNumber(i), zPrivateSeed);
|
|
12
|
+
*
|
|
13
|
+
* NOTE: This module provides number-based wrappers for backward compatibility.
|
|
14
|
+
* The canonical implementations are in Streamable.ts which uses bigint for precision.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.serializeMiniNumber = serializeMiniNumber;
|
|
18
|
+
exports.serializeMiniData = serializeMiniData;
|
|
19
|
+
exports.hashAllObjects = hashAllObjects;
|
|
20
|
+
exports.deriveChainSeedJava = deriveChainSeedJava;
|
|
21
|
+
exports.deriveChildTreeSeedJava = deriveChildTreeSeedJava;
|
|
22
|
+
exports.hashObject = hashObject;
|
|
23
|
+
exports.indexToMiniDataBytes = indexToMiniDataBytes;
|
|
24
|
+
exports.derivePerAddressSeed = derivePerAddressSeed;
|
|
25
|
+
exports.serializeMiniNumberZERO = serializeMiniNumberZERO;
|
|
26
|
+
exports.serializeMiniNumberONE = serializeMiniNumberONE;
|
|
27
|
+
exports.writeHashToStream = writeHashToStream;
|
|
28
|
+
exports.javaHashAllObjects = javaHashAllObjects;
|
|
29
|
+
exports.createMMREntryNumber = createMMREntryNumber;
|
|
30
|
+
exports.serializeMMREntryNumber = serializeMMREntryNumber;
|
|
31
|
+
exports.serializeMMRData = serializeMMRData;
|
|
32
|
+
exports.serializeMMREntry = serializeMMREntry;
|
|
33
|
+
exports.deriveRootPrivSeed = deriveRootPrivSeed;
|
|
34
|
+
exports.deriveUnifiedChildSeed = deriveUnifiedChildSeed;
|
|
35
|
+
exports.precomputeTransactionCoinID = precomputeTransactionCoinID;
|
|
36
|
+
const wasm_sync_js_1 = require("./wasm-sync.js");
|
|
37
|
+
const Streamable_js_1 = require("./Streamable.js");
|
|
38
|
+
/**
|
|
39
|
+
* Serialize a number in MiniNumber format (Java compatible)
|
|
40
|
+
*
|
|
41
|
+
* Thin wrapper over Streamable.writeMiniNumber that accepts number for backward compatibility.
|
|
42
|
+
*
|
|
43
|
+
* @param n - Non-negative integer to serialize
|
|
44
|
+
* @returns Serialized bytes matching Java MiniNumber format
|
|
45
|
+
*/
|
|
46
|
+
function serializeMiniNumber(n) {
|
|
47
|
+
if (n < 0)
|
|
48
|
+
throw new Error('MiniNumber must be non-negative');
|
|
49
|
+
return (0, Streamable_js_1.writeMiniNumber)(BigInt(n));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Serialize bytes in MiniData format (Java compatible)
|
|
53
|
+
*
|
|
54
|
+
* Re-export of Streamable.writeMiniData for backward compatibility.
|
|
55
|
+
*
|
|
56
|
+
* @param data - Bytes to serialize
|
|
57
|
+
* @returns Serialized bytes matching Java MiniData format
|
|
58
|
+
*/
|
|
59
|
+
function serializeMiniData(data) {
|
|
60
|
+
return (0, Streamable_js_1.writeMiniData)(data);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Hash multiple Streamable objects (Java compatible)
|
|
64
|
+
*
|
|
65
|
+
* From Crypto.java hashAllObjects:
|
|
66
|
+
* 1. Write each object to DataOutputStream
|
|
67
|
+
* 2. SHA3-256 hash the combined bytes
|
|
68
|
+
*
|
|
69
|
+
* This matches TreeKeyNode.java seed derivation:
|
|
70
|
+
* Crypto.getInstance().hashAllObjects(new MiniNumber(i), zPrivateSeed)
|
|
71
|
+
*
|
|
72
|
+
* @param items - Array of serialized objects (use serializeMiniNumber/serializeMiniData)
|
|
73
|
+
* @returns 32-byte SHA3-256 hash
|
|
74
|
+
*/
|
|
75
|
+
function hashAllObjects(...items) {
|
|
76
|
+
// Concatenate all serialized items
|
|
77
|
+
const totalLength = items.reduce((sum, item) => sum + item.length, 0);
|
|
78
|
+
const combined = new Uint8Array(totalLength);
|
|
79
|
+
let offset = 0;
|
|
80
|
+
for (const item of items) {
|
|
81
|
+
combined.set(item, offset);
|
|
82
|
+
offset += item.length;
|
|
83
|
+
}
|
|
84
|
+
// SHA3-256 hash
|
|
85
|
+
return (0, wasm_sync_js_1.sha3_256)(combined);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Derive per-chain seed matching Java TreeKeyNode.java exactly
|
|
89
|
+
*
|
|
90
|
+
* From TreeKeyNode.java constructor (line 43-44):
|
|
91
|
+
* MiniData seed = Crypto.getInstance().hashAllObjects(new MiniNumber(i), zPrivateSeed);
|
|
92
|
+
*
|
|
93
|
+
* @param privateSeed - 32-byte private seed
|
|
94
|
+
* @param index - Chain index (0-63)
|
|
95
|
+
* @returns 32-byte derived seed for this chain
|
|
96
|
+
*/
|
|
97
|
+
function deriveChainSeedJava(privateSeed, index) {
|
|
98
|
+
const indexSerialized = serializeMiniNumber(index);
|
|
99
|
+
const seedSerialized = serializeMiniData(privateSeed);
|
|
100
|
+
return hashAllObjects(indexSerialized, seedSerialized);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Derive child tree seed matching Java TreeKeyNode.java exactly
|
|
104
|
+
*
|
|
105
|
+
* From TreeKeyNode.java getChild (line 68):
|
|
106
|
+
* MiniData seed = Crypto.getInstance().hashAllObjects(new MiniNumber(zChild), mChildSeed);
|
|
107
|
+
*
|
|
108
|
+
* The child seed is derived from parent's private seed:
|
|
109
|
+
* mChildSeed = Crypto.getInstance().hashObject(zPrivateSeed); // line 30
|
|
110
|
+
*
|
|
111
|
+
* @param childSeed - 32-byte child seed (hash of parent's private seed)
|
|
112
|
+
* @param childIndex - Child index (0-63)
|
|
113
|
+
* @returns 32-byte derived seed for child tree
|
|
114
|
+
*/
|
|
115
|
+
function deriveChildTreeSeedJava(childSeed, childIndex) {
|
|
116
|
+
const indexSerialized = serializeMiniNumber(childIndex);
|
|
117
|
+
const seedSerialized = serializeMiniData(childSeed);
|
|
118
|
+
return hashAllObjects(indexSerialized, seedSerialized);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Hash a single object matching Java's Crypto.hashObject()
|
|
122
|
+
*
|
|
123
|
+
* From TreeKeyNode.java line 30:
|
|
124
|
+
* mChildSeed = Crypto.getInstance().hashObject(zPrivateSeed);
|
|
125
|
+
*
|
|
126
|
+
* This serializes the object as MiniData (length-prefixed) and hashes it.
|
|
127
|
+
*
|
|
128
|
+
* @param data - Raw bytes (will be serialized as MiniData)
|
|
129
|
+
* @returns 32-byte SHA3-256 hash
|
|
130
|
+
*/
|
|
131
|
+
function hashObject(data) {
|
|
132
|
+
const serialized = serializeMiniData(data);
|
|
133
|
+
return (0, wasm_sync_js_1.sha3_256)(serialized);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Convert index to MiniData bytes matching Java's:
|
|
137
|
+
* new MiniData(new BigInteger(Integer.toString(index)))
|
|
138
|
+
*
|
|
139
|
+
* BigInteger uses minimum byte representation (no leading zeros).
|
|
140
|
+
* This is used for per-address key derivation in Wallet.java.
|
|
141
|
+
*
|
|
142
|
+
* @param index - Non-negative integer (0, 1, 2, ...)
|
|
143
|
+
* @returns Minimal byte representation of the index
|
|
144
|
+
*/
|
|
145
|
+
function indexToMiniDataBytes(index) {
|
|
146
|
+
if (index === 0) {
|
|
147
|
+
return new Uint8Array([0x00]);
|
|
148
|
+
}
|
|
149
|
+
// Convert to minimum byte representation (big-endian, no leading zeros)
|
|
150
|
+
const bytes = [];
|
|
151
|
+
let n = index;
|
|
152
|
+
while (n > 0) {
|
|
153
|
+
bytes.unshift(n & 0xff);
|
|
154
|
+
n = n >> 8;
|
|
155
|
+
}
|
|
156
|
+
return new Uint8Array(bytes);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Derive per-address private seed matching Minima Wallet.java exactly
|
|
160
|
+
*
|
|
161
|
+
* From Wallet.java createNewKey() (lines 498-503):
|
|
162
|
+
* MiniData modifier = new MiniData(new BigInteger(Integer.toString(numkeys)));
|
|
163
|
+
* MiniData privseed = Crypto.getInstance().hashObjects(new MiniData(mBaseSeed.getSeed()), modifier);
|
|
164
|
+
*
|
|
165
|
+
* CRITICAL: This is DIFFERENT from deriveChainSeedJava!
|
|
166
|
+
* - deriveChainSeedJava: hashAllObjects(MiniNumber(index), MiniData(seed)) - index first as MiniNumber
|
|
167
|
+
* - This function: hashObjects(MiniData(baseSeed), MiniData(modifier)) - baseSeed first, both as MiniData
|
|
168
|
+
*
|
|
169
|
+
* @param baseSeed - 32-byte base wallet seed (from mnemonic)
|
|
170
|
+
* @param addressIndex - Address index (0, 1, 2, ... 63)
|
|
171
|
+
* @returns 32-byte private seed for this address's TreeKey
|
|
172
|
+
*/
|
|
173
|
+
function derivePerAddressSeed(baseSeed, addressIndex) {
|
|
174
|
+
// Step 1: Create modifier bytes from BigInteger representation of index
|
|
175
|
+
const modifierBytes = indexToMiniDataBytes(addressIndex);
|
|
176
|
+
// Step 2: Serialize both as MiniData (4-byte length prefix each)
|
|
177
|
+
const baseSeedSerialized = serializeMiniData(baseSeed);
|
|
178
|
+
const modifierSerialized = serializeMiniData(modifierBytes);
|
|
179
|
+
// Step 3: Concatenate and hash (baseSeed FIRST, then modifier)
|
|
180
|
+
const combined = new Uint8Array(baseSeedSerialized.length + modifierSerialized.length);
|
|
181
|
+
combined.set(baseSeedSerialized, 0);
|
|
182
|
+
combined.set(modifierSerialized, baseSeedSerialized.length);
|
|
183
|
+
const result = (0, wasm_sync_js_1.sha3_256)(combined);
|
|
184
|
+
// DIAGNOSTIC: Log first 8 bytes to detect bundle duplication or implementation differences
|
|
185
|
+
// Enable via: globalThis.TOTEM_SEED_DEBUG = true
|
|
186
|
+
if (typeof console !== 'undefined' && globalThis.TOTEM_SEED_DEBUG) {
|
|
187
|
+
const toHex = (b) => Array.from(b.slice(0, 8)).map(x => x.toString(16).padStart(2, '0')).join('');
|
|
188
|
+
console.log(`[derivePerAddressSeed] idx=${addressIndex} baseSeed[0:8]=${toHex(baseSeed)} -> result[0:8]=${toHex(result)}`);
|
|
189
|
+
}
|
|
190
|
+
return result;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Serialize MiniNumber.ZERO - cached for performance
|
|
194
|
+
* Returns: [0x00, 0x01, 0x00] = scale(0) + length(1) + value(0)
|
|
195
|
+
*/
|
|
196
|
+
function serializeMiniNumberZERO() {
|
|
197
|
+
return new Uint8Array([0x00, 0x01, 0x00]);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Serialize MiniNumber.ONE
|
|
201
|
+
* Returns: [0x00, 0x01, 0x01] = scale(0) + length(1) + value(1)
|
|
202
|
+
*/
|
|
203
|
+
function serializeMiniNumberONE() {
|
|
204
|
+
return new Uint8Array([0x00, 0x01, 0x01]);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Serialize hash in MiniData.writeHashToStream format (4-byte length prefix)
|
|
208
|
+
*
|
|
209
|
+
* Re-export of Streamable.writeHashToStream for backward compatibility.
|
|
210
|
+
*
|
|
211
|
+
* @param data - Hash bytes (max 64 bytes per MINIMA_MAX_HASH_LENGTH)
|
|
212
|
+
* @returns Serialized bytes with 4-byte length prefix
|
|
213
|
+
*/
|
|
214
|
+
function writeHashToStream(data) {
|
|
215
|
+
return (0, Streamable_js_1.writeHashToStream)(data);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Java hashAllObjects for MMRData hashing
|
|
219
|
+
* Used for MMRData.CreateMMRDataLeafNode and CreateMMRDataParentNode
|
|
220
|
+
*
|
|
221
|
+
* From Crypto.java hashAllObjects:
|
|
222
|
+
* Serializes each Streamable object and hashes the concatenation.
|
|
223
|
+
*
|
|
224
|
+
* For MMRData, the serialization is:
|
|
225
|
+
* - MiniNumber: [scale][len][data] (see serializeMiniNumber)
|
|
226
|
+
* - MiniData: [4-byte len][data] for writeDataStream
|
|
227
|
+
* - Hash: [4-byte len][data] for writeHashToStream (same as MiniData)
|
|
228
|
+
*
|
|
229
|
+
* CRITICAL: Java writeHashToStream uses writeInt (4-byte prefix),
|
|
230
|
+
* identical to writeDataStream. See MiniData.java lines 282-289.
|
|
231
|
+
*
|
|
232
|
+
* @param items - Pre-serialized items to concatenate and hash
|
|
233
|
+
* @returns 32-byte SHA3-256 hash
|
|
234
|
+
*/
|
|
235
|
+
function javaHashAllObjects(...items) {
|
|
236
|
+
const totalLength = items.reduce((sum, item) => sum + item.length, 0);
|
|
237
|
+
const combined = new Uint8Array(totalLength);
|
|
238
|
+
let offset = 0;
|
|
239
|
+
for (const item of items) {
|
|
240
|
+
combined.set(item, offset);
|
|
241
|
+
offset += item.length;
|
|
242
|
+
}
|
|
243
|
+
return (0, wasm_sync_js_1.sha3_256)(combined);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Create MMREntryNumber from bigint (common case for integer positions)
|
|
247
|
+
*/
|
|
248
|
+
function createMMREntryNumber(value) {
|
|
249
|
+
return { scale: 0, unscaled: value };
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Serialize MMREntryNumber matching Java MMREntryNumber.writeDataStream()
|
|
253
|
+
*
|
|
254
|
+
* From MMREntryNumber.java:
|
|
255
|
+
* MiniNumber.WriteToStream(zOut, mNumber.scale());
|
|
256
|
+
* MiniData.WriteToStream(zOut, mNumber.unscaledValue().toByteArray());
|
|
257
|
+
*
|
|
258
|
+
* @param entry - MMREntryNumber to serialize
|
|
259
|
+
* @returns Serialized bytes: MiniNumber(scale) + MiniData(unscaled bytes)
|
|
260
|
+
*/
|
|
261
|
+
function serializeMMREntryNumber(entry) {
|
|
262
|
+
const scaleSerialized = serializeMiniNumber(entry.scale);
|
|
263
|
+
const unscaledBytes = bigintToSignedBytes(entry.unscaled);
|
|
264
|
+
const unscaledSerialized = serializeMiniData(unscaledBytes);
|
|
265
|
+
const result = new Uint8Array(scaleSerialized.length + unscaledSerialized.length);
|
|
266
|
+
result.set(scaleSerialized, 0);
|
|
267
|
+
result.set(unscaledSerialized, scaleSerialized.length);
|
|
268
|
+
return result;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Convert bigint to Java BigInteger.toByteArray() format (big-endian, signed two's complement)
|
|
272
|
+
* Uses the canonical implementation from Streamable.ts
|
|
273
|
+
*/
|
|
274
|
+
function bigintToSignedBytes(n) {
|
|
275
|
+
return (0, Streamable_js_1.bigIntToByteArray)(n);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Serialize MMRData matching Java MMRData.writeDataStream()
|
|
279
|
+
*
|
|
280
|
+
* From MMRData.java:
|
|
281
|
+
* mData.writeHashToStream(zOut); // 4-byte length prefix + hash bytes
|
|
282
|
+
* mValue.writeDataStream(zOut); // MiniNumber format
|
|
283
|
+
*
|
|
284
|
+
* @param mmrData - MMRData to serialize
|
|
285
|
+
* @returns Serialized bytes: writeHashToStream(hash) + MiniNumber(value)
|
|
286
|
+
*/
|
|
287
|
+
function serializeMMRData(mmrData) {
|
|
288
|
+
const hashSerialized = writeHashToStream(mmrData.data);
|
|
289
|
+
const valueSerialized = serializeMiniNumber(Number(mmrData.value));
|
|
290
|
+
const result = new Uint8Array(hashSerialized.length + valueSerialized.length);
|
|
291
|
+
result.set(hashSerialized, 0);
|
|
292
|
+
result.set(valueSerialized, hashSerialized.length);
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Serialize MMREntry matching Java MMREntry.writeDataStream()
|
|
297
|
+
*
|
|
298
|
+
* From MMREntry.java:
|
|
299
|
+
* MiniNumber row = new MiniNumber(mRow);
|
|
300
|
+
* row.writeDataStream(zOut);
|
|
301
|
+
* mEntryNumber.writeDataStream(zOut);
|
|
302
|
+
* mMMRData.writeDataStream(zOut);
|
|
303
|
+
*
|
|
304
|
+
* @param entry - MMREntry to serialize
|
|
305
|
+
* @returns Serialized bytes: MiniNumber(row) + MMREntryNumber + MMRData
|
|
306
|
+
*/
|
|
307
|
+
function serializeMMREntry(entry) {
|
|
308
|
+
const rowSerialized = serializeMiniNumber(entry.row);
|
|
309
|
+
const entryNumberSerialized = serializeMMREntryNumber(entry.entryNumber);
|
|
310
|
+
const mmrDataSerialized = serializeMMRData(entry.mmrData);
|
|
311
|
+
const totalLength = rowSerialized.length + entryNumberSerialized.length + mmrDataSerialized.length;
|
|
312
|
+
const result = new Uint8Array(totalLength);
|
|
313
|
+
let offset = 0;
|
|
314
|
+
result.set(rowSerialized, offset);
|
|
315
|
+
offset += rowSerialized.length;
|
|
316
|
+
result.set(entryNumberSerialized, offset);
|
|
317
|
+
offset += entryNumberSerialized.length;
|
|
318
|
+
result.set(mmrDataSerialized, offset);
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Derive the unified root private seed from a base wallet seed.
|
|
323
|
+
*
|
|
324
|
+
* Architecture:
|
|
325
|
+
* root_priv_seed = SHA3-256( serializeMiniData(baseSeed) ‖ serializeMiniData("ROOT_IDENTITY" bytes) )
|
|
326
|
+
*
|
|
327
|
+
* @param baseSeed - 32-byte wallet base seed (from mnemonic)
|
|
328
|
+
* @returns 32-byte root private seed
|
|
329
|
+
*/
|
|
330
|
+
function deriveRootPrivSeed(baseSeed) {
|
|
331
|
+
const baseSeedSerialized = serializeMiniData(baseSeed);
|
|
332
|
+
const rootIdentityBytes = new TextEncoder().encode('ROOT_IDENTITY');
|
|
333
|
+
const rootIdentitySerialized = serializeMiniData(rootIdentityBytes);
|
|
334
|
+
const combined = new Uint8Array(baseSeedSerialized.length + rootIdentitySerialized.length);
|
|
335
|
+
combined.set(baseSeedSerialized, 0);
|
|
336
|
+
combined.set(rootIdentitySerialized, baseSeedSerialized.length);
|
|
337
|
+
return (0, wasm_sync_js_1.sha3_256)(combined);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Derive a unified child seed for the address at `index`.
|
|
341
|
+
*
|
|
342
|
+
* Architecture:
|
|
343
|
+
* child_seed_i = SHA3-256( serializeMiniData(root_priv_seed) ‖ serializeMiniData(indexBytes(i)) )
|
|
344
|
+
*
|
|
345
|
+
* @param baseSeed - 32-byte wallet base seed (from mnemonic)
|
|
346
|
+
* @param index - Address index (0-63)
|
|
347
|
+
* @returns 32-byte child seed for the TreeKey at this address
|
|
348
|
+
*/
|
|
349
|
+
function deriveUnifiedChildSeed(baseSeed, index) {
|
|
350
|
+
const rootPrivSeed = deriveRootPrivSeed(baseSeed);
|
|
351
|
+
const modifierBytes = indexToMiniDataBytes(index);
|
|
352
|
+
const rootSeedSerialized = serializeMiniData(rootPrivSeed);
|
|
353
|
+
const modifierSerialized = serializeMiniData(modifierBytes);
|
|
354
|
+
const combined = new Uint8Array(rootSeedSerialized.length + modifierSerialized.length);
|
|
355
|
+
combined.set(rootSeedSerialized, 0);
|
|
356
|
+
combined.set(modifierSerialized, rootSeedSerialized.length);
|
|
357
|
+
return (0, wasm_sync_js_1.sha3_256)(combined);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Precompute output coin IDs before computing the transaction digest.
|
|
361
|
+
*
|
|
362
|
+
* CRITICAL: Java's txnsign command calls TxPoWGenerator.precomputeTransactionCoinID(txn)
|
|
363
|
+
* BEFORE calculateTransactionID(). This means the transaction digest that gets signed
|
|
364
|
+
* includes the PRECOMPUTED output coin IDs, not the placeholder 0x00.
|
|
365
|
+
*
|
|
366
|
+
* Formula: outputCoinID = SHA3-256( writeMiniData(input[0].coinID) || writeMiniNumber(outputIndex) )
|
|
367
|
+
* This matches Java's Crypto.hashObjects(baseCoinID, new MiniNumber(outputNum))
|
|
368
|
+
*
|
|
369
|
+
* Without this, the wallet signs a different digest than what the node verifies against,
|
|
370
|
+
* causing allsignaturesvalid=false on every transaction.
|
|
371
|
+
*
|
|
372
|
+
* @param inputs - Transaction inputs array, each must have a coinId (Uint8Array)
|
|
373
|
+
* @param outputs - Transaction outputs array, each will have its coinId mutated in-place
|
|
374
|
+
*/
|
|
375
|
+
function precomputeTransactionCoinID(inputs, outputs) {
|
|
376
|
+
if (inputs.length === 0)
|
|
377
|
+
return;
|
|
378
|
+
const baseCoinId = inputs[0].coinId;
|
|
379
|
+
if (baseCoinId.length < 2) {
|
|
380
|
+
throw new Error(`Cannot precompute output coinIDs: input[0].coinId is ${baseCoinId.length} byte(s) ` +
|
|
381
|
+
`(placeholder?). Real coinId from CoinProof required for signing.`);
|
|
382
|
+
}
|
|
383
|
+
for (let i = 0; i < outputs.length; i++) {
|
|
384
|
+
const baseCoinIdStream = (0, Streamable_js_1.writeMiniData)(baseCoinId);
|
|
385
|
+
const outputIndexStream = (0, Streamable_js_1.writeMiniNumber)(BigInt(i));
|
|
386
|
+
const combined = new Uint8Array(baseCoinIdStream.length + outputIndexStream.length);
|
|
387
|
+
combined.set(baseCoinIdStream, 0);
|
|
388
|
+
combined.set(outputIndexStream, baseCoinIdStream.length);
|
|
389
|
+
outputs[i].coinId = (0, wasm_sync_js_1.sha3_256)(combined);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module LeaseMonitor
|
|
3
|
+
* Monitors lease expiry and triggers renewal/cleanup actions
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Automatic expiry detection with configurable thresholds
|
|
7
|
+
* - Event-based notifications for expiring leases
|
|
8
|
+
* - Adaptive monitoring interval based on shortest TTL
|
|
9
|
+
*/
|
|
10
|
+
import type { TimerAdapter, LoggerAdapter } from '../adapters/index.js';
|
|
11
|
+
import type { LeaseStore, StoredLease } from './LeaseStore.js';
|
|
12
|
+
export interface LeaseExpiryEvent {
|
|
13
|
+
leaseId: string;
|
|
14
|
+
expiresAt: number;
|
|
15
|
+
remainingMs: number;
|
|
16
|
+
lease: StoredLease;
|
|
17
|
+
}
|
|
18
|
+
export type LeaseExpiryCallback = (event: LeaseExpiryEvent) => void;
|
|
19
|
+
export interface LeaseMonitorConfig {
|
|
20
|
+
defaultIntervalMs?: number;
|
|
21
|
+
expiryThresholdMs?: number;
|
|
22
|
+
minIntervalMs?: number;
|
|
23
|
+
maxIntervalMs?: number;
|
|
24
|
+
}
|
|
25
|
+
export declare class LeaseMonitor {
|
|
26
|
+
private readonly leaseStore;
|
|
27
|
+
private readonly timer;
|
|
28
|
+
private readonly logger;
|
|
29
|
+
private readonly config;
|
|
30
|
+
private timerHandle;
|
|
31
|
+
private isRunning;
|
|
32
|
+
private expiryCallbacks;
|
|
33
|
+
constructor(leaseStore: LeaseStore, timer?: TimerAdapter, logger?: LoggerAdapter, config?: LeaseMonitorConfig);
|
|
34
|
+
start(): void;
|
|
35
|
+
stop(): void;
|
|
36
|
+
isActive(): boolean;
|
|
37
|
+
onExpirySoon(callback: LeaseExpiryCallback): () => void;
|
|
38
|
+
removeAllListeners(): void;
|
|
39
|
+
checkNow(): Promise<LeaseExpiryEvent[]>;
|
|
40
|
+
private scheduleNextCheck;
|
|
41
|
+
private runCheck;
|
|
42
|
+
private calculateInterval;
|
|
43
|
+
private notifyExpirySoon;
|
|
44
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module LeaseMonitor
|
|
4
|
+
* Monitors lease expiry and triggers renewal/cleanup actions
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Automatic expiry detection with configurable thresholds
|
|
8
|
+
* - Event-based notifications for expiring leases
|
|
9
|
+
* - Adaptive monitoring interval based on shortest TTL
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.LeaseMonitor = void 0;
|
|
13
|
+
const index_js_1 = require("../adapters/index.js");
|
|
14
|
+
const DEFAULT_CONFIG = {
|
|
15
|
+
defaultIntervalMs: 5000,
|
|
16
|
+
expiryThresholdMs: 5000,
|
|
17
|
+
minIntervalMs: 1000,
|
|
18
|
+
maxIntervalMs: 30000,
|
|
19
|
+
};
|
|
20
|
+
class LeaseMonitor {
|
|
21
|
+
leaseStore;
|
|
22
|
+
timer;
|
|
23
|
+
logger;
|
|
24
|
+
config;
|
|
25
|
+
timerHandle = null;
|
|
26
|
+
isRunning = false;
|
|
27
|
+
expiryCallbacks = new Set();
|
|
28
|
+
constructor(leaseStore, timer = new index_js_1.DefaultTimerAdapter(), logger = new index_js_1.NoopLogger(), config = {}) {
|
|
29
|
+
this.leaseStore = leaseStore;
|
|
30
|
+
this.timer = timer;
|
|
31
|
+
this.logger = logger;
|
|
32
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
33
|
+
}
|
|
34
|
+
start() {
|
|
35
|
+
if (this.isRunning) {
|
|
36
|
+
this.logger.debug('LeaseMonitor already running');
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
this.isRunning = true;
|
|
40
|
+
this.scheduleNextCheck();
|
|
41
|
+
this.logger.debug('LeaseMonitor started');
|
|
42
|
+
}
|
|
43
|
+
stop() {
|
|
44
|
+
if (!this.isRunning)
|
|
45
|
+
return;
|
|
46
|
+
this.isRunning = false;
|
|
47
|
+
if (this.timerHandle !== null) {
|
|
48
|
+
this.timer.clearTimeout(this.timerHandle);
|
|
49
|
+
this.timerHandle = null;
|
|
50
|
+
}
|
|
51
|
+
this.logger.debug('LeaseMonitor stopped');
|
|
52
|
+
}
|
|
53
|
+
isActive() {
|
|
54
|
+
return this.isRunning;
|
|
55
|
+
}
|
|
56
|
+
onExpirySoon(callback) {
|
|
57
|
+
this.expiryCallbacks.add(callback);
|
|
58
|
+
return () => this.expiryCallbacks.delete(callback);
|
|
59
|
+
}
|
|
60
|
+
removeAllListeners() {
|
|
61
|
+
this.expiryCallbacks.clear();
|
|
62
|
+
}
|
|
63
|
+
async checkNow() {
|
|
64
|
+
const now = this.timer.now();
|
|
65
|
+
const expiringSoon = this.leaseStore.getExpiringSoon(this.config.expiryThresholdMs);
|
|
66
|
+
const events = expiringSoon.map(lease => ({
|
|
67
|
+
leaseId: lease.leaseId,
|
|
68
|
+
expiresAt: lease.expiresAt,
|
|
69
|
+
remainingMs: Math.max(0, lease.expiresAt - now),
|
|
70
|
+
lease,
|
|
71
|
+
}));
|
|
72
|
+
for (const event of events) {
|
|
73
|
+
this.notifyExpirySoon(event);
|
|
74
|
+
}
|
|
75
|
+
await this.leaseStore.cleanupExpired();
|
|
76
|
+
return events;
|
|
77
|
+
}
|
|
78
|
+
scheduleNextCheck() {
|
|
79
|
+
if (!this.isRunning)
|
|
80
|
+
return;
|
|
81
|
+
const interval = this.calculateInterval();
|
|
82
|
+
this.timerHandle = this.timer.setTimeout(() => {
|
|
83
|
+
this.runCheck();
|
|
84
|
+
}, interval);
|
|
85
|
+
this.logger.debug(`Next lease check scheduled in ${interval}ms`);
|
|
86
|
+
}
|
|
87
|
+
async runCheck() {
|
|
88
|
+
if (!this.isRunning)
|
|
89
|
+
return;
|
|
90
|
+
try {
|
|
91
|
+
await this.checkNow();
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
this.logger.error('Error during lease check:', error);
|
|
95
|
+
}
|
|
96
|
+
this.scheduleNextCheck();
|
|
97
|
+
}
|
|
98
|
+
calculateInterval() {
|
|
99
|
+
const storeInterval = this.leaseStore.calculateMonitoringInterval();
|
|
100
|
+
const interval = Math.max(this.config.minIntervalMs, Math.min(storeInterval, this.config.maxIntervalMs));
|
|
101
|
+
return interval;
|
|
102
|
+
}
|
|
103
|
+
notifyExpirySoon(event) {
|
|
104
|
+
this.logger.debug(`Lease expiring soon: ${event.leaseId}, remaining: ${event.remainingMs}ms`);
|
|
105
|
+
this.expiryCallbacks.forEach(callback => {
|
|
106
|
+
try {
|
|
107
|
+
callback(event);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
this.logger.error('Error in expiry callback:', error);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.LeaseMonitor = LeaseMonitor;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module LeaseStore
|
|
3
|
+
* WOTS lease lifecycle management with platform-agnostic storage
|
|
4
|
+
*
|
|
5
|
+
* Uses in-memory cache backed by persistent storage.
|
|
6
|
+
* Pattern:
|
|
7
|
+
* - initialize() loads from storage into memory cache
|
|
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
|
+
import type { StorageAdapter, LoggerAdapter } from '../adapters/index.js';
|
|
13
|
+
export interface WotsIndices {
|
|
14
|
+
addressIndex: number;
|
|
15
|
+
l1: number;
|
|
16
|
+
l2: number;
|
|
17
|
+
}
|
|
18
|
+
export type LeaseStatus = 'pending' | 'active' | 'expired' | 'finalized' | 'cancelled';
|
|
19
|
+
export interface StoredLease {
|
|
20
|
+
leaseId: string;
|
|
21
|
+
leaseToken: string;
|
|
22
|
+
treeId?: string;
|
|
23
|
+
indices: WotsIndices;
|
|
24
|
+
expiresAt: number;
|
|
25
|
+
status: LeaseStatus;
|
|
26
|
+
createdAt: number;
|
|
27
|
+
txId?: string;
|
|
28
|
+
leaseTTL: number;
|
|
29
|
+
}
|
|
30
|
+
export interface LeaseStoreConfig {
|
|
31
|
+
storageKey?: string;
|
|
32
|
+
}
|
|
33
|
+
export declare class LeaseStore {
|
|
34
|
+
private leases;
|
|
35
|
+
private _initialized;
|
|
36
|
+
private readonly storageKey;
|
|
37
|
+
private readonly storage;
|
|
38
|
+
private readonly logger;
|
|
39
|
+
constructor(storage: StorageAdapter, logger?: LoggerAdapter, config?: LeaseStoreConfig);
|
|
40
|
+
initialize(): Promise<void>;
|
|
41
|
+
isInitialized(): boolean;
|
|
42
|
+
private load;
|
|
43
|
+
private persist;
|
|
44
|
+
save(lease: StoredLease): Promise<void>;
|
|
45
|
+
get(leaseId: string): StoredLease | undefined;
|
|
46
|
+
getByToken(leaseToken: string): StoredLease | undefined;
|
|
47
|
+
getAll(): StoredLease[];
|
|
48
|
+
getActive(): StoredLease[];
|
|
49
|
+
getExpiringSoon(thresholdMs?: number): StoredLease[];
|
|
50
|
+
delete(leaseId: string): Promise<boolean>;
|
|
51
|
+
deleteByToken(leaseToken: string): Promise<boolean>;
|
|
52
|
+
updateStatus(leaseId: string, status: LeaseStatus): Promise<void>;
|
|
53
|
+
cleanupExpired(): Promise<number>;
|
|
54
|
+
clear(): Promise<void>;
|
|
55
|
+
getMinimumTTL(): number | null;
|
|
56
|
+
calculateMonitoringInterval(): number;
|
|
57
|
+
private flattenIndex;
|
|
58
|
+
}
|