@totemsdk/core 1.2.4 → 1.2.6
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/README.md +1 -1
- package/dist/MiniNumber.d.ts +47 -0
- package/dist/MiniNumber.js +298 -0
- 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/derive.js +7 -11
- package/dist/index.d.ts +2 -1
- package/dist/index.js +28 -141
- 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 +26 -40
- 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 +82 -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.js +56 -61
- package/dist/wots.d.ts +2 -2
- package/dist/wots.js +65 -95
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**The cryptographic engine — every other package depends on this.**
|
|
4
4
|
|
|
5
|
-
Zero production dependencies (only `@noble/hashes` as a peer). Provides
|
|
5
|
+
Zero production dependencies (only `@noble/hashes` as a peer). Provides WOTS (Winternitz) signatures, hierarchical TreeKey address derivation, BIP39 seed phrases, Merkle Mountain Range proofs, and byte-exact Minima Java-compatible transaction serialization.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare class MiniNumber {
|
|
2
|
+
readonly unscaled: bigint;
|
|
3
|
+
readonly scale: number;
|
|
4
|
+
static readonly ZERO: MiniNumber;
|
|
5
|
+
static readonly ONE: MiniNumber;
|
|
6
|
+
static readonly TWO: MiniNumber;
|
|
7
|
+
static readonly THREE: MiniNumber;
|
|
8
|
+
static readonly FOUR: MiniNumber;
|
|
9
|
+
static readonly EIGHT: MiniNumber;
|
|
10
|
+
static readonly TWELVE: MiniNumber;
|
|
11
|
+
static readonly SIXTEEN: MiniNumber;
|
|
12
|
+
static readonly TWENTY: MiniNumber;
|
|
13
|
+
static readonly THIRTYTWO: MiniNumber;
|
|
14
|
+
static readonly FIFTY: MiniNumber;
|
|
15
|
+
static readonly SIXTYFOUR: MiniNumber;
|
|
16
|
+
static readonly TWOFIVESIX: MiniNumber;
|
|
17
|
+
static readonly FIVEONE12: MiniNumber;
|
|
18
|
+
static readonly THOUSAND24: MiniNumber;
|
|
19
|
+
static readonly MINUSONE: MiniNumber;
|
|
20
|
+
constructor(value: string | number | bigint | MiniNumber);
|
|
21
|
+
getAsBigDecimal(): string;
|
|
22
|
+
getAsBigInteger(): string;
|
|
23
|
+
toNumber(): number;
|
|
24
|
+
toString(): string;
|
|
25
|
+
private assertSameScale;
|
|
26
|
+
add(other: MiniNumber): MiniNumber;
|
|
27
|
+
sub(other: MiniNumber): MiniNumber;
|
|
28
|
+
mult(other: MiniNumber): MiniNumber;
|
|
29
|
+
div(other: MiniNumber): MiniNumber;
|
|
30
|
+
modulo(other: MiniNumber): MiniNumber;
|
|
31
|
+
pow(n: number): MiniNumber;
|
|
32
|
+
sqrt(): MiniNumber;
|
|
33
|
+
floor(): MiniNumber;
|
|
34
|
+
ceil(): MiniNumber;
|
|
35
|
+
abs(): MiniNumber;
|
|
36
|
+
negate(): MiniNumber;
|
|
37
|
+
increment(): MiniNumber;
|
|
38
|
+
decrement(): MiniNumber;
|
|
39
|
+
compareTo(other: MiniNumber): number;
|
|
40
|
+
isEqual(other: MiniNumber): boolean;
|
|
41
|
+
isLess(other: MiniNumber): boolean;
|
|
42
|
+
isLessEqual(other: MiniNumber): boolean;
|
|
43
|
+
isMore(other: MiniNumber): boolean;
|
|
44
|
+
isMoreEqual(other: MiniNumber): boolean;
|
|
45
|
+
setSignificantDigits(d: number): MiniNumber;
|
|
46
|
+
decimalPlaces(): number;
|
|
47
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// Matching Java's `org.minima.objects.base.MiniNumber` backed by `BigDecimal`
|
|
2
|
+
// with MathContext(64, RoundingMode.DOWN).
|
|
3
|
+
const MAX_DIGITS = 64;
|
|
4
|
+
const MAX_DECIMAL_PLACES = MAX_DIGITS - 20;
|
|
5
|
+
const MAX_VALUE_BIG = (1n << 64n) - 1n; // 2^64 - 1
|
|
6
|
+
function checkLimits(unscaled, scale) {
|
|
7
|
+
if (scale > MAX_DECIMAL_PLACES) {
|
|
8
|
+
const diff = scale - MAX_DECIMAL_PLACES;
|
|
9
|
+
const divisor = 10n ** BigInt(diff);
|
|
10
|
+
unscaled = unscaled / divisor;
|
|
11
|
+
scale = MAX_DECIMAL_PLACES;
|
|
12
|
+
}
|
|
13
|
+
const abs = unscaled < 0n ? -unscaled : unscaled;
|
|
14
|
+
const factor = 10n ** BigInt(scale);
|
|
15
|
+
const intPart = abs / factor;
|
|
16
|
+
const rem = abs % factor;
|
|
17
|
+
if (intPart > MAX_VALUE_BIG || (intPart === MAX_VALUE_BIG && rem > 0n)) {
|
|
18
|
+
throw new RangeError(`MiniNumber too large: ${unscaled}E${-scale}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function digits(v) {
|
|
22
|
+
if (v === 0n)
|
|
23
|
+
return 0;
|
|
24
|
+
return v.toString().replace('-', '').length;
|
|
25
|
+
}
|
|
26
|
+
function roundToDigits(unscaled, scale, digits_target) {
|
|
27
|
+
let u = unscaled;
|
|
28
|
+
let s = scale;
|
|
29
|
+
const currentDigits = digits(u);
|
|
30
|
+
if (currentDigits > digits_target) {
|
|
31
|
+
const drop = currentDigits - digits_target;
|
|
32
|
+
const divisor = 10n ** BigInt(drop);
|
|
33
|
+
u = u / divisor;
|
|
34
|
+
s = s - drop;
|
|
35
|
+
}
|
|
36
|
+
if (s > MAX_DECIMAL_PLACES) {
|
|
37
|
+
const diff = s - MAX_DECIMAL_PLACES;
|
|
38
|
+
const divisor = 10n ** BigInt(diff);
|
|
39
|
+
u = u / divisor;
|
|
40
|
+
s = s - diff;
|
|
41
|
+
}
|
|
42
|
+
while (s > 0 && u % 10n === 0n) {
|
|
43
|
+
u = u / 10n;
|
|
44
|
+
s--;
|
|
45
|
+
}
|
|
46
|
+
return { unscaled: u, scale: s };
|
|
47
|
+
}
|
|
48
|
+
export class MiniNumber {
|
|
49
|
+
unscaled;
|
|
50
|
+
scale;
|
|
51
|
+
static ZERO = new MiniNumber(0);
|
|
52
|
+
static ONE = new MiniNumber(1);
|
|
53
|
+
static TWO = new MiniNumber(2);
|
|
54
|
+
static THREE = new MiniNumber(3);
|
|
55
|
+
static FOUR = new MiniNumber(4);
|
|
56
|
+
static EIGHT = new MiniNumber(8);
|
|
57
|
+
static TWELVE = new MiniNumber(12);
|
|
58
|
+
static SIXTEEN = new MiniNumber(16);
|
|
59
|
+
static TWENTY = new MiniNumber(20);
|
|
60
|
+
static THIRTYTWO = new MiniNumber(32);
|
|
61
|
+
static FIFTY = new MiniNumber(50);
|
|
62
|
+
static SIXTYFOUR = new MiniNumber(64);
|
|
63
|
+
static TWOFIVESIX = new MiniNumber(256);
|
|
64
|
+
static FIVEONE12 = new MiniNumber(512);
|
|
65
|
+
static THOUSAND24 = new MiniNumber(1024);
|
|
66
|
+
static MINUSONE = new MiniNumber(-1);
|
|
67
|
+
constructor(value) {
|
|
68
|
+
if (value instanceof MiniNumber) {
|
|
69
|
+
this.unscaled = value.unscaled;
|
|
70
|
+
this.scale = value.scale;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (typeof value === 'bigint') {
|
|
74
|
+
this.unscaled = value;
|
|
75
|
+
this.scale = 0;
|
|
76
|
+
if (value !== 0n) {
|
|
77
|
+
const r = roundToDigits(this.unscaled, this.scale, MAX_DIGITS);
|
|
78
|
+
this.unscaled = r.unscaled;
|
|
79
|
+
this.scale = r.scale;
|
|
80
|
+
checkLimits(this.unscaled, this.scale);
|
|
81
|
+
}
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (typeof value === 'number') {
|
|
85
|
+
const s = Number.isInteger(value) ? value.toFixed(0) : value.toExponential(20);
|
|
86
|
+
const parsed = parseDecimal(s);
|
|
87
|
+
this.unscaled = parsed.unscaled;
|
|
88
|
+
this.scale = parsed.scale;
|
|
89
|
+
if (this.unscaled !== 0n) {
|
|
90
|
+
const r = roundToDigits(this.unscaled, this.scale, MAX_DIGITS);
|
|
91
|
+
this.unscaled = r.unscaled;
|
|
92
|
+
this.scale = r.scale;
|
|
93
|
+
checkLimits(this.unscaled, this.scale);
|
|
94
|
+
}
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const parsed = parseDecimal(value);
|
|
98
|
+
this.unscaled = parsed.unscaled;
|
|
99
|
+
this.scale = parsed.scale;
|
|
100
|
+
if (this.unscaled !== 0n) {
|
|
101
|
+
const r = roundToDigits(this.unscaled, this.scale, MAX_DIGITS);
|
|
102
|
+
this.unscaled = r.unscaled;
|
|
103
|
+
this.scale = r.scale;
|
|
104
|
+
checkLimits(this.unscaled, this.scale);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
getAsBigDecimal() {
|
|
108
|
+
return this.toString();
|
|
109
|
+
}
|
|
110
|
+
getAsBigInteger() {
|
|
111
|
+
const s = this.toString();
|
|
112
|
+
const dot = s.indexOf('.');
|
|
113
|
+
return dot === -1 ? s : s.slice(0, dot);
|
|
114
|
+
}
|
|
115
|
+
toNumber() {
|
|
116
|
+
return Number(this.toString());
|
|
117
|
+
}
|
|
118
|
+
toString() {
|
|
119
|
+
if (this.unscaled === 0n)
|
|
120
|
+
return '0';
|
|
121
|
+
const neg = this.unscaled < 0n;
|
|
122
|
+
const abs = neg ? -this.unscaled : this.unscaled;
|
|
123
|
+
let s = abs.toString();
|
|
124
|
+
if (s.length <= this.scale) {
|
|
125
|
+
s = s.padStart(this.scale + 1, '0');
|
|
126
|
+
s = s.slice(0, s.length - this.scale) + '.' + s.slice(s.length - this.scale);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
s = s.slice(0, s.length - this.scale) + '.' + s.slice(s.length - this.scale);
|
|
130
|
+
}
|
|
131
|
+
s = s.replace(/(\.[0-9]*?)0+$/, '$1').replace(/\.$/, '');
|
|
132
|
+
return neg ? '-' + s : s;
|
|
133
|
+
}
|
|
134
|
+
assertSameScale(other) {
|
|
135
|
+
if (this.scale === other.scale)
|
|
136
|
+
return { a: this.unscaled, b: other.unscaled, outScale: this.scale };
|
|
137
|
+
if (this.scale > other.scale) {
|
|
138
|
+
const diff = this.scale - other.scale;
|
|
139
|
+
return { a: this.unscaled, b: other.unscaled * 10n ** BigInt(diff), outScale: this.scale };
|
|
140
|
+
}
|
|
141
|
+
const diff = other.scale - this.scale;
|
|
142
|
+
return { a: this.unscaled * 10n ** BigInt(diff), b: other.unscaled, outScale: other.scale };
|
|
143
|
+
}
|
|
144
|
+
add(other) {
|
|
145
|
+
const { a, b, outScale } = this.assertSameScale(other);
|
|
146
|
+
const result = a + b;
|
|
147
|
+
const r = roundToDigits(result, outScale, MAX_DIGITS);
|
|
148
|
+
return new MiniNumber(r.unscaled.toString() + 'e' + (-r.scale));
|
|
149
|
+
}
|
|
150
|
+
sub(other) {
|
|
151
|
+
const { a, b, outScale } = this.assertSameScale(other);
|
|
152
|
+
const result = a - b;
|
|
153
|
+
const r = roundToDigits(result, outScale, MAX_DIGITS);
|
|
154
|
+
return new MiniNumber(r.unscaled.toString() + 'e' + (-r.scale));
|
|
155
|
+
}
|
|
156
|
+
mult(other) {
|
|
157
|
+
const result = this.unscaled * other.unscaled;
|
|
158
|
+
const outScale = this.scale + other.scale;
|
|
159
|
+
const r = roundToDigits(result, outScale, MAX_DIGITS);
|
|
160
|
+
return new MiniNumber(r.unscaled.toString() + 'e' + (-r.scale));
|
|
161
|
+
}
|
|
162
|
+
div(other) {
|
|
163
|
+
if (other.unscaled === 0n)
|
|
164
|
+
throw new Error('Division by zero');
|
|
165
|
+
const extraScale = MAX_DIGITS;
|
|
166
|
+
const scaledThis = this.unscaled * 10n ** BigInt(extraScale);
|
|
167
|
+
const result = scaledThis / other.unscaled;
|
|
168
|
+
const outScale = this.scale + extraScale - other.scale;
|
|
169
|
+
const r = roundToDigits(result, outScale, MAX_DIGITS);
|
|
170
|
+
return new MiniNumber(r.unscaled.toString() + 'e' + (-r.scale));
|
|
171
|
+
}
|
|
172
|
+
modulo(other) {
|
|
173
|
+
if (other.unscaled === 0n)
|
|
174
|
+
throw new Error('Modulo by zero');
|
|
175
|
+
const { a, b, outScale } = this.assertSameScale(other);
|
|
176
|
+
const result = a % b;
|
|
177
|
+
return new MiniNumber(result.toString() + 'e' + (-outScale));
|
|
178
|
+
}
|
|
179
|
+
pow(n) {
|
|
180
|
+
if (n < 0)
|
|
181
|
+
throw new Error('Negative power not supported');
|
|
182
|
+
let result = MiniNumber.ONE;
|
|
183
|
+
for (let i = 0; i < n; i++)
|
|
184
|
+
result = result.mult(this);
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
sqrt() {
|
|
188
|
+
if (this.unscaled < 0n)
|
|
189
|
+
throw new Error('Square root of negative number');
|
|
190
|
+
if (this.unscaled === 0n)
|
|
191
|
+
return MiniNumber.ZERO;
|
|
192
|
+
const extra = MAX_DIGITS * 2;
|
|
193
|
+
const scaled = this.unscaled * 10n ** BigInt(extra);
|
|
194
|
+
const s = scaled / 10n ** BigInt(extra / 2);
|
|
195
|
+
let x = s;
|
|
196
|
+
const two = 2n;
|
|
197
|
+
for (let i = 0; i < 100; i++) {
|
|
198
|
+
const next = (x + scaled / x) / two;
|
|
199
|
+
if (next === x)
|
|
200
|
+
break;
|
|
201
|
+
x = next;
|
|
202
|
+
}
|
|
203
|
+
const outScale = (this.scale + extra) / 2;
|
|
204
|
+
const r = roundToDigits(x, outScale, MAX_DIGITS);
|
|
205
|
+
return new MiniNumber(r.unscaled.toString() + 'e' + (-r.scale));
|
|
206
|
+
}
|
|
207
|
+
floor() {
|
|
208
|
+
if (this.scale === 0)
|
|
209
|
+
return new MiniNumber(this);
|
|
210
|
+
const divisor = 10n ** BigInt(this.scale);
|
|
211
|
+
const q = this.unscaled / divisor;
|
|
212
|
+
const rem = this.unscaled % divisor;
|
|
213
|
+
if (this.unscaled < 0n && rem !== 0n) {
|
|
214
|
+
return new MiniNumber((q - 1n).toString());
|
|
215
|
+
}
|
|
216
|
+
return new MiniNumber(q.toString());
|
|
217
|
+
}
|
|
218
|
+
ceil() {
|
|
219
|
+
if (this.scale === 0)
|
|
220
|
+
return new MiniNumber(this);
|
|
221
|
+
const divisor = 10n ** BigInt(this.scale);
|
|
222
|
+
const q = this.unscaled / divisor;
|
|
223
|
+
const rem = this.unscaled % divisor;
|
|
224
|
+
if (this.unscaled > 0n && rem !== 0n) {
|
|
225
|
+
return new MiniNumber((q + 1n).toString());
|
|
226
|
+
}
|
|
227
|
+
return new MiniNumber(q.toString());
|
|
228
|
+
}
|
|
229
|
+
abs() {
|
|
230
|
+
if (this.unscaled >= 0n)
|
|
231
|
+
return new MiniNumber(this);
|
|
232
|
+
return new MiniNumber((-this.unscaled).toString() + 'e' + (-this.scale));
|
|
233
|
+
}
|
|
234
|
+
negate() {
|
|
235
|
+
return new MiniNumber((-this.unscaled).toString() + 'e' + (-this.scale));
|
|
236
|
+
}
|
|
237
|
+
increment() {
|
|
238
|
+
return this.add(MiniNumber.ONE);
|
|
239
|
+
}
|
|
240
|
+
decrement() {
|
|
241
|
+
return this.sub(MiniNumber.ONE);
|
|
242
|
+
}
|
|
243
|
+
compareTo(other) {
|
|
244
|
+
const { a, b } = this.assertSameScale(other);
|
|
245
|
+
if (a < b)
|
|
246
|
+
return -1;
|
|
247
|
+
if (a > b)
|
|
248
|
+
return 1;
|
|
249
|
+
return 0;
|
|
250
|
+
}
|
|
251
|
+
isEqual(other) { return this.compareTo(other) === 0; }
|
|
252
|
+
isLess(other) { return this.compareTo(other) < 0; }
|
|
253
|
+
isLessEqual(other) { return this.compareTo(other) <= 0; }
|
|
254
|
+
isMore(other) { return this.compareTo(other) > 0; }
|
|
255
|
+
isMoreEqual(other) { return this.compareTo(other) >= 0; }
|
|
256
|
+
setSignificantDigits(d) {
|
|
257
|
+
if (d > MAX_DIGITS)
|
|
258
|
+
throw new Error(`Cannot specify this many significant digits ${d}`);
|
|
259
|
+
if (d < 0)
|
|
260
|
+
throw new Error(`Cannot specify negative significant digits ${d}`);
|
|
261
|
+
const currentDigits = digits(this.unscaled);
|
|
262
|
+
if (currentDigits <= d)
|
|
263
|
+
return new MiniNumber(this);
|
|
264
|
+
const drop = currentDigits - d;
|
|
265
|
+
const divisor = 10n ** BigInt(drop);
|
|
266
|
+
const newUnscaled = this.unscaled / divisor;
|
|
267
|
+
return new MiniNumber(newUnscaled.toString() + 'e' + (-(this.scale - drop)));
|
|
268
|
+
}
|
|
269
|
+
decimalPlaces() {
|
|
270
|
+
return this.scale;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function parseDecimal(s) {
|
|
274
|
+
s = s.trim();
|
|
275
|
+
const ePos = s.toLowerCase().indexOf('e');
|
|
276
|
+
if (ePos !== -1) {
|
|
277
|
+
const mantissa = s.slice(0, ePos);
|
|
278
|
+
const exponent = parseInt(s.slice(ePos + 1), 10);
|
|
279
|
+
const { unscaled, scale } = parseDecimal(mantissa);
|
|
280
|
+
const newScale = scale - exponent;
|
|
281
|
+
if (newScale < 0) {
|
|
282
|
+
const factor = 10n ** BigInt(-newScale);
|
|
283
|
+
return { unscaled: unscaled * factor, scale: 0 };
|
|
284
|
+
}
|
|
285
|
+
return { unscaled, scale: newScale };
|
|
286
|
+
}
|
|
287
|
+
const neg = s.startsWith('-');
|
|
288
|
+
const abs = neg ? s.slice(1) : s;
|
|
289
|
+
const dot = abs.indexOf('.');
|
|
290
|
+
if (dot === -1) {
|
|
291
|
+
return { unscaled: BigInt(neg ? '-' + abs : abs), scale: 0 };
|
|
292
|
+
}
|
|
293
|
+
const intPart = abs.slice(0, dot) || '0';
|
|
294
|
+
const fracPart = abs.slice(dot + 1);
|
|
295
|
+
const full = intPart + fracPart;
|
|
296
|
+
const scale = fracPart.length;
|
|
297
|
+
return { unscaled: BigInt(neg ? '-' + full : full), scale };
|
|
298
|
+
}
|
package/dist/Streamable.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Streamable.ts - Canonical Java-Compatible Serialization Primitives
|
|
4
3
|
*
|
|
@@ -21,29 +20,7 @@
|
|
|
21
20
|
* Created: 2026-01-20
|
|
22
21
|
* Purpose: Single source of truth for all Minima type serialization
|
|
23
22
|
*/
|
|
24
|
-
|
|
25
|
-
exports.encodeMiniString = exports.encodeMiniData = exports.encodeMiniNumber = exports.STATETYPE_BOOL = exports.STATETYPE_STRING = exports.STATETYPE_NUMBER = exports.STATETYPE_HEX = void 0;
|
|
26
|
-
exports.hexToBytes = hexToBytes;
|
|
27
|
-
exports.bytesToHex = bytesToHex;
|
|
28
|
-
exports.concat = concat;
|
|
29
|
-
exports.writeMiniByte = writeMiniByte;
|
|
30
|
-
exports.writeMiniData = writeMiniData;
|
|
31
|
-
exports.writeMiniString = writeMiniString;
|
|
32
|
-
exports.bigIntToByteArray = bigIntToByteArray;
|
|
33
|
-
exports.writeMiniNumber = writeMiniNumber;
|
|
34
|
-
exports.writeHashToStream = writeHashToStream;
|
|
35
|
-
exports.writeMMREntryNumber = writeMMREntryNumber;
|
|
36
|
-
exports.normalizeHexString = normalizeHexString;
|
|
37
|
-
exports.writeStateVariable = writeStateVariable;
|
|
38
|
-
exports.writeMMRData = writeMMRData;
|
|
39
|
-
exports.writeMMRProofChunk = writeMMRProofChunk;
|
|
40
|
-
exports.writeMMRProof = writeMMRProof;
|
|
41
|
-
exports.writeSignatureProof = writeSignatureProof;
|
|
42
|
-
exports.writeSignature = writeSignature;
|
|
43
|
-
exports.writeWitness = writeWitness;
|
|
44
|
-
exports.writeHierarchicalWitness = writeHierarchicalWitness;
|
|
45
|
-
exports.writeHierarchicalWitnessToHex = writeHierarchicalWitnessToHex;
|
|
46
|
-
function hexToBytes(hex) {
|
|
23
|
+
export function hexToBytes(hex) {
|
|
47
24
|
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
48
25
|
if (cleanHex.length === 0) {
|
|
49
26
|
return new Uint8Array(0);
|
|
@@ -57,10 +34,10 @@ function hexToBytes(hex) {
|
|
|
57
34
|
}
|
|
58
35
|
return bytes;
|
|
59
36
|
}
|
|
60
|
-
function bytesToHex(bytes) {
|
|
37
|
+
export function bytesToHex(bytes) {
|
|
61
38
|
return '0x' + Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
62
39
|
}
|
|
63
|
-
function concat(...arrays) {
|
|
40
|
+
export function concat(...arrays) {
|
|
64
41
|
const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0);
|
|
65
42
|
const result = new Uint8Array(totalLength);
|
|
66
43
|
let offset = 0;
|
|
@@ -75,7 +52,7 @@ function concat(...arrays) {
|
|
|
75
52
|
*
|
|
76
53
|
* Format: single byte (0-255)
|
|
77
54
|
*/
|
|
78
|
-
function writeMiniByte(value) {
|
|
55
|
+
export function writeMiniByte(value) {
|
|
79
56
|
if (typeof value === 'boolean') {
|
|
80
57
|
return new Uint8Array([value ? 1 : 0]);
|
|
81
58
|
}
|
|
@@ -89,7 +66,7 @@ function writeMiniByte(value) {
|
|
|
89
66
|
*
|
|
90
67
|
* Format: 4-byte big-endian int length + raw bytes
|
|
91
68
|
*/
|
|
92
|
-
function writeMiniData(data) {
|
|
69
|
+
export function writeMiniData(data) {
|
|
93
70
|
const length = data.length;
|
|
94
71
|
const lengthBytes = new Uint8Array([
|
|
95
72
|
(length >> 24) & 0xff,
|
|
@@ -105,7 +82,7 @@ function writeMiniData(data) {
|
|
|
105
82
|
* Format: MiniData encoding of UTF-8 bytes
|
|
106
83
|
* = 4-byte int length + UTF-8 bytes
|
|
107
84
|
*/
|
|
108
|
-
function writeMiniString(str) {
|
|
85
|
+
export function writeMiniString(str) {
|
|
109
86
|
const utf8Bytes = new TextEncoder().encode(str);
|
|
110
87
|
return writeMiniData(utf8Bytes);
|
|
111
88
|
}
|
|
@@ -119,7 +96,7 @@ function writeMiniString(str) {
|
|
|
119
96
|
* @param value - The BigInt value (must be non-negative)
|
|
120
97
|
* @returns Uint8Array in two's complement format
|
|
121
98
|
*/
|
|
122
|
-
function bigIntToByteArray(value) {
|
|
99
|
+
export function bigIntToByteArray(value) {
|
|
123
100
|
if (value < 0n) {
|
|
124
101
|
throw new Error(`bigIntToByteArray does not support negative values: ${value}`);
|
|
125
102
|
}
|
|
@@ -152,7 +129,7 @@ function bigIntToByteArray(value) {
|
|
|
152
129
|
* @param value - The unscaled BigInt value
|
|
153
130
|
* @param scale - The scale (decimal places), default 0
|
|
154
131
|
*/
|
|
155
|
-
function writeMiniNumber(value, scale = 0) {
|
|
132
|
+
export function writeMiniNumber(value, scale = 0) {
|
|
156
133
|
if (typeof value !== 'bigint') {
|
|
157
134
|
throw new Error(`writeMiniNumber requires bigint, got ${typeof value}`);
|
|
158
135
|
}
|
|
@@ -176,7 +153,7 @@ function writeMiniNumber(value, scale = 0) {
|
|
|
176
153
|
* Format: 4-byte big-endian int length + hash bytes
|
|
177
154
|
* (Same as MiniData)
|
|
178
155
|
*/
|
|
179
|
-
function writeHashToStream(hash) {
|
|
156
|
+
export function writeHashToStream(hash) {
|
|
180
157
|
return writeMiniData(hash);
|
|
181
158
|
}
|
|
182
159
|
/**
|
|
@@ -194,7 +171,7 @@ function writeHashToStream(hash) {
|
|
|
194
171
|
* [00 01 00] - MiniNumber: scale=0, len=1, data=0x00
|
|
195
172
|
* [00 00 00 LL ...] - MiniData: 4-byte len + BigInteger bytes
|
|
196
173
|
*/
|
|
197
|
-
function writeMMREntryNumber(value, scale = 0) {
|
|
174
|
+
export function writeMMREntryNumber(value, scale = 0) {
|
|
198
175
|
if (value < 0n) {
|
|
199
176
|
throw new Error(`writeMMREntryNumber does not support negative values: ${value}`);
|
|
200
177
|
}
|
|
@@ -208,10 +185,10 @@ function writeMMREntryNumber(value, scale = 0) {
|
|
|
208
185
|
/**
|
|
209
186
|
* StateVariable type constants from Java StateVariable.java
|
|
210
187
|
*/
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
188
|
+
export const STATETYPE_HEX = 1;
|
|
189
|
+
export const STATETYPE_NUMBER = 2;
|
|
190
|
+
export const STATETYPE_STRING = 4;
|
|
191
|
+
export const STATETYPE_BOOL = 8;
|
|
215
192
|
/**
|
|
216
193
|
* Normalize hex/address string to 0x format.
|
|
217
194
|
* Handles both Mx (Minima Base32) and 0x formats.
|
|
@@ -220,7 +197,7 @@ exports.STATETYPE_BOOL = 8;
|
|
|
220
197
|
* Note: For Mx conversion, caller should use mxToHex from minima-base32
|
|
221
198
|
* This function handles 0x prefix normalization only.
|
|
222
199
|
*/
|
|
223
|
-
function normalizeHexString(value) {
|
|
200
|
+
export function normalizeHexString(value) {
|
|
224
201
|
if (!value) {
|
|
225
202
|
return '0x00';
|
|
226
203
|
}
|
|
@@ -242,7 +219,7 @@ function normalizeHexString(value) {
|
|
|
242
219
|
* @param sv - StateVariable with high-level value
|
|
243
220
|
* @param mxToHex - Optional function to convert Mx addresses to 0x hex
|
|
244
221
|
*/
|
|
245
|
-
function writeStateVariable(sv, mxToHex) {
|
|
222
|
+
export function writeStateVariable(sv, mxToHex) {
|
|
246
223
|
if (sv.port < 0 || sv.port > 255) {
|
|
247
224
|
throw new Error(`StateVariable port must be 0-255, got ${sv.port}`);
|
|
248
225
|
}
|
|
@@ -251,7 +228,7 @@ function writeStateVariable(sv, mxToHex) {
|
|
|
251
228
|
let dataBytes;
|
|
252
229
|
switch (sv.type) {
|
|
253
230
|
case 'bool':
|
|
254
|
-
typeByte = writeMiniByte(
|
|
231
|
+
typeByte = writeMiniByte(STATETYPE_BOOL);
|
|
255
232
|
if (typeof sv.value === 'boolean') {
|
|
256
233
|
dataBytes = writeMiniByte(sv.value);
|
|
257
234
|
}
|
|
@@ -263,7 +240,7 @@ function writeStateVariable(sv, mxToHex) {
|
|
|
263
240
|
}
|
|
264
241
|
break;
|
|
265
242
|
case 'number':
|
|
266
|
-
typeByte = writeMiniByte(
|
|
243
|
+
typeByte = writeMiniByte(STATETYPE_NUMBER);
|
|
267
244
|
if (typeof sv.value === 'bigint') {
|
|
268
245
|
dataBytes = writeMiniNumber(sv.value);
|
|
269
246
|
}
|
|
@@ -275,7 +252,7 @@ function writeStateVariable(sv, mxToHex) {
|
|
|
275
252
|
}
|
|
276
253
|
break;
|
|
277
254
|
case 'hex':
|
|
278
|
-
typeByte = writeMiniByte(
|
|
255
|
+
typeByte = writeMiniByte(STATETYPE_HEX);
|
|
279
256
|
if (typeof sv.value === 'string') {
|
|
280
257
|
let normalizedHex = sv.value;
|
|
281
258
|
if (sv.value.toLowerCase().startsWith('mx') && mxToHex) {
|
|
@@ -294,7 +271,7 @@ function writeStateVariable(sv, mxToHex) {
|
|
|
294
271
|
}
|
|
295
272
|
break;
|
|
296
273
|
case 'string':
|
|
297
|
-
typeByte = writeMiniByte(
|
|
274
|
+
typeByte = writeMiniByte(STATETYPE_STRING);
|
|
298
275
|
if (typeof sv.value === 'string') {
|
|
299
276
|
let bracketedValue = sv.value;
|
|
300
277
|
if (!sv.value.startsWith('[') || !sv.value.endsWith(']')) {
|
|
@@ -318,7 +295,7 @@ function writeStateVariable(sv, mxToHex) {
|
|
|
318
295
|
* mData.writeHashToStream(zOut); // 4-byte len + hash
|
|
319
296
|
* mValue.writeDataStream(zOut); // MiniNumber
|
|
320
297
|
*/
|
|
321
|
-
function writeMMRData(mmrData) {
|
|
298
|
+
export function writeMMRData(mmrData) {
|
|
322
299
|
const hashBytes = writeHashToStream(mmrData.data);
|
|
323
300
|
const valueBytes = writeMiniNumber(mmrData.value, 0);
|
|
324
301
|
return concat(hashBytes, valueBytes);
|
|
@@ -330,7 +307,7 @@ function writeMMRData(mmrData) {
|
|
|
330
307
|
* mLeft.writeDataStream(zOut); // MiniByte (1 byte)
|
|
331
308
|
* mMMRData.writeDataStream(zOut); // MMRData
|
|
332
309
|
*/
|
|
333
|
-
function writeMMRProofChunk(chunk) {
|
|
310
|
+
export function writeMMRProofChunk(chunk) {
|
|
334
311
|
const leftByte = writeMiniByte(chunk.isLeft);
|
|
335
312
|
const dataBytes = writeMMRData(chunk.mmrData);
|
|
336
313
|
return concat(leftByte, dataBytes);
|
|
@@ -343,7 +320,7 @@ function writeMMRProofChunk(chunk) {
|
|
|
343
320
|
* MiniNumber.WriteToStream(zOut, len); // Array length as MiniNumber
|
|
344
321
|
* for each chunk: chunk.writeDataStream(zOut);
|
|
345
322
|
*/
|
|
346
|
-
function writeMMRProof(proof) {
|
|
323
|
+
export function writeMMRProof(proof) {
|
|
347
324
|
const blockTimeBytes = writeMiniNumber(proof.blockTime, 0);
|
|
348
325
|
const lengthBytes = writeMiniNumber(BigInt(proof.chunks.length), 0);
|
|
349
326
|
const parts = [blockTimeBytes, lengthBytes];
|
|
@@ -360,7 +337,7 @@ function writeMMRProof(proof) {
|
|
|
360
337
|
* mSignature.writeDataStream(zOut); // MiniData
|
|
361
338
|
* mProof.writeDataStream(zOut); // MMRProof
|
|
362
339
|
*/
|
|
363
|
-
function writeSignatureProof(sigProof) {
|
|
340
|
+
export function writeSignatureProof(sigProof) {
|
|
364
341
|
const pubKeyBytes = writeMiniData(sigProof.leafPubkey);
|
|
365
342
|
const sigBytes = writeMiniData(sigProof.signature);
|
|
366
343
|
const proofBytes = writeMMRProof(sigProof.mmrProof);
|
|
@@ -373,7 +350,7 @@ function writeSignatureProof(sigProof) {
|
|
|
373
350
|
* MiniNumber.WriteToStream(zOut, mSignatures.size());
|
|
374
351
|
* for each sig: sig.writeDataStream(zOut);
|
|
375
352
|
*/
|
|
376
|
-
function writeSignature(sig) {
|
|
353
|
+
export function writeSignature(sig) {
|
|
377
354
|
const lengthBytes = writeMiniNumber(BigInt(sig.proofs.length), 0);
|
|
378
355
|
const parts = [lengthBytes];
|
|
379
356
|
for (const proof of sig.proofs) {
|
|
@@ -392,7 +369,7 @@ function writeSignature(sig) {
|
|
|
392
369
|
* MiniNumber.WriteToStream(zOut, mScriptProofs.size());
|
|
393
370
|
* for each sp: sp.writeDataStream(zOut);
|
|
394
371
|
*/
|
|
395
|
-
function writeWitness(witness) {
|
|
372
|
+
export function writeWitness(witness) {
|
|
396
373
|
const parts = [];
|
|
397
374
|
// Signatures
|
|
398
375
|
parts.push(writeMiniNumber(BigInt(witness.signatures.length), 0));
|
|
@@ -415,7 +392,7 @@ function writeWitness(witness) {
|
|
|
415
392
|
* Write a simplified hierarchical witness (signatures only)
|
|
416
393
|
* Used for WOTS transaction signing flow
|
|
417
394
|
*/
|
|
418
|
-
function writeHierarchicalWitness(bundle) {
|
|
395
|
+
export function writeHierarchicalWitness(bundle) {
|
|
419
396
|
const lengthBytes = writeMiniNumber(BigInt(bundle.signatures.length), 0);
|
|
420
397
|
const parts = [lengthBytes];
|
|
421
398
|
for (const sig of bundle.signatures) {
|
|
@@ -426,7 +403,7 @@ function writeHierarchicalWitness(bundle) {
|
|
|
426
403
|
/**
|
|
427
404
|
* Write hierarchical witness and return as hex string
|
|
428
405
|
*/
|
|
429
|
-
function writeHierarchicalWitnessToHex(bundle) {
|
|
406
|
+
export function writeHierarchicalWitnessToHex(bundle) {
|
|
430
407
|
return bytesToHex(writeHierarchicalWitness(bundle));
|
|
431
408
|
}
|
|
432
409
|
// ============================================================
|
|
@@ -434,6 +411,6 @@ function writeHierarchicalWitnessToHex(bundle) {
|
|
|
434
411
|
// ============================================================
|
|
435
412
|
// The extension's WitnessSerializer uses encode* naming, while SDK uses write* naming.
|
|
436
413
|
// These aliases provide compatibility with extension code.
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
414
|
+
export const encodeMiniNumber = writeMiniNumber;
|
|
415
|
+
export const encodeMiniData = writeMiniData;
|
|
416
|
+
export const encodeMiniString = writeMiniString;
|
package/dist/adapters/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @module @totemsdk/core/adapters
|
|
4
3
|
* Platform-agnostic adapter interfaces for SDK portability
|
|
@@ -10,17 +9,13 @@
|
|
|
10
9
|
*
|
|
11
10
|
* Each platform provides its own implementation of these adapters.
|
|
12
11
|
*/
|
|
13
|
-
|
|
14
|
-
exports.NoopLifecycleAdapter = exports.DefaultTimerAdapter = exports.NoopMetrics = exports.ConsoleLogger = exports.NoopLogger = exports.WebSocketReadyState = void 0;
|
|
15
|
-
exports.createCancellationToken = createCancellationToken;
|
|
16
|
-
exports.createAdapterRegistry = createAdapterRegistry;
|
|
17
|
-
exports.WebSocketReadyState = {
|
|
12
|
+
export const WebSocketReadyState = {
|
|
18
13
|
CONNECTING: 0,
|
|
19
14
|
OPEN: 1,
|
|
20
15
|
CLOSING: 2,
|
|
21
16
|
CLOSED: 3,
|
|
22
17
|
};
|
|
23
|
-
function createCancellationToken() {
|
|
18
|
+
export function createCancellationToken() {
|
|
24
19
|
let cancelled = false;
|
|
25
20
|
const callbacks = new Set();
|
|
26
21
|
const token = {
|
|
@@ -45,7 +40,7 @@ function createCancellationToken() {
|
|
|
45
40
|
}
|
|
46
41
|
};
|
|
47
42
|
}
|
|
48
|
-
function createAdapterRegistry(adapters) {
|
|
43
|
+
export function createAdapterRegistry(adapters) {
|
|
49
44
|
const required = [
|
|
50
45
|
'storage', 'auth', 'websocket', 'http', 'config', 'logger', 'timer', 'crypto'
|
|
51
46
|
];
|
|
@@ -56,14 +51,14 @@ function createAdapterRegistry(adapters) {
|
|
|
56
51
|
}
|
|
57
52
|
return adapters;
|
|
58
53
|
}
|
|
59
|
-
class NoopLogger {
|
|
54
|
+
export class NoopLogger {
|
|
60
55
|
debug(_message, ..._args) { }
|
|
61
56
|
info(_message, ..._args) { }
|
|
62
57
|
warn(_message, ..._args) { }
|
|
63
58
|
error(_message, ..._args) { }
|
|
64
59
|
}
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
export class ConsoleLogger {
|
|
61
|
+
prefix;
|
|
67
62
|
constructor(prefix = '[SDK]') {
|
|
68
63
|
this.prefix = prefix;
|
|
69
64
|
}
|
|
@@ -80,15 +75,13 @@ class ConsoleLogger {
|
|
|
80
75
|
console.error(`${this.prefix} ${message}`, ...args);
|
|
81
76
|
}
|
|
82
77
|
}
|
|
83
|
-
|
|
84
|
-
class NoopMetrics {
|
|
78
|
+
export class NoopMetrics {
|
|
85
79
|
increment(_name, _value, _tags) { }
|
|
86
80
|
gauge(_name, _value, _tags) { }
|
|
87
81
|
timing(_name, _durationMs, _tags) { }
|
|
88
82
|
histogram(_name, _value, _tags) { }
|
|
89
83
|
}
|
|
90
|
-
|
|
91
|
-
class DefaultTimerAdapter {
|
|
84
|
+
export class DefaultTimerAdapter {
|
|
92
85
|
setTimeout(callback, ms) {
|
|
93
86
|
return setTimeout(callback, ms);
|
|
94
87
|
}
|
|
@@ -105,8 +98,7 @@ class DefaultTimerAdapter {
|
|
|
105
98
|
return Date.now();
|
|
106
99
|
}
|
|
107
100
|
}
|
|
108
|
-
|
|
109
|
-
class NoopLifecycleAdapter {
|
|
101
|
+
export class NoopLifecycleAdapter {
|
|
110
102
|
onSuspend(_callback) {
|
|
111
103
|
return () => { };
|
|
112
104
|
}
|
|
@@ -114,4 +106,3 @@ class NoopLifecycleAdapter {
|
|
|
114
106
|
return () => { };
|
|
115
107
|
}
|
|
116
108
|
}
|
|
117
|
-
exports.NoopLifecycleAdapter = NoopLifecycleAdapter;
|