1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::Result;
use data_encoding::HEXUPPER;
use ring::digest::{Context, Digest, SHA256};
use serde::{Deserialize, Serialize};
use std::io::Read;
use uuid::Uuid;
use wascap::jwt::Claims;
use wascap::prelude::KeyPair;
const URL_SCHEME: &str = "wasmbus";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Invocation {
pub origin: Entity,
pub target: Entity,
pub operation: String,
pub msg: Vec<u8>,
pub id: String,
pub encoded_claims: String,
pub host_id: String,
}
impl Invocation {
pub fn new(
hostkey: &KeyPair,
origin: Entity,
target: Entity,
op: &str,
msg: Vec<u8>,
) -> Invocation {
let subject = format!("{}", Uuid::new_v4());
let issuer = hostkey.public_key();
let target_url = format!("{}/{}", target.url(), op);
let claims = Claims::<wascap::prelude::Invocation>::new(
issuer.to_string(),
subject.to_string(),
&target_url,
&origin.url(),
&invocation_hash(&target_url, &origin.url(), &msg),
);
Invocation {
origin,
target,
operation: op.to_string(),
msg,
id: subject,
encoded_claims: claims.encode(&hostkey).unwrap(),
host_id: issuer,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct InvocationResponse {
pub msg: Vec<u8>,
pub error: Option<String>,
pub invocation_id: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Eq, Hash)]
pub enum Entity {
Actor(String),
Capability {
id: String,
contract_id: String,
link_name: String,
},
}
impl Entity {
pub fn url(&self) -> String {
match self {
Entity::Actor(pk) => format!("{}://{}", URL_SCHEME, pk),
Entity::Capability {
id,
contract_id,
link_name,
} => format!(
"{}://{}/{}/{}",
URL_SCHEME,
contract_id
.replace(":", "/")
.replace(" ", "_")
.to_lowercase(),
link_name.replace(" ", "_").to_lowercase(),
id
),
}
}
}
fn sha256_digest<R: Read>(mut reader: R) -> Result<Digest> {
let mut context = Context::new(&SHA256);
let mut buffer = [0; 1024];
loop {
let count = reader.read(&mut buffer)?;
if count == 0 {
break;
}
context.update(&buffer[..count]);
}
Ok(context.finish())
}
pub(crate) fn invocation_hash(target_url: &str, origin_url: &str, msg: &[u8]) -> String {
use std::io::Write;
let mut cleanbytes: Vec<u8> = Vec::new();
cleanbytes.write_all(origin_url.as_bytes()).unwrap();
cleanbytes.write_all(target_url.as_bytes()).unwrap();
cleanbytes.write_all(msg).unwrap();
let digest = sha256_digest(cleanbytes.as_slice()).unwrap();
HEXUPPER.encode(digest.as_ref())
}