hedera 0.9.0

The SDK for interacting with Hedera Hashgraph.
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * ‌
 * Hedera Rust SDK
 * ​
 * Copyright (C) 2022 - 2023 Hedera Hashgraph, LLC
 * ​
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ‍
 */

use std::ffi::{
    CStr,
    CString,
};
use std::os::raw::c_char;
use std::str::FromStr;
use std::{
    ptr,
    slice,
};

use libc::size_t;

use super::{
    parse_bytes,
    parse_str,
};
use crate::ffi::error::Error;
use crate::ffi::key::to_bytes;
use crate::ffi::util;
use crate::{
    Mnemonic,
    PrivateKey,
    PublicKey,
};

/// Generates a new Ed25519 private key.
#[no_mangle]
pub extern "C" fn hedera_private_key_generate_ed25519() -> *mut PrivateKey {
    let key = PrivateKey::generate_ed25519();

    Box::into_raw(Box::new(key))
}

/// Generates a new ECDSA(secp256k1) private key.
#[no_mangle]
pub extern "C" fn hedera_private_key_generate_ecdsa() -> *mut PrivateKey {
    let key = PrivateKey::generate_ecdsa();

    Box::into_raw(Box::new(key))
}

/// Gets the public key which corresponds to this [`PrivateKey`].
///
/// # Safety:
/// - `key` must be valid for reads according to [*Rust* pointer rules]
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_get_public_key(key: *mut PrivateKey) -> *mut PublicKey {
    // safety: caller promises that `key` must be valid for reads.
    let sk = unsafe { key.as_ref().unwrap() };

    let pk = sk.public_key();

    Box::into_raw(Box::new(pk))
}

/// Parse a `PrivateKey` from a sequence of bytes.
///
/// # Safety
/// - `bytes` must be valid for reads of up to `bytes_size` bytes.
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `bytes` cannot be parsed into a `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_bytes(
    bytes: *const u8,
    bytes_size: size_t,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_bytes(bytes, bytes_size, key, PrivateKey::from_bytes) }
}

/// Parse a `PrivateKey` from a sequence of bytes.
///
/// # Safety
/// - `bytes` must be valid for reads of up to `bytes_size` bytes.
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `bytes` cannot be parsed into a ed25519 `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_bytes_ed25519(
    bytes: *const u8,
    bytes_size: size_t,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_bytes(bytes, bytes_size, key, PrivateKey::from_bytes_ed25519) }
}

/// Parse a `PrivateKey` from a sequence of bytes.
///
/// # Safety
/// - `bytes` must be valid for reads of up to `bytes_size` bytes.
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `bytes` cannot be parsed into a ECDSA(secp256k1) `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_bytes_ecdsa(
    bytes: *const u8,
    bytes_size: size_t,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_bytes(bytes, bytes_size, key, PrivateKey::from_bytes_ecdsa) }
}

/// Parse a `PrivateKey` from a sequence of bytes.
///
/// # Safety
/// - `bytes` must be valid for reads of up to `bytes_size` bytes.
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `bytes` cannot be parsed into a `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_bytes_der(
    bytes: *const u8,
    bytes_size: size_t,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_bytes(bytes, bytes_size, key, PrivateKey::from_bytes_der) }
}

/// Parse a Hedera private key from the passed string.
///
/// Optionally strips a `0x` prefix.
/// See [`hedera_private_key_from_bytes`]
///
/// # Safety
/// - `s` must be a valid string
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `s` cannot be parsed into a `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_string(
    s: *const c_char,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_str(s, key, PrivateKey::from_str) }
}

/// Parse a `PrivateKey` from a der encoded string.
///
/// Optionally strips a `0x` prefix.
/// See [`hedera_private_key_from_bytes_der`].
///
/// # Safety
/// - `s` must be a valid string
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `s` cannot be parsed into a `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_string_der(
    s: *const c_char,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_str(s, key, PrivateKey::from_str_der) }
}

/// Parse a Ed25519 `PrivateKey` from a string containing the raw key material.
///
/// Optionally strips a `0x` prefix.
/// See: [`hedera_private_key_from_bytes_ed25519`]
///
/// # Safety
/// - `s` must be a valid string
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `s` cannot be parsed into a ed25519 `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_string_ed25519(
    s: *const c_char,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_str(s, key, PrivateKey::from_str_der) }
}

/// Parse a ECDSA(secp256k1) `PrivateKey` from a string containing the raw key material.
///
/// Optionally strips a `0x` prefix.
/// See: [`hedera_private_key_from_bytes_ecdsa`]
///
/// # Safety
/// - `s` must be a valid string
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyParse`] if `s` cannot be parsed into a ECDSA(secp256k1) `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_string_ecdsa(
    s: *const c_char,
    key: *mut *mut PrivateKey,
) -> Error {
    // safety: invariants are passed through from the caller.
    unsafe { parse_str(s, key, PrivateKey::from_str_der) }
}

/// Parse a Hedera private key from the passed pem encoded string
///
/// # Safety
/// - `pem` must be a valid string
/// - `key` must be a valid for writes according to [*Rust* pointer rules].
///   The inner pointer need not point to a valid `PrivateKey`, however.
///
/// # Errors
/// - [`Error::KeyParse`] if `pem` is not valid PEM.
/// - [`Error::KeyParse`] if the type label (BEGIN XYZ) is not `PRIVATE KEY`.
/// - [`Error::KeyParse`] if the data contained inside the PEM is not a valid `PrivateKey`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_pem(
    pem: *const c_char,
    key: *mut *mut PrivateKey,
) -> Error {
    assert!(!key.is_null());

    // safety: function contract requires that pem is a valid `CStr`.
    let pem = unsafe { CStr::from_ptr(pem) };
    let parsed = ffi_try!(PrivateKey::from_pem(pem.to_bytes()));

    let parsed = Box::into_raw(Box::new(parsed));
    // safety:
    // function contract requires that `key` points to a valid `*mut *mut PrivateKey`
    // function contract requires us *not* to inspect the old value.
    //  ^ and we don't, which is good.
    unsafe { ptr::write(key, parsed) }

    Error::Ok
}

/// Return `key`, serialized as der encoded bytes.
///
/// Note: the returned `buf` must be freed via `hedera_bytes_free` in order to prevent a memory leak.
///
/// # Safety
/// - `key` must be valid for reads according to [*Rust* pointer rules]
/// - `buf` must be valid for writes according to [*Rust* pointer rules]
/// - the length of the returned buffer must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_bytes_der(
    key: *mut PrivateKey,
    buf: *mut *mut u8,
) -> size_t {
    // safety: invariants are passed through from the caller.
    unsafe { to_bytes(key, buf, PrivateKey::to_bytes_der) }
}

/// Return `key`, serialized as bytes.
///
/// Note: `buf` must be freed via `hedera_bytes_free` in order to prevent a memory leak.
///
/// If this is an ed25519 private key, this is equivalent to [`hedera_private_key_to_bytes_raw`]
/// If this is an ecdsa private key, this is equivalent to [`hedera_private_key_to_bytes_der`]
/// # Safety
/// - `key` must be valid for reads according to [*Rust* pointer rules]
/// - `buf` must be valid for writes according to [*Rust* pointer rules]
/// - the length of the returned buffer must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_bytes(
    key: *mut PrivateKey,
    buf: *mut *mut u8,
) -> size_t {
    // safety: invariants are passed through from the caller.
    unsafe { to_bytes(key, buf, PrivateKey::to_bytes) }
}

/// Return `key`, serialized as bytes.
///
/// Note: `buf` must be freed via `hedera_bytes_free` in order to prevent a memory leak.
///
/// # Safety
/// - `key` must be valid for reads according to [*Rust* pointer rules]
/// - `buf` must be valid for writes according to [*Rust* pointer rules]
/// - the length of the returned buffer must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_bytes_raw(
    key: *mut PrivateKey,
    buf: *mut *mut u8,
) -> size_t {
    // safety: invariants are passed through from the caller.
    unsafe { to_bytes(key, buf, PrivateKey::to_bytes_raw) }
}

/// Format a Hedera private key as a string.
///
/// Note: the returned string must be freed via `hedera_string_free` in order to prevent a memory leak.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - the length of the returned string must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_string(key: *mut PrivateKey) -> *mut c_char {
    // potential optimization: `PrivateKey::to_string` just calls `PrivateKey::to_string_der`,
    // so, this function *could* just call `hedera_private_key_to_string_der`
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    // if this unwrap fails the called method's impl has a bug,
    // because hex-encoded anything doesn't contain `\0`.
    CString::new(key.to_string()).unwrap().into_raw()
}

/// Format a Hedera private key as a der encoded string.
///
/// Note: the returned string must be freed via `hedera_string_free` in order to prevent a memory leak.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - the length of the returned string must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_string_der(key: *mut PrivateKey) -> *mut c_char {
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    // if this unwrap fails the called method's impl has a bug,
    // because hex-encoded anything doesn't contain `\0`.
    CString::new(key.to_string_der()).unwrap().into_raw()
}

/// Format a Hedera private key as a string.
///
/// Note: the returned string must be freed via `hedera_string_free` in order to prevent a memory leak.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - the length of the returned string must not be modified.
/// - the returned pointer must NOT be freed with `free`.
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_to_string_raw(key: *mut PrivateKey) -> *mut c_char {
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    // if this unwrap fails the called method's impl has a bug,
    // because hex-encoded anything doesn't contain `\0`.
    CString::new(key.to_string_raw()).unwrap().into_raw()
}

// ffi note: `bool` is defined to have the same layout as c17's `_Bool`.
// see: https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#bool
// the wording is a little confusing because it says "which is implementation defined"
// but it means that Rust `bool` == c17 `bool` == "something"
/// Returns `true` if `key` is an Ed25519 `PrivateKey`.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_is_ed25519(key: *mut PrivateKey) -> bool {
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    key.is_ed25519()
}

/// Returns `true` if `key` is an ECDSA(secp256k1) `PrivateKey`.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_is_ecdsa(key: *mut PrivateKey) -> bool {
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    key.is_ecdsa()
}

/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - `message` must be valid for reads of up to `message_size` bytes.
/// - `buf` must be valid for writes according to [*Rust* pointer rules]
/// - the length of the returned buffer must not be modified.
/// - the returned pointer must NOT be freed with `free`.
/// [*Rust* pointer rules]: <https://doc.rust-lang.org/std/ptr/index.html#safety>
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_sign(
    key: *mut PrivateKey,
    message: *const u8,
    message_size: size_t,
    buf: *mut *mut u8,
) -> size_t {
    assert!(!message.is_null());
    assert!(!key.is_null());

    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    // safety: caller promises that `message` is valid for r/w of up to `message_size`, which is exactly what `slice::from_raw_parts` wants.
    let message = unsafe { slice::from_raw_parts(message, message_size) };

    let bytes = key.sign(message).into_boxed_slice();

    let bytes = Box::leak(bytes);
    let len = bytes.len();
    let bytes = bytes.as_mut_ptr();

    // safety: invariants promise that `buf` must be valid for writes.
    unsafe {
        ptr::write(buf, bytes);
    }

    len
}

/// Returns true if calling [`derive`](Self::derive) on `key` would succeed.
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_is_derivable(key: *mut PrivateKey) -> bool {
    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    key.is_derivable()
}

/// Derives a child key based on `index`.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - `derived` must be a pointer that is valid for writes according to the [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyDerive`] if this is an Ecdsa key (unsupported operation)
/// - [`Error::KeyDerive`] if this key has no `chain_code` (key is not derivable)
///
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_derive(
    key: *mut PrivateKey,
    index: i32,
    derived: *mut *mut PrivateKey,
) -> Error {
    assert!(!derived.is_null());

    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    let output = ffi_try!(key.derive(index));

    // safety: caller promises that `derived` must be valid for reads
    unsafe {
        ptr::write(derived, Box::into_raw(Box::new(output)));
    }

    Error::Ok
}

/// Derive a `PrivateKey` based on `index`.
///
/// # Safety
/// - `key` must be a pointer that is valid for reads according to the [*Rust* pointer rules].
/// - `derived` must be a pointer that is valid for writes according to the [*Rust* pointer rules].
///
/// # Errors
/// - [`Error::KeyDerive`] if this is an Ecdsa key (unsupported operation)
///  
/// [*Rust* pointer rules]: https://doc.rust-lang.org/std/ptr/index.html#safety
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_legacy_derive(
    key: *mut PrivateKey,
    index: i64,
    derived: *mut *mut PrivateKey,
) -> Error {
    assert!(!derived.is_null());

    // safety: caller promises that `key` must be valid for reads
    let key = unsafe { key.as_ref().unwrap() };

    let output = ffi_try!(key.legacy_derive(index));

    // safety: caller promises that `derived` must be valid for reads
    unsafe {
        ptr::write(derived, Box::into_raw(Box::new(output)));
    }

    Error::Ok
}

/// Recover a `PrivateKey` from a mnemonic phrase and a passphrase.
///
/// # Safety
/// - `mnemonic` must be valid for reads according to the [*Rust* pointer rules].
/// - `passphrase` must be valid for reads up until and including the first NUL (`'\0'`) byte.
/// - the retured `PrivateKey` must only be freed via [`hedera_private_key_free`], notably, this means that it *must not* be freed with `free`.

#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_from_mnemonic(
    mnemonic: *mut Mnemonic,
    passphrase: *const c_char,
) -> *mut PrivateKey {
    let mnemonic = unsafe { mnemonic.as_ref() }.unwrap();
    let passphrase = unsafe { util::cstr_from_ptr(passphrase) };

    let sk = PrivateKey::from_mnemonic(mnemonic, &passphrase);
    Box::into_raw(Box::new(sk))
}

/// Releases memory associated with the private key.
#[no_mangle]
pub unsafe extern "C" fn hedera_private_key_free(key: *mut PrivateKey) {
    assert!(!key.is_null());

    drop(unsafe { Box::from_raw(key) });
}