@totemsdk/core 1.3.0 → 1.4.1

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.
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Compatibility adapters for the legacy JSON transaction surface.
3
+ *
4
+ * The WASM bindings are retained as `*Wasm` exports, but the public
5
+ * `serializeTransaction` / `precomputeTransactionCoinID` names route through
6
+ * the TypeScript implementation. This avoids the WASM serializer crash on
7
+ * token+state outputs and the incorrect coin-ID precompute semantics.
8
+ */
9
+ import { hexToBytes, bytesToHex } from './Streamable.js';
10
+ import { deserializeTransaction, parseDecimalToMiniNumber, precomputeTransactionCoinID as precomputeObject, serializeTransaction as serializeObject, } from './transaction.js';
11
+ import { writeMiniData, writeMiniNumber, concat } from './Streamable.js';
12
+ import { sha3_256 } from './wasm-async.js';
13
+ function bytes(value) {
14
+ if (!value || value === '0x')
15
+ return new Uint8Array([0]);
16
+ return hexToBytes(value);
17
+ }
18
+ function evenHex(value) {
19
+ const raw = value.trim().replace(/^0x/i, '');
20
+ return '0x' + (raw.length % 2 === 0 ? raw : `0${raw}`);
21
+ }
22
+ function stateVariables(input) {
23
+ return (input ?? []).map((sv) => {
24
+ const rawType = (sv.svtype ?? sv.type ?? '').toLowerCase();
25
+ if (rawType === 'bool' || rawType === 'boolean') {
26
+ return { port: sv.port, type: 'bool', value: sv.data === 'true' || sv.data === '1' || sv.data === '01' };
27
+ }
28
+ if (rawType === 'number')
29
+ return { port: sv.port, type: 'number', value: sv.data };
30
+ if (rawType === 'string')
31
+ return { port: sv.port, type: 'string', value: sv.data };
32
+ return { port: sv.port, type: 'hex', value: evenHex(sv.data) };
33
+ });
34
+ }
35
+ function token(input) {
36
+ if (!input)
37
+ return null;
38
+ const parsed = parseDecimalToMiniNumber(input.totalamount ?? '0');
39
+ return {
40
+ coinId: bytes(input.coinid),
41
+ scale: Number(input.scale ?? 0),
42
+ totalAmount: parsed.unscaledValue,
43
+ totalAmountScale: parsed.scale,
44
+ name: new TextEncoder().encode(input.name ?? ''),
45
+ script: bytes(input.script),
46
+ created: BigInt(input.created ?? '0'),
47
+ };
48
+ }
49
+ function coin(input) {
50
+ return {
51
+ coinId: bytes(input.coinid),
52
+ address: bytes(input.address),
53
+ amount: input.amount,
54
+ tokenId: bytes(input.tokenid),
55
+ token: token(input.token),
56
+ storeState: input.storestate ?? false,
57
+ state: stateVariables(input.state),
58
+ mmrEntryNumber: BigInt(input.mmrentry ?? '0'),
59
+ spent: input.spent ?? false,
60
+ created: BigInt(input.created ?? '0'),
61
+ };
62
+ }
63
+ /** Convert the legacy JSON transaction shape into the TS object shape. */
64
+ export function minimaTransactionFromJson(json) {
65
+ const parsed = JSON.parse(json);
66
+ return {
67
+ inputs: (parsed.inputs ?? []).map(coin),
68
+ outputs: (parsed.outputs ?? []).map(coin),
69
+ state: stateVariables(parsed.state),
70
+ linkHash: bytes(parsed.linkhash ?? '0x00'),
71
+ };
72
+ }
73
+ /** Public serializer: accepts the TS object or the legacy JSON string. */
74
+ export function serializeTransaction(tx) {
75
+ const value = typeof tx === 'string' ? minimaTransactionFromJson(tx) : tx;
76
+ return serializeObject(value);
77
+ }
78
+ /** Coin-ID rollover: SHA3(MiniData(coinId) || MiniNumber(index)). */
79
+ export function rollCoinId(coinId, outputIndex = 0) {
80
+ return sha3_256(concat(writeMiniData(coinId), writeMiniNumber(BigInt(outputIndex), 0)));
81
+ }
82
+ export function precomputeTransactionCoinID(tx, outputIndex = 0) {
83
+ if (typeof tx === 'object' && !(tx instanceof Uint8Array)) {
84
+ precomputeObject(tx);
85
+ return;
86
+ }
87
+ if (typeof tx === 'string') {
88
+ const value = minimaTransactionFromJson(tx);
89
+ precomputeObject(value);
90
+ return value.outputs[outputIndex]?.coinId ?? new Uint8Array();
91
+ }
92
+ // Legacy callers pass either a 32-byte input coin ID (rollover) or serialized
93
+ // transaction bytes (output coin ID). Prefer the transaction interpretation,
94
+ // but fall back to coin-ID rollover for a bare 32-byte value.
95
+ try {
96
+ const value = deserializeTransaction(tx);
97
+ if (value.inputs.length > 0) {
98
+ precomputeObject(value);
99
+ return value.outputs[outputIndex]?.coinId ?? new Uint8Array();
100
+ }
101
+ }
102
+ catch {
103
+ // not a serialized transaction
104
+ }
105
+ return rollCoinId(tx, outputIndex);
106
+ }
107
+ export { bytesToHex };
@@ -69,3 +69,13 @@ export declare function wotsKeypairFromSeed(seed: Uint8Array, index: number): {
69
69
  index: number;
70
70
  pk: Uint8Array;
71
71
  };
72
+ /**
73
+ * Compatibility initializer for the synchronous bridge.
74
+ *
75
+ * The CJS/sync build has already instantiated WASM at module load; this exists
76
+ * so the public ESM and CJS APIs expose the same `init()` contract. The ESM
77
+ * build rewrites this module to `wasm-async.js`, where init is the real
78
+ * wasm-bindgen initializer.
79
+ */
80
+ export declare function init(): Promise<void>;
81
+ export declare function initSync(): void;
package/dist/wasm-sync.js CHANGED
@@ -94,3 +94,17 @@ export function wotsKeypairFromSeed(seed, index) {
94
94
  const pk = derive_pk_digest_wasm(seed, index);
95
95
  return { seed, index, pk };
96
96
  }
97
+ /**
98
+ * Compatibility initializer for the synchronous bridge.
99
+ *
100
+ * The CJS/sync build has already instantiated WASM at module load; this exists
101
+ * so the public ESM and CJS APIs expose the same `init()` contract. The ESM
102
+ * build rewrites this module to `wasm-async.js`, where init is the real
103
+ * wasm-bindgen initializer.
104
+ */
105
+ export async function init() {
106
+ // no-op: the synchronous bridge is initialized at import time
107
+ }
108
+ export function initSync() {
109
+ // no-op: the synchronous bridge is initialized at import time
110
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@totemsdk/core",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Core cryptographic primitives for Totem SDK — backed by Rust/WASM",
5
5
  "type": "module",
6
6
  "main": "dist/cjs/index.js",
@@ -158,11 +158,12 @@
158
158
  "files": [
159
159
  "dist",
160
160
  "README.md",
161
- "LICENSE"
161
+ "LICENSE",
162
+ "NOTICE"
162
163
  ],
163
164
  "dependencies": {
164
165
  "@noble/hashes": "^1.8.0",
165
- "@totemsdk/core-wasm": "^1.0.2"
166
+ "@totemsdk/core-wasm": "^1.0.4"
166
167
  },
167
168
  "engines": {
168
169
  "node": ">=16.0.0"
@@ -177,7 +178,7 @@
177
178
  "publishConfig": {
178
179
  "access": "public"
179
180
  },
180
- "license": "MIT",
181
+ "license": "Apache-2.0",
181
182
  "author": "Totem SDK",
182
183
  "homepage": "https://docs.totem.ing",
183
184
  "bugs": {