Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callformat interop test #2033

Open
wants to merge 3 commits into
base: pro-wh/feature/webcrypto
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions client-sdk/go/callformat/callformat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ type metaEncryptedX25519DeoxysII struct {
pk *x25519.PublicKey
}

func encodeCallEncryptedX25519DeoxysII(call *types.Call, pk *x25519.PublicKey, sk *x25519.PrivateKey, nonce [deoxysii.NonceSize]byte, cfg *EncodeConfig) (*types.Call, *metaEncryptedX25519DeoxysII) {
// Seal serialized plain call.
rawCall := cbor.Marshal(call)
sealedCall := mraeDeoxysii.Box.Seal(nil, nonce[:], rawCall, nil, &cfg.PublicKey.PublicKey, sk)

encoded := &types.Call{
Format: types.CallFormatEncryptedX25519DeoxysII,
Method: "",
Body: cbor.Marshal(&types.CallEnvelopeX25519DeoxysII{
Pk: *pk,
Nonce: nonce,
Epoch: cfg.Epoch,
Data: sealedCall,
}),
ReadOnly: call.ReadOnly,
}
meta := &metaEncryptedX25519DeoxysII{
sk: sk,
pk: &cfg.PublicKey.PublicKey,
}
return encoded, meta
}

// EncodeCall encodes a call based on its configured call format.
//
// It returns the encoded call and any metadata needed to successfully decode the result.
Expand All @@ -52,25 +75,7 @@ func EncodeCall(call *types.Call, cf types.CallFormat, cfg *EncodeConfig) (*type
return nil, nil, fmt.Errorf("callformat: failed to generate random nonce: %w", err)
}

// Seal serialized plain call.
rawCall := cbor.Marshal(call)
sealedCall := mraeDeoxysii.Box.Seal(nil, nonce[:], rawCall, nil, &cfg.PublicKey.PublicKey, sk)

encoded := &types.Call{
Format: types.CallFormatEncryptedX25519DeoxysII,
Method: "",
Body: cbor.Marshal(&types.CallEnvelopeX25519DeoxysII{
Pk: *pk,
Nonce: nonce,
Epoch: cfg.Epoch,
Data: sealedCall,
}),
ReadOnly: call.ReadOnly,
}
meta := &metaEncryptedX25519DeoxysII{
sk: sk,
pk: &cfg.PublicKey.PublicKey,
}
encoded, meta := encodeCallEncryptedX25519DeoxysII(call, pk, sk, nonce, cfg)
return encoded, meta, nil
default:
return nil, nil, fmt.Errorf("callformat: unsupported call format: %s", cf)
Expand Down
51 changes: 51 additions & 0 deletions client-sdk/go/callformat/callformat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package callformat

import (
"crypto/sha512"
"encoding/hex"
"testing"

"github.com/oasisprotocol/curve25519-voi/primitives/x25519"
"github.com/oasisprotocol/deoxysii"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
)

func TestInterop(t *testing.T) {
clientSK := (x25519.PrivateKey)(sha512.Sum512_256([]byte("callformat test client")))
clientPK := clientSK.Public()
runtimeSK := (x25519.PrivateKey)(sha512.Sum512_256([]byte("callformat test runtime")))
runtimePK := runtimeSK.Public()

call := types.Call{
Method: "mock",
Body: nil,
}
var nonce [deoxysii.NonceSize]byte
cfg := EncodeConfig{
PublicKey: &types.SignedPublicKey{PublicKey: *runtimePK},
Epoch: 1,
}
callEnc, metadata := encodeCallEncryptedX25519DeoxysII(&call, clientPK, &clientSK, nonce, &cfg)

// If these change, update runtime-sdk/src/callformat.rs too.
require.Equal(t, "a264626f6479f6666d6574686f64646d6f636b", hex.EncodeToString(cbor.Marshal(call)))
require.Equal(t, "a264626f6479a462706b5820eedc75d3c500fc1b2d321757c383e276ab705c5a02013b3f1966e9caf73cdb0264646174615823c4635f2f9496a033a578e3f1e007be5d6cfa9631fb2fe2c8c76d26b322b6afb2fa5cdf6565706f636801656e6f6e63654f00000000000000000000000000000066666f726d617401", hex.EncodeToString(cbor.Marshal(callEnc)))

resultCBOR, err := hex.DecodeString("a1626f6bf6")
require.NoError(t, err)
var result types.CallResult
err = cbor.Unmarshal(resultCBOR, &result)
require.NoError(t, err)
resultEncCBOR, err := hex.DecodeString("a167756e6b6e6f776ea264646174615528d1c5eedc5e54e1ef140ba905e84e0bea8daf60af656e6f6e63654f000000000000000000000000000000")
require.NoError(t, err)
var resultEnc types.CallResult
err = cbor.Unmarshal(resultEncCBOR, &resultEnc)
require.NoError(t, err)

resultOurs, err := DecodeResult(&resultEnc, metadata)
require.NoError(t, err)
require.Equal(t, &result, resultOurs)
}
49 changes: 49 additions & 0 deletions runtime-sdk/src/callformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,52 @@ pub fn decode_result<C: Context>(
}
}
}

#[cfg(test)]
mod test {
use digest::Digest;
use oasis_core_keymanager::crypto::{InputKeyPair, KeyPair};
use oasis_core_runtime::common::crypto::x25519;
use crate::{callformat, crypto, module, testing, types};

#[test]
fn test_interop() {
let _guard = crypto::signature::context::test_using_chain_context();
crypto::signature::context::set_chain_context(Default::default(), "test");

let runtime_sk = x25519::PrivateKey::from(<[u8; x25519::PRIVATE_KEY_LENGTH]>::from(sha2::Sha512_256::digest("callformat test runtime")));
let runtime_pk = runtime_sk.public_key();

let mut mock = testing::mock::Mock::default();
let our_key_pair_id = callformat::get_key_pair_id(mock.epoch);
let our_key_pair = KeyPair {
input_keypair: InputKeyPair {
pk: runtime_pk,
sk: runtime_sk,
},
..Default::default()
};
let ephemeral_keys = std::collections::HashMap::from([
(our_key_pair_id, our_key_pair),
]);
let key_manager = testing::keymanager::MockKeyManagerClient {
ephemeral_keys: ephemeral_keys.into(),
..Default::default()
};
let ctx = mock.create_ctx_with_key_manager(Box::new(key_manager));

let call = cbor::from_slice::<types::transaction::Call>(hex::decode("a264626f6479f6666d6574686f64646d6f636b").unwrap().as_slice()).unwrap();
let call_enc = cbor::from_slice::<types::transaction::Call>(hex::decode("a264626f6479a462706b5820eedc75d3c500fc1b2d321757c383e276ab705c5a02013b3f1966e9caf73cdb0264646174615823c4635f2f9496a033a578e3f1e007be5d6cfa9631fb2fe2c8c76d26b322b6afb2fa5cdf6565706f636801656e6f6e63654f00000000000000000000000000000066666f726d617401").unwrap().as_slice()).unwrap();

let (call_ours, metadata) = callformat::decode_call(&ctx, call_enc, 0).unwrap().unwrap();
assert_eq!(cbor::to_vec(call_ours), cbor::to_vec(call));

let result_m_g = || module::CallResult::Ok(cbor::Value::Simple(cbor::SimpleValue::NullValue));
let result = types::transaction::CallResult::from(result_m_g());
let result_enc = callformat::encode_result(&ctx, result_m_g(), metadata);

// If these change, update client-sdk/go/callformat/callformat_test.go too.
assert_eq!(hex::encode(cbor::to_vec(result)), "a1626f6bf6");
assert_eq!(hex::encode(cbor::to_vec(result_enc)), "a167756e6b6e6f776ea264646174615528d1c5eedc5e54e1ef140ba905e84e0bea8daf60af656e6f6e63654f000000000000000000000000000000");
}
}
4 changes: 2 additions & 2 deletions runtime-sdk/src/testing/keymanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{

#[derive(Default)]
pub struct MockKeyManagerClient {
keys: Mutex<HashMap<KeyPairId, KeyPair>>,
ephemeral_keys: Mutex<HashMap<KeyPairId, KeyPair>>,
pub keys: Mutex<HashMap<KeyPairId, KeyPair>>,
pub ephemeral_keys: Mutex<HashMap<KeyPairId, KeyPair>>,
}

impl Clone for MockKeyManagerClient {
Expand Down
14 changes: 14 additions & 0 deletions runtime-sdk/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ impl Mock {
)
}

/// Create a new mock dispatch context.
pub fn create_ctx_with_key_manager(&mut self, key_manager: Box<dyn KeyManager>) -> RuntimeBatchContext<'_, EmptyRuntime> {
RuntimeBatchContext::new(
&self.host_info,
Some(key_manager),
&self.runtime_header,
&self.runtime_round_results,
&self.consensus_state,
&self.history,
self.epoch,
self.max_messages,
)
}

/// Create an instance with the given local configuration.
pub fn with_local_config(local_config: BTreeMap<String, cbor::Value>) -> Self {
// Ensure a current state is always available during tests. Note that one can always use a
Expand Down