syndicate-nim/src/syndicate/sturdy.nim

35 lines
1.0 KiB
Nim
Raw Normal View History

2021-09-21 14:39:15 +00:00
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: Unlicense
import preserves
2021-09-07 10:01:42 +00:00
import ../syndicate/protocols/schemas/sturdy, ./private/hmacs
2021-09-21 14:39:15 +00:00
proc mint*(key: openarray[byte]; oid: Preserve): SturdyRef =
2021-09-07 10:01:42 +00:00
SturdyRef(oid: oid, sig: hmacSha256(key, encode(oid), key.len))
proc attenuate*(r: SturdyRef; caveats: Attenuation): SturdyRef =
result = SturdyRef(
oid: r.oid,
caveatChain: r.caveatChain,
sig: hmacSha256(r.sig, caveats.encode))
result.caveatChain.add caveats
proc validate*(key: openarray[byte]; r: SturdyRef): bool =
var sig = hmacSha256(key, r.oid.encode, key.len)
for a in r.caveatChain:
sig = hmacSha256(sig, a.encode)
r.sig == sig
when isMainModule:
import unittest
2021-09-21 14:39:15 +00:00
import preserves/parse
test "sturdy":
2021-09-07 10:01:42 +00:00
var
key: array[16, byte]
2021-09-21 14:39:15 +00:00
oid = "syndicate".toPreserve
2021-09-07 10:01:42 +00:00
sRef = mint(key, oid)
control = parsePreserves"""<ref "syndicate" [] #[pkgN9TBmEd3Q04grVG4Zdw]>"""
check(sRef.toPreserve == control)
let aRef = attenuate(sRef, newSeq[Caveat]())
check validate(key, aRef)