1

I try to serialize the PublicKey struct of sodiumoxide (Rust bindings for libsodium) to a file (e.g. JSON, but binary would be okay, too).

Here is my code:

extern crate serde;
extern crate serde_json;
extern crate sodiumoxide;

use serde::Serialize;
use serde_json::ser::Serializer;
use sodiumoxide::crypto::sign;

fn main() {
    let (pk, _) = sign::gen_keypair();    
    let pk_ser = serde_json::to_string(&pk);
}

I get the following error message:

error: the trait bound `sodiumoxide::crypto::sign::PublicKey: serde::Serialize` is not satisfied [E0277]

So the compiler tells me that PublicKey should implement the serde::Serialize trait. But it does implement serde::Serialize as stated here: https://dnaq.github.io/sodiumoxide/sodiumoxide/crypto/sign/ed25519/struct.PublicKey.html

So, what is the problem?

Edit:

Cargo.toml:

[package]
name = ...
version = ...
authors = ...

[dependencies]
serde       = "*"
serde_json  = "*"
sodiumoxide = "*"
duesee
  • 141
  • 1
  • 9

1 Answers1

2

The latest available version of sodiumoxide on crates.io is currently 0.0.10 which doesn't support serde. You can see this if you look at the Cargo.toml file for the 0.0.10 tag.

What you can do for now is to use the dependency from github instead of crates.io until they put out a new version. Edit your Cargo.toml file like this:

[dependencies]
serde       = "*"
serde_json  = "*"
sodiumoxide = { git = "https://github.com/dnaq/sodiumoxide" }

Since you're using the version of sodiumoxide from github, you'll also need to use the github version of its FFI wrapper libsodium-sys. You can do this by adding this to your Cargo.toml:

[replace]
"libsodium-sys:0.0.10" = { git = "https://github.com/dnaq/sodiumoxide/" }
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69