-
Notifications
You must be signed in to change notification settings - Fork 83
/
wasm_document.rs
388 lines (326 loc) · 12.8 KB
/
wasm_document.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use std::str::FromStr;
use identity::core::decode_b58;
use identity::crypto::merkle_key::MerkleDigestTag;
use identity::crypto::merkle_key::MerkleKey;
use identity::crypto::merkle_key::Sha256;
use identity::crypto::merkle_tree::Proof;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity::did::verifiable;
use identity::did::MethodScope;
use identity::iota::Error;
use identity::iota::IotaDID;
use identity::iota::IotaDocument;
use identity::iota::IotaVerificationMethod;
use identity::iota::MessageId;
use identity::iota::TangleRef;
use wasm_bindgen::prelude::*;
use crate::common::WasmTimestamp;
use crate::credential::VerifiableCredential;
use crate::credential::VerifiablePresentation;
use crate::crypto::KeyPair;
use crate::crypto::KeyType;
use crate::did::WasmVerificationMethod;
use crate::did::{WasmDID, WasmDocumentDiff};
use crate::error::{Result, WasmResult};
use crate::service::Service;
#[wasm_bindgen(inspectable)]
pub struct NewDocument {
key: KeyPair,
doc: WasmDocument,
}
#[wasm_bindgen]
impl NewDocument {
#[wasm_bindgen(getter)]
pub fn key(&self) -> KeyPair {
self.key.clone()
}
#[wasm_bindgen(getter)]
pub fn doc(&self) -> WasmDocument {
self.doc.clone()
}
}
// =============================================================================
// =============================================================================
#[wasm_bindgen(js_name = Document, inspectable)]
#[derive(Clone, Debug, PartialEq)]
pub struct WasmDocument(pub(crate) IotaDocument);
#[wasm_bindgen(js_class = Document)]
impl WasmDocument {
/// Creates a new DID Document from the given KeyPair.
#[wasm_bindgen(constructor)]
#[allow(clippy::new_ret_no_self)]
pub fn new(type_: KeyType, network: Option<String>, tag: Option<String>) -> Result<NewDocument> {
let keypair: KeyPair = KeyPair::new(type_)?;
let public: &PublicKey = keypair.0.public();
let did: IotaDID = if let Some(network) = network.as_deref() {
IotaDID::new_with_network(public.as_ref(), network).wasm_result()?
} else {
IotaDID::new(public.as_ref()).wasm_result()?
};
let method: IotaVerificationMethod =
IotaVerificationMethod::from_did(did, &keypair.0, tag.as_deref()).wasm_result()?;
let document: IotaDocument = IotaDocument::from_authentication(method).wasm_result()?;
Ok(NewDocument {
key: keypair,
doc: Self(document),
})
}
/// Creates a new DID Document from the given KeyPair and optional network.
///
/// If unspecified, network defaults to the mainnet.
#[wasm_bindgen(js_name = fromKeyPair)]
pub fn from_keypair(key: &KeyPair, network: Option<String>) -> Result<WasmDocument> {
let doc = match network {
Some(net) => IotaDocument::from_keypair_with_network(&key.0, &net),
None => IotaDocument::from_keypair(&key.0),
};
doc.map(Self).wasm_result()
}
/// Creates a new DID Document from the given verification [`method`][`Method`].
#[wasm_bindgen(js_name = fromAuthentication)]
pub fn from_authentication(method: &WasmVerificationMethod) -> Result<WasmDocument> {
IotaDocument::from_authentication(method.0.clone())
.map(Self)
.wasm_result()
}
// ===========================================================================
// Properties
// ===========================================================================
/// Returns the DID Document `id`.
#[wasm_bindgen(getter)]
pub fn id(&self) -> WasmDID {
WasmDID(self.0.id().clone())
}
/// Returns the timestamp of when the DID document was created.
#[wasm_bindgen(getter)]
pub fn created(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.created())
}
/// Sets the timestamp of when the DID document was created.
#[wasm_bindgen(setter = created)]
pub fn set_created(&mut self, timestamp: WasmTimestamp) {
self.0.set_created(timestamp.0)
}
/// Returns the timestamp of the last DID document update.
#[wasm_bindgen(getter)]
pub fn updated(&self) -> WasmTimestamp {
WasmTimestamp::from(self.0.updated())
}
/// Sets the timestamp of the last DID document update.
#[wasm_bindgen(setter = updated)]
pub fn set_updated(&mut self, timestamp: WasmTimestamp) {
self.0.set_updated(timestamp.0)
}
/// Returns the DID Document `proof` object.
#[wasm_bindgen(getter)]
pub fn proof(&self) -> Result<JsValue> {
match self.0.proof() {
Some(proof) => JsValue::from_serde(proof).wasm_result(),
None => Ok(JsValue::NULL),
}
}
/// Returns the default Verification Method of the DID Document.
#[wasm_bindgen]
pub fn authentication(&self) -> WasmVerificationMethod {
WasmVerificationMethod(self.0.authentication().clone())
}
/// Get the message_id of the DID Document.
#[wasm_bindgen(getter = messageId)]
pub fn message_id(&self) -> String {
self.0.message_id().to_string()
}
/// Set the message_id of the DID Document.
#[wasm_bindgen(setter = messageId)]
pub fn set_message_id(&mut self, message_id: &str) -> Result<()> {
let message_id: MessageId = MessageId::from_str(message_id).wasm_result()?;
self.0.set_message_id(message_id);
Ok(())
}
#[wasm_bindgen(getter = previousMessageId)]
pub fn previous_message_id(&self) -> String {
self.0.previous_message_id().to_string()
}
#[wasm_bindgen(setter = previousMessageId)]
pub fn set_previous_message_id(&mut self, value: &str) -> Result<()> {
let message: MessageId = MessageId::from_str(value).wasm_result()?;
self.0.set_previous_message_id(message);
Ok(())
}
// ===========================================================================
// Verification Methods
// ===========================================================================
#[wasm_bindgen(js_name = insertMethod)]
pub fn insert_method(&mut self, method: &WasmVerificationMethod, scope: Option<String>) -> Result<bool> {
let scope: MethodScope = scope.unwrap_or_default().parse().wasm_result()?;
Ok(self.0.insert_method(scope, method.0.clone()))
}
#[wasm_bindgen(js_name = removeMethod)]
pub fn remove_method(&mut self, did: &WasmDID) -> Result<()> {
self.0.remove_method(&did.0).wasm_result()
}
#[wasm_bindgen(js_name = insertService)]
pub fn insert_service(&mut self, service: &Service) -> Result<bool> {
Ok(self.0.insert_service(service.0.clone()))
}
#[wasm_bindgen(js_name = removeService)]
pub fn remove_service(&mut self, did: &WasmDID) -> Result<()> {
self.0.remove_service(&did.0).wasm_result()
}
// ===========================================================================
// Signatures
// ===========================================================================
/// Signs the DID Document with the default authentication method.
#[wasm_bindgen]
pub fn sign(&mut self, key: &KeyPair) -> Result<()> {
self.0.sign(key.0.private()).wasm_result()
}
/// Verify the signature with the authentication_key
#[wasm_bindgen]
pub fn verify(&self) -> bool {
self.0.verify().is_ok()
}
#[wasm_bindgen(js_name = signCredential)]
pub fn sign_credential(&self, data: &JsValue, args: &JsValue) -> Result<VerifiableCredential> {
let json: JsValue = self.sign_data(data, args)?;
let data: VerifiableCredential = VerifiableCredential::from_json(&json)?;
Ok(data)
}
#[wasm_bindgen(js_name = signPresentation)]
pub fn sign_presentation(&self, data: &JsValue, args: &JsValue) -> Result<VerifiablePresentation> {
let json: JsValue = self.sign_data(data, args)?;
let data: VerifiablePresentation = VerifiablePresentation::from_json(&json)?;
Ok(data)
}
/// Creates a signature for the given `data` with the specified DID Document
/// Verification Method.
///
/// An additional `proof` property is required if using a Merkle Key
/// Collection verification Method.
#[wasm_bindgen(js_name = signData)]
pub fn sign_data(&self, data: &JsValue, args: &JsValue) -> Result<JsValue> {
#[derive(Deserialize)]
#[serde(untagged)]
enum Args {
MerkleKey {
method: String,
public: String,
private: String,
proof: String,
},
Default {
method: String,
private: String,
},
}
let mut data: verifiable::Properties = data.into_serde().wasm_result()?;
let args: Args = args.into_serde().wasm_result()?;
match args {
Args::MerkleKey {
method,
public,
private,
proof,
} => {
let merkle_key: Vec<u8> = self
.0
.try_resolve(&*method)
.and_then(|method| method.key_data().try_decode().map_err(Error::InvalidDoc))
.wasm_result()?;
let public: PublicKey = decode_b58(&public).map(Into::into).wasm_result()?;
let private: PrivateKey = decode_b58(&private).map(Into::into).wasm_result()?;
let digest: MerkleDigestTag = MerkleKey::extract_tags(&merkle_key).wasm_result()?.1;
let proof: Vec<u8> = decode_b58(&proof).wasm_result()?;
let signer: _ = self.0.signer(&private).method(&method);
match digest {
MerkleDigestTag::SHA256 => match Proof::<Sha256>::decode(&proof) {
Some(proof) => signer.merkle_key((&public, &proof)).sign(&mut data).wasm_result()?,
None => return Err("Invalid Public Key Proof".into()),
},
_ => return Err("Invalid Merkle Key Digest".into()),
}
}
Args::Default { method, private } => {
let private: PrivateKey = decode_b58(&private).wasm_result().map(Into::into)?;
self.0.signer(&private).method(&method).sign(&mut data).wasm_result()?;
}
}
JsValue::from_serde(&data).wasm_result()
}
/// Verifies the authenticity of `data` using the target verification method.
#[wasm_bindgen(js_name = verifyData)]
pub fn verify_data(&self, data: &JsValue) -> Result<bool> {
let data: verifiable::Properties = data.into_serde().wasm_result()?;
let result: bool = self.0.verifier().verify(&data).is_ok();
Ok(result)
}
#[wasm_bindgen(js_name = resolveKey)]
pub fn resolve_key(&mut self, query: &str) -> Result<WasmVerificationMethod> {
Ok(WasmVerificationMethod(self.0.try_resolve(query).wasm_result()?.clone()))
}
#[wasm_bindgen(js_name = revokeMerkleKey)]
pub fn revoke_merkle_key(&mut self, query: &str, index: usize) -> Result<bool> {
let method: &mut IotaVerificationMethod = self
.0
.try_resolve_mut(query)
.and_then(IotaVerificationMethod::try_from_mut)
.wasm_result()?;
method.revoke_merkle_key(index).wasm_result()
}
// ===========================================================================
// Diffs
// ===========================================================================
/// Generate the difference between two DID Documents and sign it
#[wasm_bindgen]
pub fn diff(&self, other: &WasmDocument, message: &str, key: &KeyPair) -> Result<WasmDocumentDiff> {
self
.0
.diff(&other.0, MessageId::from_str(message).wasm_result()?, key.0.private())
.map(WasmDocumentDiff::from)
.wasm_result()
}
/// Verifies a `DocumentDiff` signature and merges the changes into `self`.
#[wasm_bindgen]
pub fn merge(&mut self, diff: &WasmDocumentDiff) -> Result<()> {
self.0.merge(&diff.0).wasm_result()
}
// ===========================================================================
// Publishing
// ===========================================================================
/// Returns the Tangle index of the integration chain for this DID.
///
/// This is simply the tag segment of the [`IotaDID`].
/// E.g.
/// For an [`IotaDocument`] `doc` with DID: did:iota:1234567890abcdefghijklmnopqrstuvxyzABCDEFGHI,
/// `doc.integration_index()` == "1234567890abcdefghijklmnopqrstuvxyzABCDEFGHI"
#[wasm_bindgen(js_name = integrationIndex)]
pub fn integration_index(&self) -> String {
self.0.integration_index().to_owned()
}
/// Returns the Tangle index of the DID diff chain. This should only be called on documents
/// published on the integration chain.
///
/// This is the Base58-btc encoded SHA-256 digest of the hex-encoded message id.
#[wasm_bindgen(js_name = diffIndex)]
pub fn diff_index(message_id: &str) -> Result<String> {
let message_id = MessageId::from_str(message_id).wasm_result()?;
IotaDocument::diff_index(&message_id).wasm_result()
}
/// Serializes a `Document` object as a JSON object.
#[wasm_bindgen(js_name = toJSON)]
pub fn to_json(&self) -> Result<JsValue> {
JsValue::from_serde(&self.0).wasm_result()
}
/// Deserializes a `Document` object from a JSON object.
#[wasm_bindgen(js_name = fromJSON)]
pub fn from_json(json: &JsValue) -> Result<WasmDocument> {
json.into_serde().map(Self).wasm_result()
}
}
impl From<IotaDocument> for WasmDocument {
fn from(document: IotaDocument) -> Self {
Self(document)
}
}