Skip to content

Commit

Permalink
delegation/envelope: small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Sep 24, 2024
1 parent b146710 commit 371bf3b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
16 changes: 3 additions & 13 deletions delegation/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ucan-wg/go-ucan/capability/command"
"github.com/ucan-wg/go-ucan/capability/policy"
"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/internal/envelope"
"github.com/ucan-wg/go-ucan/pkg/meta"
)

Expand Down Expand Up @@ -54,6 +53,7 @@ func New(privKey crypto.PrivKey, aud did.DID, cmd command.Command, pol policy.Po
policy: pol,
meta: meta.NewMeta(),
nonce: nil,
cid: cid.Undef,
}

for _, opt := range opts {
Expand All @@ -73,18 +73,6 @@ func New(privKey crypto.PrivKey, aud did.DID, cmd command.Command, pol policy.Po
return nil, err
}

cbor, err := tkn.ToDagCbor(privKey)
if err != nil {
return nil, err
}

id, err := envelope.CIDFromBytes(cbor)
if err != nil {
return nil, err
}

tkn.cid = id

return tkn, nil
}

Expand Down Expand Up @@ -150,6 +138,7 @@ func (t *Token) Expiration() *time.Time {

// CID returns the content identifier of the Token model when enclosed
// in an Envelope and encoded to DAG-CBOR.
// Returns cid.Undef if the token has not been serialized or deserialized yet.
func (t *Token) CID() cid.Cid {
return t.cid
}
Expand Down Expand Up @@ -299,6 +288,7 @@ func tokenFromModel(m tokenPayloadModel) (*Token, error) {
}

// generateNonce creates a 12-byte random nonce.
// TODO: some crypto scheme require more, is that our case?
func generateNonce() ([]byte, error) {
res := make([]byte, 12)
_, err := rand.Read(res)
Expand Down
13 changes: 7 additions & 6 deletions internal/envelope/cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func CIDToBase58BTC(id cid.Cid) string {
}

// CID returns the UCAN content identifier a Tokener.
// TODO: remove?
func CID(privKey crypto.PrivKey, token Tokener) (cid.Cid, error) {
data, err := ToDagCbor(privKey, token)
if err != nil {
Expand Down Expand Up @@ -53,11 +54,11 @@ type CIDReader struct {
// NewCIDReader initializes a hash.Hash to calculate the CID's hash and
// and returns a wrapped io.Reader.
func NewCIDReader(r io.Reader) *CIDReader {
hash := sha256.New()
hash.Reset()
h := sha256.New()
h.Reset()

return &CIDReader{
hash: hash,
hash: h,
r: r,
}
}
Expand Down Expand Up @@ -95,11 +96,11 @@ type CIDWriter struct {
}

func NewCIDWriter(w io.Writer) *CIDWriter {
hash := sha256.New()
hash.Reset()
h := sha256.New()
h.Reset()

return &CIDWriter{
hash: hash,
hash: h,
w: w,
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/envelope/cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ucan-wg/go-ucan/internal/envelope"
"gotest.tools/v3/golden"

"github.com/ucan-wg/go-ucan/internal/envelope"
)

func TestCid(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions internal/envelope/ipld.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ipld/go-ipld-prime/node/bindnode"
"github.com/ipld/go-ipld-prime/schema"
"github.com/libp2p/go-libp2p/core/crypto"

"github.com/ucan-wg/go-ucan/did"
"github.com/ucan-wg/go-ucan/internal/varsig"
)
Expand Down Expand Up @@ -164,19 +165,17 @@ func fromIPLD[T Tokener](node datamodel.Node) (T, error) {
return undef, err
}

// This needs to be done before converting this node to it's schema
// This needs to be done before converting this node to its schema
// representation (afterwards, the field might be renamed os it's safer
// to use the wire name).
issuerNode, err := tokenPayloadNode.LookupByString("iss")
if err != nil {
return undef, err
}
// ^^^

// Replaces the datamodel.Node in tokenPayloadNode with a
// schema.TypedNode so that we can cast it to a *token.Token after
// unwrapping it.
// vvv
nb := undef.Prototype().Representation().NewBuilder()

err = nb.AssignNode(tokenPayloadNode)
Expand All @@ -185,7 +184,6 @@ func fromIPLD[T Tokener](node datamodel.Node) (T, error) {
}

tokenPayloadNode = nb.Build()
// ^^^

tokenPayload := bindnode.Unwrap(tokenPayloadNode)
if tokenPayload == nil {
Expand All @@ -199,7 +197,6 @@ func fromIPLD[T Tokener](node datamodel.Node) (T, error) {

// Check that the issuer's DID contains a public key with a type that
// matches the VarsigHeader and then verify the SigPayload.
// vvv
issuer, err := issuerNode.AsString()
if err != nil {
return undef, err
Expand Down Expand Up @@ -238,7 +235,6 @@ func fromIPLD[T Tokener](node datamodel.Node) (T, error) {
if err != nil || !ok {
return undef, errors.New("failed to verify the token's signature")
}
// ^^^

return tkn, nil
}
Expand Down

0 comments on commit 371bf3b

Please sign in to comment.