@totemsdk/core 1.0.8 → 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 +35 -6
- package/src/Streamable.d.ts +0 -265
- package/src/Streamable.js +0 -431
- 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 -328
- 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 -159
- package/src/mmr.js +0 -352
- 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.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,289 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WOTS Parity Test
|
|
3
|
-
*
|
|
4
|
-
* This test file enables debug logging and runs WOTS operations with a known seed
|
|
5
|
-
* to capture all intermediate values for comparison with Java reference implementation.
|
|
6
|
-
*
|
|
7
|
-
* Usage:
|
|
8
|
-
* npx jest packages/totem-sdk/packages/core/src/__tests__/wots-parity.test.ts --verbose
|
|
9
|
-
*
|
|
10
|
-
* The debug output can be compared against Java's TreeKeyNode and Winternitz output
|
|
11
|
-
* to identify exactly where the implementations diverge.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { sha3_256 } from '@noble/hashes/sha3.js';
|
|
15
|
-
import {
|
|
16
|
-
setWotsLogger,
|
|
17
|
-
disableWotsLogger,
|
|
18
|
-
wotsSign,
|
|
19
|
-
deriveFullPublicKey,
|
|
20
|
-
wotsVerify,
|
|
21
|
-
expandPrivateKey,
|
|
22
|
-
toWinternitzDigits,
|
|
23
|
-
deriveIndexedSeed,
|
|
24
|
-
GMSSRandom,
|
|
25
|
-
hex,
|
|
26
|
-
fromHex,
|
|
27
|
-
F,
|
|
28
|
-
hashChain
|
|
29
|
-
} from '../wots';
|
|
30
|
-
import {
|
|
31
|
-
setTreeKeyLogger,
|
|
32
|
-
disableTreeKeyLogger,
|
|
33
|
-
TreeKey,
|
|
34
|
-
TreeKeyNode
|
|
35
|
-
} from '../treekey';
|
|
36
|
-
import { getParamSet } from '../params';
|
|
37
|
-
import { ConsoleLogger } from '../adapters';
|
|
38
|
-
|
|
39
|
-
describe('WOTS Parity Tests', () => {
|
|
40
|
-
const consoleLogger = new ConsoleLogger('[WOTS-PARITY]');
|
|
41
|
-
|
|
42
|
-
beforeEach(() => {
|
|
43
|
-
setWotsLogger(consoleLogger);
|
|
44
|
-
setTreeKeyLogger(consoleLogger);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
afterEach(() => {
|
|
48
|
-
disableWotsLogger();
|
|
49
|
-
disableTreeKeyLogger();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should verify hashChain rounds=0 returns input unchanged', () => {
|
|
53
|
-
console.log('\n========== hashChain Boundary Test ==========');
|
|
54
|
-
|
|
55
|
-
const ps = getParamSet();
|
|
56
|
-
const seed = new Uint8Array(32);
|
|
57
|
-
seed[0] = 0xAA;
|
|
58
|
-
|
|
59
|
-
const privateKeys = expandPrivateKey(seed, ps);
|
|
60
|
-
const pk0 = privateKeys[0];
|
|
61
|
-
|
|
62
|
-
const result0 = hashChain(pk0, 0);
|
|
63
|
-
const result1 = hashChain(pk0, 1);
|
|
64
|
-
const result2 = hashChain(pk0, 2);
|
|
65
|
-
const result255 = hashChain(pk0, 255);
|
|
66
|
-
|
|
67
|
-
console.log(`pk0: ${hex(pk0).substring(0, 32)}...`);
|
|
68
|
-
console.log(`hashChain(pk0, 0): ${hex(result0).substring(0, 32)}...`);
|
|
69
|
-
console.log(`hashChain(pk0, 1): ${hex(result1).substring(0, 32)}...`);
|
|
70
|
-
console.log(`hashChain(pk0, 2): ${hex(result2).substring(0, 32)}...`);
|
|
71
|
-
console.log(`hashChain(pk0, 255): ${hex(result255).substring(0, 32)}...`);
|
|
72
|
-
|
|
73
|
-
expect(Buffer.from(result0).equals(Buffer.from(pk0))).toBe(true);
|
|
74
|
-
expect(Buffer.from(result1).equals(Buffer.from(result0))).toBe(false);
|
|
75
|
-
expect(Buffer.from(F(pk0)).equals(Buffer.from(result1))).toBe(true);
|
|
76
|
-
expect(Buffer.from(F(F(pk0))).equals(Buffer.from(result2))).toBe(true);
|
|
77
|
-
|
|
78
|
-
console.log('hashChain rounds=0 returns unchanged: PASS');
|
|
79
|
-
console.log('hashChain rounds=1 = F(x): PASS');
|
|
80
|
-
console.log('hashChain rounds=2 = F(F(x)): PASS');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should dump GMSSRandom intermediate values', () => {
|
|
84
|
-
console.log('\n========== GMSSRandom Test ==========');
|
|
85
|
-
|
|
86
|
-
const seed = new Uint8Array(32);
|
|
87
|
-
seed[0] = 0x01; // Simple test seed
|
|
88
|
-
|
|
89
|
-
console.log(`Initial seed: ${hex(seed)}`);
|
|
90
|
-
|
|
91
|
-
const state = new Uint8Array(seed);
|
|
92
|
-
|
|
93
|
-
for (let i = 0; i < 5; i++) {
|
|
94
|
-
const rand = GMSSRandom.nextSeed(state);
|
|
95
|
-
console.log(`GMSSRandom.nextSeed[${i}]:`);
|
|
96
|
-
console.log(` rand: ${hex(rand)}`);
|
|
97
|
-
console.log(` state after: ${hex(state)}`);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should dump deriveIndexedSeed intermediate values', () => {
|
|
102
|
-
console.log('\n========== deriveIndexedSeed Test ==========');
|
|
103
|
-
|
|
104
|
-
const seed = new Uint8Array(32);
|
|
105
|
-
seed[0] = 0x01;
|
|
106
|
-
|
|
107
|
-
console.log(`Master seed: ${hex(seed)}`);
|
|
108
|
-
|
|
109
|
-
for (let i = 0; i < 3; i++) {
|
|
110
|
-
const indexed = deriveIndexedSeed(seed, i);
|
|
111
|
-
console.log(`deriveIndexedSeed(seed, ${i}): ${hex(indexed)}`);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('should dump expandPrivateKey for a known seed', () => {
|
|
116
|
-
console.log('\n========== expandPrivateKey Test ==========');
|
|
117
|
-
|
|
118
|
-
const ps = getParamSet();
|
|
119
|
-
const seed = new Uint8Array(32);
|
|
120
|
-
seed[0] = 0xAB;
|
|
121
|
-
seed[1] = 0xCD;
|
|
122
|
-
|
|
123
|
-
console.log(`Seed: ${hex(seed)}`);
|
|
124
|
-
console.log(`L=${ps.L}, w=${ps.w}`);
|
|
125
|
-
|
|
126
|
-
const privateKeys = expandPrivateKey(seed, ps);
|
|
127
|
-
|
|
128
|
-
console.log(`Generated ${privateKeys.length} private keys`);
|
|
129
|
-
for (let i = 0; i < 5; i++) {
|
|
130
|
-
console.log(`pk[${i}]: ${hex(privateKeys[i])}`);
|
|
131
|
-
}
|
|
132
|
-
console.log(`...`);
|
|
133
|
-
console.log(`pk[${ps.L - 1}]: ${hex(privateKeys[ps.L - 1])}`);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should dump toWinternitzDigits for a known hash', () => {
|
|
137
|
-
console.log('\n========== toWinternitzDigits Test ==========');
|
|
138
|
-
|
|
139
|
-
const ps = getParamSet();
|
|
140
|
-
const hash = sha3_256(new Uint8Array([0x01, 0x02, 0x03]));
|
|
141
|
-
|
|
142
|
-
console.log(`Input hash: ${hex(hash)}`);
|
|
143
|
-
|
|
144
|
-
const { digits, checksumDigits, total } = toWinternitzDigits(hash, ps);
|
|
145
|
-
|
|
146
|
-
console.log(`Message digits (${digits.length}): [${digits.join(', ')}]`);
|
|
147
|
-
console.log(`Checksum digits (${checksumDigits.length}): [${checksumDigits.join(', ')}]`);
|
|
148
|
-
console.log(`Total: ${total} (expected L=${ps.L})`);
|
|
149
|
-
|
|
150
|
-
const sum = digits.reduce((a, b) => a + b, 0);
|
|
151
|
-
console.log(`Digit sum: ${sum}`);
|
|
152
|
-
console.log(`Checksum = (32 << 8) - ${sum} = ${(32 << 8) - sum}`);
|
|
153
|
-
console.log(`Checksum digit[0] = ${checksumDigits[0]} (low 8 bits)`);
|
|
154
|
-
console.log(`Checksum digit[1] = ${checksumDigits[1]} (next 8 bits)`);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should dump full WOTS sign/verify cycle', () => {
|
|
158
|
-
console.log('\n========== WOTS Sign/Verify Test ==========');
|
|
159
|
-
|
|
160
|
-
const ps = getParamSet();
|
|
161
|
-
const seed = new Uint8Array(32);
|
|
162
|
-
seed[0] = 0xDE;
|
|
163
|
-
seed[1] = 0xAD;
|
|
164
|
-
seed[2] = 0xBE;
|
|
165
|
-
seed[3] = 0xEF;
|
|
166
|
-
const keyIndex = 0;
|
|
167
|
-
// CRITICAL: Both wotsSign and wotsVerify hash internally to match Java/BouncyCastle
|
|
168
|
-
// Use 32-byte raw message (like a tx digest) - both functions will hash it internally
|
|
169
|
-
const rawMessage = new Uint8Array(32).fill(0xab);
|
|
170
|
-
rawMessage[0] = 0x01;
|
|
171
|
-
rawMessage[1] = 0x02;
|
|
172
|
-
rawMessage[2] = 0x03;
|
|
173
|
-
rawMessage[3] = 0x04;
|
|
174
|
-
|
|
175
|
-
console.log(`Seed: ${hex(seed)}`);
|
|
176
|
-
console.log(`Key index: ${keyIndex}`);
|
|
177
|
-
console.log(`Message (raw 32B): ${hex(rawMessage)}`);
|
|
178
|
-
|
|
179
|
-
console.log('\n--- Deriving public key ---');
|
|
180
|
-
const pkFull = deriveFullPublicKey(seed, keyIndex, ps);
|
|
181
|
-
console.log(`Full public key (${pkFull.length}B): ${hex(pkFull).substring(0, 128)}...`);
|
|
182
|
-
|
|
183
|
-
console.log('\n--- Signing ---');
|
|
184
|
-
// Pass raw message - wotsSign hashes internally
|
|
185
|
-
const signature = wotsSign(seed, keyIndex, rawMessage, ps);
|
|
186
|
-
console.log(`Signature (${signature.length}B): ${hex(signature).substring(0, 128)}...`);
|
|
187
|
-
|
|
188
|
-
console.log('\n--- Verifying locally ---');
|
|
189
|
-
// Pass same raw message - wotsVerify hashes internally
|
|
190
|
-
const valid = wotsVerify(signature, rawMessage, pkFull, ps);
|
|
191
|
-
console.log(`Local verification result: ${valid}`);
|
|
192
|
-
|
|
193
|
-
expect(pkFull.length).toBe(1088);
|
|
194
|
-
expect(signature.length).toBe(1088);
|
|
195
|
-
expect(valid).toBe(true);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it('should dump TreeKeyNode initialization', () => {
|
|
199
|
-
console.log('\n========== TreeKeyNode Init Test ==========');
|
|
200
|
-
|
|
201
|
-
const seed = new Uint8Array(32);
|
|
202
|
-
seed[0] = 0x11;
|
|
203
|
-
seed[1] = 0x22;
|
|
204
|
-
seed[2] = 0x33;
|
|
205
|
-
|
|
206
|
-
console.log(`Seed: ${hex(seed)}`);
|
|
207
|
-
|
|
208
|
-
const node = new TreeKeyNode(seed, 64);
|
|
209
|
-
|
|
210
|
-
console.log(`Root public key: ${hex(node.getPublicKey())}`);
|
|
211
|
-
console.log(`WOTS key[0] full: ${hex(node.getWOTSPublicKey(0)).substring(0, 64)}...`);
|
|
212
|
-
console.log(`WOTS key[0] digest: ${hex(node.getWOTSPublicKeyDigest(0))}`);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it('should dump TreeKeyNode sign operation', () => {
|
|
216
|
-
console.log('\n========== TreeKeyNode Sign Test ==========');
|
|
217
|
-
|
|
218
|
-
const seed = new Uint8Array(32);
|
|
219
|
-
seed[0] = 0x44;
|
|
220
|
-
seed[1] = 0x55;
|
|
221
|
-
seed[2] = 0x66;
|
|
222
|
-
|
|
223
|
-
const node = new TreeKeyNode(seed, 64);
|
|
224
|
-
const data = new Uint8Array([0xAA, 0xBB, 0xCC, 0xDD]);
|
|
225
|
-
|
|
226
|
-
console.log(`Seed: ${hex(seed)}`);
|
|
227
|
-
console.log(`Data to sign: ${hex(data)}`);
|
|
228
|
-
console.log(`Data hash: ${hex(sha3_256(data))}`);
|
|
229
|
-
|
|
230
|
-
const sigProof = node.sign(0, data);
|
|
231
|
-
|
|
232
|
-
console.log(`Leaf pubkey (${sigProof.leafPubkey.length}B): ${hex(sigProof.leafPubkey)}`);
|
|
233
|
-
console.log(`Signature (${sigProof.signature.length}B): ${hex(sigProof.signature).substring(0, 64)}...`);
|
|
234
|
-
console.log(`MMR proof chunks: ${sigProof.mmrProof.chunks.length}`);
|
|
235
|
-
|
|
236
|
-
// CRITICAL FIX (January 2026): leafPubkey is 32-byte DIGEST, not 1088-byte full key
|
|
237
|
-
expect(sigProof.leafPubkey.length).toBe(32); // SHA3-256 digest
|
|
238
|
-
expect(sigProof.signature.length).toBe(1088); // Signature is still 1088 bytes
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
it('should verify local signature matches public key', () => {
|
|
242
|
-
console.log('\n========== Local Verification Consistency Test ==========');
|
|
243
|
-
|
|
244
|
-
const ps = getParamSet();
|
|
245
|
-
const seed = new Uint8Array(32);
|
|
246
|
-
seed[0] = 0x77;
|
|
247
|
-
seed[1] = 0x88;
|
|
248
|
-
|
|
249
|
-
const node = new TreeKeyNode(seed, 64);
|
|
250
|
-
const data = new Uint8Array([0x01, 0x02, 0x03]);
|
|
251
|
-
|
|
252
|
-
const sigProof = node.sign(0, data);
|
|
253
|
-
|
|
254
|
-
const valid = wotsVerify(sigProof.signature, data, sigProof.leafPubkey, ps);
|
|
255
|
-
console.log(`Stored public key matches signature verification: ${valid}`);
|
|
256
|
-
|
|
257
|
-
expect(valid).toBe(true);
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
it('should dump full TreeKey hierarchy signing', () => {
|
|
261
|
-
console.log('\n========== TreeKey Hierarchy Sign Test ==========');
|
|
262
|
-
|
|
263
|
-
const seed = new Uint8Array(32);
|
|
264
|
-
seed[0] = 0x99;
|
|
265
|
-
seed[1] = 0xAA;
|
|
266
|
-
seed[2] = 0xBB;
|
|
267
|
-
|
|
268
|
-
console.log(`Root seed: ${hex(seed)}`);
|
|
269
|
-
|
|
270
|
-
const treeKey = new TreeKey(seed, 64, 3);
|
|
271
|
-
|
|
272
|
-
console.log(`TreeKey public key: ${hex(treeKey.getPublicKey())}`);
|
|
273
|
-
|
|
274
|
-
const data = new Uint8Array([0x12, 0x34, 0x56, 0x78]);
|
|
275
|
-
console.log(`Data to sign: ${hex(data)}`);
|
|
276
|
-
|
|
277
|
-
treeKey.setUses(0);
|
|
278
|
-
const multiSig = treeKey.sign(data);
|
|
279
|
-
|
|
280
|
-
console.log(`Multi-signature has ${multiSig.proofs.length} levels`);
|
|
281
|
-
for (let i = 0; i < multiSig.proofs.length; i++) {
|
|
282
|
-
const p = multiSig.proofs[i];
|
|
283
|
-
console.log(`Level ${i}:`);
|
|
284
|
-
console.log(` leafPubkey (${p.leafPubkey.length}B): ${hex(p.leafPubkey).substring(0, 32)}...`);
|
|
285
|
-
console.log(` signature (${p.signature.length}B): ${hex(p.signature).substring(0, 32)}...`);
|
|
286
|
-
console.log(` mmrProof chunks: ${p.mmrProof.chunks.length}`);
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
});
|
package/src/adapters/index.d.ts
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @totemsdk/core/adapters
|
|
3
|
-
* Platform-agnostic adapter interfaces for SDK portability
|
|
4
|
-
*
|
|
5
|
-
* These interfaces enable the SDK to work across multiple platforms:
|
|
6
|
-
* - Browser (Chrome Extensions, web apps)
|
|
7
|
-
* - Node.js (server-side, CLI tools)
|
|
8
|
-
* - React Native (mobile apps) - follow-up
|
|
9
|
-
*
|
|
10
|
-
* Each platform provides its own implementation of these adapters.
|
|
11
|
-
*/
|
|
12
|
-
export interface StorageAdapter {
|
|
13
|
-
get<T>(key: string): Promise<T | null>;
|
|
14
|
-
set<T>(key: string, value: T): Promise<void>;
|
|
15
|
-
remove(key: string): Promise<boolean>;
|
|
16
|
-
clear(): Promise<void>;
|
|
17
|
-
keys(): Promise<string[]>;
|
|
18
|
-
has(key: string): Promise<boolean>;
|
|
19
|
-
}
|
|
20
|
-
export interface AuthTokenProvider {
|
|
21
|
-
getToken(): Promise<string | null>;
|
|
22
|
-
setToken(token: string): Promise<void>;
|
|
23
|
-
clearToken(): Promise<void>;
|
|
24
|
-
onTokenChange(callback: (token: string | null) => void): () => void;
|
|
25
|
-
isAuthenticated(): Promise<boolean>;
|
|
26
|
-
}
|
|
27
|
-
export type BinaryData = Uint8Array | ArrayBuffer;
|
|
28
|
-
export interface WebSocketFactoryOptions {
|
|
29
|
-
pingIntervalMs?: number;
|
|
30
|
-
pongTimeoutMs?: number;
|
|
31
|
-
maxPayloadBytes?: number;
|
|
32
|
-
}
|
|
33
|
-
export interface WebSocketOpenEvent {
|
|
34
|
-
type: 'open';
|
|
35
|
-
}
|
|
36
|
-
export interface WebSocketCloseEvent {
|
|
37
|
-
type: 'close';
|
|
38
|
-
code: number;
|
|
39
|
-
reason: string;
|
|
40
|
-
wasClean: boolean;
|
|
41
|
-
}
|
|
42
|
-
export interface WebSocketMessageEvent {
|
|
43
|
-
type: 'message';
|
|
44
|
-
data: string | BinaryData;
|
|
45
|
-
}
|
|
46
|
-
export interface WebSocketErrorEvent {
|
|
47
|
-
type: 'error';
|
|
48
|
-
message?: string;
|
|
49
|
-
error?: Error;
|
|
50
|
-
}
|
|
51
|
-
export type WebSocketEventMap = {
|
|
52
|
-
open: WebSocketOpenEvent;
|
|
53
|
-
close: WebSocketCloseEvent;
|
|
54
|
-
message: WebSocketMessageEvent;
|
|
55
|
-
error: WebSocketErrorEvent;
|
|
56
|
-
};
|
|
57
|
-
export interface WebSocketFactory {
|
|
58
|
-
create(url: string, protocols?: string[], options?: WebSocketFactoryOptions): WebSocketClient;
|
|
59
|
-
dispose(): void;
|
|
60
|
-
}
|
|
61
|
-
export interface WebSocketClient {
|
|
62
|
-
readonly readyState: number;
|
|
63
|
-
readonly url: string;
|
|
64
|
-
send(data: string | BinaryData): void;
|
|
65
|
-
close(code?: number, reason?: string): void;
|
|
66
|
-
terminate(): void;
|
|
67
|
-
addEventListener<K extends keyof WebSocketEventMap>(event: K, listener: (ev: WebSocketEventMap[K]) => void): void;
|
|
68
|
-
removeEventListener<K extends keyof WebSocketEventMap>(event: K, listener: (ev: WebSocketEventMap[K]) => void): void;
|
|
69
|
-
removeAllListeners(): void;
|
|
70
|
-
onopen: ((ev: WebSocketOpenEvent) => void) | null;
|
|
71
|
-
onclose: ((ev: WebSocketCloseEvent) => void) | null;
|
|
72
|
-
onmessage: ((ev: WebSocketMessageEvent) => void) | null;
|
|
73
|
-
onerror: ((ev: WebSocketErrorEvent) => void) | null;
|
|
74
|
-
}
|
|
75
|
-
export declare const WebSocketReadyState: {
|
|
76
|
-
readonly CONNECTING: 0;
|
|
77
|
-
readonly OPEN: 1;
|
|
78
|
-
readonly CLOSING: 2;
|
|
79
|
-
readonly CLOSED: 3;
|
|
80
|
-
};
|
|
81
|
-
export interface CancellationToken {
|
|
82
|
-
readonly cancelled: boolean;
|
|
83
|
-
onCancel(callback: () => void): () => void;
|
|
84
|
-
}
|
|
85
|
-
export interface CancellationTokenSource {
|
|
86
|
-
readonly token: CancellationToken;
|
|
87
|
-
cancel(): void;
|
|
88
|
-
}
|
|
89
|
-
export declare function createCancellationToken(): CancellationTokenSource;
|
|
90
|
-
export interface HttpClient {
|
|
91
|
-
get<T>(url: string, options?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
92
|
-
post<T>(url: string, body?: unknown, options?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
93
|
-
put<T>(url: string, body?: unknown, options?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
94
|
-
delete<T>(url: string, options?: HttpRequestOptions): Promise<HttpResponse<T>>;
|
|
95
|
-
}
|
|
96
|
-
export interface HttpRequestOptions {
|
|
97
|
-
headers?: Record<string, string>;
|
|
98
|
-
timeout?: number;
|
|
99
|
-
cancellationToken?: CancellationToken;
|
|
100
|
-
}
|
|
101
|
-
export interface HttpResponse<T> {
|
|
102
|
-
ok: boolean;
|
|
103
|
-
status: number;
|
|
104
|
-
statusText: string;
|
|
105
|
-
headers: Record<string, string>;
|
|
106
|
-
data: T;
|
|
107
|
-
}
|
|
108
|
-
export interface ConfigProvider {
|
|
109
|
-
get<T>(key: string): T | undefined;
|
|
110
|
-
get<T>(key: string, defaultValue: T): T;
|
|
111
|
-
set<T>(key: string, value: T): void;
|
|
112
|
-
has(key: string): boolean;
|
|
113
|
-
getAll(): Record<string, unknown>;
|
|
114
|
-
readonly apiUrl: string;
|
|
115
|
-
readonly wsUrl: string;
|
|
116
|
-
readonly network: 'mainnet' | 'testnet' | 'devnet';
|
|
117
|
-
readonly apiKey?: string;
|
|
118
|
-
}
|
|
119
|
-
export interface LoggerAdapter {
|
|
120
|
-
debug(message: string, ...args: unknown[]): void;
|
|
121
|
-
info(message: string, ...args: unknown[]): void;
|
|
122
|
-
warn(message: string, ...args: unknown[]): void;
|
|
123
|
-
error(message: string, ...args: unknown[]): void;
|
|
124
|
-
}
|
|
125
|
-
export interface MetricsAdapter {
|
|
126
|
-
increment(name: string, value?: number, tags?: Record<string, string>): void;
|
|
127
|
-
gauge(name: string, value: number, tags?: Record<string, string>): void;
|
|
128
|
-
timing(name: string, durationMs: number, tags?: Record<string, string>): void;
|
|
129
|
-
histogram(name: string, value: number, tags?: Record<string, string>): void;
|
|
130
|
-
}
|
|
131
|
-
export interface TimerAdapter {
|
|
132
|
-
setTimeout(callback: () => void, ms: number): TimerHandle;
|
|
133
|
-
setInterval(callback: () => void, ms: number): TimerHandle;
|
|
134
|
-
clearTimeout(handle: TimerHandle): void;
|
|
135
|
-
clearInterval(handle: TimerHandle): void;
|
|
136
|
-
now(): number;
|
|
137
|
-
}
|
|
138
|
-
export type TimerHandle = ReturnType<typeof setTimeout>;
|
|
139
|
-
export interface CryptoAdapter {
|
|
140
|
-
randomBytes(length: number): Uint8Array;
|
|
141
|
-
sha256(data: Uint8Array): Uint8Array;
|
|
142
|
-
sha256Async(data: Uint8Array): Promise<Uint8Array>;
|
|
143
|
-
}
|
|
144
|
-
export interface AdapterRegistry {
|
|
145
|
-
storage: StorageAdapter;
|
|
146
|
-
auth: AuthTokenProvider;
|
|
147
|
-
websocket: WebSocketFactory;
|
|
148
|
-
http: HttpClient;
|
|
149
|
-
config: ConfigProvider;
|
|
150
|
-
logger: LoggerAdapter;
|
|
151
|
-
metrics?: MetricsAdapter;
|
|
152
|
-
timer: TimerAdapter;
|
|
153
|
-
crypto: CryptoAdapter;
|
|
154
|
-
}
|
|
155
|
-
export declare function createAdapterRegistry(adapters: Partial<AdapterRegistry>): AdapterRegistry;
|
|
156
|
-
export declare class NoopLogger implements LoggerAdapter {
|
|
157
|
-
debug(_message: string, ..._args: unknown[]): void;
|
|
158
|
-
info(_message: string, ..._args: unknown[]): void;
|
|
159
|
-
warn(_message: string, ..._args: unknown[]): void;
|
|
160
|
-
error(_message: string, ..._args: unknown[]): void;
|
|
161
|
-
}
|
|
162
|
-
export declare class ConsoleLogger implements LoggerAdapter {
|
|
163
|
-
private prefix;
|
|
164
|
-
constructor(prefix?: string);
|
|
165
|
-
debug(message: string, ...args: unknown[]): void;
|
|
166
|
-
info(message: string, ...args: unknown[]): void;
|
|
167
|
-
warn(message: string, ...args: unknown[]): void;
|
|
168
|
-
error(message: string, ...args: unknown[]): void;
|
|
169
|
-
}
|
|
170
|
-
export declare class NoopMetrics implements MetricsAdapter {
|
|
171
|
-
increment(_name: string, _value?: number, _tags?: Record<string, string>): void;
|
|
172
|
-
gauge(_name: string, _value: number, _tags?: Record<string, string>): void;
|
|
173
|
-
timing(_name: string, _durationMs: number, _tags?: Record<string, string>): void;
|
|
174
|
-
histogram(_name: string, _value: number, _tags?: Record<string, string>): void;
|
|
175
|
-
}
|
|
176
|
-
export declare class DefaultTimerAdapter implements TimerAdapter {
|
|
177
|
-
setTimeout(callback: () => void, ms: number): TimerHandle;
|
|
178
|
-
setInterval(callback: () => void, ms: number): TimerHandle;
|
|
179
|
-
clearTimeout(handle: TimerHandle): void;
|
|
180
|
-
clearInterval(handle: TimerHandle): void;
|
|
181
|
-
now(): number;
|
|
182
|
-
}
|
|
183
|
-
export interface LifecycleAdapter {
|
|
184
|
-
onSuspend(callback: () => void): () => void;
|
|
185
|
-
onResume?(callback: () => void): () => void;
|
|
186
|
-
}
|
|
187
|
-
export declare class NoopLifecycleAdapter implements LifecycleAdapter {
|
|
188
|
-
onSuspend(_callback: () => void): () => void;
|
|
189
|
-
onResume(_callback: () => void): () => void;
|
|
190
|
-
}
|
package/src/adapters/index.js
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @module @totemsdk/core/adapters
|
|
4
|
-
* Platform-agnostic adapter interfaces for SDK portability
|
|
5
|
-
*
|
|
6
|
-
* These interfaces enable the SDK to work across multiple platforms:
|
|
7
|
-
* - Browser (Chrome Extensions, web apps)
|
|
8
|
-
* - Node.js (server-side, CLI tools)
|
|
9
|
-
* - React Native (mobile apps) - follow-up
|
|
10
|
-
*
|
|
11
|
-
* Each platform provides its own implementation of these adapters.
|
|
12
|
-
*/
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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 = {
|
|
18
|
-
CONNECTING: 0,
|
|
19
|
-
OPEN: 1,
|
|
20
|
-
CLOSING: 2,
|
|
21
|
-
CLOSED: 3,
|
|
22
|
-
};
|
|
23
|
-
function createCancellationToken() {
|
|
24
|
-
let cancelled = false;
|
|
25
|
-
const callbacks = new Set();
|
|
26
|
-
const token = {
|
|
27
|
-
get cancelled() { return cancelled; },
|
|
28
|
-
onCancel(callback) {
|
|
29
|
-
if (cancelled) {
|
|
30
|
-
callback();
|
|
31
|
-
return () => { };
|
|
32
|
-
}
|
|
33
|
-
callbacks.add(callback);
|
|
34
|
-
return () => callbacks.delete(callback);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
return {
|
|
38
|
-
token,
|
|
39
|
-
cancel() {
|
|
40
|
-
if (!cancelled) {
|
|
41
|
-
cancelled = true;
|
|
42
|
-
callbacks.forEach(cb => cb());
|
|
43
|
-
callbacks.clear();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
function createAdapterRegistry(adapters) {
|
|
49
|
-
const required = [
|
|
50
|
-
'storage', 'auth', 'websocket', 'http', 'config', 'logger', 'timer', 'crypto'
|
|
51
|
-
];
|
|
52
|
-
for (const key of required) {
|
|
53
|
-
if (!adapters[key]) {
|
|
54
|
-
throw new Error(`Missing required adapter: ${key}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return adapters;
|
|
58
|
-
}
|
|
59
|
-
class NoopLogger {
|
|
60
|
-
debug(_message, ..._args) { }
|
|
61
|
-
info(_message, ..._args) { }
|
|
62
|
-
warn(_message, ..._args) { }
|
|
63
|
-
error(_message, ..._args) { }
|
|
64
|
-
}
|
|
65
|
-
exports.NoopLogger = NoopLogger;
|
|
66
|
-
class ConsoleLogger {
|
|
67
|
-
constructor(prefix = '[SDK]') {
|
|
68
|
-
this.prefix = prefix;
|
|
69
|
-
}
|
|
70
|
-
debug(message, ...args) {
|
|
71
|
-
console.debug(`${this.prefix} ${message}`, ...args);
|
|
72
|
-
}
|
|
73
|
-
info(message, ...args) {
|
|
74
|
-
console.info(`${this.prefix} ${message}`, ...args);
|
|
75
|
-
}
|
|
76
|
-
warn(message, ...args) {
|
|
77
|
-
console.warn(`${this.prefix} ${message}`, ...args);
|
|
78
|
-
}
|
|
79
|
-
error(message, ...args) {
|
|
80
|
-
console.error(`${this.prefix} ${message}`, ...args);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
exports.ConsoleLogger = ConsoleLogger;
|
|
84
|
-
class NoopMetrics {
|
|
85
|
-
increment(_name, _value, _tags) { }
|
|
86
|
-
gauge(_name, _value, _tags) { }
|
|
87
|
-
timing(_name, _durationMs, _tags) { }
|
|
88
|
-
histogram(_name, _value, _tags) { }
|
|
89
|
-
}
|
|
90
|
-
exports.NoopMetrics = NoopMetrics;
|
|
91
|
-
class DefaultTimerAdapter {
|
|
92
|
-
setTimeout(callback, ms) {
|
|
93
|
-
return setTimeout(callback, ms);
|
|
94
|
-
}
|
|
95
|
-
setInterval(callback, ms) {
|
|
96
|
-
return setInterval(callback, ms);
|
|
97
|
-
}
|
|
98
|
-
clearTimeout(handle) {
|
|
99
|
-
clearTimeout(handle);
|
|
100
|
-
}
|
|
101
|
-
clearInterval(handle) {
|
|
102
|
-
clearInterval(handle);
|
|
103
|
-
}
|
|
104
|
-
now() {
|
|
105
|
-
return Date.now();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
exports.DefaultTimerAdapter = DefaultTimerAdapter;
|
|
109
|
-
class NoopLifecycleAdapter {
|
|
110
|
-
onSuspend(_callback) {
|
|
111
|
-
return () => { };
|
|
112
|
-
}
|
|
113
|
-
onResume(_callback) {
|
|
114
|
-
return () => { };
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
exports.NoopLifecycleAdapter = NoopLifecycleAdapter;
|