generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
373 lines (349 loc) · 13.4 KB
/
lib.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
#![deny(clippy::all)]
#[macro_use]
extern crate napi_derive;
use napi::{CallContext, JsArrayBuffer, JsBuffer, JsObject, JsString, JsUndefined, Result};
use universal_wallet::{
locked::LockedWallet,
prelude::{Content, KeyType},
unlocked::UnlockedWallet,
};
mod didcomm;
#[cfg(all(
any(windows, unix),
target_arch = "x86_64",
not(target_env = "musl"),
not(debug_assertions)
))]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
struct Wallet {
unlocked: UnlockedWallet,
}
/// Unlocks locked and encoded wallet into UnlockedWallet instance
/// # Parameters
/// * `locked_wallet` - Base64URL encoded encrypted wallet
/// * `login` - [String] id under which wallet was encrypted
/// * `pass` - [Base64 encoded String] password to unlock wallet
///
/// Wallet will be instantiated into JsObject, which then can be used
/// for further calls. Details: https://napi.rs/concepts/wrap
///
#[js_function(3)]
fn attach_wallet(ctx: CallContext) -> Result<JsUndefined> {
// can be optimized using buffers instead Strings
let locked_wallet = ctx.get::<JsString>(0)?.into_utf8()?;
let login = ctx.get::<JsString>(1)?.into_utf8()?;
let pass = ctx.get::<JsString>(2)?.into_utf8()?;
let mut this: JsObject = ctx.this_unchecked();
let wallet = LockedWallet::new(login.as_str()?, decode_b64(locked_wallet.as_str()?)?);
ctx.env.wrap(
&mut this,
Wallet {
unlocked: wallet
.unlock(&decode_b64(pass.as_str()?)?)
.map_err(|e| napi::Error::from_reason(e.to_string()))?,
},
)?;
ctx.env.get_undefined()
}
/// Locks wallet and returns locked wallet as B64URL string
/// # Parameters
/// * `pass` - password for locking. must be B64URL encoded
///
#[js_function(1)]
fn detach_wallet(ctx: CallContext) -> Result<JsString> {
// can be optimized using buffer instead String
let pass = ctx.get::<JsString>(0)?.into_utf8()?;
let wallet = get_wallet_from_context(&ctx)?;
let locked = wallet
.unlocked
.lock(&decode_b64(pass.as_str()?)?)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
ctx.env
.create_string(&base64::encode_config(locked.ciphertext, base64::URL_SAFE))
}
/// Next set of functions adds key pair of selected type
/// to unlocked wallet.
/// # Parameters
/// * `controller` - [String] name of the key controller
/// * `this` - should be called on `JsObject` of instantiated
/// wallet (done with `new()' or `attach()` methods)
///
#[js_function(1)]
fn new_ecdsa_secp1_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::EcdsaSecp256k1VerificationKey2019)
}
#[js_function(1)]
fn new_ecdsa_recovery_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::EcdsaSecp256k1RecoveryMethod2020)
}
#[js_function(1)]
fn new_ed256_verification_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::Ed25519VerificationKey2018)
}
#[js_function(1)]
fn new_gpg_verification_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::GpgVerificationKey2020)
}
#[js_function(1)]
fn new_jws_verification_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::JwsVerificationKey2020)
}
#[js_function(1)]
fn new_rsa_verification_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::RsaVerificationKey2018)
}
#[js_function(1)]
fn new_schnorr_secp256k1_verification_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::SchnorrSecp256k1VerificationKey2019)
}
#[js_function(1)]
fn new_x25519_key(ctx: CallContext) -> Result<JsUndefined> {
add_key_by_ctx(ctx, KeyType::X25519KeyAgreementKey2019)
}
fn add_key_by_ctx(ctx: CallContext, key_type: KeyType) -> Result<JsUndefined> {
let controller = ctx.get::<JsString>(0)?.into_utf8()?;
let wallet = get_wallet_from_context(&ctx)?;
match wallet
.unlocked
.new_key(key_type, Some(vec![controller.as_str()?.into()]))
{
Err(e) => Err(napi::Error::from_reason(e.to_string())),
_ => ctx.env.get_undefined(),
}
}
// End of key addition section
/// Instantiates new and empty wallet into JS context
/// Exact as `universal_wallet::unlocked::new(id: &str)'
/// # Parameters
/// * `login` - [String] id of newly created wallet.
/// used later to lock/unlock wallet.
///
#[js_function(1)]
fn new_wallet(ctx: CallContext) -> Result<JsUndefined> {
let mut this: JsObject = ctx.this_unchecked();
// can be optimized using buffers instead String
let login = ctx.get::<JsString>(0)?.into_utf8()?;
ctx.env.wrap(
&mut this,
Wallet {
unlocked: UnlockedWallet::new(login.as_str()?),
},
)?;
ctx.env.get_undefined()
}
/// Fetch key as `ContentEntry` from the wallet into JS
/// # Parameters
/// * `key_ref` - [String] search string to the key to fetch
///
#[js_function(1)]
fn get_key(ctx: CallContext) -> Result<JsString> {
let wallet = get_wallet_from_context(&ctx)?;
let key_ref = ctx.get::<JsString>(0)?.into_utf8()?;
match wallet.unlocked.get_key(key_ref.as_str()?) {
Some(content) => ctx.env.create_string(
&serde_json::to_string(&content)
.map_err(|e| napi::Error::from_reason(e.to_string()))?,
),
None => Err(napi::Error::from_reason(format!(
"Key not found for {}",
key_ref.as_str()?
))),
}
}
/// Fetch key as `ContentEntry` from the wallet into JS by controller
/// # Parameters
/// * `controller` - [String] of the controller we want to get content for
///
#[js_function(1)]
fn get_key_by_controller(ctx: CallContext) -> Result<JsString> {
let wallet = get_wallet_from_context(&ctx)?;
let controller = ctx.get::<JsString>(0)?.into_utf8()?;
match wallet.unlocked.get_key_by_controller(controller.as_str()?) {
Some(content) => ctx.env.create_string(
&serde_json::to_string(&content)
.map_err(|e| napi::Error::from_reason(e.to_string()))?,
),
None => Err(napi::Error::from_reason(format!(
"Key not found for {}",
controller.as_str()?
))),
}
}
/// Sets controller of `key_ref` to `controller` value
/// # Parameters
/// * `key_ref` - [String] search string for key to update
/// * `controller` - [String] new value for the controller of the key
///
/// Will return error if any of the parameters is an empty string
///
#[js_function(2)]
fn set_key_controller(ctx: CallContext) -> Result<JsUndefined> {
let wallet = get_wallet_from_context(&ctx)?;
let key_ref = ctx.get::<JsString>(0)?.into_utf8()?;
let controller = ctx.get::<JsString>(1)?.into_utf8()?;
if key_ref.as_str()?.is_empty() || controller.as_str()?.is_empty() {
return Err(napi::Error::from_reason(
"parameters cannot be empty strings".into(),
));
}
match wallet
.unlocked
.set_key_controller(key_ref.as_str()?, controller.as_str()?)
{
Some(()) => ctx.env.get_undefined(),
None => Err(napi::Error::from_reason(format!(
"no key found for {}",
key_ref.as_str()?
))),
}
}
/// Sign arbitrary data with referred key and return signature
/// # Parameters
/// * `key_ref` - [String] key identifier for which key to use for signing
/// * `data` - [Buffer of bytes \ &[u8]] data to be signed
///
/// Returns `JsArrayBuffer` with signature on success or error otherwise.
///
#[js_function(2)]
fn sign_raw(ctx: CallContext) -> Result<JsArrayBuffer> {
let wallet = get_wallet_from_context(&ctx)?;
let key_ref = ctx.get::<JsString>(0)?.into_utf8()?;
let data = &ctx.get::<JsBuffer>(1)?.into_value()?;
if key_ref.as_str()?.is_empty() {
return Err(napi::Error::from_reason(
"key ref cannot be empty string".into(),
));
} else if data.len() == 0 {
return Err(napi::Error::from_reason("can not sign empty data".into()));
}
let signature = wallet
.unlocked
.sign_raw(key_ref.as_str()?, data)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
Ok(ctx.env.create_arraybuffer_with_data(signature)?.into_raw())
}
/// Decrypts provided cypher text using desired key by reference
///
/// # Parameters
///
/// * key_ref - [String] key to be fetched to use for decryption
/// * data - [Buffer of bytes \ &[u8]] cipher to be decrypted
/// * aad - [Buffer of bytes \ &[u8]] `Option` to be used for AAD algorithm
///
/// Return `JsArrayBuffer` with decrypted data on success or error otherwise.
///
#[js_function(3)]
fn decrypt(ctx: CallContext) -> Result<JsArrayBuffer> {
let wallet = get_wallet_from_context(&ctx)?;
let key_ref = ctx.get::<JsString>(0)?.into_utf8()?;
let data = &ctx.get::<JsBuffer>(1)?.into_value()?;
let js_aad = ctx.get::<JsBuffer>(2)?.into_value();
let aad = match &js_aad {
Ok(v) => Some(v.as_ref()),
Err(_) => None,
};
let decrypted = wallet
.unlocked
.decrypt(key_ref.as_str()?, data, aad)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
Ok(ctx.env.create_arraybuffer_with_data(decrypted)?.into_raw())
}
/// Performs ECDH Key Agreement
///
/// # Parameters
///
/// * key_ref - [String] our private key ref for ECDH
/// * other - [Buffer of bytes \ &[u8]] other public key for ECDH
///
/// Return `JsArrayBuffer` with key agreement resulting key on success
/// or error otherwise.
///
#[js_function(2)]
fn ecdh_key_agreement(ctx: CallContext) -> Result<JsArrayBuffer> {
let wallet = get_wallet_from_context(&ctx)?;
let key_ref = ctx.get::<JsString>(0)?.into_utf8()?;
let other = &ctx.get::<JsBuffer>(1)?.into_value()?;
if key_ref.as_str()?.is_empty() {
return Err(napi::Error::from_reason("key_ref can not be empty".into()));
} else if other.len() == 0 {
return Err(napi::Error::from_reason(
"can not agree with empty key".into(),
));
}
let agreement = wallet
.unlocked
.ecdh_key_agreement(key_ref.as_str()?, other)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
Ok(ctx.env.create_arraybuffer_with_data(agreement)?.into_raw())
}
/// Adds `universal_wallet::contents::Content` into current wallet
///
/// # Parameters
///
/// * str_content - JSON serialized Content to be added
///
/// Return JSON serialized `universal_wallet::contents::ContentEntry` on success
///
#[js_function(1)]
fn add_content(ctx: CallContext) -> Result<JsString> {
let wallet = get_wallet_from_context(&ctx)?;
let str_content = ctx.get::<JsString>(0)?.into_utf8()?;
let content: Content = serde_json::from_str(str_content.as_str()?)
.map_err(|e| napi::Error::from_reason(e.to_string()))?;
match wallet.unlocked.import_content(&content) {
Some(ce) => ctx.env.create_string(
&serde_json::to_string(&ce).map_err(|e| napi::Error::from_reason(e.to_string()))?,
),
None => Err(napi::Error::from_reason(
"failed to add content to the wallet :(".into(),
)),
}
}
#[module_exports]
fn init(mut exports: JsObject) -> Result<()> {
exports.create_named_method("attach", attach_wallet)?;
exports.create_named_method("detach", detach_wallet)?;
exports.create_named_method("create", new_wallet)?;
exports.create_named_method("newEcdsaSecp1Key", new_ecdsa_secp1_key)?;
exports.create_named_method("newEcdsaRecoveryKey", new_ecdsa_recovery_key)?;
exports.create_named_method("newEd256VerificationKey", new_ed256_verification_key)?;
exports.create_named_method("newGpgVerificationKey", new_gpg_verification_key)?;
exports.create_named_method("newJwsVerificationKey", new_jws_verification_key)?;
exports.create_named_method("newRsaVerificationKey", new_rsa_verification_key)?;
exports.create_named_method(
"newSchnorrSecp256k1Key",
new_schnorr_secp256k1_verification_key,
)?;
exports.create_named_method("newX25519Key", new_x25519_key)?;
exports.create_named_method("getKey", get_key)?;
exports.create_named_method("getKeyByController", get_key_by_controller)?;
exports.create_named_method("setKeyController", set_key_controller)?;
exports.create_named_method("signRaw", sign_raw)?;
exports.create_named_method("decrypt", decrypt)?;
exports.create_named_method("addContent", add_content)?;
exports.create_named_method("ecdhKeyAgreement", ecdh_key_agreement)?;
exports.create_named_method("createMessage", didcomm::create_message)?;
exports.create_named_method("sealEncrypted", didcomm::seal_encrypted)?;
exports.create_named_method("receiveMessage", didcomm::receive_message)?;
exports.create_named_method("sealJsonMessageJwe", didcomm::seal_encrypted_str)?;
exports.create_named_method("sealJsonMessageJws", didcomm::seal_signed_str)?;
exports.create_named_method("createXc20pJwe", didcomm::create_xc20p_jwe)?;
exports.create_named_method("createAes256GcmJwe", didcomm::create_aes256gcm_jwe)?;
Ok(())
}
pub(crate) fn get_wallet_from_context<'ctx>(ctx: &'ctx CallContext) -> Result<&'ctx mut Wallet> {
let this: JsObject = ctx.this_unchecked();
ctx.env.unwrap::<Wallet>(&this)
}
fn decode_b64(data: &str) -> Result<Vec<u8>> {
base64::decode_config(data, base64::URL_SAFE)
.map_err(|e| napi::Error::from_reason(e.to_string()))
}
#[test]
fn b64_test() {
let conv = base64::encode_config("alice", base64::URL_SAFE);
assert_eq!("YWxpY2U=", &conv);
let dec = base64::decode_config("YWxpY2U=", base64::URL_SAFE).unwrap();
assert_eq!("alice", &String::from_utf8(dec).unwrap());
}