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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! Conversions from types generated by `wit-bindgen`.
//!
//! Allows converting types returned from a WASM module into types that can be used with the rest
//! of the crate.

#![allow(clippy::duplicate_mod)]

use super::{contract, contract_system_api, service_system_api};
use crate::{
    ApplicationCallResult, ChannelName, Destination, RawExecutionResult, SessionCallResult,
    SessionId, UserApplicationId,
};
use linera_base::{
    crypto::CryptoHash,
    data_types::BlockHeight,
    identifiers::{BytecodeId, ChainId, MessageId},
};

impl From<contract::SessionCallResult> for (SessionCallResult, Vec<u8>) {
    fn from(result: contract::SessionCallResult) -> Self {
        let session_call_result = SessionCallResult {
            inner: result.inner.into(),
            close_session: result.new_state.is_some(),
        };

        let updated_session_state = result.new_state.unwrap_or_default();

        (session_call_result, updated_session_state)
    }
}

impl From<contract::ApplicationCallResult> for ApplicationCallResult {
    fn from(result: contract::ApplicationCallResult) -> Self {
        ApplicationCallResult {
            create_sessions: result.create_sessions,
            execution_result: result.execution_result.into(),
            value: result.value,
        }
    }
}

impl From<contract::ExecutionResult> for RawExecutionResult<Vec<u8>> {
    fn from(result: contract::ExecutionResult) -> Self {
        let messages = result
            .messages
            .into_iter()
            .map(|(destination, authenticated, message)| {
                (destination.into(), authenticated, message)
            })
            .collect();

        let subscribe = result
            .subscribe
            .into_iter()
            .map(|(subscription, chain_id)| (subscription.into(), chain_id.into()))
            .collect();

        let unsubscribe = result
            .unsubscribe
            .into_iter()
            .map(|(subscription, chain_id)| (subscription.into(), chain_id.into()))
            .collect();

        RawExecutionResult {
            authenticated_signer: None,
            messages,
            subscribe,
            unsubscribe,
        }
    }
}

impl From<contract::Destination> for Destination {
    fn from(guest: contract::Destination) -> Self {
        match guest {
            contract::Destination::Recipient(chain_id) => Destination::Recipient(chain_id.into()),
            contract::Destination::Subscribers(subscription) => {
                Destination::Subscribers(subscription.into())
            }
        }
    }
}

impl From<contract::ChannelName> for ChannelName {
    fn from(guest: contract::ChannelName) -> Self {
        guest.name.into()
    }
}

impl From<contract::CryptoHash> for CryptoHash {
    fn from(guest: contract::CryptoHash) -> Self {
        let mut bytes = [0u8; 32];

        bytes[0..8].copy_from_slice(&guest.part1.to_le_bytes());
        bytes[8..16].copy_from_slice(&guest.part2.to_le_bytes());
        bytes[16..24].copy_from_slice(&guest.part3.to_le_bytes());
        bytes[24..32].copy_from_slice(&guest.part4.to_le_bytes());

        CryptoHash::try_from(&bytes[..]).expect("Incorrect byte count for `CryptoHash`")
    }
}

impl From<contract::ChainId> for ChainId {
    fn from(guest: contract::ChainId) -> Self {
        ChainId(guest.into())
    }
}

impl From<contract_system_api::SessionId> for SessionId {
    fn from(guest: contract_system_api::SessionId) -> Self {
        SessionId {
            application_id: guest.application_id.into(),
            index: guest.index,
        }
    }
}

impl From<contract_system_api::ApplicationId> for UserApplicationId {
    fn from(guest: contract_system_api::ApplicationId) -> Self {
        UserApplicationId {
            bytecode_id: guest.bytecode_id.into(),
            creation: guest.creation.into(),
        }
    }
}

impl From<contract_system_api::MessageId> for BytecodeId {
    fn from(guest: contract_system_api::MessageId) -> Self {
        BytecodeId::new(guest.into())
    }
}

impl From<contract_system_api::MessageId> for MessageId {
    fn from(guest: contract_system_api::MessageId) -> Self {
        MessageId {
            chain_id: guest.chain_id.into(),
            height: BlockHeight(guest.height),
            index: guest.index,
        }
    }
}

impl From<contract_system_api::CryptoHash> for ChainId {
    fn from(guest: contract_system_api::CryptoHash) -> Self {
        ChainId(guest.into())
    }
}

impl From<contract_system_api::CryptoHash> for CryptoHash {
    fn from(guest: contract_system_api::CryptoHash) -> Self {
        let mut bytes = [0u8; 32];

        bytes[0..8].copy_from_slice(&guest.part1.to_le_bytes());
        bytes[8..16].copy_from_slice(&guest.part2.to_le_bytes());
        bytes[16..24].copy_from_slice(&guest.part3.to_le_bytes());
        bytes[24..32].copy_from_slice(&guest.part4.to_le_bytes());

        CryptoHash::try_from(&bytes[..]).expect("Incorrect byte count for `CryptoHash`")
    }
}

impl From<service_system_api::ApplicationId> for UserApplicationId {
    fn from(guest: service_system_api::ApplicationId) -> Self {
        UserApplicationId {
            bytecode_id: guest.bytecode_id.into(),
            creation: guest.creation.into(),
        }
    }
}

impl From<service_system_api::MessageId> for BytecodeId {
    fn from(guest: service_system_api::MessageId) -> Self {
        BytecodeId::new(guest.into())
    }
}

impl From<service_system_api::MessageId> for MessageId {
    fn from(guest: service_system_api::MessageId) -> Self {
        MessageId {
            chain_id: guest.chain_id.into(),
            height: BlockHeight(guest.height),
            index: guest.index,
        }
    }
}

impl From<service_system_api::CryptoHash> for ChainId {
    fn from(guest: service_system_api::CryptoHash) -> Self {
        ChainId(guest.into())
    }
}

impl From<service_system_api::CryptoHash> for CryptoHash {
    fn from(guest: service_system_api::CryptoHash) -> Self {
        let mut bytes = [0u8; 32];

        bytes[0..8].copy_from_slice(&guest.part1.to_le_bytes());
        bytes[8..16].copy_from_slice(&guest.part2.to_le_bytes());
        bytes[16..24].copy_from_slice(&guest.part3.to_le_bytes());
        bytes[24..32].copy_from_slice(&guest.part4.to_le_bytes());

        CryptoHash::try_from(&bytes[..]).expect("Incorrect byte count for `CryptoHash`")
    }
}