-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(wallet): split KeyPair (signer) and the wallet contract structs
- Loading branch information
1 parent
8d7a6b3
commit 116c41e
Showing
8 changed files
with
175 additions
and
87 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use anyhow::anyhow; | ||
use nacl::sign::{signature, Keypair}; | ||
|
||
pub use nacl::sign::{PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH}; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
pub struct KeyPair { | ||
/// Secret key of this pair. | ||
pub secret_key: [u8; SECRET_KEY_LENGTH], | ||
|
||
/// Public key of this pair. | ||
pub public_key: [u8; PUBLIC_KEY_LENGTH], | ||
} | ||
|
||
impl From<Keypair> for KeyPair { | ||
fn from(Keypair { skey, pkey }: Keypair) -> Self { | ||
Self { | ||
secret_key: skey, | ||
public_key: pkey, | ||
} | ||
} | ||
} | ||
|
||
impl KeyPair { | ||
#[inline] | ||
pub const fn new( | ||
secret_key: [u8; SECRET_KEY_LENGTH], | ||
public_key: [u8; PUBLIC_KEY_LENGTH], | ||
) -> Self { | ||
Self { | ||
secret_key, | ||
public_key, | ||
} | ||
} | ||
|
||
pub fn sign(&self, msg: impl AsRef<[u8]>) -> anyhow::Result<[u8; 64]> { | ||
signature(msg.as_ref(), self.secret_key.as_slice()) | ||
.map_err(|e| anyhow!("{}", e.message))? | ||
.try_into() | ||
.map_err(|sig: Vec<_>| { | ||
anyhow!( | ||
"got signature of a wrong size, expected 64, got: {}", | ||
sig.len() | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::sync::Arc; | ||
|
||
use chrono::{DateTime, Utc}; | ||
use tlb::{ser::CellSerialize, Cell}; | ||
use tlb_ton::{action::SendMsgAction, state_init::StateInit}; | ||
|
||
use super::PUBLIC_KEY_LENGTH; | ||
|
||
/// Version of [`Wallet`] | ||
pub trait WalletVersion { | ||
type Data: CellSerialize; | ||
type SignBody: CellSerialize; | ||
type ExternalMsgBody: CellSerialize; | ||
|
||
/// Code of the wallet for use with [`StateInit`] | ||
fn code() -> Arc<Cell>; | ||
|
||
/// Init data for use with [`StateInit`] | ||
fn init_data(wallet_id: u32, pubkey: [u8; PUBLIC_KEY_LENGTH]) -> Self::Data; | ||
|
||
/// Creates body for further signing with | ||
/// [`.wrap_signed_external()`](WalletVersion::wrap_signed_external) | ||
fn create_sign_body( | ||
wallet_id: u32, | ||
expire_at: DateTime<Utc>, | ||
seqno: u32, | ||
msgs: impl IntoIterator<Item = SendMsgAction>, | ||
) -> Self::SignBody; | ||
|
||
/// Wraps signed body into external [`Message::body`] | ||
fn wrap_signed_external(body: Self::SignBody, signature: [u8; 64]) -> Self::ExternalMsgBody; | ||
|
||
#[inline] | ||
fn state_init( | ||
wallet_id: u32, | ||
pubkey: [u8; PUBLIC_KEY_LENGTH], | ||
) -> StateInit<Arc<Cell>, Self::Data> { | ||
StateInit { | ||
code: Some(Self::code()), | ||
data: Some(Self::init_data(wallet_id, pubkey)), | ||
..Default::default() | ||
} | ||
} | ||
} |
Oops, something went wrong.