From 5bb6d2dced285b3519f012b840fef8192993193d Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Fri, 8 Mar 2024 15:51:29 +0100 Subject: [PATCH] fix(sign): use right signature algorithm --- client/credential/sign.go | 40 +++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/client/credential/sign.go b/client/credential/sign.go index 8b5fb61e..84fc1fc4 100644 --- a/client/credential/sign.go +++ b/client/credential/sign.go @@ -9,9 +9,14 @@ import ( "strings" "time" + "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2020" + + "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ecdsasecp256k1signature2019" + "github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld" "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite" - "github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite/ed25519signature2018" "github.com/hyperledger/aries-framework-go/pkg/doc/verifiable" "github.com/piprate/json-gold/ld" "github.com/spf13/cobra" @@ -249,13 +254,32 @@ func signVerifiableCredential( return err } - return vc.AddLinkedDataProof(&verifiable.LinkedDataProofContext{ - Created: &date, - SignatureType: "Ed25519Signature2018", - Suite: ed25519signature2018.New(suite.WithSigner(signer)), - SignatureRepresentation: verifiable.SignatureProofValue, - VerificationMethod: didKeyID, - }, jsonld.WithDocumentLoader(documentLoader)) + pubKey, err := signer.PubKey() + if err != nil { + return err + } + + switch pubKey.(type) { + case *ed25519.PubKey: + return vc.AddLinkedDataProof(&verifiable.LinkedDataProofContext{ + Created: &date, + SignatureType: "Ed25519Signature2020", + Suite: ed25519signature2020.New(suite.WithSigner(signer)), + SignatureRepresentation: verifiable.SignatureProofValue, + VerificationMethod: didKeyID, + }, jsonld.WithDocumentLoader(documentLoader)) + case *secp256k1.PubKey: + return vc.AddLinkedDataProof(&verifiable.LinkedDataProofContext{ + Created: &date, + SignatureType: "EcdsaSecp256k1Signature2019", + Suite: ecdsasecp256k1signature2019.New(suite.WithSigner(signer)), + SignatureRepresentation: verifiable.SignatureJWS, + VerificationMethod: didKeyID, + }, jsonld.WithDocumentLoader(documentLoader)) + default: + return fmt.Errorf("invalid pubkey type: %s; expected oneof %+q", + pubKey.Type(), []string{(&ed25519.PubKey{}).Type(), (&secp256k1.PubKey{}).Type()}) + } } func parseStringAsDate(cmd *cobra.Command, flag string) (time.Time, error) {