Expand description
§Binary Field Encodings (BFE) for Secure Scuttlebutt (SSB).
Based on the JavaScript reference implementation: ssb-bfe (written according to the specification).
While encode()
and decode()
are the two primary functions exposed by this crate, the
various helper functions and values are also exported for public use.
§Encode
The encoder expects JSON input in the form of a serde_json::Value enum
. The encoded value is returned
as an BfeValue
(a custom enum
provided by this library).
§Decode
The decoder expects input in the form of an BfeValue
(a custom enum
provided by this
library). The decoded value is returned as JSON in the form of a serde_json::Value enum
.
Deserialize
and Serialize
traits have been derived for BfeValue
,
meaning that encoded JSON objects can be parsed into the BfeValue
type if required (for example, if the value is received as a byte slice of
serialized JSON data). See the serde
documentation on
Parsing JSON as strongly typed data structures for an example and further explanation.
§Example
use serde_json::json;
let value = json!({
"author": "@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.ed25519",
"previous": "%R8heq/tQoxEIPkWf0Kxn1nCm/CsxG2CDpUYnAvdbXY8=.sha256",
"bb_msg": "ssb:message/bendybutt-v1/HZVnEzm0NgoSVfG0Hx4gMFbMMHhFvhJsG2zK_pijYII="
});
let encoded = ssb_bfe_rs::encode(&value);
let encoded_value = encoded.unwrap();
println!("{:X?}", encoded_value);
// Object({"author": Buffer([0, 0, E8, 20, 31, 38, 8D, DF, F8, B5, E, 56, B6, C0, 97, 42, 1E, 9A, A8, 92, EC, 4, E9, 42, FA, FD, 31, DC, 3D, 2C, 2E, 3E, 52, FD]), "previous": Buffer([1, 0, 47, C8, 5E, AB, FB, 50, A3, 11, 8, 3E, 45, 9F, D0, AC, 67, D6, 70, A6, FC, 2B, 31, 1B, 60, 83, A5, 46, 27, 2, F7, 5B, 5D, 8F]), "bb_msg": Buffer([1, 4, 1D, 95, 67, 13, 39, B4, 36, A, 12, 55, F1, B4, 1F, 1E, 20, 30, 56, CC, 30, 78, 45, BE, 12, 6C, 1B, 6C, CA, FE, 98, A3, 60, 82])})
let decoded = ssb_bfe_rs::decode(&encoded_value);
let decoded_value = decoded.unwrap();
println!("{:?}", decoded_value);
// Object({"author": String("@6CAxOI3f+LUOVrbAl0IemqiS7ATpQvr9Mdw9LC4+Uv0=.ed25519"), "previous": String("%R8heq/tQoxEIPkWf0Kxn1nCm/CsxG2CDpUYnAvdbXY8=.sha256"), "bb_msg": String("ssb:message/bendybutt-v1/HZVnEzm0NgoSVfG0Hx4gMFbMMHhFvhJsG2zK_pijYII=")})
Modules§
Enums§
- BfeValue
- Represents any valid BFE return value, including values for types which are encoded and those which are not (ie. integers and floats).
Functions§
- decode
- Take a BFE value, match on the value type(s) and call the appropriate decoder(s).
- decode_
blob - Take a blob ID as an encoded byte vector and return a decoded string representation.
- decode_
bool - Take a boolean key as an encoded byte vector and return a boolean value.
- decode_
box - Take a private box as an encoded byte vector and return a decoded string representation.
- decode_
feed - Take a feed ID (key) as an encoded byte vector and return a decoded string representation.
- decode_
msg - Take a message ID as an encoded byte vector and return a decoded string representation.
- decode_
sig - Take a signature as an encoded byte vector and return a string.
- decode_
string - Take a string as an encoded byte vector and return a string.
- decode_
uri - Take an SSB URI as an encoded byte vector and return a string.
- encode
- Take a JSON value, match on the value type(s) and call the appropriate encoder(s).
- encode_
blob - Take a blob ID as a string and return the encoded bytes as a vector.
- encode_
bool - Take a boolean value as a string and return the encoded bytes as a vector.
- encode_
box - Take a box key as a string and return the encoded bytes as a vector.
- encode_
feed - Take a feed ID (key) as a string and return the encoded bytes as a vector.
- encode_
msg - Take a message ID as a string and return the encoded bytes as a vector.
- encode_
sig - Take a signature as a string and return the encoded bytes as a vector.
- encode_
string - Take a string value and return the encoded bytes as a vector.
- encode_
uri - Take an SSB URI as a string and return the encoded bytes as a vector.
- get_
blob_ type - Take a blob ID as a string and return the encoded bytes representing the blob type-format.
- get_
box_ type - Take a box as a string and return the encoded bytes representing the box type-format.
- get_
feed_ type - Take a feed ID (key) as a string and return the encoded bytes representing the feed type-format.
- get_
msg_ type - Take a message ID as a string and return the encoded bytes representing the message type-format.