rootchain-wasm 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 RootChain
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # rootchain-wasm
2
+
3
+ ### WebAssembly Cryptographic Primitives for the RootChain Ecosystem
4
+
5
+ `rootchain-wasm` provides the core cryptographic and transaction-processing logic for RootChain, compiled to highly-optimized WebAssembly. It is designed to be used in performance-critical environments like the **@rootchain/sdk** to handle secure key derivation and offline transaction signing.
6
+
7
+ ## ⚡ Core Functions
8
+
9
+ ### 1. Account Derivation
10
+ Derive Ed25519 accounts deterministically from BIP-39 mnemonics.
11
+
12
+ ```typescript
13
+ import { derive_account } from 'rootchain-wasm';
14
+
15
+ const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
16
+ const account = derive_account(mnemonic, 0);
17
+
18
+ console.log(`Address: ${account.address}`);
19
+ console.log(`Secret Key: ${account.private_key}`);
20
+ ```
21
+
22
+ ### 2. Transaction Signing
23
+ Serialize and sign RootChain transactions safely within the WASM sandbox.
24
+
25
+ ```typescript
26
+ import { sign_transaction } from 'rootchain-wasm';
27
+
28
+ const tx = {
29
+ tx_type: "Transfer",
30
+ from: Array(32).fill(0), // Automatically filled by signer
31
+ to: Array(32).fill(1),
32
+ amount: "1000",
33
+ fee: "10",
34
+ nonce: 1,
35
+ payload: [],
36
+ signature: Array(64).fill(0)
37
+ };
38
+
39
+ const signedTxJson = sign_transaction(JSON.stringify(tx), privateKeyHex);
40
+ ```
41
+
42
+ ### 3. Utility Primitives
43
+ - **`generate_mnemonic()`**: Generate new 12-word English recovery phrases.
44
+ - **`get_address_from_key(privKey)`**: Resolve a public address from a raw secret key.
45
+ - **`get_contract_address(from, nonce)`**: Deterministically calculate the address for the next deployed smart contract.
46
+
47
+ ## 🛠️ Build Requirements
48
+
49
+ To rebuild the package from source, you will need `wasm-pack`:
50
+
51
+ ```bash
52
+ cargo install wasm-pack
53
+ wasm-pack build --target bundler
54
+ ```
55
+
56
+ ## 📄 License
57
+ This project is licensed under the MIT License.
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "rootchain-wasm",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Charles Bronzon"
6
+ ],
7
+ "description": "WebAssembly primitives for RootChain: transaction signing, account derivation, and cryptography.",
8
+ "version": "1.0.1",
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/BronzonTech-Cloud/rootchain"
13
+ },
14
+ "files": [
15
+ "rootchain_wasm_bg.wasm",
16
+ "rootchain_wasm.js",
17
+ "rootchain_wasm_bg.js",
18
+ "rootchain_wasm.d.ts"
19
+ ],
20
+ "main": "rootchain_wasm.js",
21
+ "types": "rootchain_wasm.d.ts",
22
+ "sideEffects": [
23
+ "./rootchain_wasm.js",
24
+ "./snippets/*"
25
+ ],
26
+ "keywords": [
27
+ "rootchain",
28
+ "blockchain",
29
+ "wasm",
30
+ "cryptography",
31
+ "ed25519"
32
+ ]
33
+ }
@@ -0,0 +1,12 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export function derive_account(phrase: string, index: number): any;
5
+
6
+ export function generate_mnemonic(): string;
7
+
8
+ export function get_address_from_key(private_key_hex: string): string;
9
+
10
+ export function get_contract_address(from_hex: string, nonce: bigint): string;
11
+
12
+ export function sign_transaction(tx_json: string, private_key_hex: string): string;
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./rootchain_wasm.d.ts" */
2
+
3
+ import * as wasm from "./rootchain_wasm_bg.wasm";
4
+ import { __wbg_set_wasm } from "./rootchain_wasm_bg.js";
5
+ __wbg_set_wasm(wasm);
6
+ wasm.__wbindgen_start();
7
+ export {
8
+ derive_account, generate_mnemonic, get_address_from_key, get_contract_address, sign_transaction
9
+ } from "./rootchain_wasm_bg.js";
@@ -0,0 +1,349 @@
1
+ /**
2
+ * @param {string} phrase
3
+ * @param {number} index
4
+ * @returns {any}
5
+ */
6
+ export function derive_account(phrase, index) {
7
+ const ptr0 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
8
+ const len0 = WASM_VECTOR_LEN;
9
+ const ret = wasm.derive_account(ptr0, len0, index);
10
+ if (ret[2]) {
11
+ throw takeFromExternrefTable0(ret[1]);
12
+ }
13
+ return takeFromExternrefTable0(ret[0]);
14
+ }
15
+
16
+ /**
17
+ * @returns {string}
18
+ */
19
+ export function generate_mnemonic() {
20
+ let deferred1_0;
21
+ let deferred1_1;
22
+ try {
23
+ const ret = wasm.generate_mnemonic();
24
+ deferred1_0 = ret[0];
25
+ deferred1_1 = ret[1];
26
+ return getStringFromWasm0(ret[0], ret[1]);
27
+ } finally {
28
+ wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @param {string} private_key_hex
34
+ * @returns {string}
35
+ */
36
+ export function get_address_from_key(private_key_hex) {
37
+ let deferred3_0;
38
+ let deferred3_1;
39
+ try {
40
+ const ptr0 = passStringToWasm0(private_key_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
41
+ const len0 = WASM_VECTOR_LEN;
42
+ const ret = wasm.get_address_from_key(ptr0, len0);
43
+ var ptr2 = ret[0];
44
+ var len2 = ret[1];
45
+ if (ret[3]) {
46
+ ptr2 = 0; len2 = 0;
47
+ throw takeFromExternrefTable0(ret[2]);
48
+ }
49
+ deferred3_0 = ptr2;
50
+ deferred3_1 = len2;
51
+ return getStringFromWasm0(ptr2, len2);
52
+ } finally {
53
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * @param {string} from_hex
59
+ * @param {bigint} nonce
60
+ * @returns {string}
61
+ */
62
+ export function get_contract_address(from_hex, nonce) {
63
+ let deferred3_0;
64
+ let deferred3_1;
65
+ try {
66
+ const ptr0 = passStringToWasm0(from_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
67
+ const len0 = WASM_VECTOR_LEN;
68
+ const ret = wasm.get_contract_address(ptr0, len0, nonce);
69
+ var ptr2 = ret[0];
70
+ var len2 = ret[1];
71
+ if (ret[3]) {
72
+ ptr2 = 0; len2 = 0;
73
+ throw takeFromExternrefTable0(ret[2]);
74
+ }
75
+ deferred3_0 = ptr2;
76
+ deferred3_1 = len2;
77
+ return getStringFromWasm0(ptr2, len2);
78
+ } finally {
79
+ wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
80
+ }
81
+ }
82
+
83
+ /**
84
+ * @param {string} tx_json
85
+ * @param {string} private_key_hex
86
+ * @returns {string}
87
+ */
88
+ export function sign_transaction(tx_json, private_key_hex) {
89
+ let deferred4_0;
90
+ let deferred4_1;
91
+ try {
92
+ const ptr0 = passStringToWasm0(tx_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
93
+ const len0 = WASM_VECTOR_LEN;
94
+ const ptr1 = passStringToWasm0(private_key_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
95
+ const len1 = WASM_VECTOR_LEN;
96
+ const ret = wasm.sign_transaction(ptr0, len0, ptr1, len1);
97
+ var ptr3 = ret[0];
98
+ var len3 = ret[1];
99
+ if (ret[3]) {
100
+ ptr3 = 0; len3 = 0;
101
+ throw takeFromExternrefTable0(ret[2]);
102
+ }
103
+ deferred4_0 = ptr3;
104
+ deferred4_1 = len3;
105
+ return getStringFromWasm0(ptr3, len3);
106
+ } finally {
107
+ wasm.__wbindgen_free(deferred4_0, deferred4_1, 1);
108
+ }
109
+ }
110
+ export function __wbg_String_8564e559799eccda(arg0, arg1) {
111
+ const ret = String(arg1);
112
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
113
+ const len1 = WASM_VECTOR_LEN;
114
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
115
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
116
+ }
117
+ export function __wbg___wbindgen_is_function_3c846841762788c1(arg0) {
118
+ const ret = typeof(arg0) === 'function';
119
+ return ret;
120
+ }
121
+ export function __wbg___wbindgen_is_object_781bc9f159099513(arg0) {
122
+ const val = arg0;
123
+ const ret = typeof(val) === 'object' && val !== null;
124
+ return ret;
125
+ }
126
+ export function __wbg___wbindgen_is_string_7ef6b97b02428fae(arg0) {
127
+ const ret = typeof(arg0) === 'string';
128
+ return ret;
129
+ }
130
+ export function __wbg___wbindgen_is_undefined_52709e72fb9f179c(arg0) {
131
+ const ret = arg0 === undefined;
132
+ return ret;
133
+ }
134
+ export function __wbg___wbindgen_throw_6ddd609b62940d55(arg0, arg1) {
135
+ throw new Error(getStringFromWasm0(arg0, arg1));
136
+ }
137
+ export function __wbg_call_2d781c1f4d5c0ef8() { return handleError(function (arg0, arg1, arg2) {
138
+ const ret = arg0.call(arg1, arg2);
139
+ return ret;
140
+ }, arguments); }
141
+ export function __wbg_crypto_38df2bab126b63dc(arg0) {
142
+ const ret = arg0.crypto;
143
+ return ret;
144
+ }
145
+ export function __wbg_getRandomValues_c44a50d8cfdaebeb() { return handleError(function (arg0, arg1) {
146
+ arg0.getRandomValues(arg1);
147
+ }, arguments); }
148
+ export function __wbg_length_ea16607d7b61445b(arg0) {
149
+ const ret = arg0.length;
150
+ return ret;
151
+ }
152
+ export function __wbg_msCrypto_bd5a034af96bcba6(arg0) {
153
+ const ret = arg0.msCrypto;
154
+ return ret;
155
+ }
156
+ export function __wbg_new_ab79df5bd7c26067() {
157
+ const ret = new Object();
158
+ return ret;
159
+ }
160
+ export function __wbg_new_with_length_825018a1616e9e55(arg0) {
161
+ const ret = new Uint8Array(arg0 >>> 0);
162
+ return ret;
163
+ }
164
+ export function __wbg_node_84ea875411254db1(arg0) {
165
+ const ret = arg0.node;
166
+ return ret;
167
+ }
168
+ export function __wbg_process_44c7a14e11e9f69e(arg0) {
169
+ const ret = arg0.process;
170
+ return ret;
171
+ }
172
+ export function __wbg_prototypesetcall_d62e5099504357e6(arg0, arg1, arg2) {
173
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
174
+ }
175
+ export function __wbg_randomFillSync_6c25eac9869eb53c() { return handleError(function (arg0, arg1) {
176
+ arg0.randomFillSync(arg1);
177
+ }, arguments); }
178
+ export function __wbg_require_b4edbdcf3e2a1ef0() { return handleError(function () {
179
+ const ret = module.require;
180
+ return ret;
181
+ }, arguments); }
182
+ export function __wbg_set_6be42768c690e380(arg0, arg1, arg2) {
183
+ arg0[arg1] = arg2;
184
+ }
185
+ export function __wbg_static_accessor_GLOBAL_8adb955bd33fac2f() {
186
+ const ret = typeof global === 'undefined' ? null : global;
187
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
188
+ }
189
+ export function __wbg_static_accessor_GLOBAL_THIS_ad356e0db91c7913() {
190
+ const ret = typeof globalThis === 'undefined' ? null : globalThis;
191
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
192
+ }
193
+ export function __wbg_static_accessor_SELF_f207c857566db248() {
194
+ const ret = typeof self === 'undefined' ? null : self;
195
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
196
+ }
197
+ export function __wbg_static_accessor_WINDOW_bb9f1ba69d61b386() {
198
+ const ret = typeof window === 'undefined' ? null : window;
199
+ return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
200
+ }
201
+ export function __wbg_subarray_a068d24e39478a8a(arg0, arg1, arg2) {
202
+ const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0);
203
+ return ret;
204
+ }
205
+ export function __wbg_versions_276b2795b1c6a219(arg0) {
206
+ const ret = arg0.versions;
207
+ return ret;
208
+ }
209
+ export function __wbindgen_cast_0000000000000001(arg0, arg1) {
210
+ // Cast intrinsic for `Ref(Slice(U8)) -> NamedExternref("Uint8Array")`.
211
+ const ret = getArrayU8FromWasm0(arg0, arg1);
212
+ return ret;
213
+ }
214
+ export function __wbindgen_cast_0000000000000002(arg0, arg1) {
215
+ // Cast intrinsic for `Ref(String) -> Externref`.
216
+ const ret = getStringFromWasm0(arg0, arg1);
217
+ return ret;
218
+ }
219
+ export function __wbindgen_init_externref_table() {
220
+ const table = wasm.__wbindgen_externrefs;
221
+ const offset = table.grow(4);
222
+ table.set(0, undefined);
223
+ table.set(offset + 0, undefined);
224
+ table.set(offset + 1, null);
225
+ table.set(offset + 2, true);
226
+ table.set(offset + 3, false);
227
+ }
228
+ function addToExternrefTable0(obj) {
229
+ const idx = wasm.__externref_table_alloc();
230
+ wasm.__wbindgen_externrefs.set(idx, obj);
231
+ return idx;
232
+ }
233
+
234
+ function getArrayU8FromWasm0(ptr, len) {
235
+ ptr = ptr >>> 0;
236
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
237
+ }
238
+
239
+ let cachedDataViewMemory0 = null;
240
+ function getDataViewMemory0() {
241
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
242
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
243
+ }
244
+ return cachedDataViewMemory0;
245
+ }
246
+
247
+ function getStringFromWasm0(ptr, len) {
248
+ ptr = ptr >>> 0;
249
+ return decodeText(ptr, len);
250
+ }
251
+
252
+ let cachedUint8ArrayMemory0 = null;
253
+ function getUint8ArrayMemory0() {
254
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
255
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
256
+ }
257
+ return cachedUint8ArrayMemory0;
258
+ }
259
+
260
+ function handleError(f, args) {
261
+ try {
262
+ return f.apply(this, args);
263
+ } catch (e) {
264
+ const idx = addToExternrefTable0(e);
265
+ wasm.__wbindgen_exn_store(idx);
266
+ }
267
+ }
268
+
269
+ function isLikeNone(x) {
270
+ return x === undefined || x === null;
271
+ }
272
+
273
+ function passStringToWasm0(arg, malloc, realloc) {
274
+ if (realloc === undefined) {
275
+ const buf = cachedTextEncoder.encode(arg);
276
+ const ptr = malloc(buf.length, 1) >>> 0;
277
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
278
+ WASM_VECTOR_LEN = buf.length;
279
+ return ptr;
280
+ }
281
+
282
+ let len = arg.length;
283
+ let ptr = malloc(len, 1) >>> 0;
284
+
285
+ const mem = getUint8ArrayMemory0();
286
+
287
+ let offset = 0;
288
+
289
+ for (; offset < len; offset++) {
290
+ const code = arg.charCodeAt(offset);
291
+ if (code > 0x7F) break;
292
+ mem[ptr + offset] = code;
293
+ }
294
+ if (offset !== len) {
295
+ if (offset !== 0) {
296
+ arg = arg.slice(offset);
297
+ }
298
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
299
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
300
+ const ret = cachedTextEncoder.encodeInto(arg, view);
301
+
302
+ offset += ret.written;
303
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
304
+ }
305
+
306
+ WASM_VECTOR_LEN = offset;
307
+ return ptr;
308
+ }
309
+
310
+ function takeFromExternrefTable0(idx) {
311
+ const value = wasm.__wbindgen_externrefs.get(idx);
312
+ wasm.__externref_table_dealloc(idx);
313
+ return value;
314
+ }
315
+
316
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
317
+ cachedTextDecoder.decode();
318
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
319
+ let numBytesDecoded = 0;
320
+ function decodeText(ptr, len) {
321
+ numBytesDecoded += len;
322
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
323
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
324
+ cachedTextDecoder.decode();
325
+ numBytesDecoded = len;
326
+ }
327
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
328
+ }
329
+
330
+ const cachedTextEncoder = new TextEncoder();
331
+
332
+ if (!('encodeInto' in cachedTextEncoder)) {
333
+ cachedTextEncoder.encodeInto = function (arg, view) {
334
+ const buf = cachedTextEncoder.encode(arg);
335
+ view.set(buf);
336
+ return {
337
+ read: arg.length,
338
+ written: buf.length
339
+ };
340
+ };
341
+ }
342
+
343
+ let WASM_VECTOR_LEN = 0;
344
+
345
+
346
+ let wasm;
347
+ export function __wbg_set_wasm(val) {
348
+ wasm = val;
349
+ }
Binary file