use crate::Jam;
use service::{OpaqueHash, ServiceId, service::ServiceAccount};
#[derive(Default)]
pub struct Auth {
pub token: Vec<u8>,
pub host: ServiceId,
pub code_hash: OpaqueHash,
pub config: Vec<u8>,
}
impl Auth {
pub fn with_token(mut self, token: Vec<u8>) -> Self {
self.token = token;
self
}
pub fn with_authorizer(mut self, service: ServiceId, code: OpaqueHash) -> Self {
self.host = service;
self.code_hash = code;
self
}
pub fn with_config(mut self, config: Vec<u8>) -> Self {
self.config = config;
self
}
}
impl Jam {
pub fn with_auth(mut self, service: ServiceId, code: Vec<u8>) -> Self {
let mut auth = ServiceAccount::default();
auth.info.balance = 1000;
auth.info.creation = self.chain.best.slot;
self.add_account(service, auth);
let hash = self.add_preimage(service, code);
if let Some(account) = self.chain.accounts.get_mut(&service) {
account.info.code = hash;
}
self.auth.code_hash = hash;
self.auth.host = service;
self
}
pub fn with_auth_token(mut self, token: Vec<u8>) -> Self {
self.auth.token = token;
self
}
pub fn with_auth_config(mut self, config: Vec<u8>) -> Self {
self.auth.config = config;
self
}
pub fn with_authorizer(mut self, auth: Auth) -> Self {
self.auth = auth;
self
}
}