Skip to content

Commit

Permalink
working cbor (#25)
Browse files Browse the repository at this point in the history
* DIdPlc stuff (#21)

* remove .so file, upgrade rustler

* begin DidPlc

* started DidPlc classes (don't look please)

* closer to working did generation

* doctest for cbor encoding

* okay fine

* use to_bytes

* decode16

* add round trip cbor test

* fix map key syntax

* trying libipld for encoding dag-cbor

* missing cid tag

maybe?

* what if we construct a tagged IPLD node with the CID tag and insert it into the result map

* manually tag it? idk

* fortune favors the bold

* mission failed, we'll get 'em next time

* update README to reflect new goals :) (#23)

* fix example case in dagcbor docs (#24)

---------

Co-authored-by: flicknow <[email protected]>
Co-authored-by: nova <[email protected]>
Co-authored-by: Samuel Newman <[email protected]>
Co-authored-by: mark <[email protected]>
  • Loading branch information
5 people authored Feb 23, 2024
1 parent 0f5d8f3 commit 3ed03ca
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# hexpds
A minimum viable atproto PDS in Elixir
An ATProto PDS in Elixir/Rust

## Installation

Expand Down
2 changes: 2 additions & 0 deletions lib/hexpds/dagcbor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Hexpds.DagCBOR do
def decode_dag_cbor(_cbor), do: :erlang.nif_error(:nif_not_loaded)
end

def encode_json(json) do
@spec encode(binary() | map()) :: {:error, binary()} | {:ok, binary()}
@doc """
Encodes a JSON string or a map into a CBOR binary.
Expand All @@ -24,6 +25,7 @@ defmodule Hexpds.DagCBOR do
{:ok, to_string(cbor)}
end
end

def encode(%{} = json) do
with {:ok, json} <- Jason.encode(json), do: encode(json)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/hexpds/k256.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule Hexpds.K256 do
def create(), do: %__MODULE__{privkey: :crypto.strong_rand_bytes(32)}

@spec from_binary(binary()) :: t()

@doc """
Wraps a Secp256k1 private key from its raw bytes.
"""
Expand All @@ -32,6 +33,7 @@ defmodule Hexpds.K256 do
def from_hex(hex), do: from_binary(Base.decode16!(hex, case: :lower))

@spec to_hex(t()) :: String.t()

@doc """
Converts a Secp256k1 private key to a hex-encoded string.
"""
Expand All @@ -45,6 +47,7 @@ defmodule Hexpds.K256 do
@doc """
Signs a binary message with a Secp256k1 private key. Returns a binary signature.
"""

def sign(%__MODULE__{privkey: privkey}, message) when is_binary(message) and is_valid_key(privkey) do
with {:ok, sig} <- Hexpds.K256.Internal.sign_message(privkey, message), do: to_string(sig)
end
Expand Down
1 change: 1 addition & 0 deletions native/hexpds_dagcbor_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
libipld = "0.16.0"
rustler = "0.31.0"
serde_ipld_dagcbor = "0.4.2"
serde_json = "1.0.113"
Expand Down
76 changes: 64 additions & 12 deletions native/hexpds_dagcbor_internal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use rustler::Encoder;
use rustler::Env;
use rustler::NifResult;
use rustler::Term;
use rustler::Binary;
use serde_json::from_str;
use serde_ipld_dagcbor::to_vec;
use serde_json::Value;
use libipld::Ipld;
use libipld::Cid;
use libipld::codec::Codec;
use libipld::cbor::DagCborCodec;
use std::collections::BTreeMap;
use std::str::FromStr;

mod atoms {
rustler::atoms! {
Expand All @@ -12,19 +19,64 @@ mod atoms {
}
}

pub fn json_to_ipld(val: Value) -> Ipld {
match val {
Value::Null => Ipld::Null,
Value::Bool(b) => Ipld::Bool(b),
Value::String(s) => Ipld::String(s),
Value::Number(v) => {
if let Some(f) = v.as_f64() {
if v.is_i64() {
Ipld::Integer(v.as_i64().unwrap().into())
} else if v.is_u64() {
Ipld::Integer(v.as_i64().unwrap_or_else(|| f as i64).into())
} else {
Ipld::Float(f)
}
} else {
Ipld::Null
}
},
Value::Array(l) => Ipld::List(l.into_iter().map(json_to_ipld).collect()),
Value::Object(m) => {
let map: BTreeMap<String, Ipld> = BTreeMap::from_iter(m.into_iter().map(|(k, v)| {
if k == "cid" && v.is_string() {
(k, Ipld::Link(Cid::from_str(v.as_str().unwrap()).unwrap()))
} else {
(k, json_to_ipld(v))
}
}));
Ipld::Map(map)
}
}
}

#[rustler::nif]
fn encode_dag_cbor(env: Env, json: String) -> Term {
let parsed_json: serde_json::Value = match from_str(&json) {
Ok(json) => json,
Err(e) => return (atoms::error(), format!("Failed to parse JSON: {}", e)).encode(env),
};

let cbor_data = match to_vec(&parsed_json) {
Ok(data) => data,
Err(e) => return (atoms::error(), format!("Failed to encode to DAG-CBOR: {}", e)).encode(env),
fn encode_dag_cbor(env: Env, json: String) -> NifResult<Term> {
let parsed_json: serde_json::Value = match from_str(&json) {
Ok(json) => json,
Err(e) => return Ok((atoms::error(), format!("Failed to parse JSON: {}", e)).encode(env)),
};

(atoms::ok(), cbor_data).encode(env)
let ipld_data = json_to_ipld(parsed_json);

let encoded_dag_cbor = DagCborCodec.encode(&ipld_data);

match encoded_dag_cbor {
Ok(buffer) => {
let mut binary = rustler::types::binary::OwnedBinary::new(buffer.len()).unwrap();

{
let binary_slice = binary.as_mut_slice();
binary_slice.copy_from_slice(&buffer);
}

Ok((atoms::ok(), binary.release(env)).encode(env))
},
Err(e) => {
return Ok((atoms::error(), format!("Failed to encode to DAG-CBOR: {}", e)).encode(env));
}
}
}

#[rustler::nif]
Expand All @@ -42,4 +94,4 @@ fn decode_dag_cbor<'a>(env: Env<'a>, cbor_data: Binary<'a>) -> Term<'a> {
(atoms::ok(), json).encode(env)
}

rustler::init!("Elixir.Hexpds.DagCBOR.Internal", [encode_dag_cbor, decode_dag_cbor]);
rustler::init!("Elixir.Hexpds.DagCBOR.Internal", [encode_dag_cbor, decode_dag_cbor]);
4 changes: 2 additions & 2 deletions native/hexpds_k256_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ fn sign_message<'a>(env: Env<'a>, private_key: Binary<'a>, message: Binary<'a>)

let signature_bytes = signature_low_s.to_bytes();

let signature_vec = signature_bytes.to_vec();
let signature_hex = hex::encode(signature_bytes);

(atoms::ok(), signature_vec).encode(env)
(atoms::ok(), signature_hex).encode(env)
}

rustler::init!(
Expand Down
18 changes: 18 additions & 0 deletions test/hexpds_dagcbor_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
defmodule HexpdsDagcborTest do
use ExUnit.Case
doctest Hexpds.DagCBOR

defp test_cases do
[
"test text",
["test text"],
%{"string" => "test text"},
%{"map" => %{"string" => "test text"}},
%{"list" => ["text"]},
]
end

test "cbor roundtrip" do
for input <- test_cases() do
{:ok, cbor_encoded} = Hexpds.DagCBOR.encode_json(Jason.encode!(input))
{:ok, json_encoded} = Hexpds.DagCBOR.decode_json(cbor_encoded)
assert input == Jason.decode!(json_encoded)
end
end
end

0 comments on commit 3ed03ca

Please sign in to comment.