/
key_collection.rs
145 lines (121 loc) · 4.08 KB
/
key_collection.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use identity::core::decode_b58;
use identity::core::encode_b58;
use identity::crypto::merkle_key::Blake2b256;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::KeyCollection as KeyCollection_;
use identity::crypto::PublicKey;
use identity::crypto::SecretKey;
use wasm_bindgen::prelude::*;
use crate::crypto::Digest;
use crate::crypto::KeyPair;
use crate::crypto::KeyType;
use crate::utils::err;
#[derive(Deserialize, Serialize)]
struct JsonData {
#[serde(rename = "type")]
type_: KeyType,
keys: Vec<KeyData>,
}
#[derive(Deserialize, Serialize)]
struct KeyData {
public: String,
secret: String,
}
// =============================================================================
// =============================================================================
#[wasm_bindgen(inspectable)]
#[derive(Clone, Debug)]
pub struct KeyCollection(pub(crate) KeyCollection_);
#[wasm_bindgen]
impl KeyCollection {
/// Creates a new `KeyCollection` with the specified key type.
#[wasm_bindgen(constructor)]
pub fn new(type_: KeyType, count: usize) -> Result<KeyCollection, JsValue> {
KeyCollection_::new(type_.into(), count).map_err(err).map(Self)
}
/// Returns the number of keys in the collection.
#[wasm_bindgen(getter)]
pub fn length(&self) -> usize {
self.0.len()
}
/// Returns `true` if the collection contains no keys.
#[wasm_bindgen(js_name = isEmpty)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns the keypair at the specified `index`.
#[wasm_bindgen]
pub fn keypair(&self, index: usize) -> Option<KeyPair> {
self.0.keypair(index).map(KeyPair)
}
/// Returns the public key at the specified `index` as a base58-encoded string.
#[wasm_bindgen]
pub fn public(&self, index: usize) -> Option<String> {
self.0.public(index).map(encode_b58)
}
/// Returns the secret key at the specified `index` as a base58-encoded string.
#[wasm_bindgen]
pub fn secret(&self, index: usize) -> Option<String> {
self.0.secret(index).map(encode_b58)
}
#[wasm_bindgen(js_name = merkleRoot)]
pub fn merkle_root(&self, digest: Digest) -> String {
match digest {
Digest::Sha256 => encode_b58(self.0.merkle_root::<Sha256>().as_slice()),
Digest::Blake2b256 => encode_b58(self.0.merkle_root::<Blake2b256>().as_slice()),
}
}
#[wasm_bindgen(js_name = merkleProof)]
pub fn merkle_proof(&self, digest: Digest, index: usize) -> Option<String> {
match digest {
Digest::Sha256 => {
let proof: Proof<Sha256> = match self.0.merkle_proof(index) {
Some(proof) => proof,
None => return None,
};
Some(encode_b58(&proof.encode()))
}
Digest::Blake2b256 => {
let proof: Proof<Blake2b256> = match self.0.merkle_proof(index) {
Some(proof) => proof,
None => return None,
};
Some(encode_b58(&proof.encode()))
}
}
}
/// Serializes a `KeyCollection` object as a JSON object.
#[wasm_bindgen(js_name = toJSON)]
pub fn to_json(&self) -> Result<JsValue, JsValue> {
let public: _ = self.0.iter_public();
let secret: _ = self.0.iter_secret();
let keys: Vec<KeyData> = public
.zip(secret)
.map(|(public, secret)| KeyData {
public: encode_b58(public),
secret: encode_b58(secret),
})
.collect();
let data: JsonData = JsonData {
keys,
type_: self.0.type_().into(),
};
JsValue::from_serde(&data).map_err(err)
}
/// Deserializes a `KeyCollection` object from a JSON object.
#[wasm_bindgen(js_name = fromJSON)]
pub fn from_json(json: &JsValue) -> Result<KeyCollection, JsValue> {
let data: JsonData = json.into_serde().map_err(err)?;
let iter: _ = data.keys.iter().flat_map(|data| {
let pk: PublicKey = decode_b58(&data.public).ok()?.into();
let sk: SecretKey = decode_b58(&data.secret).ok()?.into();
Some((pk, sk))
});
KeyCollection_::from_iterator(data.type_.into(), iter)
.map_err(err)
.map(Self)
}
}