Skip to content

Commit

Permalink
wasm, core: impl external key cancel order action (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc authored Dec 24, 2024
1 parent 1cac836 commit a419bc2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
32 changes: 21 additions & 11 deletions packages/core/src/actions/cancelOrder.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -18,27 +18,37 @@ export type CancelOrderReturnType = { taskId: string }
export type CancelOrderErrorType = BaseErrorType

export async function cancelOrder(
config: Config,
config: RenegadeConfig,
parameters: CancelOrderParameters,
): Promise<CancelOrderReturnType> {
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<String>
seed ?? '',
stringifyForWasm(wallet),
id,
renegadeKeyType,
newPublicKey,
signMessage,
)

try {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>}
*/
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<any>;
/**
* @param {string} seed
* @param {string} wallet_str
Expand Down
11 changes: 6 additions & 5 deletions wasm/src/external_api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
sign_message: Option<Function>,
) -> Result<JsValue, JsError> {
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,
Expand All @@ -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,
};

Expand Down

0 comments on commit a419bc2

Please sign in to comment.