1use crate::Jam;
4use service::{OpaqueHash, ServiceId, service::ServiceAccount};
5
6#[derive(Default)]
8pub struct Auth {
9 pub token: Vec<u8>,
11
12 pub host: ServiceId,
14
15 pub code_hash: OpaqueHash,
17
18 pub config: Vec<u8>,
20}
21
22impl Auth {
23 pub fn with_token(mut self, token: Vec<u8>) -> Self {
25 self.token = token;
26 self
27 }
28
29 pub fn with_authorizer(mut self, service: ServiceId, code: OpaqueHash) -> Self {
31 self.host = service;
32 self.code_hash = code;
33 self
34 }
35
36 pub fn with_config(mut self, config: Vec<u8>) -> Self {
38 self.config = config;
39 self
40 }
41}
42
43impl Jam {
44 pub fn with_auth(mut self, service: ServiceId, code: Vec<u8>) -> Self {
46 let mut auth = ServiceAccount::default();
47 auth.info.balance = 1000;
48 auth.info.creation = self.chain.best.slot;
49
50 self.add_account(service, auth);
52 let hash = self.add_preimage(service, code);
53
54 if let Some(account) = self.chain.accounts.get_mut(&service) {
56 account.info.code = hash;
57 }
58
59 self.auth.code_hash = hash;
60 self.auth.host = service;
61 self
62 }
63
64 pub fn with_auth_token(mut self, token: Vec<u8>) -> Self {
66 self.auth.token = token;
67 self
68 }
69
70 pub fn with_auth_config(mut self, config: Vec<u8>) -> Self {
72 self.auth.config = config;
73 self
74 }
75
76 pub fn with_authorizer(mut self, auth: Auth) -> Self {
78 self.auth = auth;
79 self
80 }
81}