Skip to content

Commit

Permalink
execution-core: Support unsigned moonlight transactions
Browse files Browse the repository at this point in the history
Resolves: #2304
  • Loading branch information
moCello committed Sep 9, 2024
1 parent bb0b836 commit db1b045
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions execution-core/src/transfer/moonlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ impl Transaction {
) -> Result<Self, Error> {
let data = data.map(Into::into);

if let Some(TransactionData::Memo(memo)) = data.as_ref() {
if memo.len() > MAX_MEMO_SIZE {
return Err(Error::MemoTooLarge(memo.len()));
}
}

let payload = Payload {
chain_id,
from_account: AccountPublicKey::from(from_sk),
Expand All @@ -81,6 +75,29 @@ impl Transaction {
data,
};

Self::sign_payload(from_sk, payload)
}

/// Create a transaction by signing a previously generated payload with a
/// given secret-key.
///
/// Note that this transaction will be invalid if the secret-key used for
/// signing doesn't form a valid key-pair with the public-key of the
/// `from_account`.
///
/// # Errors
/// The creation of a transaction is not possible and will error if:
/// - the payload memo, if given, is too large
pub fn sign_payload(
from_sk: &AccountSecretKey,
payload: Payload,
) -> Result<Self, Error> {
if let Some(TransactionData::Memo(memo)) = payload.data.as_ref() {
if memo.len() > MAX_MEMO_SIZE {
return Err(Error::MemoTooLarge(memo.len()));
}
}

let digest = payload.signature_message();
let signature = from_sk.sign(&digest);

Expand Down Expand Up @@ -267,7 +284,7 @@ impl Transaction {
/// The payload for a moonlight transaction.
#[derive(Debug, Clone, PartialEq, Eq, Archive, Serialize, Deserialize)]
#[archive_attr(derive(CheckBytes))]
struct Payload {
pub struct Payload {
/// ID of the chain for this transaction to execute on.
pub chain_id: u8,
/// Key of the sender of this transaction.
Expand Down

0 comments on commit db1b045

Please sign in to comment.