Ivan Boldyrev 771d42dec4
feat(data)!: flexible serialization formats (#757)
New crate air_interpreter_sede introduces a flexible serialization and deserialization infrastructure.

Low level API: the Format trait and related. It includes simple and universal mechanisms for serializing any possible (e.g. implementing the Serde traits) value.

High level API: Representation trait and related. It allows declaring representation types that can serialize and deserialize only certain types (for example, InterpreterDataRepr can serialize only the InterpreterData, but deserialize InterpreterData and Versions` types), producing newtype values as serialization results.

The serialized representation of CallResults now contains byte arrays, not strings, because the new infrastructure serializes to byte arrays only.

Also, the basic support for multicodecs is added.
2023-12-15 21:23:09 +07:00

97 lines
3.2 KiB
Rust

/*
* Copyright 2023 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use air::{min_supported_version, PreparationError};
use air_interpreter_data::{verification::DataVerifierError, InterpreterData, InterpreterDataRepr};
use air_interpreter_sede::{Format, Representation};
use air_interpreter_signatures::KeyError;
use air_test_utils::{
assert_error_eq,
prelude::{request_sent_by, unit_call_service},
test_runner::{create_avm, create_avm_with_key, NativeAirRunner, TestRunParameters},
};
use fluence_keypair::KeyFormat;
use serde_json::json;
/// Checking that other peers' key algorithms are valid.
#[test]
fn test_banned_signature() {
let air_script = r#"(call "other_peer_id" ("" "") [])"#;
let bad_algo_keypair = fluence_keypair::KeyPair::generate_secp256k1();
let bad_algo_pk = bad_algo_keypair.public();
let bad_algo_signature: air_interpreter_signatures::Signature =
air_interpreter_signatures::sign_cids(vec![], "particle_id", &bad_algo_keypair)
.unwrap()
.into();
let bad_algo_pk_ser = bs58::encode(bad_algo_pk.encode()).into_string();
let bad_signature_store = json!({
bad_algo_pk_ser: bad_algo_signature,
});
let bad_peer_id = bad_algo_pk.to_peer_id().to_string();
let trace = vec![request_sent_by("init_peer_fake_id")];
let mut data = serde_json::to_value(InterpreterData::from_execution_result(
trace.into(),
<_>::default(),
<_>::default(),
<_>::default(),
min_supported_version().clone(),
))
.unwrap();
data["signatures"] = bad_signature_store;
let current_data = InterpreterDataRepr.get_format().to_vec(&data).unwrap();
let mut avm = create_avm(unit_call_service(), "other_peer_id");
let res = avm
.call(
air_script,
"",
current_data,
TestRunParameters::from_init_peer_id("init_peer_fake_id"),
)
.unwrap();
assert_error_eq!(
&res,
PreparationError::DataSignatureCheckError(DataVerifierError::MalformedKey {
error: KeyError::AlgorithmNotWhitelisted(KeyFormat::Secp256k1),
peer_id: bad_peer_id
})
);
}
/// Checking that local key is valid.
#[test]
fn test_banned_signing_key() {
let air_script = "(null)";
let bad_algo_keypair = fluence_keypair::KeyPair::generate_secp256k1();
let mut avm = create_avm_with_key::<NativeAirRunner>(bad_algo_keypair, unit_call_service());
let res = avm
.call(air_script, "", "", TestRunParameters::from_init_peer_id("init_peer_id"))
.unwrap();
assert_error_eq!(
&res,
PreparationError::MalformedKeyPairData(KeyError::AlgorithmNotWhitelisted(KeyFormat::Secp256k1))
);
}