Refactors the offchain worker api (#3150)

* Update offchain primitives.

* Update offchain worker.

* Update im-online.

* Update service.

* Update node and node-template.

* Update runtime version.

* Fix build.

* Fix offchain worker tests.

* Generalize authority_pubkey.

* Add test.

* Update lib.rs
This commit is contained in:
David Craven
2019-07-22 11:20:57 +02:00
committed by Gavin Wood
parent 2edeef5825
commit a3d19baea3
17 changed files with 509 additions and 327 deletions
+20 -43
View File
@@ -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) {
@@ -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")?;
@@ -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 {
@@ -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();
@@ -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 {
@@ -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 {
@@ -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 {