use super::{ReadClient, ReadError, TimePolicy, authenticate_certificate};
use crate::{
ed25519::{Signature, Signer, SigningKey, VerifyingKey},
ingress::{self, Content, Envelope},
};
use candidate_types::Principal;
use ed25519_dalek::pkcs8::DecodePrivateKey;
use ic_certification::LookupResult;
use serde::Serialize;
#[derive(Clone, Copy, Debug)]
pub struct UpdateArgs<'a> {
pub recipient: Principal,
pub effective: Principal,
pub method: &'a str,
pub arg: &'a [u8],
pub expiry: u64,
pub nonce: Option<&'a [u8]>,
}
pub struct Ed25519Identity(SigningKey);
impl Ed25519Identity {
pub fn from_pem(pem: &str, maximum_bytes: usize) -> Result<Self, ReadError> {
if pem.len() > maximum_bytes {
return Err(ReadError::ByteLimit);
}
SigningKey::from_pkcs8_pem(pem)
.map(Self)
.map_err(|_| ReadError::Packet)
}
fn public_key(&self) -> Vec<u8> {
[
b"\x30\x2a\x30\x05\x06\x03\x2b\x65\x70\x03\x21\x00".as_slice(),
&self.0.verifying_key().to_bytes(),
]
.concat()
}
#[must_use]
pub fn principal(&self) -> Principal {
Principal::self_authenticating(self.public_key())
}
fn envelope(&self, content: Content, maximum: usize) -> Result<Vec<u8>, ReadError> {
let signature = self
.0
.sign(&content.signable().map_err(|_| ReadError::Packet)?);
encode(
&Envelope {
content,
sender_pubkey: Some(self.public_key()),
sender_sig: Some(signature.to_bytes().to_vec()),
sender_delegation: None,
},
maximum,
)
}
pub fn prepare_update(
&self,
args: UpdateArgs<'_>,
maximum_bytes: usize,
) -> Result<PreparedUpdate, ReadError> {
let UpdateArgs {
recipient,
effective,
method,
arg,
expiry,
nonce,
} = args;
if method
.len()
.checked_add(arg.len())
.and_then(|n| n.checked_add(nonce.map_or(0, <[u8]>::len)))
.is_none_or(|n| n > maximum_bytes)
{
return Err(ReadError::ByteLimit);
}
let bytes = self.envelope(
Content::Call {
nonce: nonce.map(<[u8]>::to_vec),
ingress_expiry: expiry,
sender: self.principal(),
canister_id: recipient,
method_name: method.to_owned(),
arg: arg.to_vec(),
sender_info: None,
},
maximum_bytes,
)?;
PreparedUpdate::from_bytes(&bytes, effective, maximum_bytes)
}
}
fn encode<T: Serialize>(value: &T, maximum: usize) -> Result<Vec<u8>, ReadError> {
struct Count {
count: usize,
maximum: usize,
}
impl std::io::Write for Count {
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
self.count = self
.count
.checked_add(bytes.len())
.filter(|n| *n <= self.maximum)
.ok_or_else(|| std::io::Error::other("message limit"))?;
Ok(bytes.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let mut count = Count { count: 0, maximum };
serde_cbor::to_writer(&mut count, value).map_err(|_| ReadError::ByteLimit)?;
let mut bytes = Vec::new();
bytes
.try_reserve_exact(count.count)
.map_err(|_| ReadError::ByteLimit)?;
serde_cbor::to_writer(&mut bytes, value).map_err(|_| ReadError::Packet)?;
Ok(bytes)
}
#[derive(Debug)]
pub struct PreparedUpdate {
bytes: Vec<u8>,
id: [u8; 32],
expiry: u64,
sender: Principal,
recipient: Principal,
effective: Principal,
}
impl PreparedUpdate {
pub fn from_bytes(
bytes: &[u8],
effective: Principal,
maximum_bytes: usize,
) -> Result<Self, ReadError> {
let envelope = ingress::decode_envelope(bytes, maximum_bytes)
.map_err(|_| ReadError::Packet)?
.value;
let Content::Call {
ingress_expiry,
sender,
canister_id,
ref sender_info,
..
} = envelope.content
else {
return Err(ReadError::Packet);
};
if envelope.sender_delegation.is_some() || sender_info.is_some() {
return Err(ReadError::Packet);
}
let public = envelope.sender_pubkey.as_deref().ok_or(ReadError::Packet)?;
let raw: &[u8; 32] = public
.strip_prefix(b"\x30\x2a\x30\x05\x06\x03\x2b\x65\x70\x03\x21\x00")
.and_then(|p| p.try_into().ok())
.ok_or(ReadError::Packet)?;
let key = VerifyingKey::from_bytes(raw).map_err(|_| ReadError::Packet)?;
if key.is_weak() || Principal::self_authenticating(public) != sender {
return Err(ReadError::Packet);
}
let signature =
Signature::from_slice(envelope.sender_sig.as_deref().ok_or(ReadError::Packet)?)
.map_err(|_| ReadError::Packet)?;
key.verify_strict(
&envelope.content.signable().map_err(|_| ReadError::Packet)?,
&signature,
)
.map_err(|_| ReadError::Packet)?;
Ok(Self {
bytes: bytes.to_vec(),
id: envelope
.content
.request_id()
.map_err(|_| ReadError::Packet)?,
expiry: ingress_expiry,
sender,
recipient: canister_id,
effective,
})
}
#[must_use]
pub fn envelope_bytes(&self) -> &[u8] {
&self.bytes
}
#[must_use]
pub const fn request_id(&self) -> &[u8; 32] {
&self.id
}
#[must_use]
pub const fn expiry(&self) -> u64 {
self.expiry
}
#[must_use]
pub const fn sender(&self) -> Principal {
self.sender
}
#[must_use]
pub const fn recipient(&self) -> Principal {
self.recipient
}
#[must_use]
pub const fn effective_canister(&self) -> Principal {
self.effective
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RequestStatus {
Received,
Processing,
Replied(Vec<u8>),
Rejected {
code: u64,
message: String,
error_code: Option<String>,
},
Done,
Unknown,
Unavailable,
}
#[derive(Debug)]
pub struct RequestObservation {
certificate: Vec<u8>,
id: [u8; 32],
effective: Principal,
time_ns: u64,
status: RequestStatus,
}
impl RequestObservation {
#[must_use]
pub fn certificate_bytes(&self) -> &[u8] {
&self.certificate
}
#[must_use]
pub const fn request_id(&self) -> &[u8; 32] {
&self.id
}
#[must_use]
pub const fn effective_canister(&self) -> Principal {
self.effective
}
#[must_use]
pub const fn time_ns(&self) -> u64 {
self.time_ns
}
#[must_use]
pub const fn status(&self) -> &RequestStatus {
&self.status
}
}
#[derive(Debug)]
pub enum Dispatch {
Accepted,
Observed(RequestObservation),
UncertifiedRejection {
code: u64,
message: String,
error_code: Option<String>,
},
}
impl ReadClient {
pub async fn dispatch(
&self,
update: &PreparedUpdate,
time: TimePolicy,
) -> Result<Dispatch, ReadError> {
if update.bytes.len() > self.config.message_bytes {
return Err(ReadError::ByteLimit);
}
let (status, bytes) = self
.post_response(
&format!("api/v3/canister/{}/call", update.effective),
update.bytes.clone(),
)
.await?;
if bytes.is_empty() && status == reqwest::StatusCode::ACCEPTED {
return Ok(Dispatch::Accepted);
}
match ingress::decode_call_response(&bytes, self.config.message_bytes)
.map_err(|_| ReadError::Packet)?
.value
{
ingress::CallResponse::Accepted => Ok(Dispatch::Accepted),
ingress::CallResponse::Replied { certificate } => self
.authenticate_request(update, &certificate, time)
.map(Dispatch::Observed),
ingress::CallResponse::NonReplicatedRejection {
reject_code,
reject_message,
error_code,
} => Ok(Dispatch::UncertifiedRejection {
code: reject_code,
message: reject_message,
error_code,
}),
}
}
pub async fn observe_request(
&self,
identity: &Ed25519Identity,
update: &PreparedUpdate,
read_expiry: u64,
time: TimePolicy,
) -> Result<RequestObservation, ReadError> {
if identity.principal() != update.sender {
return Err(ReadError::Packet);
}
let body = identity.envelope(
Content::ReadState {
ingress_expiry: read_expiry,
sender: update.sender,
paths: vec![
vec![b"request_status".to_vec(), update.id.to_vec()],
vec![b"time".to_vec()],
],
},
self.config.message_bytes,
)?;
let bytes = self
.post_bytes(
&format!("api/v3/canister/{}/read_state", update.effective),
body,
)
.await?;
let response = ingress::decode_read_state_response(&bytes, self.config.message_bytes)
.map_err(|_| ReadError::Packet)?;
self.authenticate_request(update, &response.value.certificate, time)
}
pub fn authenticate_request(
&self,
update: &PreparedUpdate,
certificate: &[u8],
time: TimePolicy,
) -> Result<RequestObservation, ReadError> {
let evidence = authenticate_certificate(
self.decode(certificate)?,
&self.config.root,
update.effective,
time,
self.config.verification,
)?;
let tree = evidence.tree();
let lookup = |name: &[u8]| {
tree.lookup_path([b"request_status".as_slice(), update.id.as_slice(), name])
};
let required = |name: &[u8]| match lookup(name) {
LookupResult::Found(bytes) => Ok(bytes),
_ => Err(ReadError::MissingPath),
};
let text = |name: &[u8]| {
std::str::from_utf8(required(name)?)
.map(str::to_owned)
.map_err(|_| ReadError::Packet)
};
let status = match lookup(b"status") {
LookupResult::Absent => RequestStatus::Unknown,
LookupResult::Unknown => RequestStatus::Unavailable,
LookupResult::Error => return Err(ReadError::Packet),
LookupResult::Found(status) => match status {
b"received" => RequestStatus::Received,
b"processing" => RequestStatus::Processing,
b"done" => RequestStatus::Done,
b"replied" => RequestStatus::Replied(required(b"reply")?.to_vec()),
b"rejected" => RequestStatus::Rejected {
code: unsigned(required(b"reject_code")?)?,
message: text(b"reject_message")?,
error_code: match lookup(b"error_code") {
LookupResult::Absent => None,
LookupResult::Found(value) => Some(
std::str::from_utf8(value)
.map_err(|_| ReadError::Packet)?
.to_owned(),
),
_ => return Err(ReadError::MissingPath),
},
},
_ => return Err(ReadError::Packet),
},
};
Ok(RequestObservation {
certificate: certificate.to_vec(),
id: update.id,
effective: update.effective,
time_ns: evidence.time_ns(),
status,
})
}
pub async fn signed_query(
&self,
identity: &Ed25519Identity,
canister: Principal,
method: &str,
args: &[u8],
expiry: u64,
time: TimePolicy,
) -> Result<Vec<u8>, ReadError> {
if args
.len()
.checked_add(method.len())
.is_none_or(|n| n > self.config.message_bytes)
{
return Err(ReadError::ByteLimit);
}
let content = ic_transport_types::EnvelopeContent::Query {
sender: identity
.principal()
.to_string()
.parse()
.map_err(|_| ReadError::Packet)?,
canister_id: canister
.to_string()
.parse()
.map_err(|_| ReadError::Packet)?,
ingress_expiry: expiry,
method_name: method.to_owned(),
arg: args.to_vec(),
nonce: None,
sender_info: None,
};
let id = ic_transport_types::to_request_id(&content).map_err(|_| ReadError::Packet)?;
let envelope = ic_transport_types::Envelope {
content: std::borrow::Cow::Borrowed(&content),
sender_pubkey: Some(identity.public_key()),
sender_sig: Some(identity.0.sign(&id.signable()).to_bytes().to_vec()),
sender_delegation: None,
};
self.query_content(
canister,
&content,
expiry,
time,
Some(encode(&envelope, self.config.message_bytes)?),
)
.await
}
}
fn unsigned(bytes: &[u8]) -> Result<u64, ReadError> {
let mut value = 0;
for (index, byte) in bytes.iter().copied().enumerate() {
if index > 9 || (index == 9 && byte > 1) {
return Err(ReadError::Packet);
}
value |= u64::from(byte & 127) << (index * 7);
if byte & 128 == 0 {
return if index + 1 == bytes.len() {
Ok(value)
} else {
Err(ReadError::Packet)
};
}
}
Err(ReadError::Packet)
}
impl ReadClient {
pub async fn canister_module_hash(
&self,
canister: Principal,
expiry: u64,
time: TimePolicy,
) -> Result<[u8; 32], ReadError> {
let bytes = self
.state(
"canister",
canister,
vec![
vec![
b"canister".to_vec(),
canister.as_slice().to_vec(),
b"module_hash".to_vec(),
],
vec![b"time".to_vec()],
],
expiry,
)
.await?;
let evidence = authenticate_certificate(
self.decode(&bytes)?,
&self.config.root,
canister,
time,
self.config.verification,
)?;
match evidence.tree().lookup_path([
b"canister".as_slice(),
canister.as_slice(),
b"module_hash".as_slice(),
]) {
LookupResult::Found(hash) => hash.try_into().map_err(|_| ReadError::Packet),
_ => Err(ReadError::MissingPath),
}
}
}