2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-20 16:13:21 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-02-20 16:13:21 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
use std::collections::BTreeMap;
|
2018-04-13 21:14:53 +02:00
|
|
|
use std::sync::Arc;
|
2017-04-03 11:13:51 +02:00
|
|
|
use serde_json;
|
2017-11-02 15:33:11 +01:00
|
|
|
use tiny_keccak::Keccak;
|
2018-03-20 03:03:51 +01:00
|
|
|
use ethereum_types::{H256, Address};
|
|
|
|
use ethkey::{Secret, Public, public_to_address};
|
2018-04-13 21:14:53 +02:00
|
|
|
use kvdb::KeyValueDB;
|
2018-05-01 15:02:14 +02:00
|
|
|
use types::{Error, ServerKeyId, NodeId};
|
2018-03-20 03:03:51 +01:00
|
|
|
use serialization::{SerializablePublic, SerializableSecret, SerializableH256, SerializableAddress};
|
2017-04-03 11:13:51 +02:00
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
/// Key of version value.
|
|
|
|
const DB_META_KEY_VERSION: &'static [u8; 7] = b"version";
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Current db version.
|
2018-03-20 03:03:51 +01:00
|
|
|
const CURRENT_VERSION: u8 = 3;
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Current type of serialized key shares.
|
2018-03-20 03:03:51 +01:00
|
|
|
type CurrentSerializableDocumentKeyShare = SerializableDocumentKeyShareV3;
|
2017-11-02 15:33:11 +01:00
|
|
|
/// Current type of serialized key shares versions.
|
2018-03-20 03:03:51 +01:00
|
|
|
type CurrentSerializableDocumentKeyVersion = SerializableDocumentKeyShareVersionV3;
|
2017-07-06 14:02:10 +02:00
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Encrypted key share, stored by key storage on the single key server.
|
2019-06-05 14:04:00 +02:00
|
|
|
#[derive(Debug, Default, Clone, PartialEq)]
|
2017-04-03 11:13:51 +02:00
|
|
|
pub struct DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
/// Author of the entry.
|
2018-03-20 03:03:51 +01:00
|
|
|
pub author: Address,
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
2017-11-22 09:09:48 +01:00
|
|
|
/// Server public key.
|
|
|
|
pub public: Public,
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Common (shared) encryption point.
|
2017-07-06 14:02:10 +02:00
|
|
|
pub common_point: Option<Public>,
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Encrypted point.
|
2017-07-06 14:02:10 +02:00
|
|
|
pub encrypted_point: Option<Public>,
|
2017-11-02 15:33:11 +01:00
|
|
|
/// Key share versions.
|
|
|
|
pub versions: Vec<DocumentKeyShareVersion>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Versioned portion of document key share.
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct DocumentKeyShareVersion {
|
|
|
|
/// Version hash (Keccak(time + id_numbers)).
|
|
|
|
pub hash: H256,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<NodeId, Secret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: Secret,
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
/// Document encryption keys storage
|
|
|
|
pub trait KeyStorage: Send + Sync {
|
|
|
|
/// Insert document encryption key
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error>;
|
|
|
|
/// Update document encryption key
|
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error>;
|
2017-02-20 16:13:21 +01:00
|
|
|
/// Get document encryption key
|
2017-11-02 15:33:11 +01:00
|
|
|
fn get(&self, document: &ServerKeyId) -> Result<Option<DocumentKeyShare>, Error>;
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Remove document encryption key
|
|
|
|
fn remove(&self, document: &ServerKeyId) -> Result<(), Error>;
|
2017-11-02 15:33:11 +01:00
|
|
|
/// Clears the database
|
|
|
|
fn clear(&self) -> Result<(), Error>;
|
2017-04-08 11:26:16 +02:00
|
|
|
/// Check if storage contains document encryption key
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> bool;
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Iterate through storage
|
|
|
|
fn iter<'a>(&'a self) -> Box<Iterator<Item=(ServerKeyId, DocumentKeyShare)> + 'a>;
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Persistent document encryption keys storage
|
|
|
|
pub struct PersistentKeyStorage {
|
2018-04-13 21:14:53 +02:00
|
|
|
db: Arc<KeyValueDB>,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Persistent document encryption keys storage iterator
|
|
|
|
pub struct PersistentKeyStorageIterator<'a> {
|
2018-04-13 21:14:53 +02:00
|
|
|
iter: Box<Iterator<Item=(Box<[u8]>, Box<[u8]>)> + 'a>,
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
/// V0 of encrypted key share, as it is stored by key storage on the single key server.
|
2017-09-06 11:09:22 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2018-04-13 21:14:53 +02:00
|
|
|
pub struct SerializableDocumentKeyShareV0 {
|
2017-04-03 11:13:51 +02:00
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<SerializablePublic, SerializableSecret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: SerializableSecret,
|
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: SerializablePublic,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: SerializablePublic,
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
/// V1 of encrypted key share, as it is stored by key storage on the single key server.
|
2017-09-06 11:09:22 +02:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2017-07-06 14:02:10 +02:00
|
|
|
struct SerializableDocumentKeyShareV1 {
|
2018-05-16 20:09:59 +02:00
|
|
|
/// Author of the entry.
|
2017-07-06 14:02:10 +02:00
|
|
|
pub author: SerializablePublic,
|
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<SerializablePublic, SerializableSecret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: SerializableSecret,
|
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: Option<SerializablePublic>,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: Option<SerializablePublic>,
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:27:31 +02:00
|
|
|
/// V2 of encrypted key share, as it is stored by key storage on the single key server.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct SerializableDocumentKeyShareV2 {
|
2017-11-22 09:09:48 +01:00
|
|
|
/// Author of the entry.
|
2017-10-02 15:27:31 +02:00
|
|
|
pub author: SerializablePublic,
|
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
2017-11-22 09:09:48 +01:00
|
|
|
/// Server public.
|
|
|
|
pub public: SerializablePublic,
|
2017-10-02 15:27:31 +02:00
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: Option<SerializablePublic>,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: Option<SerializablePublic>,
|
2017-11-02 15:33:11 +01:00
|
|
|
/// Versions.
|
2018-03-20 03:03:51 +01:00
|
|
|
pub versions: Vec<SerializableDocumentKeyShareVersionV2>
|
|
|
|
}
|
2017-11-02 15:33:11 +01:00
|
|
|
|
|
|
|
/// V2 of encrypted key share version, as it is stored by key storage on the single key server.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct SerializableDocumentKeyShareVersionV2 {
|
|
|
|
/// Version hash.
|
|
|
|
pub hash: SerializableH256,
|
|
|
|
/// Nodes ids numbers.
|
|
|
|
pub id_numbers: BTreeMap<SerializablePublic, SerializableSecret>,
|
|
|
|
/// Node secret share.
|
|
|
|
pub secret_share: SerializableSecret,
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 03:03:51 +01:00
|
|
|
/// V3 of encrypted key share, as it is stored by key storage on the single key server.
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct SerializableDocumentKeyShareV3 {
|
|
|
|
/// Author of the entry.
|
|
|
|
pub author: SerializableAddress,
|
|
|
|
/// Decryption threshold (at least threshold + 1 nodes are required to decrypt data).
|
|
|
|
pub threshold: usize,
|
|
|
|
/// Server public.
|
|
|
|
pub public: SerializablePublic,
|
|
|
|
/// Common (shared) encryption point.
|
|
|
|
pub common_point: Option<SerializablePublic>,
|
|
|
|
/// Encrypted point.
|
|
|
|
pub encrypted_point: Option<SerializablePublic>,
|
|
|
|
/// Versions.
|
|
|
|
pub versions: Vec<SerializableDocumentKeyShareVersionV3>
|
|
|
|
}
|
|
|
|
|
|
|
|
/// V3 of encrypted key share version, as it is stored by key storage on the single key server.
|
|
|
|
type SerializableDocumentKeyShareVersionV3 = SerializableDocumentKeyShareVersionV2;
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
impl PersistentKeyStorage {
|
|
|
|
/// Create new persistent document encryption keys storage
|
2018-04-13 21:14:53 +02:00
|
|
|
pub fn new(db: Arc<KeyValueDB>) -> Result<Self, Error> {
|
2017-07-06 14:02:10 +02:00
|
|
|
let db = upgrade_db(db)?;
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
Ok(PersistentKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
db: db,
|
2017-02-20 16:13:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
fn upgrade_db(db: Arc<KeyValueDB>) -> Result<Arc<KeyValueDB>, Error> {
|
2017-10-16 14:55:11 +02:00
|
|
|
let version = db.get(None, DB_META_KEY_VERSION)?;
|
2017-07-06 14:02:10 +02:00
|
|
|
let version = version.and_then(|v| v.get(0).cloned()).unwrap_or(0);
|
|
|
|
match version {
|
|
|
|
0 => {
|
|
|
|
let mut batch = db.transaction();
|
2017-10-02 15:27:31 +02:00
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]);
|
2018-04-13 21:14:53 +02:00
|
|
|
for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) {
|
2017-07-06 14:02:10 +02:00
|
|
|
let v0_key = serde_json::from_slice::<SerializableDocumentKeyShareV0>(&db_value).map_err(|e| Error::Database(e.to_string()))?;
|
2017-11-02 15:33:11 +01:00
|
|
|
let current_key = CurrentSerializableDocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
// author is used in separate generation + encrypt sessions.
|
|
|
|
// in v0 there have been only simultaneous GenEnc sessions.
|
2019-06-03 15:36:21 +02:00
|
|
|
author: Address::zero().into(), // added in v1
|
2017-07-06 14:02:10 +02:00
|
|
|
threshold: v0_key.threshold,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: Public::default().into(), // addded in v2
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: Some(v0_key.common_point),
|
|
|
|
encrypted_point: Some(v0_key.encrypted_point),
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: vec![CurrentSerializableDocumentKeyVersion {
|
2019-06-03 15:36:21 +02:00
|
|
|
hash: DocumentKeyShareVersion::data_hash(v0_key.id_numbers.iter().map(|(k, v)| (k.as_bytes(), v.as_bytes()))).into(),
|
2017-11-02 15:33:11 +01:00
|
|
|
id_numbers: v0_key.id_numbers,
|
|
|
|
secret_share: v0_key.secret_share,
|
|
|
|
}],
|
2017-07-06 14:02:10 +02:00
|
|
|
};
|
2017-11-02 15:33:11 +01:00
|
|
|
let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?;
|
2017-07-06 14:02:10 +02:00
|
|
|
batch.put(None, &*db_key, &*db_value);
|
|
|
|
}
|
2017-10-16 14:55:11 +02:00
|
|
|
db.write(batch)?;
|
2017-07-06 14:02:10 +02:00
|
|
|
Ok(db)
|
|
|
|
},
|
2017-10-02 15:27:31 +02:00
|
|
|
1 => {
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]);
|
2018-04-13 21:14:53 +02:00
|
|
|
for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) {
|
2017-10-02 15:27:31 +02:00
|
|
|
let v1_key = serde_json::from_slice::<SerializableDocumentKeyShareV1>(&db_value).map_err(|e| Error::Database(e.to_string()))?;
|
2017-11-02 15:33:11 +01:00
|
|
|
let current_key = CurrentSerializableDocumentKeyShare {
|
2018-03-20 03:03:51 +01:00
|
|
|
author: public_to_address(&v1_key.author).into(), // added in v1 + changed in v3
|
2017-10-02 15:27:31 +02:00
|
|
|
threshold: v1_key.threshold,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: Public::default().into(), // addded in v2
|
2017-10-02 15:27:31 +02:00
|
|
|
common_point: v1_key.common_point,
|
|
|
|
encrypted_point: v1_key.encrypted_point,
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: vec![CurrentSerializableDocumentKeyVersion {
|
2019-06-03 15:36:21 +02:00
|
|
|
hash: DocumentKeyShareVersion::data_hash(v1_key.id_numbers.iter().map(|(k, v)| (k.as_bytes(), v.as_bytes()))).into(),
|
2017-11-02 15:33:11 +01:00
|
|
|
id_numbers: v1_key.id_numbers,
|
|
|
|
secret_share: v1_key.secret_share,
|
|
|
|
}],
|
2017-10-02 15:27:31 +02:00
|
|
|
};
|
2017-11-02 15:33:11 +01:00
|
|
|
let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?;
|
2017-10-02 15:27:31 +02:00
|
|
|
batch.put(None, &*db_key, &*db_value);
|
|
|
|
}
|
2017-10-16 14:55:11 +02:00
|
|
|
db.write(batch)?;
|
2017-10-02 15:27:31 +02:00
|
|
|
Ok(db)
|
|
|
|
}
|
2018-03-20 03:03:51 +01:00
|
|
|
2 => {
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[CURRENT_VERSION]);
|
2018-04-13 21:14:53 +02:00
|
|
|
for (db_key, db_value) in db.iter(None).into_iter().filter(|&(ref k, _)| **k != *DB_META_KEY_VERSION) {
|
2018-03-20 03:03:51 +01:00
|
|
|
let v2_key = serde_json::from_slice::<SerializableDocumentKeyShareV2>(&db_value).map_err(|e| Error::Database(e.to_string()))?;
|
|
|
|
let current_key = CurrentSerializableDocumentKeyShare {
|
|
|
|
author: public_to_address(&v2_key.author).into(), // changed in v3
|
|
|
|
threshold: v2_key.threshold,
|
|
|
|
public: v2_key.public,
|
|
|
|
common_point: v2_key.common_point,
|
|
|
|
encrypted_point: v2_key.encrypted_point,
|
|
|
|
versions: v2_key.versions,
|
|
|
|
};
|
|
|
|
let db_value = serde_json::to_vec(¤t_key).map_err(|e| Error::Database(e.to_string()))?;
|
|
|
|
batch.put(None, &*db_key, &*db_value);
|
|
|
|
}
|
|
|
|
db.write(batch)?;
|
|
|
|
Ok(db)
|
|
|
|
},
|
|
|
|
3 => Ok(db),
|
2017-10-02 15:27:31 +02:00
|
|
|
_ => Err(Error::Database(format!("unsupported SecretStore database version: {}", version))),
|
2017-07-06 14:02:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 16:13:21 +01:00
|
|
|
impl KeyStorage for PersistentKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
2017-10-02 15:27:31 +02:00
|
|
|
let key: CurrentSerializableDocumentKeyShare = key.into();
|
2017-04-03 11:13:51 +02:00
|
|
|
let key = serde_json::to_vec(&key).map_err(|e| Error::Database(e.to_string()))?;
|
2017-02-20 16:13:21 +01:00
|
|
|
let mut batch = self.db.transaction();
|
2019-06-03 15:36:21 +02:00
|
|
|
batch.put(None, document.as_bytes(), &key);
|
2017-10-16 14:55:11 +02:00
|
|
|
self.db.write(batch).map_err(Into::into)
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
self.insert(document, key)
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:33:11 +01:00
|
|
|
fn get(&self, document: &ServerKeyId) -> Result<Option<DocumentKeyShare>, Error> {
|
2019-06-03 15:36:21 +02:00
|
|
|
self.db.get(None, document.as_bytes())
|
2017-11-02 15:33:11 +01:00
|
|
|
.map_err(|e| Error::Database(e.to_string()))
|
|
|
|
.and_then(|key| match key {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(key) => serde_json::from_slice::<CurrentSerializableDocumentKeyShare>(&key)
|
|
|
|
.map_err(|e| Error::Database(e.to_string()))
|
|
|
|
.map(Into::into)
|
|
|
|
.map(Some),
|
|
|
|
})
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
|
2017-10-02 15:27:31 +02:00
|
|
|
fn remove(&self, document: &ServerKeyId) -> Result<(), Error> {
|
|
|
|
let mut batch = self.db.transaction();
|
2019-06-03 15:36:21 +02:00
|
|
|
batch.delete(None, document.as_bytes());
|
2017-10-16 14:55:11 +02:00
|
|
|
self.db.write(batch).map_err(Into::into)
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 15:33:11 +01:00
|
|
|
fn clear(&self) -> Result<(), Error> {
|
|
|
|
let mut batch = self.db.transaction();
|
|
|
|
for (key, _) in self.iter() {
|
2019-06-03 15:36:21 +02:00
|
|
|
batch.delete(None, key.as_bytes());
|
2017-11-02 15:33:11 +01:00
|
|
|
}
|
|
|
|
self.db.write(batch)
|
|
|
|
.map_err(|e| Error::Database(e.to_string()))
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> bool {
|
2019-06-03 15:36:21 +02:00
|
|
|
self.db.get(None, document.as_bytes())
|
2017-04-08 11:26:16 +02:00
|
|
|
.map(|k| k.is_some())
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
2017-10-02 15:27:31 +02:00
|
|
|
|
|
|
|
fn iter<'a>(&'a self) -> Box<Iterator<Item=(ServerKeyId, DocumentKeyShare)> + 'a> {
|
|
|
|
Box::new(PersistentKeyStorageIterator {
|
|
|
|
iter: self.db.iter(None),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Iterator for PersistentKeyStorageIterator<'a> {
|
|
|
|
type Item = (ServerKeyId, DocumentKeyShare);
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<(ServerKeyId, DocumentKeyShare)> {
|
2018-04-13 21:14:53 +02:00
|
|
|
self.iter.as_mut().next()
|
|
|
|
.and_then(|(db_key, db_val)| serde_json::from_slice::<CurrentSerializableDocumentKeyShare>(&db_val)
|
|
|
|
.ok()
|
2019-06-03 15:36:21 +02:00
|
|
|
.map(|key| (ServerKeyId::from_slice(&*db_key), key.into())))
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:33:11 +01:00
|
|
|
impl DocumentKeyShare {
|
|
|
|
/// Get last version reference.
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn last_version(&self) -> Result<&DocumentKeyShareVersion, Error> {
|
|
|
|
self.versions.iter().rev()
|
|
|
|
.nth(0)
|
|
|
|
.ok_or_else(|| Error::Database("key version is not found".into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get given version reference.
|
|
|
|
pub fn version(&self, version: &H256) -> Result<&DocumentKeyShareVersion, Error> {
|
|
|
|
self.versions.iter().rev()
|
|
|
|
.find(|v| &v.hash == version)
|
|
|
|
.ok_or_else(|| Error::Database("key version is not found".into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DocumentKeyShareVersion {
|
|
|
|
/// Create new version
|
|
|
|
pub fn new(id_numbers: BTreeMap<NodeId, Secret>, secret_share: Secret) -> Self {
|
|
|
|
DocumentKeyShareVersion {
|
2019-06-03 15:36:21 +02:00
|
|
|
hash: Self::data_hash(id_numbers.iter().map(|(k, v)| (k.as_bytes(), v.as_bytes()))),
|
2017-11-02 15:33:11 +01:00
|
|
|
id_numbers: id_numbers,
|
|
|
|
secret_share: secret_share,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate hash of given version data.
|
|
|
|
pub fn data_hash<'a, I>(id_numbers: I) -> H256 where I: Iterator<Item=(&'a [u8], &'a [u8])> {
|
|
|
|
let mut nodes_keccak = Keccak::new_keccak256();
|
|
|
|
|
|
|
|
for (node, node_number) in id_numbers {
|
|
|
|
nodes_keccak.update(node);
|
|
|
|
nodes_keccak.update(node_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut nodes_keccak_value = [0u8; 32];
|
|
|
|
nodes_keccak.finalize(&mut nodes_keccak_value);
|
|
|
|
|
|
|
|
nodes_keccak_value.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 03:03:51 +01:00
|
|
|
impl From<DocumentKeyShare> for SerializableDocumentKeyShareV3 {
|
2017-10-02 15:27:31 +02:00
|
|
|
fn from(key: DocumentKeyShare) -> Self {
|
2018-03-20 03:03:51 +01:00
|
|
|
SerializableDocumentKeyShareV3 {
|
2017-10-02 15:27:31 +02:00
|
|
|
author: key.author.into(),
|
|
|
|
threshold: key.threshold,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: key.public.into(),
|
2017-10-02 15:27:31 +02:00
|
|
|
common_point: key.common_point.map(Into::into),
|
|
|
|
encrypted_point: key.encrypted_point.map(Into::into),
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: key.versions.into_iter().map(Into::into).collect(),
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 03:03:51 +01:00
|
|
|
impl From<DocumentKeyShareVersion> for SerializableDocumentKeyShareVersionV3 {
|
2017-11-02 15:33:11 +01:00
|
|
|
fn from(version: DocumentKeyShareVersion) -> Self {
|
2018-03-20 03:03:51 +01:00
|
|
|
SerializableDocumentKeyShareVersionV3 {
|
2017-11-02 15:33:11 +01:00
|
|
|
hash: version.hash.into(),
|
|
|
|
id_numbers: version.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: version.secret_share.into(),
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 03:03:51 +01:00
|
|
|
impl From<SerializableDocumentKeyShareV3> for DocumentKeyShare {
|
|
|
|
fn from(key: SerializableDocumentKeyShareV3) -> Self {
|
2017-04-03 11:13:51 +02:00
|
|
|
DocumentKeyShare {
|
2017-07-06 14:02:10 +02:00
|
|
|
author: key.author.into(),
|
2017-04-03 11:13:51 +02:00
|
|
|
threshold: key.threshold,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: key.public.into(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: key.common_point.map(Into::into),
|
|
|
|
encrypted_point: key.encrypted_point.map(Into::into),
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: key.versions.into_iter()
|
|
|
|
.map(|v| DocumentKeyShareVersion {
|
|
|
|
hash: v.hash.into(),
|
|
|
|
id_numbers: v.id_numbers.into_iter().map(|(k, v)| (k.into(), v.into())).collect(),
|
|
|
|
secret_share: v.secret_share.into(),
|
|
|
|
})
|
|
|
|
.collect(),
|
2017-04-03 11:13:51 +02:00
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
2017-11-14 17:47:41 +01:00
|
|
|
extern crate tempdir;
|
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
2019-06-03 15:36:21 +02:00
|
|
|
use std::str::FromStr;
|
2017-02-20 16:13:21 +01:00
|
|
|
use parking_lot::RwLock;
|
2017-07-06 14:02:10 +02:00
|
|
|
use serde_json;
|
2017-11-14 17:47:41 +01:00
|
|
|
use self::tempdir::TempDir;
|
2019-06-03 15:36:21 +02:00
|
|
|
use ethereum_types::{Address, H256, H512};
|
2018-03-20 03:03:51 +01:00
|
|
|
use ethkey::{Random, Generator, Public, Secret, public_to_address};
|
2017-10-12 15:36:27 +02:00
|
|
|
use kvdb_rocksdb::Database;
|
2018-05-01 15:02:14 +02:00
|
|
|
use types::{Error, ServerKeyId};
|
2017-10-02 15:27:31 +02:00
|
|
|
use super::{DB_META_KEY_VERSION, CURRENT_VERSION, KeyStorage, PersistentKeyStorage, DocumentKeyShare,
|
2018-03-20 03:03:51 +01:00
|
|
|
DocumentKeyShareVersion, CurrentSerializableDocumentKeyShare, upgrade_db, SerializableDocumentKeyShareV0,
|
|
|
|
SerializableDocumentKeyShareV1, SerializableDocumentKeyShareV2, SerializableDocumentKeyShareVersionV2};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
/// In-memory document encryption keys storage
|
2017-09-06 11:09:22 +02:00
|
|
|
#[derive(Default)]
|
2017-02-20 16:13:21 +01:00
|
|
|
pub struct DummyKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
keys: RwLock<HashMap<ServerKeyId, DocumentKeyShare>>,
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyStorage for DummyKeyStorage {
|
2017-07-06 14:02:10 +02:00
|
|
|
fn insert(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
|
|
|
self.keys.write().insert(document, key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&self, document: ServerKeyId, key: DocumentKeyShare) -> Result<(), Error> {
|
2017-02-20 16:13:21 +01:00
|
|
|
self.keys.write().insert(document, key);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:33:11 +01:00
|
|
|
fn get(&self, document: &ServerKeyId) -> Result<Option<DocumentKeyShare>, Error> {
|
|
|
|
Ok(self.keys.read().get(document).cloned())
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
2017-04-08 11:26:16 +02:00
|
|
|
|
2017-10-02 15:27:31 +02:00
|
|
|
fn remove(&self, document: &ServerKeyId) -> Result<(), Error> {
|
|
|
|
self.keys.write().remove(document);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-11-02 15:33:11 +01:00
|
|
|
fn clear(&self) -> Result<(), Error> {
|
|
|
|
self.keys.write().clear();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-07-06 14:02:10 +02:00
|
|
|
fn contains(&self, document: &ServerKeyId) -> bool {
|
2017-04-08 11:26:16 +02:00
|
|
|
self.keys.read().contains_key(document)
|
|
|
|
}
|
2017-10-02 15:27:31 +02:00
|
|
|
|
|
|
|
fn iter<'a>(&'a self) -> Box<Iterator<Item=(ServerKeyId, DocumentKeyShare)> + 'a> {
|
|
|
|
Box::new(self.keys.read().clone().into_iter())
|
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn persistent_key_storage() {
|
2017-11-14 17:47:41 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
2019-06-03 15:36:21 +02:00
|
|
|
let key1 = ServerKeyId::from_low_u64_be(1);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value1 = DocumentKeyShare {
|
2018-03-20 03:03:51 +01:00
|
|
|
author: Default::default(),
|
2017-04-03 11:13:51 +02:00
|
|
|
threshold: 100,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: Public::default(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: Some(Random.generate().unwrap().public().clone()),
|
|
|
|
encrypted_point: Some(Random.generate().unwrap().public().clone()),
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: vec![DocumentKeyShareVersion {
|
|
|
|
hash: Default::default(),
|
|
|
|
id_numbers: vec![
|
|
|
|
(Random.generate().unwrap().public().clone(), Random.generate().unwrap().secret().clone())
|
|
|
|
].into_iter().collect(),
|
|
|
|
secret_share: Random.generate().unwrap().secret().clone(),
|
|
|
|
}],
|
2017-04-03 11:13:51 +02:00
|
|
|
};
|
2019-06-03 15:36:21 +02:00
|
|
|
let key2 = ServerKeyId::from_low_u64_be(2);
|
2017-04-03 11:13:51 +02:00
|
|
|
let value2 = DocumentKeyShare {
|
2018-03-20 03:03:51 +01:00
|
|
|
author: Default::default(),
|
2017-04-03 11:13:51 +02:00
|
|
|
threshold: 200,
|
2017-11-22 09:09:48 +01:00
|
|
|
public: Public::default(),
|
2017-07-06 14:02:10 +02:00
|
|
|
common_point: Some(Random.generate().unwrap().public().clone()),
|
|
|
|
encrypted_point: Some(Random.generate().unwrap().public().clone()),
|
2017-11-02 15:33:11 +01:00
|
|
|
versions: vec![DocumentKeyShareVersion {
|
|
|
|
hash: Default::default(),
|
|
|
|
id_numbers: vec![
|
|
|
|
(Random.generate().unwrap().public().clone(), Random.generate().unwrap().secret().clone())
|
|
|
|
].into_iter().collect(),
|
|
|
|
secret_share: Random.generate().unwrap().secret().clone(),
|
|
|
|
}],
|
2017-04-03 11:13:51 +02:00
|
|
|
};
|
2019-06-03 15:36:21 +02:00
|
|
|
let key3 = ServerKeyId::from_low_u64_be(3);
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
|
|
|
|
|
|
|
|
let key_storage = PersistentKeyStorage::new(Arc::new(db)).unwrap();
|
2017-02-20 16:13:21 +01:00
|
|
|
key_storage.insert(key1.clone(), value1.clone()).unwrap();
|
|
|
|
key_storage.insert(key2.clone(), value2.clone()).unwrap();
|
2017-11-02 15:33:11 +01:00
|
|
|
assert_eq!(key_storage.get(&key1), Ok(Some(value1.clone())));
|
|
|
|
assert_eq!(key_storage.get(&key2), Ok(Some(value2.clone())));
|
|
|
|
assert_eq!(key_storage.get(&key3), Ok(None));
|
2017-02-20 16:13:21 +01:00
|
|
|
drop(key_storage);
|
|
|
|
|
2018-04-13 21:14:53 +02:00
|
|
|
let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
|
|
|
|
|
|
|
|
let key_storage = PersistentKeyStorage::new(Arc::new(db)).unwrap();
|
2017-11-02 15:33:11 +01:00
|
|
|
assert_eq!(key_storage.get(&key1), Ok(Some(value1)));
|
|
|
|
assert_eq!(key_storage.get(&key2), Ok(Some(value2)));
|
|
|
|
assert_eq!(key_storage.get(&key3), Ok(None));
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|
2017-07-06 14:02:10 +02:00
|
|
|
|
|
|
|
#[test]
|
2017-10-02 15:27:31 +02:00
|
|
|
fn upgrade_db_from_0() {
|
2017-11-14 17:47:41 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
|
2017-07-06 14:02:10 +02:00
|
|
|
|
|
|
|
// prepare v0 database
|
|
|
|
{
|
|
|
|
let key = serde_json::to_vec(&SerializableDocumentKeyShareV0 {
|
|
|
|
threshold: 777,
|
|
|
|
id_numbers: vec![(
|
2019-06-03 15:36:21 +02:00
|
|
|
H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2017-07-06 14:02:10 +02:00
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap().into(),
|
|
|
|
)].into_iter().collect(),
|
|
|
|
secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap().into(),
|
2019-06-03 15:36:21 +02:00
|
|
|
common_point: H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into(),
|
|
|
|
encrypted_point: H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into(),
|
2017-07-06 14:02:10 +02:00
|
|
|
}).unwrap();
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, &[7], &key);
|
|
|
|
db.write(batch).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgrade database
|
2018-04-13 21:14:53 +02:00
|
|
|
let db = upgrade_db(Arc::new(db)).unwrap();
|
2017-07-06 14:02:10 +02:00
|
|
|
|
|
|
|
// check upgrade
|
2017-10-02 15:27:31 +02:00
|
|
|
assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION);
|
|
|
|
let key = serde_json::from_slice::<CurrentSerializableDocumentKeyShare>(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap();
|
2019-06-03 15:36:21 +02:00
|
|
|
assert_eq!(Address::zero(), key.author.clone().into());
|
2017-07-06 14:02:10 +02:00
|
|
|
assert_eq!(777, key.threshold);
|
2017-11-02 15:33:11 +01:00
|
|
|
assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::<Public>().unwrap()), key.common_point.clone().map(Into::into));
|
|
|
|
assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::<Public>().unwrap()), key.encrypted_point.clone().map(Into::into));
|
|
|
|
|
|
|
|
assert_eq!(key.versions.len(), 1);
|
2017-07-06 14:02:10 +02:00
|
|
|
assert_eq!(vec![(
|
|
|
|
"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::<Public>().unwrap(),
|
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap(),
|
2017-11-02 15:33:11 +01:00
|
|
|
)], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::<Vec<(Public, Secret)>>());
|
|
|
|
assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap(), key.versions[0].secret_share.clone().into());
|
2017-07-06 14:02:10 +02:00
|
|
|
}
|
2017-10-02 15:27:31 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upgrade_db_from_1() {
|
2017-11-14 17:47:41 +01:00
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
|
2017-10-02 15:27:31 +02:00
|
|
|
|
|
|
|
// prepare v1 database
|
|
|
|
{
|
|
|
|
let key = serde_json::to_vec(&SerializableDocumentKeyShareV1 {
|
2019-06-03 15:36:21 +02:00
|
|
|
author: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2017-10-02 15:27:31 +02:00
|
|
|
threshold: 777,
|
|
|
|
id_numbers: vec![(
|
2019-06-03 15:36:21 +02:00
|
|
|
H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2017-10-02 15:27:31 +02:00
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap().into(),
|
|
|
|
)].into_iter().collect(),
|
|
|
|
secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap().into(),
|
2019-06-03 15:36:21 +02:00
|
|
|
common_point: Some(H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into()),
|
|
|
|
encrypted_point: Some(H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into()),
|
2017-10-02 15:27:31 +02:00
|
|
|
}).unwrap();
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[1]);
|
|
|
|
batch.put(None, &[7], &key);
|
|
|
|
db.write(batch).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgrade database
|
2018-04-13 21:14:53 +02:00
|
|
|
let db = upgrade_db(Arc::new(db)).unwrap();
|
2017-10-02 15:27:31 +02:00
|
|
|
|
|
|
|
// check upgrade
|
|
|
|
assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION);
|
|
|
|
let key = serde_json::from_slice::<CurrentSerializableDocumentKeyShare>(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap();
|
|
|
|
assert_eq!(777, key.threshold);
|
|
|
|
assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::<Public>().unwrap()), key.common_point.clone().map(Into::into));
|
|
|
|
assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::<Public>().unwrap()), key.encrypted_point.clone().map(Into::into));
|
2019-06-03 15:36:21 +02:00
|
|
|
assert_eq!(key.author.0, public_to_address(&H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap()));
|
2018-03-20 03:03:51 +01:00
|
|
|
|
|
|
|
assert_eq!(key.versions.len(), 1);
|
|
|
|
assert_eq!(vec![(
|
|
|
|
"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::<Public>().unwrap(),
|
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap(),
|
|
|
|
)], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::<Vec<(Public, Secret)>>());
|
|
|
|
|
|
|
|
assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap(), key.versions[0].secret_share.clone().into());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn upgrade_db_from_2() {
|
|
|
|
let tempdir = TempDir::new("").unwrap();
|
|
|
|
let db = Database::open_default(&tempdir.path().display().to_string()).unwrap();
|
|
|
|
|
|
|
|
// prepare v2 database
|
|
|
|
{
|
|
|
|
let key = serde_json::to_vec(&SerializableDocumentKeyShareV2 {
|
2019-06-03 15:36:21 +02:00
|
|
|
author: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2018-03-20 03:03:51 +01:00
|
|
|
threshold: 777,
|
2019-06-03 15:36:21 +02:00
|
|
|
common_point: Some(H512::from_str("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc").unwrap().into()),
|
|
|
|
encrypted_point: Some(H512::from_str("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c").unwrap().into()),
|
|
|
|
public: H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2018-03-20 03:03:51 +01:00
|
|
|
versions: vec![SerializableDocumentKeyShareVersionV2 {
|
|
|
|
hash: "281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<H256>().unwrap().into(),
|
|
|
|
id_numbers: vec![(
|
2019-06-03 15:36:21 +02:00
|
|
|
H512::from_str("b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8").unwrap().into(),
|
2018-03-20 03:03:51 +01:00
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap().into(),
|
|
|
|
)].into_iter().collect(),
|
|
|
|
secret_share: "00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap().into(),
|
|
|
|
}],
|
|
|
|
}).unwrap();
|
|
|
|
let mut batch = db.transaction();
|
|
|
|
batch.put(None, DB_META_KEY_VERSION, &[2]);
|
|
|
|
batch.put(None, &[7], &key);
|
|
|
|
db.write(batch).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgrade database
|
2018-04-13 21:14:53 +02:00
|
|
|
let db = upgrade_db(Arc::new(db)).unwrap();
|
2018-03-20 03:03:51 +01:00
|
|
|
|
|
|
|
// check upgrade
|
|
|
|
assert_eq!(db.get(None, DB_META_KEY_VERSION).unwrap().unwrap()[0], CURRENT_VERSION);
|
|
|
|
let key = serde_json::from_slice::<CurrentSerializableDocumentKeyShare>(&db.get(None, &[7]).unwrap().map(|key| key.to_vec()).unwrap()).unwrap();
|
|
|
|
assert_eq!(777, key.threshold);
|
|
|
|
assert_eq!(Some("99e82b163b062d55a64085bacfd407bb55f194ba5fb7a1af9c34b84435455520f1372e0e650a4f91aed0058cb823f62146ccb5599c8d13372c300dea866b69fc".parse::<Public>().unwrap()), key.common_point.clone().map(Into::into));
|
|
|
|
assert_eq!(Some("7e05df9dd077ec21ed4bc45c9fe9e0a43d65fa4be540630de615ced5e95cf5c3003035eb713317237d7667feeeb64335525158f5f7411f67aca9645169ea554c".parse::<Public>().unwrap()), key.encrypted_point.clone().map(Into::into));
|
|
|
|
assert_eq!(key.author.0, public_to_address(&"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse().unwrap()));
|
2017-11-02 15:33:11 +01:00
|
|
|
|
|
|
|
assert_eq!(key.versions.len(), 1);
|
|
|
|
assert_eq!(vec![(
|
|
|
|
"b486d3840218837b035c66196ecb15e6b067ca20101e11bd5e626288ab6806ecc70b8307012626bd512bad1559112d11d21025cef48cc7a1d2f3976da08f36c8".parse::<Public>().unwrap(),
|
|
|
|
"281b6bf43cb86d0dc7b98e1b7def4a80f3ce16d28d2308f934f116767306f06c".parse::<Secret>().unwrap(),
|
|
|
|
)], key.versions[0].id_numbers.clone().into_iter().map(|(k, v)| (k.into(), v.into())).collect::<Vec<(Public, Secret)>>());
|
|
|
|
|
|
|
|
assert_eq!("00125d85a05e5e63e214cb60fe63f132eec8a103aa29266b7e6e6c5b7597230b".parse::<Secret>().unwrap(), key.versions[0].secret_share.clone().into());
|
2017-10-02 15:27:31 +02:00
|
|
|
}
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|