-
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.
Ethereum and Bitcoin key repository (#2454)
* init eth and btc wallet * fmt --------- Co-authored-by: Kai <7630809+Kailai-Wang@users.noreply.github.com>
- Loading branch information
1 parent
4276678
commit 81e8402
Showing
8 changed files
with
206 additions
and
4 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
168 changes: 168 additions & 0 deletions
168
bitacross-worker/core-primitives/sgx/crypto/src/secp256k1.rs
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,168 @@ | ||
// Copyright 2020-2024 Trust Computing GmbH. | ||
// This file is part of Litentry. | ||
// | ||
// Litentry is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Litentry is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Litentry. If not, see <https://www.gnu.org/licenses/>. | ||
#[cfg(feature = "sgx")] | ||
pub use sgx::*; | ||
|
||
pub use libsecp256k1; | ||
|
||
use libsecp256k1::{PublicKey, SecretKey}; | ||
|
||
/// File name of the sealed seed file. | ||
pub const SEALED_SIGNER_SEED_FILE: &str = "secp256k1_key_sealed.bin"; | ||
|
||
#[derive(Clone, PartialEq)] | ||
pub struct Pair { | ||
pub public: PublicKey, | ||
pub secret: SecretKey, | ||
} | ||
|
||
#[cfg(feature = "sgx")] | ||
pub mod sgx { | ||
use super::SEALED_SIGNER_SEED_FILE; | ||
use crate::{ | ||
error::{Error, Result}, | ||
key_repository::KeyRepository, | ||
secp256k1::Pair, | ||
std::string::ToString, | ||
}; | ||
use itp_sgx_io::{seal, unseal, SealedIO}; | ||
use libsecp256k1::{PublicKey, SecretKey}; | ||
use log::*; | ||
use sgx_rand::{Rng, StdRng}; | ||
use std::{path::PathBuf, string::String}; | ||
|
||
/// Creates a repository for an secp256k1 keypair and initializes | ||
/// a fresh private key if it doesn't exist at `path`. | ||
pub fn create_secp256k1_repository( | ||
path: PathBuf, | ||
key_file_prefix: &str, | ||
) -> Result<KeyRepository<Pair, Seal>> { | ||
let seal = Seal::new(path, key_file_prefix.to_string()); | ||
Ok(KeyRepository::new(seal.init()?, seal.into())) | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Seal { | ||
base_path: PathBuf, | ||
key_file_prefix: String, | ||
} | ||
|
||
impl Seal { | ||
pub fn new(base_path: PathBuf, key_file_prefix: String) -> Self { | ||
Self { base_path, key_file_prefix } | ||
} | ||
|
||
pub fn path(&self) -> PathBuf { | ||
self.base_path | ||
.join(self.key_file_prefix.clone() + "_" + SEALED_SIGNER_SEED_FILE) | ||
} | ||
} | ||
|
||
impl Seal { | ||
fn unseal_pair(&self) -> Result<Pair> { | ||
self.unseal() | ||
} | ||
|
||
fn exists(&self) -> bool { | ||
self.path().exists() | ||
} | ||
|
||
fn init(&self) -> Result<Pair> { | ||
if !self.exists() { | ||
info!("Keyfile not found, creating new! {}", self.path().display()); | ||
let mut seed = [0u8; 32]; | ||
let mut rand = StdRng::new()?; | ||
rand.fill_bytes(&mut seed); | ||
seal(&seed, self.path())?; | ||
} | ||
self.unseal_pair() | ||
} | ||
} | ||
|
||
impl SealedIO for Seal { | ||
type Error = Error; | ||
type Unsealed = Pair; | ||
|
||
fn unseal(&self) -> Result<Self::Unsealed> { | ||
let raw = unseal(self.path())?; | ||
|
||
let secret = SecretKey::parse_slice(&raw) | ||
.map_err(|e| Error::Other(format!("{:?}", e).into()))?; | ||
let public = PublicKey::from_secret_key(&secret); | ||
|
||
Ok(Pair { public, secret }) | ||
} | ||
|
||
fn seal(&self, unsealed: &Self::Unsealed) -> Result<()> { | ||
let raw = unsealed.secret.serialize(); | ||
seal(&raw, self.path()).map_err(|e| e.into()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "test")] | ||
pub mod sgx_tests { | ||
use super::sgx::*; | ||
use crate::{key_repository::AccessKey, secp256k1::Pair, std::string::ToString, ToPubkey}; | ||
use itp_sgx_temp_dir::TempDir; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[test] | ||
pub fn creating_repository_with_same_path_and_prefix_results_in_same_key() { | ||
let key_file_prefix = "test"; | ||
fn get_key_from_repo(path: PathBuf, prefix: &str) -> Pair { | ||
create_secp256k1_repository(path, prefix).unwrap().retrieve_key().unwrap() | ||
} | ||
let temp_dir = TempDir::with_prefix( | ||
"creating_repository_with_same_path_and_prefix_results_in_same_key", | ||
) | ||
.unwrap(); | ||
let temp_path = temp_dir.path().to_path_buf(); | ||
assert_eq!( | ||
get_key_from_repo(temp_path.clone(), key_file_prefix), | ||
get_key_from_repo(temp_path.clone(), key_file_prefix) | ||
); | ||
} | ||
|
||
#[test] | ||
pub fn seal_init_should_create_new_key_if_not_present() { | ||
//given | ||
let temp_dir = | ||
TempDir::with_prefix("seal_init_should_create_new_key_if_not_present").unwrap(); | ||
let seal = Seal::new(temp_dir.path().to_path_buf(), "test".to_string()); | ||
assert!(!seal.exists()); | ||
|
||
//when | ||
seal.init().unwrap(); | ||
|
||
//then | ||
assert!(seal.exists()); | ||
} | ||
|
||
#[test] | ||
pub fn seal_init_should_not_change_key_if_exists() { | ||
//given | ||
let temp_dir = TempDir::with_prefix("seal_init_should_not_change_key_if_exists").unwrap(); | ||
let seal = Seal::new(temp_dir.path().to_path_buf(), "test".to_string()); | ||
let pair = seal.init().unwrap(); | ||
|
||
//when | ||
let new_pair = seal.init().unwrap(); | ||
|
||
//then | ||
assert!(pair, new_pair); | ||
} | ||
} |
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