Skip to content

Commit

Permalink
Fix keyset marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 22, 2024
1 parent 9de585c commit 7bf3d91
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions key/set/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,58 @@ package set
import (
"fmt"

"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/key"
mf "github.com/bahner/go-ma/utils"
cbor "github.com/fxamacker/cbor/v2"
"github.com/libp2p/go-libp2p/core/crypto"
log "github.com/sirupsen/logrus"
)

func (k Keyset) MarshalToCBOR() ([]byte, error) {
return cbor.Marshal(k)
func (ks *Keyset) MarshalToCBOR() ([]byte, error) {
identityBytes, err := crypto.MarshalPrivateKey(ks.Identity)
if err != nil {
return nil, fmt.Errorf("marshal identity key: %w", err)
}
temp := struct {
Identity []byte
DID did.DID
EncryptionKey key.EncryptionKey
SigningKey key.SigningKey
}{
Identity: identityBytes,
DID: ks.DID,
EncryptionKey: ks.EncryptionKey,
SigningKey: ks.SigningKey,
}

return cbor.Marshal(temp)
}

func UnmarshalFromCBOR(data []byte) (Keyset, error) {
var k Keyset
err := cbor.Unmarshal(data, &k)
temp := struct {
Identity []byte
DID did.DID
EncryptionKey key.EncryptionKey
SigningKey key.SigningKey
}{}

err := cbor.Unmarshal(data, &temp)
if err != nil {
return Keyset{}, fmt.Errorf("KeysetUnmarshalFromCBOR: %w", err)
return Keyset{}, fmt.Errorf("unmarshal keyset: %w", err)
}

log.Debugf("Unmarshaled keyset: %v", k)
identity, err := crypto.UnmarshalPrivateKey(temp.Identity)
if err != nil {
return Keyset{}, fmt.Errorf("unmarshal identity key: %w", err)
}

return k, nil
return Keyset{
Identity: identity,
DID: temp.DID,
EncryptionKey: temp.EncryptionKey,
SigningKey: temp.SigningKey,
}, nil
}

func (k Keyset) Pack() (string, error) {
Expand Down

0 comments on commit 7bf3d91

Please sign in to comment.