Skip to content

Commit

Permalink
fix: use misc.hex.encode styles
Browse files Browse the repository at this point in the history
from misc.encoding.encodeHex
  • Loading branch information
XuJiandong committed Jan 9, 2025
1 parent f54b5fb commit be9b87b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
34 changes: 22 additions & 12 deletions src/misc_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ static JSValue js_decode_base64(JSContext *ctx, JSValueConst this_val, int argc,
return JS_NewArrayBufferCopy(ctx, data, data_len);
}

// Update the function list to include hex and base64 functions under separate objects
static const JSCFunctionListEntry js_hex_funcs[] = {
JS_CFUNC_DEF("encode", 1, js_encode_hex),
JS_CFUNC_DEF("decode", 1, js_decode_hex),
};

static const JSCFunctionListEntry js_base64_funcs[] = {
JS_CFUNC_DEF("encode", 1, js_encode_base64),
JS_CFUNC_DEF("decode", 1, js_decode_base64),
};

// SMT class definition
static const JSCFunctionListEntry js_smt_proto_funcs[] = {
JS_CFUNC_DEF("insert", 2, js_smt_insert),
Expand All @@ -278,13 +289,6 @@ static const JSClassDef js_smt_class = {
.finalizer = js_smt_finalizer,
};

// Update the function list to include both SMT and encoding functions
static const JSCFunctionListEntry js_misc_funcs[] = {
JS_CFUNC_DEF("encodeHex", 1, js_encode_hex),
JS_CFUNC_DEF("decodeHex", 1, js_decode_hex),
JS_CFUNC_DEF("encodeBase64", 1, js_encode_base64),
JS_CFUNC_DEF("decodeBase64", 1, js_decode_base64),
};

static int js_misc_init(JSContext *ctx, JSModuleDef *m) {
JSValue proto, obj;
Expand All @@ -305,10 +309,15 @@ static int js_misc_init(JSContext *ctx, JSModuleDef *m) {
// Export the SMT constructor and encoding functions
JS_SetModuleExport(ctx, m, "Smt", obj);

// Create module object for encoding functions
JSValue encoding = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, encoding, js_misc_funcs, countof(js_misc_funcs));
JS_SetModuleExport(ctx, m, "encoding", encoding);
// Create hex object and add functions
JSValue hex = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, hex, js_hex_funcs, countof(js_hex_funcs));
JS_SetModuleExport(ctx, m, "hex", hex);

// Create base64 object and add functions
JSValue base64 = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, base64, js_base64_funcs, countof(js_base64_funcs));
JS_SetModuleExport(ctx, m, "base64", base64);

return 0;
}
Expand All @@ -319,6 +328,7 @@ int js_init_module_misc(JSContext *ctx) {
if (!m) return -1;

JS_AddModuleExport(ctx, m, "Smt");
JS_AddModuleExport(ctx, m, "encoding");
JS_AddModuleExport(ctx, m, "hex");
JS_AddModuleExport(ctx, m, "base64");
return 0;
}
2 changes: 1 addition & 1 deletion tests/module/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ endef
all:
$(call run,test_secp256k1.js)
$(call run,test_hash.js)
$(call debug,test_misc.js)
$(call run,test_misc.js)
42 changes: 21 additions & 21 deletions tests/module/test_misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function test_ckb_smt_verify1() {
const proofHex =
'4c4fff51ff322de8a89fe589987f97220cfcb6820bd798b31a0b56ffea221093d35f909e580b00000000000000000000000000000000000000000000000000000000000000';

// Update the conversions to use misc.encoding.decodeHex
const key = misc.encoding.decodeHex(keyHex);
const value = misc.encoding.decodeHex(valueHex);
const rootHash = misc.encoding.decodeHex(rootHashHex);
const proof = misc.encoding.decodeHex(proofHex);
// Update the conversions to use misc.hex.decode
const key = misc.hex.decode(keyHex);
const value = misc.hex.decode(valueHex);
const rootHash = misc.hex.decode(rootHashHex);
const proof = misc.hex.decode(proofHex);

// Create new SMT instance
const smt = new misc.Smt();
Expand Down Expand Up @@ -47,10 +47,10 @@ function test_ckb_smt_verify2() {
const proofHex =
'4c4fff51fa8aaa2aece17b92ec3f202a40a09f7286522bae1e5581a2a49195ab6781b1b8090000000000000000000000000000000000000000000000000000000000000000';

const key = misc.encoding.decodeHex(keyHex);
const value = misc.encoding.decodeHex(valueHex);
const rootHash = misc.encoding.decodeHex(rootHashHex);
const proof = misc.encoding.decodeHex(proofHex);
const key = misc.hex.decode(keyHex);
const value = misc.hex.decode(valueHex);
const rootHash = misc.hex.decode(rootHashHex);
const proof = misc.hex.decode(proofHex);

const smt = new misc.Smt();

Expand Down Expand Up @@ -78,10 +78,10 @@ function test_ckb_smt_verify3() {
const proofHex =
'4c4fff51fa8aaa2aece17b92ec3f202a40a09f7286522bae1e5581a2a49195ab6781b1b8090000000000000000000000000000000000000000000000000000000000000000';

const key = misc.encoding.decodeHex(keyHex);
const value = misc.encoding.decodeHex(valueHex);
const rootHash = misc.encoding.decodeHex(rootHashHex);
const proof = misc.encoding.decodeHex(proofHex);
const key = misc.hex.decode(keyHex);
const value = misc.hex.decode(valueHex);
const rootHash = misc.hex.decode(rootHashHex);
const proof = misc.hex.decode(proofHex);

const smt = new misc.Smt();

Expand Down Expand Up @@ -109,10 +109,10 @@ function test_ckb_smt_verify_invalid() {
const proofHex =
'4c50fe32845309d34f132cd6f7ac6a7881962401adc35c19a18d4fffeb511b97eabf86';

const key = misc.encoding.decodeHex(keyHex);
const value = misc.encoding.decodeHex(valueHex);
const rootHash = misc.encoding.decodeHex(rootHashHex);
const proof = misc.encoding.decodeHex(proofHex);
const key = misc.hex.decode(keyHex);
const value = misc.hex.decode(valueHex);
const rootHash = misc.hex.decode(rootHashHex);
const proof = misc.hex.decode(proofHex);

const smt = new misc.Smt();

Expand All @@ -134,8 +134,8 @@ function test_base64_encode() {
const inputHex = '48656c6c6f20576f726c6421'; // "Hello World!" in hex
const expectedBase64 = 'SGVsbG8gV29ybGQh';

const input = misc.encoding.decodeHex(inputHex);
const encoded = misc.encoding.encodeBase64(input);
const input = misc.hex.decode(inputHex);
const encoded = misc.base64.encode(input);

console.assert(encoded === expectedBase64, 'Base64 encoding failed');
console.log('test_base64_encode ok');
Expand All @@ -145,8 +145,8 @@ function test_base64_decode() {
const base64Input = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64
const expectedHex = '48656c6c6f20576f726c6421';

const decoded = misc.encoding.decodeBase64(base64Input);
const result = misc.encoding.encodeHex(decoded);
const decoded = misc.base64.decode(base64Input);
const result = misc.hex.encode(decoded);

console.assert(result === expectedHex, 'Base64 decoding failed');
console.log('test_base64_decode ok');
Expand Down

0 comments on commit be9b87b

Please sign in to comment.