@totemsdk/core 1.2.6 → 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.
Files changed (95) hide show
  1. package/dist/canonical.d.ts +3 -0
  2. package/dist/canonical.js +22 -0
  3. package/dist/cjs/MiniNumber.d.ts +47 -0
  4. package/dist/cjs/MiniNumber.js +302 -0
  5. package/dist/cjs/Streamable.d.ts +268 -0
  6. package/dist/cjs/Streamable.js +439 -0
  7. package/dist/cjs/adapters/index.d.ts +190 -0
  8. package/dist/cjs/adapters/index.js +118 -0
  9. package/dist/cjs/adapters/types.d.ts +150 -0
  10. package/dist/cjs/adapters/types.js +81 -0
  11. package/dist/cjs/binio.d.ts +9 -0
  12. package/dist/cjs/binio.js +25 -0
  13. package/dist/cjs/bip39.d.ts +91 -0
  14. package/dist/cjs/bip39.js +434 -0
  15. package/dist/cjs/canonical.d.ts +3 -0
  16. package/dist/cjs/canonical.js +27 -0
  17. package/dist/cjs/derive.d.ts +2 -0
  18. package/dist/cjs/derive.js +13 -0
  19. package/dist/cjs/index.d.ts +49 -0
  20. package/dist/cjs/index.js +231 -0
  21. package/dist/cjs/javaStreamables.d.ts +254 -0
  22. package/dist/cjs/javaStreamables.js +391 -0
  23. package/dist/cjs/lease/LeaseMonitor.d.ts +44 -0
  24. package/dist/cjs/lease/LeaseMonitor.js +115 -0
  25. package/dist/cjs/lease/LeaseStore.d.ts +58 -0
  26. package/dist/cjs/lease/LeaseStore.js +151 -0
  27. package/dist/cjs/lease/WatermarkStore.d.ts +61 -0
  28. package/dist/cjs/lease/WatermarkStore.js +209 -0
  29. package/dist/cjs/lease/index.d.ts +14 -0
  30. package/dist/cjs/lease/index.js +22 -0
  31. package/dist/cjs/lease-client.d.ts +31 -0
  32. package/dist/cjs/lease-client.js +44 -0
  33. package/dist/cjs/legacy.d.ts +23 -0
  34. package/dist/cjs/legacy.js +163 -0
  35. package/dist/cjs/minima32.d.ts +18 -0
  36. package/dist/cjs/minima32.js +121 -0
  37. package/dist/cjs/minimaWireSerializer.d.ts +60 -0
  38. package/dist/cjs/minimaWireSerializer.js +77 -0
  39. package/dist/cjs/mmr.d.ts +157 -0
  40. package/dist/cjs/mmr.js +352 -0
  41. package/dist/cjs/mx.d.ts +1 -0
  42. package/dist/cjs/mx.js +10 -0
  43. package/dist/cjs/package.json +1 -0
  44. package/dist/cjs/params.d.ts +29 -0
  45. package/dist/cjs/params.js +20 -0
  46. package/dist/cjs/script.d.ts +7 -0
  47. package/dist/cjs/script.js +27 -0
  48. package/dist/cjs/scripts/contract-helpers.d.ts +252 -0
  49. package/dist/cjs/scripts/contract-helpers.js +407 -0
  50. package/dist/cjs/scripts/dapp-types.d.ts +119 -0
  51. package/dist/cjs/scripts/dapp-types.js +77 -0
  52. package/dist/cjs/scripts/index.d.ts +4 -0
  53. package/dist/cjs/scripts/index.js +38 -0
  54. package/dist/cjs/scripts/types.d.ts +81 -0
  55. package/dist/cjs/scripts/types.js +159 -0
  56. package/dist/cjs/scripts/witness-serializer.d.ts +29 -0
  57. package/dist/cjs/scripts/witness-serializer.js +306 -0
  58. package/dist/cjs/transaction.d.ts +106 -0
  59. package/dist/cjs/transaction.js +293 -0
  60. package/dist/cjs/treekey.d.ts +399 -0
  61. package/dist/cjs/treekey.js +827 -0
  62. package/dist/cjs/tx/TransactionLifecycle.d.ts +55 -0
  63. package/dist/cjs/tx/TransactionLifecycle.js +205 -0
  64. package/dist/cjs/tx/TransactionReceiptStore.d.ts +36 -0
  65. package/dist/cjs/tx/TransactionReceiptStore.js +93 -0
  66. package/dist/cjs/tx/TransactionService.d.ts +50 -0
  67. package/dist/cjs/tx/TransactionService.js +186 -0
  68. package/dist/cjs/tx/index.d.ts +13 -0
  69. package/dist/cjs/tx/index.js +20 -0
  70. package/dist/cjs/tx/types.d.ts +115 -0
  71. package/dist/cjs/tx/types.js +6 -0
  72. package/dist/cjs/util.d.ts +1 -0
  73. package/dist/cjs/util.js +5 -0
  74. package/dist/cjs/utils.d.ts +8 -0
  75. package/dist/cjs/utils.js +42 -0
  76. package/dist/cjs/verify.d.ts +18 -0
  77. package/dist/cjs/verify.js +174 -0
  78. package/dist/cjs/version.d.ts +12 -0
  79. package/dist/cjs/version.js +15 -0
  80. package/dist/cjs/wasm-sync.d.ts +71 -0
  81. package/dist/cjs/wasm-sync.js +97 -0
  82. package/dist/cjs/wots.d.ts +232 -0
  83. package/dist/cjs/wots.js +589 -0
  84. package/dist/index.d.ts +9 -3
  85. package/dist/index.js +25 -2
  86. package/dist/minimaWireSerializer.js +2 -2
  87. package/dist/mmr.js +1 -1
  88. package/dist/transaction.js +10 -10
  89. package/dist/treekey.js +14 -0
  90. package/dist/wasm-sync.d.ts +2 -2
  91. package/dist/wasm-sync.js +3 -1
  92. package/dist/wots.d.ts +1 -1
  93. package/package.json +84 -45
  94. package/dist/wasm-bridge.d.ts +0 -54
  95. package/dist/wasm-bridge.js +0 -303
@@ -0,0 +1,232 @@
1
+ /**
2
+ * WOTS Implementation - BouncyCastle Compatible
3
+ *
4
+ * This implementation matches BouncyCastle's WinternitzOTSignature exactly,
5
+ * which is used by Minima's Winternitz.java class.
6
+ *
7
+ * Key differences from standard WOTS (Winternitz):
8
+ * - w=8 means 8 BITS per digit (not base-8), so each byte is one digit (0-255)
9
+ * - Stateful PRNG (GMSSRandom) for chain seed derivation, not stateless H(i||seed)
10
+ * - 34 total chains (32 message + 2 checksum), not 89
11
+ * - Each chain hashed up to 255 times (2^8 - 1)
12
+ *
13
+ * Source: org.bouncycastle.pqc.crypto.gmss.util.{WinternitzOTSignature, WinternitzOTSVerify, GMSSRandom}
14
+ */
15
+ import { ParamSet } from "./params.js";
16
+ import type { LoggerAdapter } from "./adapters/index.js";
17
+ /**
18
+ * Enable WOTS debug logging with a custom logger
19
+ * Use this for parity testing to capture all intermediate values
20
+ *
21
+ * WARNING: Debug logging outputs sensitive cryptographic material including
22
+ * private key chains and seeds. NEVER enable in production builds.
23
+ * This is intended for development/testing parity verification only.
24
+ */
25
+ export declare function setWotsLogger(logger: LoggerAdapter): void;
26
+ /**
27
+ * Disable WOTS debug logging
28
+ */
29
+ export declare function disableWotsLogger(): void;
30
+ /**
31
+ * Check if WOTS debug logging is enabled
32
+ */
33
+ export declare function isWotsDebugEnabled(): boolean;
34
+ export declare const F: (x: Uint8Array) => Uint8Array<ArrayBufferLike>;
35
+ export declare const hex: (u: Uint8Array) => string;
36
+ export declare const fromHex: (h: string) => Uint8Array;
37
+ export declare const concatBytes: (...arrs: Uint8Array[]) => Uint8Array<ArrayBuffer>;
38
+ export declare const u16be: (n: number) => Uint8Array<ArrayBuffer>;
39
+ export declare const u32be: (n: number) => Uint8Array<ArrayBuffer>;
40
+ export declare function assert32(u: Uint8Array, label?: string): void;
41
+ export declare const h: (x: Uint8Array) => Uint8Array<ArrayBufferLike>;
42
+ /**
43
+ * GMSSRandom - BouncyCastle's stateful PRNG for WOTS chain seed derivation
44
+ *
45
+ * Algorithm from GMSSRandom.java:
46
+ * rand = H(state)
47
+ * state = state + rand + 1 (byte-wise addition with carry)
48
+ * return rand
49
+ *
50
+ * The state is MUTATED in place after each call.
51
+ */
52
+ export declare class GMSSRandom {
53
+ /**
54
+ * Generate next random value and update state
55
+ *
56
+ * @param state - 32-byte state array (MUTATED in place)
57
+ * @returns 32-byte random value
58
+ */
59
+ static nextSeed(state: Uint8Array): Uint8Array;
60
+ /**
61
+ * Add two byte arrays: a = a + b (little-endian, with carry)
62
+ * Matches GMSSRandom.addByteArrays()
63
+ */
64
+ private static addByteArrays;
65
+ /**
66
+ * Add one to byte array: a = a + 1 (little-endian, with carry)
67
+ * Matches GMSSRandom.addOne()
68
+ */
69
+ private static addOne;
70
+ }
71
+ /**
72
+ * getLog - Calculate ceil(log2(x))
73
+ *
74
+ * Matches WinternitzOTSignature.getLog():
75
+ * int log = 1;
76
+ * int i = 2;
77
+ * while (i < intValue) { i <<= 1; log++; }
78
+ * return log;
79
+ */
80
+ export declare function getLog(intValue: number): number;
81
+ /**
82
+ * Expand master seed into L private key chains using GMSSRandom
83
+ *
84
+ * Matches WinternitzOTSignature constructor:
85
+ * byte[] dummy = new byte[mdsize];
86
+ * System.arraycopy(seed0, 0, dummy, 0, dummy.length);
87
+ * for (int i = 0; i < keysize; i++) {
88
+ * privateKeyOTS[i] = gmssRandom.nextSeed(dummy);
89
+ * }
90
+ */
91
+ export declare function expandPrivateKey(seed: Uint8Array, ps?: ParamSet): Uint8Array[];
92
+ /**
93
+ * Convert message hash to Winternitz digits with checksum
94
+ *
95
+ * For w=8 (8 bits per digit), since 8 % 8 == 0:
96
+ * - Each byte of the hash IS one digit (0-255)
97
+ * - messagesize = 32 digits
98
+ * - checksum = (messagesize << w) - sum = 8192 - sum
99
+ * - checksumsize = 14 bits, extracted as 2 digits
100
+ *
101
+ * Matches WinternitzOTSignature.getSignature() for w=8 case
102
+ */
103
+ export declare function toWinternitzDigits(hash32: Uint8Array, ps?: ParamSet): {
104
+ digits: number[];
105
+ checksumDigits: number[];
106
+ total: number;
107
+ };
108
+ /**
109
+ * Decompose digest into base-w digits + checksum
110
+ * Returns flat array of all L digits
111
+ */
112
+ export declare function baseWWithChecksum(msgHash: Uint8Array, paramSet?: ParamSet): number[];
113
+ /**
114
+ * Hash a value k times
115
+ *
116
+ * Matches WinternitzOTSignature.hashPrivateKeyBlock():
117
+ * - If rounds < 1: return input unchanged
118
+ * - Otherwise: hash `rounds` times
119
+ */
120
+ export declare function hashChain(x: Uint8Array, rounds: number): Uint8Array;
121
+ export type WotsKeypair = {
122
+ seed: Uint8Array;
123
+ index: number;
124
+ pk: Uint8Array;
125
+ };
126
+ /**
127
+ * Derive unique seed for a specific key index
128
+ *
129
+ * DEPRECATED: Use deriveChainSeedJava for Java parity.
130
+ * This function is preserved for backward compatibility only.
131
+ *
132
+ * The correct derivation matching Minima's TreeKeyNode.java:
133
+ * MiniData seed = Crypto.getInstance().hashAllObjects(new MiniNumber(i), zPrivateSeed);
134
+ *
135
+ * This old implementation used H(seed || u32be(i)) which is NOT Java compatible.
136
+ * @deprecated Use deriveChainSeedJava from javaStreamables.ts
137
+ */
138
+ export declare function deriveIndexedSeed(seed: Uint8Array, keyIndex: number): Uint8Array;
139
+ /**
140
+ * Derive WOTS public key digest from seed and key index
141
+ *
142
+ * The key index is mixed into the seed first (deriveIndexedSeed),
143
+ * then expanded using GMSSRandom to get unique chain seeds.
144
+ *
145
+ * Matches WinternitzOTSignature.getPublicKey():
146
+ * int rounds = (1 << w) - 1; // 255 for w=8
147
+ * for (int i = 0; i < keysize; i++) {
148
+ * hashPrivateKeyBlock(i, rounds, buf, pos);
149
+ * }
150
+ * return H(buf);
151
+ */
152
+ export declare function derivePKdigest(seed: Uint8Array, i: number, ps?: ParamSet): Uint8Array;
153
+ /**
154
+ * Derive full WOTS public key (L × 32 bytes) without final hash
155
+ *
156
+ * Returns the concatenation of all chain tops BEFORE hashing.
157
+ * Required by Minima's SignatureProof which expects the full public key.
158
+ */
159
+ export declare function deriveFullPublicKey(seed: Uint8Array, i: number, ps?: ParamSet): Uint8Array;
160
+ export declare function wotsKeypairFromSeed(seed: Uint8Array, index: number, paramSet?: ParamSet): WotsKeypair;
161
+ export type WotsSignature = {
162
+ index: number;
163
+ w: number;
164
+ sig: Uint8Array[];
165
+ };
166
+ /**
167
+ * Sign a message using WOTS
168
+ *
169
+ * Matches Java's WinternitzOTSignature.getSignature():
170
+ * 1. Hash the message internally: hashedMsg = SHA3-256(message)
171
+ * 2. For each digit d[i]: sig[i] = hash(privateKey[i], d[i] times)
172
+ *
173
+ * IMPORTANT: This function hashes the message internally to match Java/BouncyCastle.
174
+ * Callers pass RAW 32-byte data (tx digest, child root), NOT pre-hashed!
175
+ *
176
+ * @param seed - 32-byte master seed
177
+ * @param i - Key index
178
+ * @param message - Raw 32-byte message (will be hashed internally)
179
+ * @param ps - WOTS parameter set (default: minima)
180
+ * @returns Flat signature (L × 32 = 1088 bytes)
181
+ */
182
+ export declare function wotsSign(seed: Uint8Array, i: number, message: Uint8Array, ps?: ParamSet): Uint8Array;
183
+ /**
184
+ * Legacy wrapper returning structured signature
185
+ */
186
+ export declare function wotsSignLegacy(msgHash: Uint8Array, seed: Uint8Array, index: number, paramSet?: ParamSet): WotsSignature;
187
+ /**
188
+ * Recover public key from signature
189
+ *
190
+ * Matches WinternitzOTSVerify.Verify():
191
+ * 1. Hash the message: hashedMsg = SHA3-256(message)
192
+ * 2. for each digit d[i]:
193
+ * top[i] = hash(sig[i], (255 - d[i]) times)
194
+ * 3. return H(concat(tops))
195
+ *
196
+ * CRITICAL: This function hashes the message internally to match Java/BouncyCastle
197
+ * Winternitz behavior. Both wotsSign and wotsPkFromSig hash internally for parity.
198
+ */
199
+ export declare function wotsPkFromSig(message: Uint8Array, signature: WotsSignature, paramSet?: ParamSet): Uint8Array;
200
+ /**
201
+ * Verify WOTS signature against a FULL 1088-byte public key
202
+ *
203
+ * Matches Java's Winternitz.verify():
204
+ * 1. Hash the message internally: hashedMsg = SHA3-256(message)
205
+ * 2. Recover FULL public key from signature using hashedMsg (1088 bytes)
206
+ * 3. Compare FULL reconstructed key to expected FULL public key
207
+ *
208
+ * CRITICAL: Java's Winternitz.verify() compares the FULL 1088-byte reconstructed
209
+ * public key against the FULL 1088-byte stored public key, NOT a 32-byte digest!
210
+ *
211
+ * IMPORTANT: This function hashes the message internally to match Java/BouncyCastle.
212
+ * Callers pass RAW 32-byte data (tx digest, child root), NOT pre-hashed!
213
+ *
214
+ * @param sig - The 1088-byte WOTS signature
215
+ * @param message - Raw 32-byte message (will be hashed internally)
216
+ * @param pkFull - The FULL 1088-byte WOTS public key (L=34 × 32 bytes)
217
+ * @param ps - WOTS parameter set (default: minima)
218
+ */
219
+ export declare function wotsVerify(sig: Uint8Array, message: Uint8Array, pkFull: Uint8Array, ps?: ParamSet): boolean;
220
+ /**
221
+ * Legacy verify function that accepts a 32-byte digest
222
+ * @deprecated Use wotsVerify with full 1088-byte public key instead
223
+ */
224
+ export declare function wotsVerifyDigest(sig: Uint8Array, message: Uint8Array, pkDigest: Uint8Array, ps?: ParamSet): boolean;
225
+ /**
226
+ * Generate WOTS public key from seed (convenience wrapper)
227
+ */
228
+ export declare function wotsPublicKeyFromSeed(seed: Uint8Array, index?: number, paramSet?: ParamSet): Uint8Array;
229
+ /**
230
+ * @deprecated Use expandPrivateKey instead
231
+ */
232
+ export declare function prfChainSeed(seed: Uint8Array, i: number, j: number, _paramSet: ParamSet): Uint8Array;