Skip to content

Commit

Permalink
Remove complexity around blake3 hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 2, 2024
1 parent 201208d commit 8a80376
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 31 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/internal"
"github.com/bahner/go-ma/key"
"github.com/bahner/go-ma/multi"
)

func (d *Document) AssertionMethodPublicKey() (ed25519.PublicKey, error) {
Expand All @@ -15,7 +15,7 @@ func (d *Document) AssertionMethodPublicKey() (ed25519.PublicKey, error) {
if err != nil {
return nil, ErrVerificationMethoddUnkownID
}
codec, pubKeyBytes, err := internal.DecodePublicKeyMultibase(vm.PublicKeyMultibase)
codec, pubKeyBytes, err := multi.PublicKeyMultibaseDecode(vm.PublicKeyMultibase)
if err != nil {
return nil, ErrPublicKeyMultibaseInvalid
}
Expand Down
4 changes: 2 additions & 2 deletions did/doc/key_agreement.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package doc
import (
"fmt"

"github.com/bahner/go-ma/internal"
"github.com/bahner/go-ma/key"
"github.com/bahner/go-ma/multi"
"golang.org/x/crypto/curve25519"
)

Expand All @@ -15,7 +15,7 @@ func (d *Document) KeyAgreementPublicKeyBytes() ([]byte, error) {
return nil, fmt.Errorf("doc/key_agreement_public_key: Error getting verification method by ID: %w", err)
}
// Decode the multibase-encoded public key
codec, pubKeyBytes, err := internal.DecodePublicKeyMultibase(vm.PublicKeyMultibase)
codec, pubKeyBytes, err := multi.PublicKeyMultibaseDecode(vm.PublicKeyMultibase)
if err != nil {
return nil, fmt.Errorf("doc/key_agreement_public_key: Error decoding publicKeyMultibase: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions did/doc/payload.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package doc

import (
"github.com/bahner/go-ma"
"github.com/bahner/go-ma/internal"
"github.com/bahner/go-ma/multi"
cbor "github.com/fxamacker/cbor/v2"
"lukechampine.com/blake3"
)
Expand Down Expand Up @@ -36,7 +35,8 @@ func (d *Document) PayloadHash() ([]byte, error) {

// Hash the payload
hashed := blake3.Sum256(p)
multicodecHashed, err := internal.MulticodecEncode(ma.HASH_ALGORITHM_MULTICODEC_STRING, hashed[:])
multicodecHashed, err := multi.MulticodecEncode(hashed[:])
// multicodecHashed, err := multi.MulticodecEncode(ma.HASH_ALGORITHM_MULTICODEC_STRING, 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 @@ -7,6 +7,7 @@ import (
"github.com/bahner/go-ma"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/internal"
"github.com/bahner/go-ma/multi"
nanoid "github.com/matoous/go-nanoid/v2"
mc "github.com/multiformats/go-multicodec"
"golang.org/x/crypto/curve25519"
Expand Down Expand Up @@ -53,7 +54,7 @@ func NewEncryptionKey(identifier string) (EncryptionKey, error) {
curve25519.ScalarBaseMult(&pubKey, &privKey)

// Encode the public key to multibase
publicKeyMultibase, err := internal.EncodePublicKeyMultibase(pubKey[:], KEY_AGREEMENT_MULTICODEC_STRING)
publicKeyMultibase, err := multi.PublicKeyMultibaseEncode(pubKey[:], KEY_AGREEMENT_MULTICODEC_STRING)
if err != nil {
return EncryptionKey{}, fmt.Errorf("NewEncryptionKey: %w", err)
}
Expand Down Expand Up @@ -94,15 +95,15 @@ func (k EncryptionKey) Verify() error {
return ErrNoPublicKeyMultibase
}

if !internal.IsValidMultibase(k.PublicKeyMultibase) {
if !multi.IsValidMultibase(k.PublicKeyMultibase) {
return ErrInvalidPublicKeyMultibase
}

if k.PublicKeyMultibase == "" {
return ErrNoPublicKeyMultibase
}

key, err := internal.MultibaseDecode(k.PublicKeyMultibase)
key, err := multi.MultibaseDecode(k.PublicKeyMultibase)
if err != nil {
return ErrInvalidPublicKeyMultibase
}
Expand Down
6 changes: 3 additions & 3 deletions key/set/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package set
import (
"fmt"

"github.com/bahner/go-ma/internal"
"github.com/bahner/go-ma/multi"
cbor "github.com/fxamacker/cbor/v2"
)

Expand All @@ -27,12 +27,12 @@ func (k Keyset) Pack() (string, error) {
return "", fmt.Errorf("KeysetPack: %w", err)
}

return internal.MultibaseEncode(data)
return multi.MultibaseEncode(data)
}

func Unpack(data string) (Keyset, error) {

decoded, err := internal.MultibaseDecode(data)
decoded, err := multi.MultibaseDecode(data)
if err != nil {
return Keyset{}, fmt.Errorf("KeysetUnpack: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions key/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/bahner/go-ma"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/internal"
"github.com/bahner/go-ma/multi"
nanoid "github.com/matoous/go-nanoid/v2"
mc "github.com/multiformats/go-multicodec"
)
Expand Down Expand Up @@ -52,7 +53,7 @@ func NewSigningKey(identifier string) (SigningKey, error) {
return SigningKey{}, fmt.Errorf("NewSigningKey: %w", err)
}

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

key, err := internal.MultibaseDecode(s.PublicKeyMultibase)
key, err := multi.MultibaseDecode(s.PublicKeyMultibase)
if err != nil {
return fmt.Errorf("SigningKeyVerify: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions ma.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ const (
RENDEZVOUS = "/" + NAME + "/" + VERSION

// BLAKE3 label for symmetric key generation.
HASH_ALGORITHM_MULTICODEC_STRING = "blake3"
BLAKE3_LABEL = NAME
BLAKE3_SUM_SIZE = 32 // 256 bits
BLAKE3_LABEL = NAME
BLAKE3_SUM_SIZE = 32 // 256 bits

// Message constants
MESSAGE_TYPE = "/ma/message/" + VERSION
Expand Down
2 changes: 1 addition & 1 deletion internal/multibase.go → multi/multibase.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package multi

import (
"github.com/multiformats/go-multibase"
Expand Down
11 changes: 3 additions & 8 deletions internal/multicodec.go → multi/multicodec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package multi

import (
"fmt"
Expand All @@ -7,13 +7,9 @@ import (
"github.com/multiformats/go-varint"
)

func MulticodecEncode(codecName string, payload []byte) ([]byte, error) {
func MulticodecEncode(payload []byte) ([]byte, error) {

var officialCodec multicodec.Code
if err := officialCodec.Set(codecName); err != nil {
return nil, fmt.Errorf("invalid codec name: %w", err)
}
codec := uint64(officialCodec)
codec := uint64(multicodec.Blake3)

codecBytes := varint.ToUvarint(codec)
encoded := append(codecBytes, payload...)
Expand All @@ -35,7 +31,6 @@ func MulticodecDecode(encoded []byte) (string, []byte, error) {
if n < 1 || n >= len(encoded) {
return "", nil, fmt.Errorf("error decoding: invalid varint size")
}
// log.Debugf("mutlticodecdecode: code %d", code)

codecName := multicodec.Code(code).String()
if codecName == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package internal
package multi

import "fmt"

func EncodePublicKeyMultibase(publicKey []byte, codecName string) (string, error) {
func PublicKeyMultibaseEncode(publicKey []byte, codecName string) (string, error) {

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

}

func DecodePublicKeyMultibase(publicKey string) (string, []byte, error) {
func PublicKeyMultibaseDecode(publicKey string) (string, []byte, error) {

decodedPublicKeyMultibase, err := MultibaseDecode(publicKey)
if err != nil {
Expand Down

0 comments on commit 8a80376

Please sign in to comment.