Skip to content

Commit 8237fc8

Browse files
committed
EPROD-1192 upgrade to ic-cdk 0.18
1 parent 4ef0d70 commit 8237fc8

5 files changed

Lines changed: 51 additions & 74 deletions

File tree

Cargo.toml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ homepage = "https://github.com/bitfinity-network/bitfinity-evm-sdk"
2222
include = ["src/**/*", "LICENSE", "README.md"]
2323
license = "MIT"
2424
repository = "https://github.com/bitfinity-network/bitfinity-evm-sdk"
25-
version = "0.50.0"
25+
version = "0.51.0"
2626

2727
[workspace.dependencies]
2828
did = { path = "src/did" }
@@ -42,7 +42,7 @@ alloy = { version = "0.15", default-features = false, features = [
4242
"rlp",
4343
"serde",
4444
] }
45-
anyhow = "1.0"
45+
anyhow = "1"
4646
async-trait = "0.1"
4747
bincode = "1.3"
4848
bytes = "1"
@@ -52,11 +52,12 @@ chrono = { version = "0.4", default-features = false }
5252
derive_more = { version = "2", features = ["display", "from", "into"] }
5353
env_logger = { version = "0.11.4", default-features = false }
5454
futures = { version = "0.3", default-features = false }
55-
ic-canister = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-canister", tag = "v0.24.x" }
56-
ic-canister-client = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-canister-client", tag = "v0.24.x" }
57-
ic-exports = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-exports", tag = "v0.24.x" }
58-
ic-log = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-log", tag = "v0.24.x" }
59-
ic-stable-structures = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-stable-structures", tag = "v0.24.x" }
55+
ic-canister = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-canister", branch = "EPROD-1192-upgrade-to-ic-cdk-0-18" }
56+
ic-canister-client = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-canister-client", branch = "EPROD-1192-upgrade-to-ic-cdk-0-18" }
57+
ic-cdk = "0.18"
58+
ic-exports = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-exports", branch = "EPROD-1192-upgrade-to-ic-cdk-0-18" }
59+
ic-log = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-log", branch = "EPROD-1192-upgrade-to-ic-cdk-0-18" }
60+
ic-stable-structures = { git = "https://github.com/bitfinity-network/canister-sdk", package = "ic-stable-structures", branch = "EPROD-1192-upgrade-to-ic-cdk-0-18" }
6061
itertools = "0.14"
6162
jsonrpsee = { version = "0.25", features = ["server", "macros"] }
6263
lightspeed_scheduler = "0.63"
@@ -73,7 +74,7 @@ serde_json = "1.0"
7374
serde_with = "3.3"
7475
sha2 = "0.10"
7576
sha3 = "0.10"
76-
sqlx = { version = "0.8.1", default-features = false, features = [
77+
sqlx = { version = "0.8", default-features = false, features = [
7778
"macros",
7879
"migrate",
7980
"json",

src/eth-signer/src/ic_sign.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use alloy::signers::utils::public_key_to_address;
77
use candid::{CandidType, Principal};
88
use did::transaction::{Parity, Signature as DidSignature};
99
use ic_canister::virtual_canister_call;
10-
use ic_exports::ic_cdk::api::call::RejectionCode;
11-
use ic_exports::ic_cdk::api::management_canister::ecdsa::{
12-
EcdsaCurve, EcdsaKeyId, EcdsaPublicKeyArgument, EcdsaPublicKeyResponse, SignWithEcdsaArgument,
13-
SignWithEcdsaResponse,
10+
use ic_exports::ic_cdk::call::Error as CallError;
11+
use ic_exports::ic_cdk::management_canister::{
12+
EcdsaCurve, EcdsaKeyId, EcdsaPublicKeyArgs, EcdsaPublicKeyResult, SignWithEcdsaArgs,
13+
SignWithEcdsaResult,
1414
};
1515
use serde::{Deserialize, Serialize};
1616
use thiserror::Error;
@@ -19,8 +19,8 @@ pub type DerivationPath = Vec<Vec<u8>>;
1919

2020
#[derive(Debug, Error)]
2121
pub enum IcSignerError {
22-
#[error("IC failed to sign data with rejection code {0:?}: {1}")]
23-
SigningFailed(RejectionCode, String),
22+
#[error("IC failed to sign data: {0}")]
23+
SigningFailed(#[from] CallError),
2424

2525
#[error("from address is not specified in transaction")]
2626
FromAddressNotPresent,
@@ -128,7 +128,7 @@ impl IcSigner {
128128
key_id: SigningKeyId,
129129
derivation_path: DerivationPath,
130130
) -> Result<DidSignature, IcSignerError> {
131-
let request = SignWithEcdsaArgument {
131+
let request = SignWithEcdsaArgs {
132132
key_id: EcdsaKeyId {
133133
curve: EcdsaCurve::Secp256k1,
134134
name: key_id.to_string(),
@@ -141,11 +141,10 @@ impl IcSigner {
141141
Principal::management_canister(),
142142
"sign_with_ecdsa",
143143
(request,),
144-
SignWithEcdsaResponse,
144+
SignWithEcdsaResult,
145145
100_000_000_000
146146
)
147-
.await
148-
.map_err(|(code, msg)| IcSignerError::SigningFailed(code, msg))?
147+
.await?
149148
.signature;
150149

151150
// IC doesn't support recovery id signature parameter, so we
@@ -175,7 +174,7 @@ impl IcSigner {
175174
key_id: SigningKeyId,
176175
derivation_path: DerivationPath,
177176
) -> Result<Vec<u8>, IcSignerError> {
178-
let request = EcdsaPublicKeyArgument {
177+
let request = EcdsaPublicKeyArgs {
179178
canister_id: None,
180179
derivation_path,
181180
key_id: EcdsaKeyId {
@@ -187,10 +186,10 @@ impl IcSigner {
187186
Principal::management_canister(),
188187
"ecdsa_public_key",
189188
(request,),
190-
EcdsaPublicKeyResponse
189+
EcdsaPublicKeyResult
191190
)
192191
.await
193-
.map_err(|(code, msg)| IcSignerError::SigningFailed(code, msg))
192+
.map_err(IcSignerError::from)
194193
.map(|response| response.public_key)
195194
}
196195

@@ -212,9 +211,8 @@ mod tests {
212211
use candid::Principal;
213212
use did::H256;
214213
use ic_canister::register_virtual_responder;
215-
use ic_exports::ic_cdk::api::management_canister::ecdsa::{
216-
EcdsaPublicKeyArgument, EcdsaPublicKeyResponse, SignWithEcdsaArgument,
217-
SignWithEcdsaResponse,
214+
use ic_exports::ic_cdk::management_canister::{
215+
EcdsaPublicKeyArgs, EcdsaPublicKeyResult, SignWithEcdsaArgs, SignWithEcdsaResult,
218216
};
219217
use ic_exports::ic_kit::MockContext;
220218

@@ -232,19 +230,19 @@ mod tests {
232230
register_virtual_responder(
233231
Principal::management_canister(),
234232
"sign_with_ecdsa",
235-
move |args: (SignWithEcdsaArgument,)| {
233+
move |args: (SignWithEcdsaArgs,)| {
236234
let hash = args.0.message_hash;
237235
let h256 = H256::from_slice(&hash);
238236
let signature = wallet_to_sign.sign_hash_sync(&h256.0).unwrap();
239237
let signature: Vec<u8> = signature.as_bytes().into_iter().take(64).collect();
240-
SignWithEcdsaResponse { signature }
238+
SignWithEcdsaResult { signature }
241239
},
242240
);
243241

244242
register_virtual_responder(
245243
Principal::management_canister(),
246244
"ecdsa_public_key",
247-
move |_: (EcdsaPublicKeyArgument,)| EcdsaPublicKeyResponse {
245+
move |_: (EcdsaPublicKeyArgs,)| EcdsaPublicKeyResult {
248246
public_key: pubkey.as_bytes().to_vec(),
249247
chain_code: vec![],
250248
},

src/ethereum-json-rpc-client/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ reqwest = ["dep:reqwest"]
1919
http-outcall = ["dep:url"]
2020
# Adds an API method `sanitize_http_response` to the canister and `HttpOutcallClient::new_sanitized` method to use it.
2121
# We feature-gate it because it changes the API of the canister which is not always necessary.
22-
sanitize-http-outcall = []
22+
sanitize-http-outcall = ["dep:ic-cdk"]
2323

2424
[dependencies]
2525
alloy = { workspace = true }
2626
candid = { workspace = true }
2727
did = { workspace = true }
2828
ic-canister-client = { workspace = true, optional = true }
29+
ic-cdk = { workspace = true, optional = true }
2930
ic-exports = { workspace = true }
3031
itertools = { workspace = true }
3132
log = { workspace = true }

src/ethereum-json-rpc-client/src/error.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use did::H256;
44
use did::rpc::response::Failure;
5-
use ic_exports::ic_kit::RejectionCode;
5+
use ic_exports::ic_cdk::call::Error as CallError;
66
use thiserror::Error;
77

88
/// Result type for the Ethereum JSON-RPC client.
@@ -15,13 +15,8 @@ pub enum JsonRpcError {
1515
#[cfg(feature = "ic-canister-client")]
1616
#[error("Canister client error: {0}")]
1717
CanisterClient(#[from] ic_canister_client::CanisterClientError),
18-
#[error("Canister call failed with code: {rejection_code:?}: {message}")]
19-
CanisterCall {
20-
/// Canister [`RejectionCode`].
21-
rejection_code: RejectionCode,
22-
/// Canister rejection message.
23-
message: String,
24-
},
18+
#[error("Canister call failed: {0}")]
19+
CanisterCall(#[from] CallError),
2520
/// Error while parsing the JSON response.
2621
#[error("Invalid JSON response: {0}")]
2722
Json(#[from] serde_json::Error),

src/ethereum-json-rpc-client/src/http_outcall.rs

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use std::pin::Pin;
33

44
use did::rpc::request::RpcRequest;
55
use did::rpc::response::RpcResponse;
6-
use ic_cdk::api::management_canister::http_request::{
7-
self, CanisterHttpRequestArgument, HttpHeader, HttpMethod, TransformContext,
6+
use ic_exports::ic_cdk;
7+
use ic_exports::ic_cdk::management_canister::{
8+
self, HttpHeader, HttpMethod, HttpRequestArgs, TransformContext,
89
};
910
#[cfg(feature = "sanitize-http-outcall")]
10-
use ic_cdk::api::management_canister::http_request::{HttpResponse, TransformArgs};
11-
use ic_exports::ic_cdk;
11+
use ic_exports::ic_cdk::management_canister::{HttpRequestResult, TransformArgs};
1212

1313
use crate::{Client, JsonRpcError, JsonRpcResult};
1414

@@ -69,10 +69,15 @@ impl HttpOutcallClient {
6969
/// Only available with Cargo feature `sanitize-http-outcall`.
7070
#[cfg(feature = "sanitize-http-outcall")]
7171
pub fn sanitized(mut self) -> Self {
72-
self.transform_context = Some(TransformContext::from_name(
73-
"sanitize_http_response".into(),
74-
vec![],
75-
));
72+
use ic_exports::ic_cdk::management_canister::TransformFunc;
73+
74+
self.transform_context = Some(TransformContext {
75+
function: TransformFunc(candid::Func {
76+
method: "sanitize_http_response".to_string(),
77+
principal: ic_exports::ic_cdk::api::canister_self(),
78+
}),
79+
context: vec![],
80+
});
7681
self
7782
}
7883

@@ -91,7 +96,7 @@ impl HttpOutcallClient {
9196

9297
#[cfg(feature = "sanitize-http-outcall")]
9398
#[ic_cdk::query]
94-
fn sanitize_http_response(raw_response: TransformArgs) -> HttpResponse {
99+
fn sanitize_http_response(raw_response: TransformArgs) -> HttpRequestResult {
95100
const USE_HEADERS: &[&str] = &["content-encoding", "content-length", "content-type", "host"];
96101
let TransformArgs { mut response, .. } = raw_response;
97102
response
@@ -133,7 +138,7 @@ impl Client for HttpOutcallClient {
133138
log::trace!("Making http request to {url} with headers: {headers:?}");
134139
log::trace!("Request body is: {}", String::from_utf8_lossy(&body));
135140

136-
let request = CanisterHttpRequestArgument {
141+
let request = HttpRequestArgs {
137142
url,
138143
max_response_bytes,
139144
method: HttpMethod::POST,
@@ -142,23 +147,17 @@ impl Client for HttpOutcallClient {
142147
transform,
143148
};
144149

145-
let cost = http_request_required_cycles(&request);
150+
let cost = management_canister::cost_http_request(&request);
146151

147-
let cycles_available = ic_exports::ic_cdk::api::canister_balance128();
152+
let cycles_available = ic_exports::ic_cdk::api::canister_cycle_balance();
148153
if cycles_available < cost {
149154
return Err(JsonRpcError::InsufficientCycles {
150155
available: cycles_available,
151156
cost,
152157
});
153158
}
154159

155-
let http_response = http_request::http_request(request, cost)
156-
.await
157-
.map(|(res,)| res)
158-
.map_err(|(r, m)| JsonRpcError::CanisterCall {
159-
rejection_code: r,
160-
message: m,
161-
})?;
160+
let http_response = management_canister::http_request(&request).await?;
162161

163162
log::trace!(
164163
"CanisterClient - Response from http_outcall'. Response: {} {:?}. Body: {}",
@@ -176,23 +175,6 @@ impl Client for HttpOutcallClient {
176175
}
177176
}
178177

179-
// Calculate cycles for http_request
180-
// NOTE:
181-
// https://github.com/dfinity/cdk-rs/blob/710a6cdcc3eb03d2392df1dfd5f047dff9deee80/examples/management_canister/src/caller/lib.rs#L7-L19
182-
pub fn http_request_required_cycles(arg: &CanisterHttpRequestArgument) -> u128 {
183-
let max_response_bytes = match arg.max_response_bytes {
184-
Some(ref n) => *n as u128,
185-
None => 2 * 1024 * 1024u128, // default 2MiB
186-
};
187-
let arg_raw = candid::utils::encode_args((arg,)).expect("Failed to encode arguments.");
188-
// The fee is for a 13-node subnet to demonstrate a typical usage.
189-
(3_000_000u128
190-
+ 60_000u128 * 13
191-
+ (arg_raw.len() as u128 + "http_request".len() as u128) * 400
192-
+ max_response_bytes * 800)
193-
* 13
194-
}
195-
196178
#[cfg(test)]
197179
#[cfg(feature = "sanitize-http-outcall")]
198180
mod tests {
@@ -203,7 +185,7 @@ mod tests {
203185
#[test]
204186
fn sanitize_http_response_removes_extra_headers() {
205187
let transform_args = TransformArgs {
206-
response: HttpResponse {
188+
response: HttpRequestResult {
207189
status: 200u128.into(),
208190
headers: vec![
209191
HttpHeader {
@@ -228,7 +210,7 @@ mod tests {
228210
context: vec![],
229211
};
230212

231-
let sanitized: HttpResponse = sanitize_http_response(transform_args);
213+
let sanitized: HttpRequestResult = sanitize_http_response(transform_args);
232214
assert_eq!(sanitized.headers.len(), 3);
233215
assert_eq!(sanitized.status, Nat::from(200u128));
234216
assert!(

0 commit comments

Comments
 (0)