Skip to content

Commit

Permalink
Fix regression where multicodec werenæt properly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 3, 2024
1 parent 8a80376 commit 55c93d9
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 35 deletions.
4 changes: 2 additions & 2 deletions did/doc/assertion_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"crypto/ed25519"

"github.com/bahner/go-ma/key"
"github.com/bahner/go-ma/multi"
"github.com/multiformats/go-multicodec"
)

func (d *Document) AssertionMethodPublicKey() (ed25519.PublicKey, error) {
Expand All @@ -20,7 +20,7 @@ func (d *Document) AssertionMethodPublicKey() (ed25519.PublicKey, error) {
return nil, ErrPublicKeyMultibaseInvalid
}

if codec != key.ASSERTION_METHOD_KEY_MULTICODEC_STRING {
if codec != multicodec.Ed25519Pub {
return nil, ErrMultiCodecInvalid
}

Expand Down
2 changes: 1 addition & 1 deletion did/doc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (
ErrPublicKeyMultibaseInvalid = errors.New("failed to decode public key multibase")
ErrPublicKeyMultibaseEmpty = errors.New("public key multibase is empty")
ErrPublicKeyMultibaseMismatch = errors.New("public key multibase mismatch")
ErrMultiCodecInvalid = fmt.Errorf("codec must be %s", key.ASSERTION_METHOD_KEY_MULTICODEC_STRING)
ErrMultiCodecInvalid = fmt.Errorf("codec must be %s", key.KEY_AGREEMENT_MULTICODEC.String())

ErrPublicKeyLengthInvalid = fmt.Errorf("public keysize must be %d", ed25519.PublicKeySize)

Expand Down
2 changes: 1 addition & 1 deletion did/doc/key_agreement.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (d *Document) KeyAgreementPublicKeyBytes() ([]byte, error) {
return nil, fmt.Errorf("doc/key_agreement_public_key: Error decoding publicKeyMultibase: %w", err)
}

if codec != key.KEY_AGREEMENT_MULTICODEC_STRING {
if codec != key.KEY_AGREEMENT_MULTICODEC {
return nil, ErrMultiCodecInvalid
}

Expand Down
4 changes: 2 additions & 2 deletions did/doc/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package doc
import (
"github.com/bahner/go-ma/multi"
cbor "github.com/fxamacker/cbor/v2"
"github.com/multiformats/go-multicodec"
"lukechampine.com/blake3"
)

Expand Down Expand Up @@ -35,8 +36,7 @@ func (d *Document) PayloadHash() ([]byte, error) {

// Hash the payload
hashed := blake3.Sum256(p)
multicodecHashed, err := multi.MulticodecEncode(hashed[:])
// multicodecHashed, err := multi.MulticodecEncode(ma.HASH_ALGORITHM_MULTICODEC_STRING, hashed[:])
multicodecHashed, err := multi.MulticodecEncode(multicodec.Blake3, hashed[:])
if err != nil {
return nil, ErrPayloadMultiencode
}
Expand Down
7 changes: 4 additions & 3 deletions key/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
)

const (
KEY_AGREEMENT_MULTICODEC_STRING = "x25519-pub"
KEY_AGREEMENT_KEY_TYPE = "MultiKey"
// KEY_AGREEMENT_MULTICODEC_STRING = "x25519-pub"
KEY_AGREEMENT_MULTICODEC = mc.X25519Pub
KEY_AGREEMENT_KEY_TYPE = "MultiKey"
)

type EncryptionKey struct {
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewEncryptionKey(identifier string) (EncryptionKey, error) {
curve25519.ScalarBaseMult(&pubKey, &privKey)

// Encode the public key to multibase
publicKeyMultibase, err := multi.PublicKeyMultibaseEncode(pubKey[:], KEY_AGREEMENT_MULTICODEC_STRING)
publicKeyMultibase, err := multi.PublicKeyMultibaseEncode(KEY_AGREEMENT_MULTICODEC, pubKey[:])
if err != nil {
return EncryptionKey{}, fmt.Errorf("NewEncryptionKey: %w", err)
}
Expand Down
8 changes: 7 additions & 1 deletion key/set/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/bahner/go-ma/multi"
cbor "github.com/fxamacker/cbor/v2"
log "github.com/sirupsen/logrus"
)

func (k Keyset) MarshalToCBOR() ([]byte, error) {
Expand All @@ -17,6 +18,8 @@ func UnmarshalFromCBOR(data []byte) (Keyset, error) {
return Keyset{}, fmt.Errorf("KeysetUnmarshalFromCBOR: %w", err)
}

log.Debugf("Unmarshaled keyset: %v", k)

return k, nil
}

Expand All @@ -37,5 +40,8 @@ func Unpack(data string) (Keyset, error) {
return Keyset{}, fmt.Errorf("KeysetUnpack: %w", err)
}

return UnmarshalFromCBOR(decoded)
keyset, err := UnmarshalFromCBOR(decoded)
log.Debugf("Unpacked keyset: %v", keyset)

return keyset, err
}
8 changes: 4 additions & 4 deletions key/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

const (
ASSERTION_METHOD_KEY_MULTICODEC_STRING = "ed25519-pub"
ASSERTION_METHOD_KEY_TYPE = "MultiKey"
ASSERTION_METHOD_KEY_TYPE = "MultiKey"
ASSERTION_METHOD_MULTICODEC = mc.Ed25519Pub
)

type SigningKey struct {
Expand Down Expand Up @@ -53,7 +53,7 @@ func NewSigningKey(identifier string) (SigningKey, error) {
return SigningKey{}, fmt.Errorf("NewSigningKey: %w", err)
}

publicKeyMultibase, err := multi.PublicKeyMultibaseEncode(publicKey, ASSERTION_METHOD_KEY_MULTICODEC_STRING)
publicKeyMultibase, err := multi.PublicKeyMultibaseEncode(ASSERTION_METHOD_MULTICODEC, publicKey)
if err != nil {
return SigningKey{}, fmt.Errorf("NewSigningKey: %w", err)
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func (s SigningKey) Verify() error {
return fmt.Errorf("SigningKeyVerify: %w", err)
}

if key[0] != byte(mc.Ed25519Pub) {
if key[0] != byte(ASSERTION_METHOD_MULTICODEC) {
return ErrInvalidMulticodec
}

Expand Down
9 changes: 9 additions & 0 deletions multi/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package multi

import "fmt"

var (
ErrNoInput = fmt.Errorf("error decoding: insufficient data")
ErrInvalidSize = fmt.Errorf("error decoding: invalid varint size")
ErrUnknownCodec = fmt.Errorf("error obtaining codec name: unknown codec")
)
28 changes: 15 additions & 13 deletions multi/multicodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,38 @@ import (
"github.com/multiformats/go-varint"
)

func MulticodecEncode(payload []byte) ([]byte, error) {
func MulticodecEncode(codec multicodec.Code, payload []byte) ([]byte, error) {

codec := uint64(multicodec.Blake3)
c := uint64(codec)

codecBytes := varint.ToUvarint(codec)
codecBytes := varint.ToUvarint(c)
encoded := append(codecBytes, payload...)
return encoded, nil
}

// Returns the codec name, payload and error of a multicodec encoded byte array
func MulticodecDecode(encoded []byte) (string, []byte, error) {
// Returns the codec, payload and error of a multicodec encoded byte array
func MulticodecDecode(encoded []byte) (multicodec.Code, []byte, error) {

var codec multicodec.Code

if len(encoded) < 1 {
return "", nil, fmt.Errorf("error decoding: insufficient data")
return codec, nil, ErrNoInput
}

// log.Debugf("mutlticodecdecode: encoded: %x", encoded)

code, n, err := varint.FromUvarint(encoded)
if err != nil {
return "", nil, fmt.Errorf("error decoding varint: %w", err)
return codec, nil, fmt.Errorf("error decoding varint: %w", err)
}
if n < 1 || n >= len(encoded) {
return "", nil, fmt.Errorf("error decoding: invalid varint size")
return codec, nil, ErrInvalidSize
}

codecName := multicodec.Code(code).String()
if codecName == "" {
return "", nil, fmt.Errorf("error obtaining codec name: unknown codec")
codec = multicodec.Code(code)
if codec == 0 {
return codec, nil, ErrUnknownCodec
}

// log.Debugf("mutlticodecdecode: codecName: %s", codecName)
return codecName, encoded[n:], nil
return codec, encoded[n:], nil
}
22 changes: 14 additions & 8 deletions multi/public_key_multibase.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package multi

import "fmt"
import (
"fmt"

func PublicKeyMultibaseEncode(publicKey []byte, codecName string) (string, error) {
"github.com/multiformats/go-multicodec"
)

multicodecedKey, err := MulticodecEncode(publicKey)
func PublicKeyMultibaseEncode(codec multicodec.Code, publicKey []byte) (string, error) {

multicodecedKey, err := MulticodecEncode(codec, publicKey)
if err != nil {
return "", fmt.Errorf("key/codec: error multicodec encoding public key: %s", err)
}
Expand All @@ -18,18 +22,20 @@ func PublicKeyMultibaseEncode(publicKey []byte, codecName string) (string, error

}

func PublicKeyMultibaseDecode(publicKey string) (string, []byte, error) {
func PublicKeyMultibaseDecode(publicKey string) (multicodec.Code, []byte, error) {

var codec multicodec.Code

decodedPublicKeyMultibase, err := MultibaseDecode(publicKey)
if err != nil {
return "", nil, fmt.Errorf("key/codec: error multibase decoding public key: %s", err)
return codec, nil, err
}

codecName, decodedPublicKey, err := MulticodecDecode(decodedPublicKeyMultibase)
codec, decodedPublicKey, err := MulticodecDecode(decodedPublicKeyMultibase)
if err != nil {
return "", nil, fmt.Errorf("key/codec: error multicodec decoding public key: %s", err)
return codec, nil, err
}

return codecName, decodedPublicKey, nil
return codec, decodedPublicKey, nil

}

0 comments on commit 55c93d9

Please sign in to comment.