Skip to content

Commit b22a672

Browse files
committed
fix(rust/signed_doc): replace ULID with UUIDv7
1 parent 424cc0a commit b22a672

File tree

3 files changed

+22
-45
lines changed

3 files changed

+22
-45
lines changed

rust/signed_doc/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@ jsonschema = "0.18.0"
2121
coset = "0.3.7"
2222
brotli = "7.0.0"
2323
ed25519-dalek = { version = "2.1.1", features = ["pem"] }
24-
uuid = { version = "1.10.0", features = ["v4", "serde"] }
25-
ulid = { version = "1.1.3", features = ["serde"] }
24+
uuid = { version = "1.10.0", features = ["v4", "v7", "serde"] }

rust/signed_doc/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ which **must** be present (most of the fields originally defined by this
2727
(this parameter is used to indicate the content encodings algorithm of the payload data,
2828
in this particular case [brotli] compression data format is used).
2929
* `type`: CBOR encoded UUID.
30-
* `id`: CBOR encoded ULID.
31-
* `ver`: CBOR encoded ULID.
32-
* `ref`: CBOR encoded ULID or two elements array of ULIDs (optional).
33-
* `template`: CBOR encoded ULID or two elements array of ULIDs (optional).
34-
* `reply`: CBOR encoded ULID or two elements array of ULIDs (optional).
30+
* `id`: CBOR encoded UUIDv7.
31+
* `ver`: CBOR encoded UUIDv7.
32+
* `ref`: CBOR encoded UUIDv7 or two elements array of UUIDv7 (optional).
33+
* `template`: CBOR encoded UUIDv7 or two elements array of UUIDv7 (optional).
34+
* `reply`: CBOR encoded UUIDv7 or two elements array of UUIDv7 (optional).
3535
* `section`: CBOR encoded string (optional).
3636
* `collabs`: CBOR encoded array of any CBOR types (optional).
3737

@@ -46,8 +46,8 @@ protected_header = {
4646
3 => 30, ; "content type": Json
4747
"content encoding" => "br", ; payload content encoding, brotli compression
4848
"type" => UUID,
49-
"id" => ULID,
50-
"ver" => ULID,
49+
"id" => UUIDv7,
50+
"ver" => UUIDv7,
5151
? "ref" => reference_type,
5252
? "template" => reference_type,
5353
? "reply" => reference_type,
@@ -56,8 +56,7 @@ protected_header = {
5656
}
5757
5858
UUID = #6.37(bytes)
59-
ULID = #6.32780(bytes)
60-
reference_type = ULID / [ULID, ULID] ; either ULID or [ULID, ULID]
59+
reference_type = UUIDv7 / [UUIDv7, UUIDv7] ; either UUIDv7 or [UUIDv7, UUIDv7]
6160
```
6261

6362
### COSE payload

rust/signed_doc/examples/mk_signed_doc.rs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,12 @@ enum Cli {
5959
const CONTENT_ENCODING_KEY: &str = "content encoding";
6060
const CONTENT_ENCODING_VALUE: &str = "br";
6161
const UUID_CBOR_TAG: u64 = 37;
62-
const ULID_CBOR_TAG: u64 = 32780;
6362

6463
#[derive(Debug, serde::Deserialize)]
6564
struct Metadata {
6665
r#type: uuid::Uuid,
67-
id: ulid::Ulid,
68-
ver: ulid::Ulid,
66+
id: uuid::Uuid,
67+
ver: uuid::Uuid,
6968
r#ref: Option<DocumentRef>,
7069
template: Option<DocumentRef>,
7170
reply: Option<DocumentRef>,
@@ -76,29 +75,9 @@ struct Metadata {
7675
#[serde(untagged)]
7776
enum DocumentRef {
7877
/// Reference to the latest document
79-
Latest { id: ulid::Ulid },
78+
Latest { id: uuid::Uuid },
8079
/// Reference to the specific document version
81-
WithVer { id: ulid::Ulid, ver: ulid::Ulid },
82-
}
83-
84-
fn encode_cbor_ulid(ulid: &ulid::Ulid) -> coset::cbor::Value {
85-
coset::cbor::Value::Tag(
86-
ULID_CBOR_TAG,
87-
coset::cbor::Value::Bytes(ulid.to_bytes().to_vec()).into(),
88-
)
89-
}
90-
91-
fn decode_cbor_ulid(val: &coset::cbor::Value) -> anyhow::Result<ulid::Ulid> {
92-
let Some((ULID_CBOR_TAG, coset::cbor::Value::Bytes(bytes))) = val.as_tag() else {
93-
anyhow::bail!("Invalid CBOR encoded ULID type");
94-
};
95-
let ulid = ulid::Ulid::from_bytes(
96-
bytes
97-
.clone()
98-
.try_into()
99-
.map_err(|_| anyhow::anyhow!("Invalid CBOR encoded ULID type, invalid bytes size"))?,
100-
);
101-
Ok(ulid)
80+
WithVer { id: uuid::Uuid, ver: uuid::Uuid },
10281
}
10382

10483
fn encode_cbor_uuid(uuid: &uuid::Uuid) -> coset::cbor::Value {
@@ -123,24 +102,24 @@ fn decode_cbor_uuid(val: &coset::cbor::Value) -> anyhow::Result<uuid::Uuid> {
123102

124103
fn encode_cbor_document_ref(doc_ref: &DocumentRef) -> coset::cbor::Value {
125104
match doc_ref {
126-
DocumentRef::Latest { id } => encode_cbor_ulid(id),
105+
DocumentRef::Latest { id } => encode_cbor_uuid(id),
127106
DocumentRef::WithVer { id, ver } => {
128-
coset::cbor::Value::Array(vec![encode_cbor_ulid(id), encode_cbor_ulid(ver)])
107+
coset::cbor::Value::Array(vec![encode_cbor_uuid(id), encode_cbor_uuid(ver)])
129108
},
130109
}
131110
}
132111

133112
#[allow(clippy::indexing_slicing)]
134113
fn decode_cbor_document_ref(val: &coset::cbor::Value) -> anyhow::Result<DocumentRef> {
135-
if let Ok(id) = decode_cbor_ulid(val) {
114+
if let Ok(id) = decode_cbor_uuid(val) {
136115
Ok(DocumentRef::Latest { id })
137116
} else {
138117
let Some(array) = val.as_array() else {
139118
anyhow::bail!("Invalid CBOR encoded document `ref` type");
140119
};
141120
anyhow::ensure!(array.len() == 2, "Invalid CBOR encoded document `ref` type");
142-
let id = decode_cbor_ulid(&array[0])?;
143-
let ver = decode_cbor_ulid(&array[1])?;
121+
let id = decode_cbor_uuid(&array[0])?;
122+
let ver = decode_cbor_uuid(&array[1])?;
144123
Ok(DocumentRef::WithVer { id, ver })
145124
}
146125
}
@@ -243,11 +222,11 @@ fn build_empty_cose_doc(doc_bytes: Vec<u8>, meta: &Metadata) -> coset::CoseSign
243222
));
244223
protected_header.rest.push((
245224
coset::Label::Text("id".to_string()),
246-
encode_cbor_ulid(&meta.id),
225+
encode_cbor_uuid(&meta.id),
247226
));
248227
protected_header.rest.push((
249228
coset::Label::Text("ver".to_string()),
250-
encode_cbor_ulid(&meta.ver),
229+
encode_cbor_uuid(&meta.ver),
251230
));
252231
if let Some(r#ref) = &meta.r#ref {
253232
protected_header.rest.push((
@@ -388,7 +367,7 @@ fn validate_cose_protected_header(cose: &coset::CoseSign) -> anyhow::Result<()>
388367
else {
389368
anyhow::bail!("Invalid COSE protected header, missing `id` field");
390369
};
391-
decode_cbor_ulid(value)
370+
decode_cbor_uuid(value)
392371
.map_err(|e| anyhow::anyhow!("Invalid COSE protected header `id` field, err: {e}"))?;
393372

394373
let Some((_, value)) = cose
@@ -400,7 +379,7 @@ fn validate_cose_protected_header(cose: &coset::CoseSign) -> anyhow::Result<()>
400379
else {
401380
anyhow::bail!("Invalid COSE protected header, missing `ver` field");
402381
};
403-
decode_cbor_ulid(value)
382+
decode_cbor_uuid(value)
404383
.map_err(|e| anyhow::anyhow!("Invalid COSE protected header `ver` field, err: {e}"))?;
405384

406385
if let Some((_, value)) = cose

0 commit comments

Comments
 (0)