sabre_sdk/protocol/
mod.rs

1// Copyright 2019 Cargill Incorporated
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod payload;
16pub mod state;
17
18use std::error::Error;
19
20use sha2::{Digest, Sha512};
21
22pub const SABRE_PROTOCOL_VERSION: &str = "1";
23
24pub const ADMINISTRATORS_SETTING_KEY: &str = "sawtooth.swa.administrators";
25
26pub const ADMINISTRATORS_SETTING_ADDRESS: &str =
27    "000000a87cb5eafdcca6a814e4add97c4b517d3c530c2f44b31d18e3b0c44298fc1c14";
28pub const NAMESPACE_REGISTRY_ADDRESS_PREFIX: &str = "00ec00";
29pub const CONTRACT_REGISTRY_ADDRESS_PREFIX: &str = "00ec01";
30pub const CONTRACT_ADDRESS_PREFIX: &str = "00ec02";
31pub const SMART_PERMISSION_ADDRESS_PREFIX: &str = "00ec03";
32pub const AGENT_ADDRESS_PREFIX: &str = "cad11d00";
33pub const ORG_ADDRESS_PREFIX: &str = "cad11d01";
34
35pub const ADMINISTRATORS_SETTING_ADDRESS_BYTES: &[u8] = &[
36    0, 0, 0, 168, 124, 181, 234, 253, 204, 166, 168, 20, 228, 173, 217, 124, 75, 81, 125, 60, 83,
37    12, 47, 68, 179, 29, 24, 227, 176, 196, 66, 152, 252, 28, 20,
38];
39pub const NAMESPACE_REGISTRY_ADDRESS_PREFIX_BYTES: &[u8] = &[0, 236, 0];
40pub const CONTRACT_REGISTRY_ADDRESS_PREFIX_BYTES: &[u8] = &[0, 236, 1];
41pub const CONTRACT_ADDRESS_PREFIX_BYTES: &[u8] = &[0, 236, 2];
42pub const SMART_PERMISSION_ADDRESS_PREFIX_BYTES: &[u8] = &[0, 236, 3];
43pub const AGENT_ADDRESS_PREFIX_BYTES: &[u8] = &[202, 209, 29, 0];
44pub const ORG_ADDRESS_PREFIX_BYTES: &[u8] = &[202, 209, 29, 1];
45
46/// Compute a state address for a given namespace registry.
47///
48/// # Arguments
49///
50/// * `namespace` - the address prefix for this namespace
51pub fn compute_namespace_registry_address(namespace: &str) -> Result<Vec<u8>, AddressingError> {
52    let prefix = match namespace.get(..6) {
53        Some(x) => x,
54        None => {
55            return Err(AddressingError::InvalidInput(format!(
56                "namespace '{}' is less than 6 characters long",
57                namespace,
58            )));
59        }
60    };
61    let hash = Sha512::digest(prefix.as_bytes());
62    Ok([NAMESPACE_REGISTRY_ADDRESS_PREFIX_BYTES, &hash[..32]].concat())
63}
64
65/// Compute a state address for a given contract registry.
66///
67/// # Arguments
68///
69/// * `name` - the name of the contract registry
70pub fn compute_contract_registry_address(name: &str) -> Result<Vec<u8>, AddressingError> {
71    let hash = Sha512::digest(name.as_bytes());
72    Ok([CONTRACT_REGISTRY_ADDRESS_PREFIX_BYTES, &hash[..32]].concat())
73}
74
75/// Compute a state address for a given contract.
76///
77/// # Arguments
78///
79/// * `name` - the name of the contract
80/// * `version` - the version of the contract
81pub fn compute_contract_address(name: &str, version: &str) -> Result<Vec<u8>, AddressingError> {
82    let s = String::from(name) + "," + version;
83    let hash = Sha512::digest(s.as_bytes());
84    Ok([CONTRACT_ADDRESS_PREFIX_BYTES, &hash[..32]].concat())
85}
86
87/// Compute a state address for a given agent name.
88///
89/// # Arguments
90///
91/// * `name` - the agent's name
92pub fn compute_agent_address(name: &[u8]) -> Result<Vec<u8>, AddressingError> {
93    let hash = Sha512::digest(name);
94    Ok([AGENT_ADDRESS_PREFIX_BYTES, &hash[..31]].concat())
95}
96
97/// Compute a state address for a given organization id.
98///
99/// # Arguments
100///
101/// * `id` - the organization's id
102pub fn compute_org_address(id: &str) -> Result<Vec<u8>, AddressingError> {
103    let hash = Sha512::digest(id.as_bytes());
104    Ok([ORG_ADDRESS_PREFIX_BYTES, &hash[..31]].concat())
105}
106
107#[derive(Debug)]
108pub enum AddressingError {
109    InvalidInput(String),
110}
111
112impl Error for AddressingError {}
113
114impl std::fmt::Display for AddressingError {
115    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
116        match self {
117            AddressingError::InvalidInput(msg) => write!(f, "addressing input is invalid: {}", msg),
118        }
119    }
120}