Skip to content

Commit 02ba857

Browse files
committed
Paych actor: Drop account req, use AuthenticateMessage to verify sigs (#824)
* Paych actor: Drop account req, use AuthenticateMessage to verify sigs * Address review * Address review
1 parent d92b48a commit 02ba857

File tree

7 files changed

+109
-172
lines changed

7 files changed

+109
-172
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

actors/paych/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ crate-type = ["cdylib", "lib"]
1515

1616
[dependencies]
1717
fil_actors_runtime = { version = "10.0.0-alpha.1", path = "../../runtime" }
18+
frc42_dispatch = "1.0.0"
1819
fvm_shared = { version = "3.0.0-alpha.12", default-features = false }
1920
num-traits = "0.2.14"
2021
num-derive = "0.3.3"

actors/paych/src/ext.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use fvm_ipld_encoding::serde_bytes;
2+
use fvm_ipld_encoding::tuple::*;
3+
4+
pub mod account {
5+
use super::*;
6+
7+
pub const AUTHENTICATE_MESSAGE_METHOD: u64 =
8+
frc42_dispatch::method_hash!("AuthenticateMessage");
9+
10+
#[derive(Serialize_tuple, Deserialize_tuple)]
11+
pub struct AuthenticateMessageParams {
12+
#[serde(with = "serde_bytes")]
13+
pub signature: Vec<u8>,
14+
#[serde(with = "serde_bytes")]
15+
pub message: Vec<u8>,
16+
}
17+
}

actors/paych/src/lib.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
use fil_actors_runtime::runtime::builtins::Type;
55
use fil_actors_runtime::runtime::{ActorCode, Runtime};
66
use fil_actors_runtime::{
7-
actor_error, cbor, resolve_to_actor_id, restrict_internal_api, ActorDowncast, ActorError, Array,
7+
actor_error, cbor, resolve_to_actor_id, restrict_internal_api, ActorDowncast, ActorError,
8+
Array, AsActorError,
89
};
910
use fvm_ipld_blockstore::Blockstore;
1011
use fvm_ipld_encoding::RawBytes;
@@ -22,6 +23,7 @@ pub use self::types::*;
2223
#[cfg(feature = "fil-actor")]
2324
fil_actors_runtime::wasm_trampoline!(Actor);
2425

26+
pub mod ext;
2527
mod state;
2628
pub mod testing;
2729
mod types;
@@ -42,17 +44,24 @@ pub const ERR_CHANNEL_STATE_UPDATE_AFTER_SETTLED: ExitCode = ExitCode::new(32);
4244

4345
/// Payment Channel actor
4446
pub struct Actor;
47+
4548
impl Actor {
4649
/// Constructor for Payment channel actor
4750
pub fn constructor(rt: &mut impl Runtime, params: ConstructorParams) -> Result<(), ActorError> {
4851
// Only InitActor can create a payment channel actor. It creates the actor on
4952
// behalf of the payer/payee.
5053
rt.validate_immediate_caller_type(std::iter::once(&Type::Init))?;
5154

52-
// Check both parties are capable of signing vouchers
53-
let to = Self::resolve_account(rt, &params.to)?;
55+
// Resolve both parties, confirming they exist in the state tree.
56+
let to = Self::resolve_address(rt, &params.to)
57+
.with_context_code(ExitCode::USR_ILLEGAL_ARGUMENT, || {
58+
format!("to address not found {}", params.to)
59+
})?;
5460

55-
let from = Self::resolve_account(rt, &params.from)?;
61+
let from = Self::resolve_address(rt, &params.from)
62+
.with_context_code(ExitCode::USR_ILLEGAL_ARGUMENT, || {
63+
format!("to address not found {}", params.to)
64+
})?;
5665

5766
let empty_arr_cid =
5867
Array::<(), _>::new_with_bit_width(rt.store(), LANE_STATES_AMT_BITWIDTH)
@@ -65,26 +74,14 @@ impl Actor {
6574
Ok(())
6675
}
6776

68-
/// Resolves an address to a canonical ID address and requires it to address an account actor.
69-
fn resolve_account(rt: &mut impl Runtime, raw: &Address) -> Result<Address, ActorError> {
77+
/// Resolves an address to a canonical ID address and confirms it exists in the state tree.
78+
fn resolve_address(rt: &mut impl Runtime, raw: &Address) -> Result<Address, ActorError> {
7079
let resolved = resolve_to_actor_id(rt, raw)?;
7180

72-
let code_cid = rt
73-
.get_actor_code_cid(&resolved)
74-
.ok_or_else(|| actor_error!(illegal_argument, "no code for address {}", resolved))?;
75-
76-
let typ = rt.resolve_builtin_actor_type(&code_cid);
77-
if typ != Some(Type::Account) {
78-
Err(actor_error!(
79-
forbidden,
80-
"actor {} must be an account, was {} ({:?})",
81-
raw,
82-
code_cid,
83-
typ
84-
))
85-
} else {
86-
Ok(Address::new_id(resolved))
87-
}
81+
// so long as we can find code for this, return `resolved`
82+
rt.get_actor_code_cid(&resolved)
83+
.map(|_| Address::new_id(resolved))
84+
.ok_or_else(|| actor_error!(illegal_argument, "no code for address {}", resolved))
8885
}
8986

9087
pub fn update_channel_state(
@@ -98,10 +95,11 @@ impl Actor {
9895
let sv = params.sv;
9996

10097
// Pull signature from signed voucher
101-
let sig = sv
98+
let sig = &sv
10299
.signature
103100
.as_ref()
104-
.ok_or_else(|| actor_error!(illegal_argument, "voucher has no signature"))?;
101+
.ok_or_else(|| actor_error!(illegal_argument, "voucher has no signature"))?
102+
.bytes;
105103

106104
if st.settling_at != 0 && rt.curr_epoch() >= st.settling_at {
107105
return Err(ActorError::unchecked(
@@ -120,9 +118,17 @@ impl Actor {
120118
})?;
121119

122120
// Validate signature
123-
rt.verify_signature(sig, &signer, &sv_bz).map_err(|e| {
124-
e.downcast_default(ExitCode::USR_ILLEGAL_ARGUMENT, "voucher signature invalid")
125-
})?;
121+
122+
rt.send(
123+
&signer,
124+
ext::account::AUTHENTICATE_MESSAGE_METHOD,
125+
RawBytes::serialize(ext::account::AuthenticateMessageParams {
126+
signature: sig.to_vec(),
127+
message: sv_bz,
128+
})?,
129+
TokenAmount::zero(),
130+
)
131+
.map_err(|e| e.wrap("voucher sig authentication failed"))?;
126132

127133
let pch_addr = rt.message().receiver();
128134
let svpch_id = rt.resolve_address(&sv.channel_addr).ok_or_else(|| {

actors/paych/src/state.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@ pub struct Merge {
5757
}
5858

5959
impl Cbor for State {}
60+
6061
impl Cbor for LaneState {}
62+
6163
impl Cbor for Merge {}

0 commit comments

Comments
 (0)