@totemsdk/core 1.2.5 → 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/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/derive.js +7 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -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 +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 +1 -1
- package/dist/wots.js +64 -94
- package/package.json +1 -1
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Contract Helpers
|
|
4
3
|
*
|
|
@@ -14,14 +13,12 @@
|
|
|
14
13
|
* Each helper validates inputs and builds the appropriate ScriptDescriptor
|
|
15
14
|
* for use with the EnhancedTransactionBuilder.
|
|
16
15
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const wasm_sync_js_1 = require("../wasm-sync.js");
|
|
20
|
-
const node_crypto_1 = require("node:crypto");
|
|
16
|
+
import { sha3_256 } from '../wasm-sync.js';
|
|
17
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
21
18
|
function sha256(data) {
|
|
22
|
-
return new Uint8Array(
|
|
19
|
+
return new Uint8Array(createHash('sha256').update(data).digest());
|
|
23
20
|
}
|
|
24
|
-
|
|
21
|
+
import { createTimelockDescriptor, createHTLCDescriptor, createMASTDescriptor, createExchangeDescriptor, createFlashCashDescriptor, createSlowCashDescriptor } from './types.js';
|
|
25
22
|
function kissHex(hex) {
|
|
26
23
|
const raw = hex.startsWith('0x') || hex.startsWith('0X') ? hex.slice(2) : hex;
|
|
27
24
|
return '0x' + raw.toUpperCase();
|
|
@@ -42,7 +39,7 @@ function bytesToHex(bytes) {
|
|
|
42
39
|
*
|
|
43
40
|
* Creates timelocked scripts that can only be spent after a certain block.
|
|
44
41
|
*/
|
|
45
|
-
class TimelockHelper {
|
|
42
|
+
export class TimelockHelper {
|
|
46
43
|
/**
|
|
47
44
|
* Create a timelock script that unlocks at a specific block.
|
|
48
45
|
*/
|
|
@@ -69,23 +66,22 @@ class TimelockHelper {
|
|
|
69
66
|
* Build ScriptDescriptor for a timelock spend.
|
|
70
67
|
*/
|
|
71
68
|
static buildDescriptor(address, publicKey, unlockBlock) {
|
|
72
|
-
return
|
|
69
|
+
return createTimelockDescriptor(address, publicKey, unlockBlock);
|
|
73
70
|
}
|
|
74
71
|
}
|
|
75
|
-
exports.TimelockHelper = TimelockHelper;
|
|
76
72
|
/**
|
|
77
73
|
* HTLC Helper
|
|
78
74
|
*
|
|
79
75
|
* Creates Hashed Timelock Contracts for atomic swaps and lightning-style payments.
|
|
80
76
|
*/
|
|
81
|
-
class HTLCHelper {
|
|
77
|
+
export class HTLCHelper {
|
|
82
78
|
/**
|
|
83
79
|
* Generate a random preimage and its hash.
|
|
84
80
|
*/
|
|
85
81
|
static generateSecret() {
|
|
86
|
-
const preimageBytes =
|
|
82
|
+
const preimageBytes = randomBytes(32);
|
|
87
83
|
const preimage = bytesToHex(preimageBytes);
|
|
88
|
-
const hashBytes =
|
|
84
|
+
const hashBytes = sha3_256(preimageBytes);
|
|
89
85
|
const hash = bytesToHex(hashBytes);
|
|
90
86
|
return { preimage, hash };
|
|
91
87
|
}
|
|
@@ -99,7 +95,7 @@ class HTLCHelper {
|
|
|
99
95
|
return bytesToHex(hashBytes);
|
|
100
96
|
}
|
|
101
97
|
else {
|
|
102
|
-
const hashBytes =
|
|
98
|
+
const hashBytes = sha3_256(preimageBytes);
|
|
103
99
|
return bytesToHex(hashBytes);
|
|
104
100
|
}
|
|
105
101
|
}
|
|
@@ -127,28 +123,27 @@ class HTLCHelper {
|
|
|
127
123
|
* Build ScriptDescriptor to claim HTLC with preimage.
|
|
128
124
|
*/
|
|
129
125
|
static buildClaimDescriptor(address, senderPublicKey, recipientPublicKey, hashLock, timeoutBlock, preimage) {
|
|
130
|
-
return
|
|
126
|
+
return createHTLCDescriptor(address, senderPublicKey, recipientPublicKey, hashLock, timeoutBlock, false, preimage);
|
|
131
127
|
}
|
|
132
128
|
/**
|
|
133
129
|
* Build ScriptDescriptor to refund HTLC after timeout.
|
|
134
130
|
*/
|
|
135
131
|
static buildRefundDescriptor(address, senderPublicKey, recipientPublicKey, hashLock, timeoutBlock) {
|
|
136
|
-
return
|
|
132
|
+
return createHTLCDescriptor(address, senderPublicKey, recipientPublicKey, hashLock, timeoutBlock, true);
|
|
137
133
|
}
|
|
138
134
|
}
|
|
139
|
-
exports.HTLCHelper = HTLCHelper;
|
|
140
135
|
/**
|
|
141
136
|
* MAST Helper
|
|
142
137
|
*
|
|
143
138
|
* Creates Merkelized Abstract Syntax Tree contracts for privacy and scalability.
|
|
144
139
|
*/
|
|
145
|
-
class MASTHelper {
|
|
140
|
+
export class MASTHelper {
|
|
146
141
|
/**
|
|
147
142
|
* Compute hash of a script for MAST leaf.
|
|
148
143
|
*/
|
|
149
144
|
static hashScript(script) {
|
|
150
145
|
const scriptBytes = new TextEncoder().encode(script.trim().toUpperCase());
|
|
151
|
-
const hashBytes =
|
|
146
|
+
const hashBytes = sha3_256(scriptBytes);
|
|
152
147
|
return bytesToHex(hashBytes);
|
|
153
148
|
}
|
|
154
149
|
/**
|
|
@@ -179,7 +174,7 @@ class MASTHelper {
|
|
|
179
174
|
proofs.set(scripts[i], { proof: proofHex, index: i });
|
|
180
175
|
}
|
|
181
176
|
const combinedBytes = hexToBytes(hashes[0].slice(2) + hashes[1].slice(2));
|
|
182
|
-
const rootBytes =
|
|
177
|
+
const rootBytes = sha3_256(combinedBytes);
|
|
183
178
|
const root = bytesToHex(rootBytes);
|
|
184
179
|
return { root, proofs };
|
|
185
180
|
}
|
|
@@ -195,16 +190,15 @@ class MASTHelper {
|
|
|
195
190
|
* Build ScriptDescriptor for spending a MAST branch.
|
|
196
191
|
*/
|
|
197
192
|
static buildDescriptor(address, rootHash, branchScript, branchProof, wotsPublicKey) {
|
|
198
|
-
return
|
|
193
|
+
return createMASTDescriptor(address, rootHash, branchScript, branchProof, wotsPublicKey);
|
|
199
194
|
}
|
|
200
195
|
}
|
|
201
|
-
exports.MASTHelper = MASTHelper;
|
|
202
196
|
/**
|
|
203
197
|
* Exchange Contract Helper
|
|
204
198
|
*
|
|
205
199
|
* Creates DEX-style exchange contracts using VERIFYOUT.
|
|
206
200
|
*/
|
|
207
|
-
class ExchangeHelper {
|
|
201
|
+
export class ExchangeHelper {
|
|
208
202
|
/**
|
|
209
203
|
* Create an exchange offer script.
|
|
210
204
|
*
|
|
@@ -231,7 +225,7 @@ class ExchangeHelper {
|
|
|
231
225
|
* Build ScriptDescriptor for taking an exchange offer.
|
|
232
226
|
*/
|
|
233
227
|
static buildTakeOfferDescriptor(address, ownerPublicKey, desiredAddress, desiredAmount, desiredTokenId) {
|
|
234
|
-
return
|
|
228
|
+
return createExchangeDescriptor(address, ownerPublicKey, desiredAddress, desiredAmount, desiredTokenId);
|
|
235
229
|
}
|
|
236
230
|
/**
|
|
237
231
|
* Validate VERIFYOUT for an exchange transaction.
|
|
@@ -253,13 +247,12 @@ class ExchangeHelper {
|
|
|
253
247
|
return { valid: true };
|
|
254
248
|
}
|
|
255
249
|
}
|
|
256
|
-
exports.ExchangeHelper = ExchangeHelper;
|
|
257
250
|
/**
|
|
258
251
|
* Vault Helper
|
|
259
252
|
*
|
|
260
253
|
* Creates vault/covenant contracts with safe house enforcement.
|
|
261
254
|
*/
|
|
262
|
-
class VaultHelper {
|
|
255
|
+
export class VaultHelper {
|
|
263
256
|
/**
|
|
264
257
|
* Generate safe house script from vault parameters.
|
|
265
258
|
*/
|
|
@@ -291,13 +284,12 @@ class VaultHelper {
|
|
|
291
284
|
];
|
|
292
285
|
}
|
|
293
286
|
}
|
|
294
|
-
exports.VaultHelper = VaultHelper;
|
|
295
287
|
/**
|
|
296
288
|
* Flash Cash Helper
|
|
297
289
|
*
|
|
298
290
|
* Creates flash loan contracts for single-transaction borrowing.
|
|
299
291
|
*/
|
|
300
|
-
class FlashCashHelper {
|
|
292
|
+
export class FlashCashHelper {
|
|
301
293
|
/**
|
|
302
294
|
* Create a flash cash contract.
|
|
303
295
|
*/
|
|
@@ -317,16 +309,15 @@ class FlashCashHelper {
|
|
|
317
309
|
* Build ScriptDescriptor for borrowing flash cash.
|
|
318
310
|
*/
|
|
319
311
|
static buildBorrowDescriptor(address, ownerPublicKey, interestMultiplier = '1.01') {
|
|
320
|
-
return
|
|
312
|
+
return createFlashCashDescriptor(address, ownerPublicKey, interestMultiplier);
|
|
321
313
|
}
|
|
322
314
|
}
|
|
323
|
-
exports.FlashCashHelper = FlashCashHelper;
|
|
324
315
|
/**
|
|
325
316
|
* Slow Cash Helper
|
|
326
317
|
*
|
|
327
318
|
* Creates rate-limited withdrawal contracts.
|
|
328
319
|
*/
|
|
329
|
-
class SlowCashHelper {
|
|
320
|
+
export class SlowCashHelper {
|
|
330
321
|
/**
|
|
331
322
|
* Create a slow cash contract.
|
|
332
323
|
*/
|
|
@@ -353,16 +344,15 @@ class SlowCashHelper {
|
|
|
353
344
|
* Build ScriptDescriptor for slow cash withdrawal.
|
|
354
345
|
*/
|
|
355
346
|
static buildWithdrawalDescriptor(address, ownerPublicKey, withdrawalPercent = '0.9', cooldownBlocks = 10000n) {
|
|
356
|
-
return
|
|
347
|
+
return createSlowCashDescriptor(address, ownerPublicKey, withdrawalPercent, cooldownBlocks);
|
|
357
348
|
}
|
|
358
349
|
}
|
|
359
|
-
exports.SlowCashHelper = SlowCashHelper;
|
|
360
350
|
/**
|
|
361
351
|
* Stateful Game Helper
|
|
362
352
|
*
|
|
363
353
|
* Creates multi-round stateful contracts (like coin flip).
|
|
364
354
|
*/
|
|
365
|
-
class StatefulGameHelper {
|
|
355
|
+
export class StatefulGameHelper {
|
|
366
356
|
/**
|
|
367
357
|
* Create a round increment assertion.
|
|
368
358
|
*/
|
|
@@ -388,11 +378,10 @@ class StatefulGameHelper {
|
|
|
388
378
|
return currentRound === previousRound + 1;
|
|
389
379
|
}
|
|
390
380
|
}
|
|
391
|
-
exports.StatefulGameHelper = StatefulGameHelper;
|
|
392
381
|
function computeScriptAddress(script) {
|
|
393
382
|
const cleanScript = script.trim().toUpperCase();
|
|
394
383
|
const scriptBytes = new TextEncoder().encode(cleanScript);
|
|
395
|
-
const hashBytes =
|
|
384
|
+
const hashBytes = sha3_256(scriptBytes);
|
|
396
385
|
return bytesToHex(hashBytes);
|
|
397
386
|
}
|
|
398
387
|
function encodeSimpleProof(siblingHash, isLeft) {
|
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.TOTEM_SEND_TRANSACTION_VERSION = void 0;
|
|
4
|
-
exports.validateSendTransactionRequest = validateSendTransactionRequest;
|
|
5
|
-
exports.simpleTotemSendRequest = simpleTotemSendRequest;
|
|
6
|
-
exports.TOTEM_SEND_TRANSACTION_VERSION = 1;
|
|
7
|
-
function validateSendTransactionRequest(request) {
|
|
1
|
+
export const TOTEM_SEND_TRANSACTION_VERSION = 1;
|
|
2
|
+
export function validateSendTransactionRequest(request) {
|
|
8
3
|
const errors = [];
|
|
9
4
|
if (!request || typeof request !== 'object') {
|
|
10
5
|
return { valid: false, errors: ['Request must be an object'] };
|
|
11
6
|
}
|
|
12
7
|
const req = request;
|
|
13
|
-
if (req.version !==
|
|
14
|
-
errors.push(`Invalid version: expected ${
|
|
8
|
+
if (req.version !== TOTEM_SEND_TRANSACTION_VERSION) {
|
|
9
|
+
errors.push(`Invalid version: expected ${TOTEM_SEND_TRANSACTION_VERSION}, got ${req.version}`);
|
|
15
10
|
}
|
|
16
11
|
const validIntents = [
|
|
17
12
|
'send', 'token_send', 'swap', 'liquidity_add', 'liquidity_remove',
|
|
@@ -64,9 +59,9 @@ function validateSendTransactionRequest(request) {
|
|
|
64
59
|
}
|
|
65
60
|
return { valid: errors.length === 0, errors };
|
|
66
61
|
}
|
|
67
|
-
function simpleTotemSendRequest(to, amount, tokenId) {
|
|
62
|
+
export function simpleTotemSendRequest(to, amount, tokenId) {
|
|
68
63
|
return {
|
|
69
|
-
version:
|
|
64
|
+
version: TOTEM_SEND_TRANSACTION_VERSION,
|
|
70
65
|
intent: tokenId && tokenId !== '0x00' ? 'token_send' : 'send',
|
|
71
66
|
outputs: [{
|
|
72
67
|
address: to,
|
package/dist/scripts/index.js
CHANGED
|
@@ -1,38 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.computeScriptAddress = exports.validateExternalSignature = exports.aggregateSignatures = exports.serializeExtraScripts = exports.deduplicateScriptDescriptors = exports.buildScriptProofFromDescriptor = exports.serializeStateVariables = exports.encodeStateValue = exports.STATETYPE_BOOL = exports.STATETYPE_STRING = exports.STATETYPE_NUMBER = exports.STATETYPE_HEX = exports.serializeScriptProofWithProof = exports.serializeMMRProofChunk = exports.encodeMiniString = exports.encodeMiniData = exports.encodeMiniNumber = void 0;
|
|
18
|
-
__exportStar(require("./types.js"), exports);
|
|
19
|
-
__exportStar(require("./dapp-types.js"), exports);
|
|
20
|
-
var witness_serializer_js_1 = require("./witness-serializer.js");
|
|
21
|
-
Object.defineProperty(exports, "encodeMiniNumber", { enumerable: true, get: function () { return witness_serializer_js_1.encodeMiniNumber; } });
|
|
22
|
-
Object.defineProperty(exports, "encodeMiniData", { enumerable: true, get: function () { return witness_serializer_js_1.encodeMiniData; } });
|
|
23
|
-
Object.defineProperty(exports, "encodeMiniString", { enumerable: true, get: function () { return witness_serializer_js_1.encodeMiniString; } });
|
|
24
|
-
Object.defineProperty(exports, "serializeMMRProofChunk", { enumerable: true, get: function () { return witness_serializer_js_1.serializeMMRProofChunk; } });
|
|
25
|
-
Object.defineProperty(exports, "serializeScriptProofWithProof", { enumerable: true, get: function () { return witness_serializer_js_1.serializeScriptProofWithProof; } });
|
|
26
|
-
Object.defineProperty(exports, "STATETYPE_HEX", { enumerable: true, get: function () { return witness_serializer_js_1.STATETYPE_HEX; } });
|
|
27
|
-
Object.defineProperty(exports, "STATETYPE_NUMBER", { enumerable: true, get: function () { return witness_serializer_js_1.STATETYPE_NUMBER; } });
|
|
28
|
-
Object.defineProperty(exports, "STATETYPE_STRING", { enumerable: true, get: function () { return witness_serializer_js_1.STATETYPE_STRING; } });
|
|
29
|
-
Object.defineProperty(exports, "STATETYPE_BOOL", { enumerable: true, get: function () { return witness_serializer_js_1.STATETYPE_BOOL; } });
|
|
30
|
-
Object.defineProperty(exports, "encodeStateValue", { enumerable: true, get: function () { return witness_serializer_js_1.encodeStateValue; } });
|
|
31
|
-
Object.defineProperty(exports, "serializeStateVariables", { enumerable: true, get: function () { return witness_serializer_js_1.serializeStateVariables; } });
|
|
32
|
-
Object.defineProperty(exports, "buildScriptProofFromDescriptor", { enumerable: true, get: function () { return witness_serializer_js_1.buildScriptProofFromDescriptor; } });
|
|
33
|
-
Object.defineProperty(exports, "deduplicateScriptDescriptors", { enumerable: true, get: function () { return witness_serializer_js_1.deduplicateScriptDescriptors; } });
|
|
34
|
-
Object.defineProperty(exports, "serializeExtraScripts", { enumerable: true, get: function () { return witness_serializer_js_1.serializeExtraScripts; } });
|
|
35
|
-
Object.defineProperty(exports, "aggregateSignatures", { enumerable: true, get: function () { return witness_serializer_js_1.aggregateSignatures; } });
|
|
36
|
-
Object.defineProperty(exports, "validateExternalSignature", { enumerable: true, get: function () { return witness_serializer_js_1.validateExternalSignature; } });
|
|
37
|
-
Object.defineProperty(exports, "computeScriptAddress", { enumerable: true, get: function () { return witness_serializer_js_1.computeScriptAddress; } });
|
|
38
|
-
__exportStar(require("./contract-helpers.js"), exports);
|
|
1
|
+
export * from './types.js';
|
|
2
|
+
export * from './dapp-types.js';
|
|
3
|
+
export { encodeMiniNumber, encodeMiniData, encodeMiniString, serializeMMRProofChunk, serializeScriptProofWithProof, STATETYPE_HEX, STATETYPE_NUMBER, STATETYPE_STRING, STATETYPE_BOOL, encodeStateValue, serializeStateVariables, buildScriptProofFromDescriptor, deduplicateScriptDescriptors, serializeExtraScripts, aggregateSignatures, validateExternalSignature, computeScriptAddress, } from './witness-serializer.js';
|
|
4
|
+
export * from './contract-helpers.js';
|
package/dist/scripts/types.js
CHANGED
|
@@ -1,28 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertFlatChunkToSDK = convertFlatChunkToSDK;
|
|
4
|
-
exports.convertLegacyProofToSDK = convertLegacyProofToSDK;
|
|
5
|
-
exports.createEmptyMMRProof = createEmptyMMRProof;
|
|
6
|
-
exports.createSignedByDescriptor = createSignedByDescriptor;
|
|
7
|
-
exports.createMultisigDescriptor = createMultisigDescriptor;
|
|
8
|
-
exports.createMofNMultisigDescriptor = createMofNMultisigDescriptor;
|
|
9
|
-
exports.createTimelockDescriptor = createTimelockDescriptor;
|
|
10
|
-
exports.createHTLCDescriptor = createHTLCDescriptor;
|
|
11
|
-
exports.createMASTDescriptor = createMASTDescriptor;
|
|
12
|
-
exports.createExchangeDescriptor = createExchangeDescriptor;
|
|
13
|
-
exports.createFlashCashDescriptor = createFlashCashDescriptor;
|
|
14
|
-
exports.createSlowCashDescriptor = createSlowCashDescriptor;
|
|
15
1
|
function kissHex(hex) {
|
|
16
2
|
const raw = hex.startsWith('0x') || hex.startsWith('0X') ? hex.slice(2) : hex;
|
|
17
3
|
return '0x' + raw.toUpperCase();
|
|
18
4
|
}
|
|
19
|
-
function convertFlatChunkToSDK(chunk) {
|
|
5
|
+
export function convertFlatChunkToSDK(chunk) {
|
|
20
6
|
return {
|
|
21
7
|
isLeft: chunk.isLeft,
|
|
22
8
|
mmrData: { data: chunk.data, value: 0n }
|
|
23
9
|
};
|
|
24
10
|
}
|
|
25
|
-
function convertLegacyProofToSDK(legacy) {
|
|
11
|
+
export function convertLegacyProofToSDK(legacy) {
|
|
26
12
|
return {
|
|
27
13
|
proof: {
|
|
28
14
|
chunks: legacy.proofChain.map(convertFlatChunkToSDK)
|
|
@@ -30,10 +16,10 @@ function convertLegacyProofToSDK(legacy) {
|
|
|
30
16
|
blockTime: legacy.blockTime
|
|
31
17
|
};
|
|
32
18
|
}
|
|
33
|
-
function createEmptyMMRProof() {
|
|
19
|
+
export function createEmptyMMRProof() {
|
|
34
20
|
return { chunks: [] };
|
|
35
21
|
}
|
|
36
|
-
function createSignedByDescriptor(address, wotsRootPublicKey) {
|
|
22
|
+
export function createSignedByDescriptor(address, wotsRootPublicKey) {
|
|
37
23
|
return {
|
|
38
24
|
address,
|
|
39
25
|
scriptType: 'signedby',
|
|
@@ -43,7 +29,7 @@ function createSignedByDescriptor(address, wotsRootPublicKey) {
|
|
|
43
29
|
storeState: false
|
|
44
30
|
};
|
|
45
31
|
}
|
|
46
|
-
function createMultisigDescriptor(address, publicKey1, publicKey2, ownPublicKey) {
|
|
32
|
+
export function createMultisigDescriptor(address, publicKey1, publicKey2, ownPublicKey) {
|
|
47
33
|
return {
|
|
48
34
|
address,
|
|
49
35
|
scriptType: 'multisig',
|
|
@@ -55,7 +41,7 @@ function createMultisigDescriptor(address, publicKey1, publicKey2, ownPublicKey)
|
|
|
55
41
|
storeState: false
|
|
56
42
|
};
|
|
57
43
|
}
|
|
58
|
-
function createMofNMultisigDescriptor(address, threshold, publicKeys, ownPublicKey) {
|
|
44
|
+
export function createMofNMultisigDescriptor(address, threshold, publicKeys, ownPublicKey) {
|
|
59
45
|
const formattedKeys = publicKeys.map(pk => kissHex(pk)).join(' ');
|
|
60
46
|
return {
|
|
61
47
|
address,
|
|
@@ -68,7 +54,7 @@ function createMofNMultisigDescriptor(address, threshold, publicKeys, ownPublicK
|
|
|
68
54
|
storeState: false
|
|
69
55
|
};
|
|
70
56
|
}
|
|
71
|
-
function createTimelockDescriptor(address, publicKey, unlockBlock) {
|
|
57
|
+
export function createTimelockDescriptor(address, publicKey, unlockBlock) {
|
|
72
58
|
return {
|
|
73
59
|
address,
|
|
74
60
|
scriptType: 'timelock',
|
|
@@ -79,7 +65,7 @@ function createTimelockDescriptor(address, publicKey, unlockBlock) {
|
|
|
79
65
|
storeState: false
|
|
80
66
|
};
|
|
81
67
|
}
|
|
82
|
-
function createHTLCDescriptor(address, ownerPublicKey, recipientPublicKey, hashLock, timeoutBlock, isOwner, preimage) {
|
|
68
|
+
export function createHTLCDescriptor(address, ownerPublicKey, recipientPublicKey, hashLock, timeoutBlock, isOwner, preimage) {
|
|
83
69
|
const script = `IF @BLOCK GT ${timeoutBlock} AND SIGNEDBY(${kissHex(ownerPublicKey)}) THEN RETURN TRUE ENDIF RETURN (SIGNEDBY(${kissHex(recipientPublicKey)}) AND SHA3(STATE(1)) EQ ${kissHex(hashLock)})`;
|
|
84
70
|
const descriptor = {
|
|
85
71
|
address,
|
|
@@ -97,7 +83,7 @@ function createHTLCDescriptor(address, ownerPublicKey, recipientPublicKey, hashL
|
|
|
97
83
|
}
|
|
98
84
|
return descriptor;
|
|
99
85
|
}
|
|
100
|
-
function createMASTDescriptor(address, rootHash, branchScript, branchProof, wotsPublicKey) {
|
|
86
|
+
export function createMASTDescriptor(address, rootHash, branchScript, branchProof, wotsPublicKey) {
|
|
101
87
|
return {
|
|
102
88
|
address,
|
|
103
89
|
scriptType: 'mast',
|
|
@@ -108,7 +94,7 @@ function createMASTDescriptor(address, rootHash, branchScript, branchProof, wots
|
|
|
108
94
|
storeState: false
|
|
109
95
|
};
|
|
110
96
|
}
|
|
111
|
-
function createExchangeDescriptor(address, ownerPublicKey, desiredAddress, desiredAmount, desiredTokenId) {
|
|
97
|
+
export function createExchangeDescriptor(address, ownerPublicKey, desiredAddress, desiredAmount, desiredTokenId) {
|
|
112
98
|
const ownerPk = kissHex(ownerPublicKey);
|
|
113
99
|
const script = `IF SIGNEDBY(PREVSTATE(0)) THEN RETURN TRUE ENDIF ASSERT VERIFYOUT(@INPUT PREVSTATE(1) PREVSTATE(2) PREVSTATE(3) TRUE) RETURN TRUE`;
|
|
114
100
|
return {
|
|
@@ -133,7 +119,7 @@ function createExchangeDescriptor(address, ownerPublicKey, desiredAddress, desir
|
|
|
133
119
|
storeState: true
|
|
134
120
|
};
|
|
135
121
|
}
|
|
136
|
-
function createFlashCashDescriptor(address, ownerPublicKey, interestMultiplier = '1.01') {
|
|
122
|
+
export function createFlashCashDescriptor(address, ownerPublicKey, interestMultiplier = '1.01') {
|
|
137
123
|
const script = `IF SIGNEDBY(PREVSTATE(1)) THEN RETURN TRUE ENDIF ASSERT SAMESTATE(1 1) RETURN VERIFYOUT(@INPUT @ADDRESS @AMOUNT*${interestMultiplier} @TOKENID TRUE)`;
|
|
138
124
|
return {
|
|
139
125
|
address,
|
|
@@ -145,7 +131,7 @@ function createFlashCashDescriptor(address, ownerPublicKey, interestMultiplier =
|
|
|
145
131
|
storeState: true
|
|
146
132
|
};
|
|
147
133
|
}
|
|
148
|
-
function createSlowCashDescriptor(address, ownerPublicKey, withdrawalPercent = '0.9', cooldownBlocks = 10000n) {
|
|
134
|
+
export function createSlowCashDescriptor(address, ownerPublicKey, withdrawalPercent = '0.9', cooldownBlocks = 10000n) {
|
|
149
135
|
const script = `IF @COINAGE LT ${cooldownBlocks} THEN RETURN FALSE ENDIF ASSERT SIGNEDBY(${kissHex(ownerPublicKey)}) AND VERIFYOUT(@INPUT @ADDRESS @AMOUNT*${withdrawalPercent} @TOKENID TRUE)`;
|
|
150
136
|
return {
|
|
151
137
|
address,
|
|
@@ -1,25 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.encodeMiniNumber = encodeMiniNumber;
|
|
5
|
-
exports.encodeMiniData = encodeMiniData;
|
|
6
|
-
exports.encodeMiniString = encodeMiniString;
|
|
7
|
-
exports.serializeMMRProofChunk = serializeMMRProofChunk;
|
|
8
|
-
exports.serializeRealMMRProof = serializeRealMMRProof;
|
|
9
|
-
exports.parseMMRProofFromHex = parseMMRProofFromHex;
|
|
10
|
-
exports.serializeScriptProofWithProof = serializeScriptProofWithProof;
|
|
11
|
-
exports.encodeStateValue = encodeStateValue;
|
|
12
|
-
exports.serializeStateVariables = serializeStateVariables;
|
|
13
|
-
exports.buildScriptProofFromDescriptor = buildScriptProofFromDescriptor;
|
|
14
|
-
exports.deduplicateScriptDescriptors = deduplicateScriptDescriptors;
|
|
15
|
-
exports.serializeExtraScripts = serializeExtraScripts;
|
|
16
|
-
exports.aggregateSignatures = aggregateSignatures;
|
|
17
|
-
exports.validateExternalSignature = validateExternalSignature;
|
|
18
|
-
exports.computeScriptAddress = computeScriptAddress;
|
|
19
|
-
exports.concat = concat;
|
|
20
|
-
const wasm_sync_js_1 = require("../wasm-sync.js");
|
|
21
|
-
const mmr_js_1 = require("../mmr.js");
|
|
22
|
-
const minima32_js_1 = require("../minima32.js");
|
|
1
|
+
import { sha3_256 } from '../wasm-sync.js';
|
|
2
|
+
import { serializeMMRProof } from '../mmr.js';
|
|
3
|
+
import { mxToHex } from '../minima32.js';
|
|
23
4
|
function hexToBytes(hex) {
|
|
24
5
|
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
25
6
|
if (cleanHex.length % 2 !== 0) {
|
|
@@ -36,7 +17,7 @@ function normalizeHexValue(value) {
|
|
|
36
17
|
return '0x00';
|
|
37
18
|
const trimmed = value.trim();
|
|
38
19
|
if (trimmed.toLowerCase().startsWith('mx')) {
|
|
39
|
-
return
|
|
20
|
+
return mxToHex(trimmed);
|
|
40
21
|
}
|
|
41
22
|
return trimmed.startsWith('0x') ? trimmed : '0x' + trimmed;
|
|
42
23
|
}
|
|
@@ -53,7 +34,7 @@ function concat(...arrays) {
|
|
|
53
34
|
}
|
|
54
35
|
return result;
|
|
55
36
|
}
|
|
56
|
-
function encodeMiniNumber(value, scale = 0) {
|
|
37
|
+
export function encodeMiniNumber(value, scale = 0) {
|
|
57
38
|
if (typeof value !== 'bigint') {
|
|
58
39
|
throw new Error(`encodeMiniNumber requires bigint, got ${typeof value}`);
|
|
59
40
|
}
|
|
@@ -84,7 +65,7 @@ function encodeMiniNumber(value, scale = 0) {
|
|
|
84
65
|
result.set(unscaledBytes, 2);
|
|
85
66
|
return result;
|
|
86
67
|
}
|
|
87
|
-
function encodeMiniData(data) {
|
|
68
|
+
export function encodeMiniData(data) {
|
|
88
69
|
const length = data.length;
|
|
89
70
|
const lengthBytes = new Uint8Array([
|
|
90
71
|
(length >> 24) & 0xff,
|
|
@@ -94,24 +75,24 @@ function encodeMiniData(data) {
|
|
|
94
75
|
]);
|
|
95
76
|
return concat(lengthBytes, data);
|
|
96
77
|
}
|
|
97
|
-
function encodeMiniString(str) {
|
|
78
|
+
export function encodeMiniString(str) {
|
|
98
79
|
const utf8Bytes = new TextEncoder().encode(str);
|
|
99
80
|
return encodeMiniData(utf8Bytes);
|
|
100
81
|
}
|
|
101
82
|
function encodeMiniByte(value) {
|
|
102
83
|
return new Uint8Array([value ? 1 : 0]);
|
|
103
84
|
}
|
|
104
|
-
function serializeMMRProofChunk(chunk) {
|
|
85
|
+
export function serializeMMRProofChunk(chunk) {
|
|
105
86
|
const parts = [];
|
|
106
87
|
parts.push(encodeMiniByte(chunk.isLeft));
|
|
107
88
|
parts.push(encodeMiniData(chunk.mmrData.data));
|
|
108
89
|
parts.push(encodeMiniNumber(chunk.mmrData.value));
|
|
109
90
|
return concat(...parts);
|
|
110
91
|
}
|
|
111
|
-
function serializeRealMMRProof(proof, blockTime = 0n) {
|
|
112
|
-
return
|
|
92
|
+
export function serializeRealMMRProof(proof, blockTime = 0n) {
|
|
93
|
+
return serializeMMRProof(proof, blockTime);
|
|
113
94
|
}
|
|
114
|
-
function parseMMRProofFromHex(proofHex) {
|
|
95
|
+
export function parseMMRProofFromHex(proofHex) {
|
|
115
96
|
const bytes = hexToBytes(proofHex);
|
|
116
97
|
let offset = 0;
|
|
117
98
|
const { value: blockTime, bytesRead: btRead } = readMiniNumber(bytes, offset);
|
|
@@ -156,16 +137,16 @@ function readMMRProofChunk(bytes, offset) {
|
|
|
156
137
|
bytesRead: pos - offset
|
|
157
138
|
};
|
|
158
139
|
}
|
|
159
|
-
function serializeScriptProofWithProof(script, proof, blockTime = 0n) {
|
|
140
|
+
export function serializeScriptProofWithProof(script, proof, blockTime = 0n) {
|
|
160
141
|
const scriptBytes = encodeMiniString(script);
|
|
161
142
|
const proofBytes = serializeRealMMRProof(proof, blockTime);
|
|
162
143
|
return concat(scriptBytes, proofBytes);
|
|
163
144
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
function encodeStateValue(stateValue) {
|
|
145
|
+
export const STATETYPE_HEX = 1;
|
|
146
|
+
export const STATETYPE_NUMBER = 2;
|
|
147
|
+
export const STATETYPE_STRING = 4;
|
|
148
|
+
export const STATETYPE_BOOL = 8;
|
|
149
|
+
export function encodeStateValue(stateValue) {
|
|
169
150
|
if (stateValue.port < 0 || stateValue.port > 255) {
|
|
170
151
|
throw new Error(`StateVariable port must be 0-255, got ${stateValue.port}`);
|
|
171
152
|
}
|
|
@@ -174,7 +155,7 @@ function encodeStateValue(stateValue) {
|
|
|
174
155
|
let dataBytes;
|
|
175
156
|
switch (stateValue.type) {
|
|
176
157
|
case 'bool':
|
|
177
|
-
typeByte = new Uint8Array([
|
|
158
|
+
typeByte = new Uint8Array([STATETYPE_BOOL]);
|
|
178
159
|
if (typeof stateValue.value === 'boolean') {
|
|
179
160
|
dataBytes = new Uint8Array([stateValue.value ? 1 : 0]);
|
|
180
161
|
}
|
|
@@ -187,7 +168,7 @@ function encodeStateValue(stateValue) {
|
|
|
187
168
|
}
|
|
188
169
|
break;
|
|
189
170
|
case 'number':
|
|
190
|
-
typeByte = new Uint8Array([
|
|
171
|
+
typeByte = new Uint8Array([STATETYPE_NUMBER]);
|
|
191
172
|
if (typeof stateValue.value === 'bigint') {
|
|
192
173
|
dataBytes = encodeMiniNumber(stateValue.value);
|
|
193
174
|
}
|
|
@@ -200,7 +181,7 @@ function encodeStateValue(stateValue) {
|
|
|
200
181
|
}
|
|
201
182
|
break;
|
|
202
183
|
case 'hex':
|
|
203
|
-
typeByte = new Uint8Array([
|
|
184
|
+
typeByte = new Uint8Array([STATETYPE_HEX]);
|
|
204
185
|
if (typeof stateValue.value === 'string') {
|
|
205
186
|
const normalizedHex = normalizeHexValue(stateValue.value);
|
|
206
187
|
dataBytes = encodeMiniData(hexToBytes(normalizedHex));
|
|
@@ -213,7 +194,7 @@ function encodeStateValue(stateValue) {
|
|
|
213
194
|
}
|
|
214
195
|
break;
|
|
215
196
|
case 'string':
|
|
216
|
-
typeByte = new Uint8Array([
|
|
197
|
+
typeByte = new Uint8Array([STATETYPE_STRING]);
|
|
217
198
|
if (typeof stateValue.value === 'string') {
|
|
218
199
|
let bracketedValue = stateValue.value;
|
|
219
200
|
if (!stateValue.value.startsWith('[') || !stateValue.value.endsWith(']')) {
|
|
@@ -230,7 +211,7 @@ function encodeStateValue(stateValue) {
|
|
|
230
211
|
}
|
|
231
212
|
return concat(portByte, typeByte, dataBytes);
|
|
232
213
|
}
|
|
233
|
-
function serializeStateVariables(stateValues) {
|
|
214
|
+
export function serializeStateVariables(stateValues) {
|
|
234
215
|
const parts = [];
|
|
235
216
|
const countBytes = encodeMiniNumber(BigInt(stateValues.length));
|
|
236
217
|
parts.push(countBytes);
|
|
@@ -240,7 +221,7 @@ function serializeStateVariables(stateValues) {
|
|
|
240
221
|
}
|
|
241
222
|
return concat(...parts);
|
|
242
223
|
}
|
|
243
|
-
function buildScriptProofFromDescriptor(descriptor) {
|
|
224
|
+
export function buildScriptProofFromDescriptor(descriptor) {
|
|
244
225
|
const proof = descriptor.mastProof || { chunks: [] };
|
|
245
226
|
const serialized = serializeScriptProofWithProof(descriptor.script, proof);
|
|
246
227
|
return {
|
|
@@ -249,7 +230,7 @@ function buildScriptProofFromDescriptor(descriptor) {
|
|
|
249
230
|
serialized
|
|
250
231
|
};
|
|
251
232
|
}
|
|
252
|
-
function deduplicateScriptDescriptors(descriptors) {
|
|
233
|
+
export function deduplicateScriptDescriptors(descriptors) {
|
|
253
234
|
const unique = new Map();
|
|
254
235
|
for (const desc of descriptors) {
|
|
255
236
|
const normalizedAddr = desc.address.toLowerCase();
|
|
@@ -259,7 +240,7 @@ function deduplicateScriptDescriptors(descriptors) {
|
|
|
259
240
|
}
|
|
260
241
|
return unique;
|
|
261
242
|
}
|
|
262
|
-
function serializeExtraScripts(extraScripts) {
|
|
243
|
+
export function serializeExtraScripts(extraScripts) {
|
|
263
244
|
const parts = [];
|
|
264
245
|
const countBytes = encodeMiniNumber(BigInt(extraScripts.size));
|
|
265
246
|
parts.push(countBytes);
|
|
@@ -271,7 +252,7 @@ function serializeExtraScripts(extraScripts) {
|
|
|
271
252
|
}
|
|
272
253
|
return concat(...parts);
|
|
273
254
|
}
|
|
274
|
-
function aggregateSignatures(totemSignature, externalSignatures) {
|
|
255
|
+
export function aggregateSignatures(totemSignature, externalSignatures) {
|
|
275
256
|
const allSigProofs = [];
|
|
276
257
|
allSigProofs.push(serializeSignatureProof(totemSignature.publicKey, totemSignature.signature, { chunks: [] }));
|
|
277
258
|
for (const extSig of externalSignatures) {
|
|
@@ -291,16 +272,17 @@ function serializeSignatureProof(publicKey, signature, proof) {
|
|
|
291
272
|
parts.push(serializeRealMMRProof(proof));
|
|
292
273
|
return concat(...parts);
|
|
293
274
|
}
|
|
294
|
-
function validateExternalSignature(signature, transactionDigest) {
|
|
275
|
+
export function validateExternalSignature(signature, transactionDigest) {
|
|
295
276
|
if (!signature.publicKey || !signature.signature) {
|
|
296
277
|
return false;
|
|
297
278
|
}
|
|
298
279
|
console.log(`[ValidateSignature] Validating signature from ${signature.publicKey.substring(0, 16)}...`);
|
|
299
280
|
return true;
|
|
300
281
|
}
|
|
301
|
-
function computeScriptAddress(script) {
|
|
282
|
+
export function computeScriptAddress(script) {
|
|
302
283
|
const cleanScript = script.trim().toUpperCase();
|
|
303
284
|
const scriptBytes = new TextEncoder().encode(cleanScript);
|
|
304
|
-
const hashBytes =
|
|
285
|
+
const hashBytes = sha3_256(scriptBytes);
|
|
305
286
|
return bytesToHex(hashBytes);
|
|
306
287
|
}
|
|
288
|
+
export { concat };
|