Skip to content

Commit 091639f

Browse files
committed
wasm, node: tidy exports and parse functions
1 parent 4658dd6 commit 091639f

File tree

13 files changed

+98
-109
lines changed

13 files changed

+98
-109
lines changed

packages/core/src/utils.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ export function create_request_signature(path: string, headers: any, body: strin
203203
*/
204204
export function b64_to_hex_hmac_key(b64_key: string): string;
205205
/**
206+
* @param {string} pk_root
207+
* @returns {any[]}
208+
*/
209+
export function byok_get_pk_root_scalars(pk_root: string): any[];
210+
/**
206211
* @param {string} seed
207212
* @param {bigint} nonce
208213
* @returns {any}
@@ -225,8 +230,3 @@ export function get_pk_root_scalars(seed: string, nonce: bigint): any[];
225230
* @returns {any}
226231
*/
227232
export function get_symmetric_key(seed: string): any;
228-
/**
229-
* @param {string} pk_root
230-
* @returns {any[]}
231-
*/
232-
export function byok_get_pk_root_scalars(pk_root: string): any[];

packages/node/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"types": "./dist/types/exports/actions.d.ts",
3636
"default": "./dist/exports/actions.js"
3737
},
38-
"./actions/byok": {
38+
"./byok": {
3939
"types": "./dist/types/exports/byok.d.ts",
4040
"default": "./dist/exports/byok.js"
4141
},
@@ -50,7 +50,7 @@
5050
"actions": [
5151
"./dist/types/exports/actions.d.ts"
5252
],
53-
"actions/byok": [
53+
"byok": [
5454
"./dist/types/exports/byok.d.ts"
5555
]
5656
}

packages/node/src/actions/byok/createWallet.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
CREATE_WALLET_ROUTE,
3-
type Config,
43
postRelayerRaw,
54
} from '@renegade-fi/core'
5+
import type { BYOKConfig } from '../../utils/createBYOKConfig.js'
66

77
export type CreateWalletReturnType = { taskId: string }
88

@@ -16,7 +16,7 @@ export type CreateWalletParameters = {
1616
}
1717

1818
export async function createWallet(
19-
config: Config,
19+
config: BYOKConfig,
2020
parameters: CreateWalletParameters,
2121
): Promise<CreateWalletReturnType> {
2222
const { walletId, blinderSeed, shareSeed, pkRoot, skMatch, symmetricKey } =
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
import type { Config } from '@renegade-fi/core'
21
import type { SignMessageReturnType } from 'viem'
2+
import { generate_wallet_secrets } from '../../../renegade-utils/index.js'
33

4+
/**
5+
* Interface representing the cryptographic secrets generated for a wallet
6+
*/
47
export interface GeneratedSecrets {
5-
wallet_id: string // UUID as string
6-
blinder_seed: `0x${string}` // Hex string
7-
share_seed: `0x${string}` // Hex string
8-
symmetric_key: `0x${string}` // Hex string
9-
sk_match: `0x${string}` // Hex string
8+
/** Unique identifier for the wallet in UUID format */
9+
wallet_id: string
10+
11+
/** Cryptographic seed for the wallet's blinder CSPRNG*/
12+
blinder_seed: `0x${string}`
13+
14+
/** Cryptographic seed for the wallet's share CSPRNG */
15+
share_seed: `0x${string}`
16+
17+
/** Encryption key for authenticating API requests */
18+
symmetric_key: `0x${string}`
19+
20+
/** Secret key used for matching operations */
21+
sk_match: `0x${string}`
1022
}
1123

1224
export async function generateWalletSecrets(
13-
config: Config,
1425
signMessage: (message: string) => Promise<SignMessageReturnType>,
15-
publicKey: string,
1626
): Promise<GeneratedSecrets> {
17-
const { utils } = config
18-
console.log('Generating wallet secrets for public key:', publicKey)
19-
const secrets = await utils.generate_wallet_secrets(signMessage, publicKey)
27+
const secrets = await generate_wallet_secrets(signMessage)
2028
const parsedSecrets = JSON.parse(secrets) as GeneratedSecrets
2129
return parsedSecrets
2230
}

packages/node/src/exports/byok.ts

+47-14
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,84 @@
1-
export {
2-
deposit,
3-
type DepositParameters,
4-
type DepositReturnType,
5-
} from '../actions/byok/deposit.js'
1+
import { createBYOKConfig as core_createBYOKConfig } from '../utils/createBYOKConfig.js'
2+
import * as RustUtils from '../../renegade-utils/index.js'
3+
4+
////////////////////////////////////////////////////////////////////////////////
5+
// Config
6+
////////////////////////////////////////////////////////////////////////////////
7+
8+
function createBYOKConfig(
9+
...args: Parameters<typeof core_createBYOKConfig>
10+
): ReturnType<typeof core_createBYOKConfig> {
11+
const config = core_createBYOKConfig({
12+
...args[0],
13+
utils: RustUtils,
14+
})
15+
return config
16+
}
17+
18+
export { createBYOKConfig }
19+
20+
////////////////////////////////////////////////////////////////////////////////
21+
// Actions (Bring Your Own Key)
22+
////////////////////////////////////////////////////////////////////////////////
23+
624
export {
725
createWallet,
826
type CreateWalletParameters,
927
type CreateWalletReturnType,
1028
} from '../actions/byok/createWallet.js'
29+
1130
export {
1231
generateWalletSecrets,
1332
type GeneratedSecrets,
1433
} from '../actions/byok/generateWalletSecrets.js'
34+
1535
export {
1636
getPkRootScalars,
1737
type GetPkRootParameters,
1838
type GetPkRootScalarsReturnType,
1939
} from '../actions/byok/getPkRoot.js'
40+
2041
export {
2142
getBackOfQueueWallet,
2243
type GetBackOfQueueWalletParameters,
2344
type GetBackOfQueueWalletReturnType,
2445
} from '../actions/byok/getBackOfQueueWallet.js'
46+
47+
export {
48+
lookupWallet,
49+
type LookupWalletParameters,
50+
type LookupWalletReturnType,
51+
} from '../actions/byok/lookupWallet.js'
52+
53+
export {
54+
deposit,
55+
type DepositParameters,
56+
type DepositReturnType,
57+
} from '../actions/byok/deposit.js'
58+
2559
export {
2660
withdraw,
2761
type WithdrawParameters,
2862
type WithdrawReturnType,
2963
} from '../actions/byok/withdraw.js'
64+
65+
export {
66+
payFees,
67+
type PayFeesReturnType,
68+
} from '../actions/byok/payFees.js'
69+
3070
export {
3171
createOrder,
3272
type CreateOrderParameters,
3373
type CreateOrderReturnType,
3474
} from '../actions/byok/createOrder.js'
75+
3576
export {
3677
cancelOrder,
3778
type CancelOrderParameters,
3879
type CancelOrderReturnType,
3980
} from '../actions/byok/cancelOrder.js'
40-
export {
41-
lookupWallet,
42-
type LookupWalletParameters,
43-
type LookupWalletReturnType,
44-
} from '../actions/byok/lookupWallet.js'
45-
export {
46-
payFees,
47-
type PayFeesReturnType,
48-
} from '../actions/byok/payFees.js'
81+
4982
export {
5083
getOrderHistory,
5184
type GetOrderHistoryParameters,

packages/node/src/exports/index.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
createAuthConfig as core_createAuthConfig,
77
createConfig as core_createConfig,
88
} from '@renegade-fi/core'
9-
import { createBYOKConfig as core_createBYOKConfig } from '../utils/createBYOKConfig.js'
109

1110
import * as RustUtils from '../../renegade-utils/index.js'
1211

@@ -30,17 +29,8 @@ function createAuthConfig(
3029
return config
3130
}
3231

33-
function createBYOKConfig(
34-
...args: Parameters<typeof core_createBYOKConfig>
35-
): ReturnType<typeof core_createBYOKConfig> {
36-
const config = core_createBYOKConfig({
37-
...args[0],
38-
utils: RustUtils,
39-
})
40-
return config
41-
}
4232

43-
export { createAuthConfig, createConfig, createBYOKConfig }
33+
export { createAuthConfig, createConfig }
4434

4535
////////////////////////////////////////////////////////////////////////////////
4636
// Actions

wasm/src/exports/byok/create_order.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,15 @@ pub async fn byok_create_order(
4343
let mut wallet = deserialize_wallet(wallet_str)?;
4444
handle_key_rotation(&mut wallet, &public_key)?;
4545

46-
let amount_u128 = params.amount_as_u128()?;
47-
let min_fill_size_u128 = params.min_fill_size_as_u128()?;
48-
4946
let order = ApiOrder {
5047
id: params.id,
5148
base_mint: params.base_mint,
5249
quote_mint: params.quote_mint,
5350
side: params.side,
54-
amount: amount_u128,
51+
amount: params.amount,
5552
worst_case_price: params.worst_case_price,
5653
type_: crate::external_api::types::ApiOrderType::Midpoint,
57-
min_fill_size: min_fill_size_u128,
54+
min_fill_size: params.min_fill_size,
5855
allow_external_matches: params.allow_external_matches,
5956
};
6057

wasm/src/exports/byok/find_wallet.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use wasm_bindgen::prelude::*;
22

33
use crate::{
44
exports::byok::parameters::FindWalletParameters, external_api::wallet::FindWalletRequest,
5-
serialize_to_js, types::scalar_to_biguint,
5+
serialize_to_js,
66
};
77

88
#[wasm_bindgen]
@@ -25,8 +25,8 @@ pub async fn byok_find_wallet(
2525

2626
let request = FindWalletRequest {
2727
wallet_id: params.wallet_id,
28-
blinder_seed: scalar_to_biguint(&params.blinder_seed),
29-
secret_share_seed: scalar_to_biguint(&params.share_seed),
28+
blinder_seed: params.blinder_seed,
29+
secret_share_seed: params.share_seed,
3030
private_keychain: params.key_chain.private_keys,
3131
};
3232

wasm/src/exports/byok/key_rotation.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn handle_key_rotation(wallet: &mut Wallet, new_public_key: &str) -> Result<
1818
let current_pk_root = wallet.key_chain.pk_root().to_uncompressed_bytes();
1919

2020
if current_pk_root != new_pk_root {
21-
// Key has been rotated
2221
wallet.key_chain.increment_nonce();
2322
let new_pk_root = PublicSigningKey::from_bytes(&new_pk_root)
2423
.map_err(|e| Error::new(format!("Failed to parse public key: {}", e)))?;

wasm/src/exports/byok/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ pub use create_wallet::byok_create_wallet;
2020
pub use deposit::byok_deposit;
2121
pub use find_wallet::byok_find_wallet;
2222
pub use generate_wallet_secrets::generate_wallet_secrets;
23-
pub use signature::{authorize_withdrawal, generate_signature, generate_statement_signature};
23+
pub use signature::{authorize_withdrawal, generate_signature};
2424
pub use withdraw::byok_withdraw;

wasm/src/exports/byok/parameters/order.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ pub struct CreateOrderParameters {
1313
pub base_mint: BigUint,
1414
pub quote_mint: BigUint,
1515
pub side: OrderSide,
16-
pub amount: BigUint,
16+
pub amount: u128,
1717
pub worst_case_price: FixedPoint,
18-
pub min_fill_size: BigUint,
18+
pub min_fill_size: u128,
1919
pub allow_external_matches: bool,
2020
}
2121

@@ -50,10 +50,16 @@ impl CreateOrderParameters {
5050
.map_err(|e| Error::invalid_parameter(format!("Invalid quote mint: {}", e)))?;
5151

5252
let amount = biguint_from_hex_string(amount)
53-
.map_err(|e| Error::invalid_parameter(format!("Invalid amount: {}", e)))?;
53+
.map_err(|e| Error::invalid_parameter(format!("Invalid amount: {}", e)))?
54+
.to_u128()
55+
.ok_or_else(|| Error::invalid_parameter(format!("Could not convert amount to u128")))?;
5456

5557
let min_fill_size = biguint_from_hex_string(min_fill_size)
56-
.map_err(|e| Error::invalid_parameter(format!("Invalid min fill size: {}", e)))?;
58+
.map_err(|e| Error::invalid_parameter(format!("Invalid min fill size: {}", e)))?
59+
.to_u128()
60+
.ok_or_else(|| {
61+
Error::invalid_parameter(format!("Could not convert min fill size to u128"))
62+
})?;
5763

5864
let worst_case_price = if worst_case_price.is_empty() {
5965
match side {
@@ -78,16 +84,4 @@ impl CreateOrderParameters {
7884
allow_external_matches,
7985
})
8086
}
81-
82-
pub fn amount_as_u128(&self) -> Result<u128, Error> {
83-
self.amount.to_u128().ok_or_else(|| {
84-
Error::invalid_parameter(format!("Could not convert {} to u128", self.amount))
85-
})
86-
}
87-
88-
pub fn min_fill_size_as_u128(&self) -> Result<u128, Error> {
89-
self.min_fill_size.to_u128().ok_or_else(|| {
90-
Error::invalid_parameter(format!("Could not convert {} to u128", self.min_fill_size))
91-
})
92-
}
9387
}

wasm/src/exports/byok/parameters/wallet.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use num_bigint::BigUint;
12
use serde::Serialize;
23
use uuid::Uuid;
34

@@ -73,8 +74,8 @@ impl CreateWalletParameters {
7374

7475
pub struct FindWalletParameters {
7576
pub wallet_id: Uuid,
76-
pub blinder_seed: Scalar,
77-
pub share_seed: Scalar,
77+
pub blinder_seed: BigUint,
78+
pub share_seed: BigUint,
7879
pub key_chain: ApiKeychain,
7980
}
8081

@@ -90,12 +91,10 @@ impl FindWalletParameters {
9091
// Wallet seed info
9192
let wallet_id = Uuid::parse_str(wallet_id)
9293
.map_err(|e| Error::invalid_parameter(format!("wallet_id: {}", e)))?;
93-
let blinder_seed_bigint = biguint_from_hex_string(blinder_seed)
94+
let blinder_seed = biguint_from_hex_string(blinder_seed)
9495
.map_err(|e| Error::invalid_parameter(format!("blinder_seed: {}", e)))?;
95-
let blinder_seed = Scalar::from(blinder_seed_bigint);
96-
let share_seed_bigint = biguint_from_hex_string(share_seed)
96+
let share_seed = biguint_from_hex_string(share_seed)
9797
.map_err(|e| Error::invalid_parameter(format!("share_seed: {}", e)))?;
98-
let share_seed = Scalar::from(share_seed_bigint);
9998

10099
// KeyChain
101100
let sk_match_bigint = biguint_from_hex_string(sk_match)

0 commit comments

Comments
 (0)