Skip to content

Commit 2e967da

Browse files
committed
fix tests and cli to send signed registration requests
1 parent da67893 commit 2e967da

File tree

7 files changed

+115
-61
lines changed

7 files changed

+115
-61
lines changed

cli/src/main.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
174174
let ic_auth = dcc_to_ic_auth(&dcc_ident);
175175

176176
info!("Registering principal: {} as {}", np_desc, dcc_ident);
177+
let pubkey_bytes = dcc_ident.to_bytes_verifying();
178+
let pubkey_signature = dcc_ident.sign(pubkey_bytes.as_ref())?;
177179
let result = ledger_canister(ic_auth)
178180
.await?
179181
.node_provider_register(
180-
&dcc_ident.to_bytes_verifying(),
181-
dcc_ident.verifying_key().as_ref(),
182+
&pubkey_bytes,
183+
pubkey_signature.to_bytes().as_slice(),
182184
)
183185
.await?;
184186
println!("Register: {}", result);
@@ -219,7 +221,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
219221
.map_err(|e| format!("Check-in failed: {}", e))?;
220222
info!("Check-in success: {}", result);
221223
} else {
222-
panic!("You must specify an identity to register");
224+
panic!("You must specify an identity");
223225
}
224226
} else if arg_matches.contains_id("update-profile") {
225227
if let Some(values) = arg_matches.get_many::<String>("update-profile") {
@@ -243,7 +245,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
243245
.map_err(|e| format!("Update profile failed: {}", e))?;
244246
info!("Profile update response: {}", result);
245247
} else {
246-
panic!("You must specify an identity of the node provider");
248+
panic!("You must specify an identity");
247249
}
248250
} else if arg_matches.contains_id("update-offering") {
249251
let values = arg_matches.get_many::<String>("update-offering").unwrap();
@@ -276,13 +278,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
276278
} else if arg_matches.contains_id("register") {
277279
match arg_matches.get_one::<String>("register") {
278280
Some(np_desc) => {
279-
let dcc_ident = DccIdentity::load_from_dir(&PathBuf::from(np_desc))?;
280-
let ic_auth = dcc_to_ic_auth(&dcc_ident);
281+
let dcc_id = DccIdentity::load_from_dir(&PathBuf::from(np_desc))?;
282+
let ic_auth = dcc_to_ic_auth(&dcc_id);
281283
let canister = ledger_canister(ic_auth).await?;
282-
let args = Encode!(
283-
&dcc_ident.to_bytes_verifying(),
284-
&dcc_ident.verifying_key().as_ref()
285-
)?;
284+
let pubkey_bytes = dcc_id.to_bytes_verifying();
285+
let pubkey_signature = dcc_id.sign(&pubkey_bytes)?;
286+
let args = Encode!(&pubkey_bytes, &pubkey_signature.to_bytes())?;
286287
let result = canister.call_update("user_register", &args).await?;
287288
let response =
288289
Decode!(&result, Result<String, String>).map_err(|e| e.to_string())?;

common/src/registration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn do_account_register(
6060

6161
// Store the pubkey in the ledger
6262
ledger
63-
.upsert(label, pubkey_bytes, vec![])
63+
.upsert(label, pubkey_bytes, signature_bytes)
6464
.map(|_| {
6565
format!(
6666
"Registration complete! Thank you. You have been charged {} tokens",

common/src/renting.rs

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,98 @@
1+
use borsh::{BorshDeserialize, BorshSerialize};
2+
use serde::{Deserialize, Serialize};
3+
14
use crate::{
2-
amount_as_string, charge_fees_to_account_no_bump_reputation, info, reward_e9s_per_block, warn,
3-
Balance, DccIdentity, ED25519_SIGNATURE_LENGTH, LABEL_NP_OFFERING, MAX_NP_OFFERING_BYTES,
4-
MAX_PUBKEY_BYTES,
5+
// amount_as_string, charge_fees_to_account_no_bump_reputation, info, reward_e9s_per_block, warn,
6+
// Balance, DccIdentity, ED25519_SIGNATURE_LENGTH, LABEL_NP_OFFERING, MAX_NP_OFFERING_BYTES,
7+
// MAX_PUBKEY_BYTES,
8+
DccIdentity,
59
};
610

7-
pub struct OfferingRequestPayloadV1 {
8-
requester_pubkey_bytes: Vec<u8>,
9-
requester_ssh_pubkey: String,
10-
requester_contact: String,
11-
provider_pubkey_bytes: Vec<u8>,
12-
offering_id: String,
13-
instance_id: Option<String>,
14-
instance_config: Option<String>,
15-
payment_amount: u64,
16-
rent_period_seconds: u64,
17-
rent_start_timestamp: Option<u64>,
18-
request_memo: String,
19-
signature: Vec<u8>,
11+
// Main struct for Offering Request
12+
#[derive(Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
13+
pub enum OfferingRequest {
14+
V1(OfferingRequestV1),
2015
}
2116

22-
pub enum OfferingRequestPayload {
23-
V1(OfferingRequestPayloadV1),
17+
// Struct for Offering Request version 1, other versions can be added below
18+
#[derive(Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
19+
pub struct OfferingRequestV1 {
20+
#[serde(skip, default)]
21+
#[borsh(skip)]
22+
requester_dcc_id: DccIdentity, // Who is making this rent request?
23+
requester_ssh_pubkey: String, // The ssh key that will be given access to the instance, preferably in ed25519 key format https://en.wikipedia.org/wiki/Ssh-keygen
24+
requester_contact: String, // Where can the requester be contacted by the provider, if needed
25+
provider_pubkey_bytes: Vec<u8>, // To which provider is this targeted?
26+
offering_id: String, // Requester would like to rent this particular offering id
27+
instance_id: Option<String>, // Optional instance id that can be provided to alter the particular instance a requester already controls
28+
instance_config: Option<String>, // Optional configuration for the rented instance, e.g. cloud-init
29+
payment_amount: u64, // How much is the requester offering to pay for renting the resource
30+
rent_period_seconds: u64, // For how many SECONDS would the requester like to rent the resource; 1 hour = 3600 seconds, 1 day = 86400 seconds
31+
rent_start_timestamp: Option<u64>, // Optionally, only start renting at this unix time (in seconds) UTC. This can be in the future.
32+
request_memo: String, // Reference to this particular request; arbitrary text. Can be used e.g. for administrative purposes
2433
}
2534

26-
impl OfferingRequestPayload {
27-
fn new(
28-
requester_pubkey_bytes: Vec<u8>,
35+
impl OfferingRequest {
36+
pub fn new(
37+
requester_dcc_id: DccIdentity,
2938
requester_ssh_pubkey: String,
3039
requester_contact: String,
31-
provider_pubkey_bytes: Vec<u8>,
40+
provider_pubkey_bytes: &[u8],
3241
offering_id: String,
3342
instance_id: Option<String>,
3443
instance_config: Option<String>,
3544
payment_amount: u64,
3645
rent_period_seconds: u64,
3746
rent_start_timestamp: Option<u64>,
3847
request_memo: String,
39-
signature: Vec<u8>,
4048
) -> Self {
41-
OfferingRequestPayload::V1(OfferingRequestPayloadV1 {
42-
requester_pubkey_bytes,
49+
OfferingRequest::V1(OfferingRequestV1 {
50+
requester_dcc_id,
4351
requester_ssh_pubkey,
4452
requester_contact,
45-
provider_pubkey_bytes,
53+
provider_pubkey_bytes: provider_pubkey_bytes.to_vec(),
4654
offering_id,
4755
instance_id,
4856
instance_config,
4957
payment_amount,
5058
rent_period_seconds,
5159
rent_start_timestamp,
5260
request_memo,
53-
signature,
61+
})
62+
}
63+
64+
pub fn to_payload_signed(&self) -> OfferingRequestPayload {
65+
OfferingRequestPayload::new(self)
66+
}
67+
68+
pub fn requester_dcc_id(&self) -> &DccIdentity {
69+
match self {
70+
OfferingRequest::V1(request) => &request.requester_dcc_id,
71+
}
72+
}
73+
}
74+
75+
#[derive(Debug, Serialize, Deserialize)]
76+
pub struct OfferingRequestPayloadV1 {
77+
offering_request_bytes: Vec<u8>,
78+
signature: Vec<u8>,
79+
}
80+
81+
#[derive(Debug, Serialize, Deserialize)]
82+
pub enum OfferingRequestPayload {
83+
V1(OfferingRequestPayloadV1),
84+
}
85+
86+
impl OfferingRequestPayload {
87+
pub fn new(offering_request: &OfferingRequest) -> Self {
88+
let offering_request_bytes = borsh::to_vec(&offering_request).unwrap();
89+
OfferingRequestPayload::V1(OfferingRequestPayloadV1 {
90+
offering_request_bytes: offering_request_bytes.clone(),
91+
signature: offering_request
92+
.requester_dcc_id()
93+
.sign(&offering_request_bytes)
94+
.unwrap()
95+
.to_vec(),
5496
})
5597
}
5698
}

ic-canister/decent_cloud.did

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,21 +354,6 @@ type OfferingEntry = record {
354354
offering_compressed: vec nat8;
355355
};
356356

357-
type OfferingRentRequest = record {
358-
requester_pubkey_bytes: vec nat8; // Who is making this rent request?
359-
requester_ssh_pubkey: text; // The ssh key that will be given access to the instance, preferably in ed25519 key format https://en.wikipedia.org/wiki/Ssh-keygen
360-
requester_contact: text; // Where can the requester be contacted by the provided, if needed
361-
provider_pubkey_bytes: vec nat8; // To which provider is this targeted?
362-
offering_id: text; // Requester would like to rent this particular offering id
363-
instance_id: opt text; // optional instance id that can be provided to alter the particular instance a requester already controls
364-
instance_config: opt text; // optional configuration for the rented instance, e.g. cloud-init
365-
payment_amount: nat; // How much is the requester offering to pay for renting the resource
366-
rent_period_seconds: nat; // For how many SECONDS would the requester like to rent the resource; 1 hour = 3600 seconds, 1 day = 86400 seconds
367-
rent_start_timestamp: opt nat; // Optionally, only start renting at this unix time (in seconds) UTC. This can be in the future.
368-
request_memo: text; // Reference to this particular request; arbitrary text. Can be used e.g. for administrative purposes
369-
signature: vec nat8; // The whole request needs to be signed by the requester's private key
370-
};
371-
372357
type OfferingRentReply = record {
373358
requester_pubkey_bytes: vec nat8; // Public key of the original requester
374359
request_memo: text; // Memo field of the original request

ic-canister/src/canister_backend/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub(crate) fn _get_registration_fee() -> Balance {
142142
account_registration_fee_e9s()
143143
}
144144

145-
pub(crate) fn _node_provider_register(
145+
pub(crate) fn _np_register(
146146
pubkey_bytes: Vec<u8>,
147147
signature_bytes: Vec<u8>,
148148
) -> Result<String, String> {

ic-canister/src/canister_endpoints/generic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ fn get_registration_fee() -> Balance {
2727

2828
#[ic_cdk::update]
2929
fn node_provider_register(pubkey_bytes: Vec<u8>, signature: Vec<u8>) -> Result<String, String> {
30-
_node_provider_register(pubkey_bytes, signature)
30+
_np_register(pubkey_bytes, signature)
3131
}
3232

3333
#[ic_cdk::update]
34-
fn user_register(pubkey_bytes: Vec<u8>, _: Vec<u8>) -> Result<String, String> {
35-
_user_register(pubkey_bytes)
34+
fn user_register(pubkey_bytes: Vec<u8>, signature: Vec<u8>) -> Result<String, String> {
35+
_user_register(pubkey_bytes, signature)
3636
}
3737

3838
#[ic_cdk::update]

ic-canister/tests/test_canister.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,14 @@ fn np_register(
278278
initial_funds,
279279
);
280280
}
281+
let pubkey_bytes = dcc_identity.to_bytes_verifying();
282+
let pubkey_signature = dcc_identity.sign(&pubkey_bytes).unwrap();
281283
let result = update_check_and_decode!(
282284
pic,
283285
can,
284286
dcc_identity.to_ic_principal(),
285287
"node_provider_register",
286-
Encode!(&dcc_identity.to_bytes_verifying(), &Vec::<u8>::new()).unwrap(),
288+
Encode!(&pubkey_bytes, &pubkey_signature.to_bytes()).unwrap(),
287289
Result<String, String>
288290
);
289291
(dcc_identity, result)
@@ -304,12 +306,14 @@ fn user_register(
304306
initial_funds,
305307
);
306308
}
309+
let pubkey_bytes = dcc_identity.to_bytes_verifying();
310+
let pubkey_signature = dcc_identity.sign(&pubkey_bytes).unwrap();
307311
let result = update_check_and_decode!(
308312
pic,
309313
can,
310314
dcc_identity.to_ic_principal(),
311315
"user_register",
312-
Encode!(&dcc_identity.to_bytes_verifying(), &Vec::<u8>::new()).unwrap(),
316+
Encode!(&pubkey_bytes, &pubkey_signature.to_bytes()).unwrap(),
313317
Result<String, String>
314318
);
315319
(dcc_identity, result)
@@ -621,10 +625,32 @@ fn offering_search<T: AsRef<str> + candid::CandidType + ?Sized>(
621625
// fn offering_request(
622626
// pic: &PocketIc,
623627
// can: Principal,
624-
// dcc_id: &DccIdentity,
628+
// requester_dcc_id: DccIdentity,
629+
// provider_pubkey_bytes: &[u8],
625630
// offering_id: &str,
626631
// ) -> Result<String, String> {
627-
632+
// let payload = OfferingRequest::new(
633+
// requester_dcc_id,
634+
// "fake ssh key".to_string(),
635+
// "fake contact info".to_string(),
636+
// provider_pubkey_bytes,
637+
// offering_id.to_string(),
638+
// None,
639+
// None,
640+
// 100,
641+
// 3600,
642+
// None,
643+
// "memo".to_string(),
644+
// );
645+
// let payload_bytes = payload.to_payload_signed();
646+
// update_check_and_decode!(
647+
// pic,
648+
// can,
649+
// requester_dcc_id.to_ic_principal(),
650+
// "offering_request",
651+
// Encode!(&payload_bytes).unwrap(),
652+
// Result<String, String>
653+
// )
628654
// }
629655

630656
#[test]

0 commit comments

Comments
 (0)