Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
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
4 changes: 2 additions & 2 deletions core/client/db/src/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use parking_lot::Mutex;
/// Offchain local storage
#[derive(Clone)]
pub struct LocalStorage {
db: Arc<KeyValueDB>,
db: Arc<dyn KeyValueDB>,
locks: Arc<Mutex<HashMap<Vec<u8>, Arc<Mutex<()>>>>>,
}

Expand All @@ -48,7 +48,7 @@ impl LocalStorage {
}

/// Create offchain local storage with given `KeyValueDB` backend.
pub fn new(db: Arc<KeyValueDB>) -> Self {
pub fn new(db: Arc<dyn KeyValueDB>) -> Self {
Self {
db,
locks: Default::default(),
Expand Down
63 changes: 20 additions & 43 deletions core/executor/src/wasm_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,6 @@ fn deadline_to_timestamp(deadline: u64) -> Option<offchain::Timestamp> {
}
}

fn u32_to_key(key: u32) -> std::result::Result<Option<offchain::CryptoKeyId>, ()> {
if key > u16::max_value() as u32 {
Err(())
} else if key == 0 {
Ok(None)
} else {
Ok(Some(offchain::CryptoKeyId(key as u16)))
}
}

impl_function_executor!(this: FunctionExecutor<'e, E>,
ext_print_utf8(utf8_data: *const u8, utf8_len: u32) => {
if let Ok(utf8) = this.memory.get(utf8_data, utf8_len as usize) {
Expand Down Expand Up @@ -721,7 +711,7 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,

Ok(if res.is_ok() { 0 } else { 1 })
},
ext_new_crypto_key(crypto: u32) -> u32 => {
ext_new_crypto_key(crypto: u32) -> u64 => {
let kind = offchain::CryptoKind::try_from(crypto)
.map_err(|_| "crypto kind OOB while ext_new_crypto_key: wasm")?;

Expand All @@ -730,26 +720,23 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
.ok_or_else(|| "Calling unavailable API ext_new_crypto_key: wasm")?;

match res {
Ok(key_id) => Ok(key_id.into()),
Err(()) => Ok(u32::max_value()),
Ok(key) => Ok(key.into()),
Err(()) => Ok(u64::max_value()),
}
},
ext_encrypt(
key: u32,
kind: u32,
key: u64,
data: *const u8,
data_len: u32,
msg_len: *mut u32
) -> *mut u8 => {
let key = u32_to_key(key)
let key = offchain::CryptoKey::try_from(key)
.map_err(|_| "Key OOB while ext_encrypt: wasm")?;
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_encrypt: wasm")?;
let message = this.memory.get(data, data_len as usize)
.map_err(|_| "OOB while ext_encrypt: wasm")?;

let res = this.ext.offchain()
.map(|api| api.encrypt(key, kind, &*message))
.map(|api| api.encrypt(key, &*message))
.ok_or_else(|| "Calling unavailable API ext_encrypt: wasm")?;

let (offset,len) = match res {
Expand Down Expand Up @@ -784,15 +771,14 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,

Ok(offset)
},
ext_authority_pubkey(
kind: u32,
ext_pubkey(
key: u64,
written_out: *mut u32
) -> *mut u8 => {
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_authority_pubkey: wasm")?;

let key = offchain::CryptoKey::try_from(key)
.map_err(|_| "Key OOB while ext_decrypt: wasm")?;
let res = this.ext.offchain()
.map(|api| api.authority_pubkey(kind))
.map(|api| api.pubkey(key))
.ok_or_else(|| "Calling unavailable API ext_authority_pubkey: wasm")?;

let encoded = res.encode();
Expand All @@ -805,21 +791,18 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
Ok(offset)
},
ext_decrypt(
key: u32,
kind: u32,
key: u64,
data: *const u8,
data_len: u32,
msg_len: *mut u32
) -> *mut u8 => {
let key = u32_to_key(key)
let key = offchain::CryptoKey::try_from(key)
.map_err(|_| "Key OOB while ext_decrypt: wasm")?;
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_decrypt: wasm")?;
let message = this.memory.get(data, data_len as usize)
.map_err(|_| "OOB while ext_decrypt: wasm")?;

let res = this.ext.offchain()
.map(|api| api.decrypt(key, kind, &*message))
.map(|api| api.decrypt(key, &*message))
.ok_or_else(|| "Calling unavailable API ext_decrypt: wasm")?;

let (offset,len) = match res {
Expand All @@ -839,21 +822,18 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
Ok(offset)
},
ext_sign(
key: u32,
kind: u32,
key: u64,
data: *const u8,
data_len: u32,
sig_data_len: *mut u32
) -> *mut u8 => {
let key = u32_to_key(key)
let key = offchain::CryptoKey::try_from(key)
.map_err(|_| "Key OOB while ext_sign: wasm")?;
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_sign: wasm")?;
let message = this.memory.get(data, data_len as usize)
.map_err(|_| "OOB while ext_sign: wasm")?;

let res = this.ext.offchain()
.map(|api| api.sign(key, kind, &*message))
.map(|api| api.sign(key, &*message))
.ok_or_else(|| "Calling unavailable API ext_sign: wasm")?;

let (offset,len) = match res {
Expand All @@ -873,24 +853,21 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
Ok(offset)
},
ext_verify(
key: u32,
kind: u32,
key: u64,
msg: *const u8,
msg_len: u32,
signature: *const u8,
signature_len: u32
) -> u32 => {
let key = u32_to_key(key)
let key = offchain::CryptoKey::try_from(key)
.map_err(|_| "Key OOB while ext_verify: wasm")?;
let kind = offchain::CryptoKind::try_from(kind)
.map_err(|_| "crypto kind OOB while ext_verify: wasm")?;
let message = this.memory.get(msg, msg_len as usize)
.map_err(|_| "OOB while ext_verify: wasm")?;
let signature = this.memory.get(signature, signature_len as usize)
.map_err(|_| "OOB while ext_verify: wasm")?;

let res = this.ext.offchain()
.map(|api| api.verify(key, kind, &*message, &*signature))
.map(|api| api.verify(key, &*message, &*signature))
.ok_or_else(|| "Calling unavailable API ext_verify: wasm")?;

match res {
Expand Down
Loading