@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/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 };
|
package/dist/transaction.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Transaction Serialization & Digest Computation
|
|
4
3
|
*
|
|
@@ -11,22 +10,14 @@
|
|
|
11
10
|
* precomputeTransactionCoinID() to set output coin IDs. Without this, the signed
|
|
12
11
|
* digest won't match what the Minima node verifies, causing allsignaturesvalid=false.
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
exports.serializeCoin = serializeCoin;
|
|
17
|
-
exports.serializeTransaction = serializeTransaction;
|
|
18
|
-
exports.computeTransactionDigest = computeTransactionDigest;
|
|
19
|
-
exports.precomputeTransactionCoinID = precomputeTransactionCoinID;
|
|
20
|
-
exports.createDefaultTransaction = createDefaultTransaction;
|
|
21
|
-
exports.buildMinimaCoin = buildMinimaCoin;
|
|
22
|
-
const wasm_sync_js_1 = require("./wasm-sync.js");
|
|
23
|
-
const Streamable_js_1 = require("./Streamable.js");
|
|
13
|
+
import { sha3_256 } from './wasm-sync.js';
|
|
14
|
+
import { writeMiniNumber, writeMiniData, writeHashToStream, writeMiniByte, writeMMREntryNumber, concat, hexToBytes, } from './Streamable.js';
|
|
24
15
|
const ZERO_TXPOWID = new Uint8Array([0x00]);
|
|
25
16
|
const STATETYPE_HEX = 1;
|
|
26
17
|
const STATETYPE_NUMBER = 2;
|
|
27
18
|
const STATETYPE_STRING = 4;
|
|
28
19
|
const STATETYPE_BOOL = 8;
|
|
29
|
-
function parseDecimalToMiniNumber(decimal) {
|
|
20
|
+
export function parseDecimalToMiniNumber(decimal) {
|
|
30
21
|
if (!decimal || decimal === '') {
|
|
31
22
|
return { scale: 0, unscaledValue: 0n };
|
|
32
23
|
}
|
|
@@ -60,7 +51,7 @@ function parseDecimalToMiniNumber(decimal) {
|
|
|
60
51
|
return { scale, unscaledValue };
|
|
61
52
|
}
|
|
62
53
|
function encodeParsedMiniNumber(parsed) {
|
|
63
|
-
return
|
|
54
|
+
return writeMiniNumber(parsed.unscaledValue, parsed.scale);
|
|
64
55
|
}
|
|
65
56
|
function normalizeAddressToHex(addr) {
|
|
66
57
|
if (!addr) {
|
|
@@ -100,10 +91,10 @@ function serializeStateVariable(sv) {
|
|
|
100
91
|
case 'number':
|
|
101
92
|
typeByte = new Uint8Array([STATETYPE_NUMBER]);
|
|
102
93
|
if (typeof sv.value === 'bigint') {
|
|
103
|
-
dataBytes =
|
|
94
|
+
dataBytes = writeMiniNumber(sv.value);
|
|
104
95
|
}
|
|
105
96
|
else if (typeof sv.value === 'string') {
|
|
106
|
-
dataBytes =
|
|
97
|
+
dataBytes = writeMiniNumber(BigInt(sv.value));
|
|
107
98
|
}
|
|
108
99
|
else {
|
|
109
100
|
throw new Error(`Invalid number StateVariable value: ${sv.value}`);
|
|
@@ -113,10 +104,10 @@ function serializeStateVariable(sv) {
|
|
|
113
104
|
typeByte = new Uint8Array([STATETYPE_HEX]);
|
|
114
105
|
if (typeof sv.value === 'string') {
|
|
115
106
|
const normalizedHex = normalizeAddressToHex(sv.value);
|
|
116
|
-
dataBytes =
|
|
107
|
+
dataBytes = writeMiniData(hexToBytes(normalizedHex));
|
|
117
108
|
}
|
|
118
109
|
else if (sv.value instanceof Uint8Array) {
|
|
119
|
-
dataBytes =
|
|
110
|
+
dataBytes = writeMiniData(sv.value);
|
|
120
111
|
}
|
|
121
112
|
else {
|
|
122
113
|
throw new Error(`Invalid hex StateVariable value: ${sv.value}`);
|
|
@@ -130,7 +121,7 @@ function serializeStateVariable(sv) {
|
|
|
130
121
|
bracketedValue = `[${sv.value}]`;
|
|
131
122
|
}
|
|
132
123
|
const utf8Bytes = new TextEncoder().encode(bracketedValue);
|
|
133
|
-
dataBytes =
|
|
124
|
+
dataBytes = writeMiniData(utf8Bytes);
|
|
134
125
|
}
|
|
135
126
|
else {
|
|
136
127
|
throw new Error(`Invalid string StateVariable value: ${sv.value}`);
|
|
@@ -139,23 +130,23 @@ function serializeStateVariable(sv) {
|
|
|
139
130
|
default:
|
|
140
131
|
throw new Error(`Unknown StateVariable type: ${sv.type}`);
|
|
141
132
|
}
|
|
142
|
-
return
|
|
133
|
+
return concat(portByte, typeByte, dataBytes);
|
|
143
134
|
}
|
|
144
135
|
function serializeToken(token) {
|
|
145
136
|
// Java Token.writeDataStream() order: coinId, script, scale, totalAmount, name, created
|
|
146
137
|
const parts = [];
|
|
147
|
-
parts.push(
|
|
148
|
-
parts.push(
|
|
149
|
-
parts.push(
|
|
150
|
-
parts.push(
|
|
151
|
-
parts.push(
|
|
152
|
-
parts.push(
|
|
153
|
-
return
|
|
138
|
+
parts.push(writeHashToStream(token.coinId));
|
|
139
|
+
parts.push(writeMiniData(token.script));
|
|
140
|
+
parts.push(writeMiniNumber(BigInt(token.scale)));
|
|
141
|
+
parts.push(writeMiniNumber(token.totalAmount, 0));
|
|
142
|
+
parts.push(writeMiniData(token.name));
|
|
143
|
+
parts.push(writeMiniNumber(token.created ?? 0n));
|
|
144
|
+
return concat(...parts);
|
|
154
145
|
}
|
|
155
|
-
function serializeCoin(coin) {
|
|
146
|
+
export function serializeCoin(coin) {
|
|
156
147
|
const parts = [];
|
|
157
|
-
parts.push(
|
|
158
|
-
parts.push(
|
|
148
|
+
parts.push(writeHashToStream(coin.coinId));
|
|
149
|
+
parts.push(writeHashToStream(coin.address));
|
|
159
150
|
let amountBytes;
|
|
160
151
|
if (coin.rawAmountBytes && coin.rawAmountBytes.length > 0) {
|
|
161
152
|
amountBytes = coin.rawAmountBytes;
|
|
@@ -165,26 +156,26 @@ function serializeCoin(coin) {
|
|
|
165
156
|
amountBytes = encodeParsedMiniNumber(parsedAmount);
|
|
166
157
|
}
|
|
167
158
|
parts.push(amountBytes);
|
|
168
|
-
parts.push(
|
|
169
|
-
parts.push(
|
|
159
|
+
parts.push(writeMiniData(coin.tokenId));
|
|
160
|
+
parts.push(writeMiniByte(coin.storeState));
|
|
170
161
|
let mmrBytes;
|
|
171
162
|
if (coin.rawMmrEntryBytes && coin.rawMmrEntryBytes.length > 0) {
|
|
172
163
|
mmrBytes = coin.rawMmrEntryBytes;
|
|
173
164
|
}
|
|
174
165
|
else {
|
|
175
|
-
mmrBytes =
|
|
166
|
+
mmrBytes = writeMMREntryNumber(coin.mmrEntryNumber);
|
|
176
167
|
}
|
|
177
168
|
parts.push(mmrBytes);
|
|
178
|
-
parts.push(
|
|
169
|
+
parts.push(writeMiniByte(coin.spent));
|
|
179
170
|
let createdBytes;
|
|
180
171
|
if (coin.rawBlockCreatedBytes && coin.rawBlockCreatedBytes.length > 0) {
|
|
181
172
|
createdBytes = coin.rawBlockCreatedBytes;
|
|
182
173
|
}
|
|
183
174
|
else {
|
|
184
|
-
createdBytes =
|
|
175
|
+
createdBytes = writeMiniNumber(coin.created);
|
|
185
176
|
}
|
|
186
177
|
parts.push(createdBytes);
|
|
187
|
-
const stateCountBytes =
|
|
178
|
+
const stateCountBytes = writeMiniNumber(BigInt(coin.state.length));
|
|
188
179
|
parts.push(stateCountBytes);
|
|
189
180
|
for (const sv of coin.state) {
|
|
190
181
|
let svBytes;
|
|
@@ -192,7 +183,7 @@ function serializeCoin(coin) {
|
|
|
192
183
|
const raw = sv;
|
|
193
184
|
const portByte = new Uint8Array([raw.port]);
|
|
194
185
|
const typeByte = new Uint8Array([raw.type]);
|
|
195
|
-
svBytes =
|
|
186
|
+
svBytes = concat(portByte, typeByte, raw.rawData);
|
|
196
187
|
}
|
|
197
188
|
else {
|
|
198
189
|
svBytes = serializeStateVariable(sv);
|
|
@@ -200,40 +191,40 @@ function serializeCoin(coin) {
|
|
|
200
191
|
parts.push(svBytes);
|
|
201
192
|
}
|
|
202
193
|
if (coin.rawTokenData && coin.rawTokenData.length > 0) {
|
|
203
|
-
parts.push(
|
|
194
|
+
parts.push(writeMiniByte(true));
|
|
204
195
|
parts.push(coin.rawTokenData);
|
|
205
196
|
}
|
|
206
197
|
else if (coin.token) {
|
|
207
|
-
parts.push(
|
|
198
|
+
parts.push(writeMiniByte(true));
|
|
208
199
|
parts.push(serializeToken(coin.token));
|
|
209
200
|
}
|
|
210
201
|
else {
|
|
211
|
-
parts.push(
|
|
202
|
+
parts.push(writeMiniByte(false));
|
|
212
203
|
}
|
|
213
|
-
return
|
|
204
|
+
return concat(...parts);
|
|
214
205
|
}
|
|
215
|
-
function serializeTransaction(tx) {
|
|
206
|
+
export function serializeTransaction(tx) {
|
|
216
207
|
const parts = [];
|
|
217
|
-
parts.push(
|
|
208
|
+
parts.push(writeMiniNumber(BigInt(tx.inputs.length)));
|
|
218
209
|
for (const input of tx.inputs) {
|
|
219
210
|
parts.push(serializeCoin(input));
|
|
220
211
|
}
|
|
221
|
-
parts.push(
|
|
212
|
+
parts.push(writeMiniNumber(BigInt(tx.outputs.length)));
|
|
222
213
|
for (const output of tx.outputs) {
|
|
223
214
|
parts.push(serializeCoin(output));
|
|
224
215
|
}
|
|
225
|
-
parts.push(
|
|
216
|
+
parts.push(writeMiniNumber(BigInt(tx.state.length)));
|
|
226
217
|
for (const sv of tx.state) {
|
|
227
218
|
parts.push(serializeStateVariable(sv));
|
|
228
219
|
}
|
|
229
|
-
parts.push(
|
|
230
|
-
return
|
|
220
|
+
parts.push(writeHashToStream(tx.linkHash));
|
|
221
|
+
return concat(...parts);
|
|
231
222
|
}
|
|
232
|
-
function computeTransactionDigest(tx) {
|
|
223
|
+
export function computeTransactionDigest(tx) {
|
|
233
224
|
const serialized = serializeTransaction(tx);
|
|
234
|
-
return
|
|
225
|
+
return sha3_256(serialized);
|
|
235
226
|
}
|
|
236
|
-
function precomputeTransactionCoinID(tx) {
|
|
227
|
+
export function precomputeTransactionCoinID(tx) {
|
|
237
228
|
if (tx.inputs.length === 0)
|
|
238
229
|
return;
|
|
239
230
|
const baseCoinId = tx.inputs[0].coinId;
|
|
@@ -242,13 +233,13 @@ function precomputeTransactionCoinID(tx) {
|
|
|
242
233
|
`(placeholder?). Real coinId from CoinProof required for signing.`);
|
|
243
234
|
}
|
|
244
235
|
for (let i = 0; i < tx.outputs.length; i++) {
|
|
245
|
-
const baseCoinIdStream =
|
|
246
|
-
const outputIndexStream =
|
|
247
|
-
const combined =
|
|
248
|
-
tx.outputs[i].coinId =
|
|
236
|
+
const baseCoinIdStream = writeMiniData(baseCoinId);
|
|
237
|
+
const outputIndexStream = writeMiniNumber(BigInt(i));
|
|
238
|
+
const combined = concat(baseCoinIdStream, outputIndexStream);
|
|
239
|
+
tx.outputs[i].coinId = sha3_256(combined);
|
|
249
240
|
}
|
|
250
241
|
}
|
|
251
|
-
function createDefaultTransaction() {
|
|
242
|
+
export function createDefaultTransaction() {
|
|
252
243
|
return {
|
|
253
244
|
linkHash: ZERO_TXPOWID,
|
|
254
245
|
inputs: [],
|
|
@@ -256,7 +247,7 @@ function createDefaultTransaction() {
|
|
|
256
247
|
state: [],
|
|
257
248
|
};
|
|
258
249
|
}
|
|
259
|
-
function buildMinimaCoin(opts) {
|
|
250
|
+
export function buildMinimaCoin(opts) {
|
|
260
251
|
if (opts.coinProofData) {
|
|
261
252
|
const cpd = opts.coinProofData;
|
|
262
253
|
return {
|