From a419bc2f5dc7386a1a868b6cefed0f4b384d9194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sehyun=20Chung=20=E2=9C=8C=EF=B8=8E?= <41171808+sehyunc@users.noreply.github.com> Date: Tue, 24 Dec 2024 12:29:27 -0500 Subject: [PATCH] wasm, core: impl external key cancel order action (#135) --- packages/core/src/actions/cancelOrder.ts | 32 ++++++++++++++++-------- packages/core/src/utils.d.ts | 5 ++-- wasm/src/external_api/http.rs | 11 ++++---- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/packages/core/src/actions/cancelOrder.ts b/packages/core/src/actions/cancelOrder.ts index ee3743a..20d1b36 100644 --- a/packages/core/src/actions/cancelOrder.ts +++ b/packages/core/src/actions/cancelOrder.ts @@ -1,7 +1,7 @@ import invariant from 'tiny-invariant' import type { Hex } from 'viem' import { CANCEL_ORDER_ROUTE } from '../constants.js' -import type { Config } from '../createConfig.js' +import type { RenegadeConfig } from '../createConfig.js' import type { BaseErrorType } from '../errors/base.js' import { stringifyForWasm } from '../utils/bigJSON.js' import { postRelayerWithAuth } from '../utils/http.js' @@ -18,27 +18,37 @@ export type CancelOrderReturnType = { taskId: string } export type CancelOrderErrorType = BaseErrorType export async function cancelOrder( - config: Config, + config: RenegadeConfig, parameters: CancelOrderParameters, ): Promise { const { id, newPublicKey } = parameters - const { - getBaseUrl, - utils, - state: { seed }, - renegadeKeyType, - } = config - invariant(seed, 'Seed is required') + const { getBaseUrl, utils, renegadeKeyType } = config const walletId = getWalletId(config) const wallet = await getBackOfQueueWallet(config) - const body = utils.cancel_order( - seed, + const seed = renegadeKeyType === 'internal' ? config.state.seed : undefined + const signMessage = + renegadeKeyType === 'external' ? config.signMessage : undefined + + if (renegadeKeyType === 'external') { + invariant( + signMessage !== undefined, + 'Sign message function is required for external key type', + ) + } + if (renegadeKeyType === 'internal') { + invariant(seed !== undefined, 'Seed is required for internal key type') + } + + const body = await utils.cancel_order( + // TODO: Change Rust to accept Option + seed ?? '', stringifyForWasm(wallet), id, renegadeKeyType, newPublicKey, + signMessage, ) try { diff --git a/packages/core/src/utils.d.ts b/packages/core/src/utils.d.ts index 07e897e..2ed2991 100644 --- a/packages/core/src/utils.d.ts +++ b/packages/core/src/utils.d.ts @@ -88,9 +88,10 @@ export function new_order_in_matching_pool(seed: string, wallet_str: string, id: * @param {string} order_id * @param {string} key_type * @param {string | undefined} [new_public_key] -* @returns {any} +* @param {Function | undefined} [sign_message] +* @returns {Promise} */ -export function cancel_order(seed: string, wallet_str: string, order_id: string, key_type: string, new_public_key?: string): any; +export function cancel_order(seed: string, wallet_str: string, order_id: string, key_type: string, new_public_key?: string, sign_message?: Function): Promise; /** * @param {string} seed * @param {string} wallet_str diff --git a/wasm/src/external_api/http.rs b/wasm/src/external_api/http.rs index 3ef20e6..650597d 100644 --- a/wasm/src/external_api/http.rs +++ b/wasm/src/external_api/http.rs @@ -503,15 +503,15 @@ pub struct CancelOrderRequest { } #[wasm_bindgen] -pub fn cancel_order( +pub async fn cancel_order( seed: &str, wallet_str: &str, order_id: &str, key_type: &str, new_public_key: Option, + sign_message: Option, ) -> Result { let mut new_wallet = deserialize_wallet(wallet_str); - let old_sk_root = derive_sk_root_scalars(seed, &new_wallet.key_chain.public_keys.nonce); let next_public_key = wrap_eyre!(handle_key_rotation( &mut new_wallet, @@ -533,11 +533,12 @@ pub fn cancel_order( new_wallet.reblind_wallet(); // Sign a commitment to the new shares - let comm = new_wallet.get_wallet_share_commitment(); - let sig = wrap_eyre!(sign_commitment(&old_sk_root, comm)).unwrap(); + let statement_sig = sign_wallet_commitment(&new_wallet, seed, key_type, sign_message.as_ref()) + .await + .map_err(|e| JsError::new(&e.to_string()))?; let update_auth = WalletUpdateAuthorization { - statement_sig: sig.to_vec(), + statement_sig, new_root_key: next_public_key, };