Skip to content

Commit

Permalink
refactor: move secp256k1 functions to a inner name space
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Jan 22, 2025
1 parent c127ce8 commit b2aa15d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
64 changes: 34 additions & 30 deletions packages/bindings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,41 +384,45 @@ export class Ripemd160 {
finalize(): ArrayBuffer;
}


/**
* Recover raw public key from signature and message hash
* @param signature - The 64-byte signature
* @param recoveryId - The recovery ID (0-3)
* @param messageHash - The 32-byte message hash
* @returns The recovered raw public key (64-bytes)
* Secp256k1 cryptographic functions
*/
export function recover(signature: ArrayBuffer, recoveryId: number, messageHash: ArrayBuffer): ArrayBuffer;
export const secp256k1: {
/**
* Recover raw public key from signature and message hash
* @param signature - The 64-byte signature
* @param recoveryId - The recovery ID (0-3)
* @param messageHash - The 32-byte message hash
* @returns The recovered raw public key (64-bytes)
*/
recover(signature: ArrayBuffer, recoveryId: number, messageHash: ArrayBuffer): ArrayBuffer;

/**
* Serialize a raw public key (64-bytes) to serialized format(compressed or uncompressed)
* @param pubkey - The raw public key to serialize
* @param compressed - Whether to use compressed format (33 bytes) or
* uncompressed (65 bytes)
* @returns The serialized public key (33 or 65 bytes)
*/
export function serializePubkey(pubkey: ArrayBuffer, compressed?: boolean): ArrayBuffer;
/**
* Serialize a raw public key (64-bytes) to serialized format(compressed or uncompressed)
* @param pubkey - The raw public key to serialize
* @param compressed - Whether to use compressed format (33 bytes) or
* uncompressed (65 bytes)
* @returns The serialized public key (33 or 65 bytes)
*/
serializePubkey(pubkey: ArrayBuffer, compressed?: boolean): ArrayBuffer;

/**
* Parse a serialized public key(compressed or uncompressed) to raw public key. It
* is the reverse function of serializePubkey.
* @param serializedPubkey - The serialized format public key (33 or 65 bytes)
* @returns The parsed raw public key (64-bytes)
*/
export function parsePubkey(serializedPubkey: ArrayBuffer): ArrayBuffer;
/**
* Parse a serialized public key(compressed or uncompressed) to raw public key. It
* is the reverse function of serializePubkey.
* @param serializedPubkey - The serialized format public key (33 or 65 bytes)
* @returns The parsed raw public key (64-bytes)
*/
parsePubkey(serializedPubkey: ArrayBuffer): ArrayBuffer;

/**
* Verify an ECDSA signature
* @param signature - The 64-byte signature
* @param messageHash - The 32-byte message hash
* @param pubkey - The raw public key (64-bytes)
* @returns True if signature is valid, false otherwise
*/
export function verify(signature: ArrayBuffer, messageHash: ArrayBuffer, pubkey: ArrayBuffer): boolean;
/**
* Verify an ECDSA signature
* @param signature - The 64-byte signature
* @param messageHash - The 32-byte message hash
* @param pubkey - The raw public key (64-bytes)
* @returns True if signature is valid, false otherwise
*/
verify(signature: ArrayBuffer, messageHash: ArrayBuffer, pubkey: ArrayBuffer): boolean;
};

/**
* Sparse Merkle Tree implementation
Expand Down
11 changes: 8 additions & 3 deletions src/secp256k1_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,25 @@ static JSValue verify(JSContext *ctx, JSValueConst this_val, int argc, JSValueCo
return JS_NewBool(ctx, result == 1);
}

static const JSCFunctionListEntry js_secp256k1_funcs[] = {
static const JSCFunctionListEntry secp256k1_obj_funcs[] = {
JS_CFUNC_DEF("recover", 3, recover),
JS_CFUNC_DEF("serializePubkey", 2, serialize_pubkey),
JS_CFUNC_DEF("parsePubkey", 1, parse_pubkey),
JS_CFUNC_DEF("verify", 3, verify),
};

int qjs_init_module_secp256k1_lazy(JSContext *ctx, JSModuleDef *m) {
JS_SetModuleExportList(ctx, m, js_secp256k1_funcs, countof(js_secp256k1_funcs));
JSValue secp256k1_obj;

secp256k1_obj = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, secp256k1_obj, secp256k1_obj_funcs, countof(secp256k1_obj_funcs));
JS_SetModuleExport(ctx, m, "secp256k1", secp256k1_obj);

return 0;
}

int qjs_init_module_secp256k1(JSContext *js_ctx, JSModuleDef *m) {
g_secp256k1_context = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
JS_AddModuleExportList(js_ctx, m, js_secp256k1_funcs, countof(js_secp256k1_funcs));
JS_AddModuleExport(js_ctx, m, "secp256k1");
return 0;
}
2 changes: 1 addition & 1 deletion tests/module/test_secp256k1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as secp256k1 from '@ckb-js-std/bindings';
import { secp256k1 } from '@ckb-js-std/bindings';
import * as ckb from "@ckb-js-std/bindings";
import * as misc from "@ckb-js-std/bindings";

Expand Down

0 comments on commit b2aa15d

Please sign in to comment.