-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a96328b
commit f8f8efe
Showing
8 changed files
with
224 additions
and
96 deletions.
There are no files selected for viewing
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
Binary file not shown.
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,116 @@ | ||
use crate::error::BundlrError; | ||
use crate::Signer as SignerTrait; | ||
use crate::Verifier as VerifierTrait; | ||
use crate::{index::SignerMap, Ed25519Signer}; | ||
|
||
use bytes::Bytes; | ||
use ed25519_dalek::Verifier; | ||
use ed25519_dalek::{Keypair, PUBLIC_KEY_LENGTH, SIGNATURE_LENGTH}; | ||
|
||
pub struct AptosSigner { | ||
signer: Ed25519Signer, | ||
} | ||
|
||
impl AptosSigner { | ||
pub fn new(keypair: Keypair) -> Self { | ||
AptosSigner { | ||
signer: Ed25519Signer::new(keypair), | ||
} | ||
} | ||
|
||
pub fn from_base58(s: &str) -> Self { | ||
Self { | ||
signer: Ed25519Signer::from_base58(s), | ||
} | ||
} | ||
} | ||
|
||
const SIG_TYPE: SignerMap = SignerMap::ED25519; | ||
const SIG_LENGTH: u16 = SIGNATURE_LENGTH as u16; | ||
const PUB_LENGTH: u16 = PUBLIC_KEY_LENGTH as u16; | ||
|
||
impl SignerTrait for AptosSigner { | ||
fn sign(&self, message: bytes::Bytes) -> Result<bytes::Bytes, crate::error::BundlrError> { | ||
let aptos_message = | ||
Bytes::copy_from_slice(&[b"APTOS\nmessage: ".as_ref(), &message[..]].concat()); | ||
let nonce = Bytes::from(b"\nnonce: bundlr".to_vec()); | ||
let full_msg = Bytes::from([aptos_message, nonce].concat()); | ||
self.signer.sign(full_msg) | ||
} | ||
|
||
fn pub_key(&self) -> bytes::Bytes { | ||
self.signer.pub_key() | ||
} | ||
|
||
fn sig_type(&self) -> SignerMap { | ||
SIG_TYPE | ||
} | ||
fn get_sig_length(&self) -> u16 { | ||
SIG_LENGTH | ||
} | ||
fn get_pub_length(&self) -> u16 { | ||
PUB_LENGTH | ||
} | ||
} | ||
|
||
impl VerifierTrait for AptosSigner { | ||
fn verify( | ||
pk: Bytes, | ||
message: Bytes, | ||
signature: Bytes, | ||
) -> Result<bool, crate::error::BundlrError> { | ||
let public_key = ed25519_dalek::PublicKey::from_bytes(&pk).unwrap_or_else(|_| { | ||
panic!( | ||
"ED25519 public keys must be {} bytes long", | ||
ed25519_dalek::PUBLIC_KEY_LENGTH | ||
) | ||
}); | ||
let sig = ed25519_dalek::Signature::from_bytes(&signature).unwrap_or_else(|_| { | ||
panic!( | ||
"ED22519 signatures keys must be {} bytes long", | ||
ed25519_dalek::SIGNATURE_LENGTH | ||
) | ||
}); | ||
let aptos_message = | ||
Bytes::copy_from_slice(&[b"APTOS\nmessage: ".as_ref(), &message[..]].concat()); | ||
let nonce = Bytes::from(b"\nnonce: bundlr".to_vec()); | ||
let full_msg = Bytes::from([aptos_message, nonce].concat()); | ||
|
||
public_key | ||
.verify(&full_msg, &sig) | ||
.map(|_| true) | ||
.map_err(|_| BundlrError::InvalidSignature) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{AptosSigner, Signer, Verifier}; | ||
use bytes::Bytes; | ||
use ed25519_dalek::Keypair; | ||
|
||
#[test] | ||
fn should_sign_and_verify() { | ||
let msg = Bytes::from(b"Message".to_vec()); | ||
|
||
let base58_secret_key = "kNykCXNxgePDjFbDWjPNvXQRa8U12Ywc19dFVaQ7tebUj3m7H4sF4KKdJwM7yxxb3rqxchdjezX9Szh8bLcQAjb"; | ||
let signer = AptosSigner::from_base58(base58_secret_key); | ||
let sig = signer.sign(msg.clone()).unwrap(); | ||
let pub_key = signer.pub_key(); | ||
println!("{:?}", pub_key.to_vec()); | ||
assert!(AptosSigner::verify(pub_key, msg.clone(), sig).unwrap()); | ||
|
||
let keypair = Keypair::from_bytes(&[ | ||
237, 158, 92, 107, 132, 192, 1, 57, 8, 20, 213, 108, 29, 227, 37, 8, 3, 105, 196, 244, | ||
8, 221, 184, 199, 62, 253, 98, 131, 33, 165, 165, 215, 14, 7, 46, 23, 221, 242, 240, | ||
226, 94, 79, 161, 31, 192, 163, 13, 25, 106, 53, 34, 215, 83, 124, 162, 156, 8, 97, | ||
194, 180, 213, 179, 33, 68, | ||
]) | ||
.unwrap(); | ||
let signer = AptosSigner::new(keypair); | ||
let sig = signer.sign(msg.clone()).unwrap(); | ||
let pub_key = signer.pub_key(); | ||
|
||
assert!(AptosSigner::verify(pub_key, msg, sig).unwrap()); | ||
} | ||
} |
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
Oops, something went wrong.