Skip to content

Commit

Permalink
Merge pull request #380 from hypersign-protocol/bjj-jsonld-context-su…
Browse files Browse the repository at this point in the history
…pport

feat: Add JSON-LD support for BJJSignature2021
  • Loading branch information
arnabghose997 authored Nov 25, 2023
2 parents 5e44dc2 + 81d8533 commit a6d2c8c
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 543 deletions.
185 changes: 11 additions & 174 deletions cmd/hid-noded/cmd/debug_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
ethercrypto "github.com/ethereum/go-ethereum/crypto"
bbs "github.com/hyperledger/aries-framework-go/component/kmscrypto/crypto/primitive/bbs12381g2pub"
hidnodecli "github.com/hypersign-protocol/hid-node/x/ssi/client/cli"
ldcontext "github.com/hypersign-protocol/hid-node/x/ssi/ld-context"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/multiformats/go-multibase"
Expand Down Expand Up @@ -333,75 +331,9 @@ func signDidDocCmd() *cobra.Command {
}

// Sign DID Document
var signature string
switch didDocProof.Type {
case types.Ed25519Signature2020:
var didDocBytes []byte
if len(didDoc.Context) > 0 {
didDocBytes, err = ldcontext.Ed25519Signature2020Normalize(&didDoc, &didDocProof)
if err != nil {
return err
}
} else {
didDocBytes = didDoc.GetSignBytes()
}

signature, err = hidnodecli.GetEd25519Signature2020(argPrivateKey, didDocBytes[:])
if err != nil {
return err
}
case types.EcdsaSecp256k1Signature2019:
var didDocBytes []byte
if len(didDoc.Context) > 0 {
didDocBytes, err = ldcontext.EcdsaSecp256k1Signature2019Normalize(&didDoc, &didDocProof)
if err != nil {
return err
}
} else {
didDocBytes = didDoc.GetSignBytes()
}

signature, err = hidnodecli.GetEcdsaSecp256k1Signature2019(argPrivateKey, didDocBytes[:])
if err != nil {
return err
}
case types.EcdsaSecp256k1RecoverySignature2020:
var didDocBytes []byte
if len(didDoc.Context) > 0 {
didDocBytes, err = ldcontext.EcdsaSecp256k1RecoverySignature2020Normalize(&didDoc, &didDocProof)
if err != nil {
return err
}
} else {
didDocBytes = didDoc.GetSignBytes()
}

signature, err = hidnodecli.GetEcdsaSecp256k1RecoverySignature2020(argPrivateKey, didDocBytes[:])
if err != nil {
return err
}
case types.BbsBlsSignature2020:
var didDocBytes []byte
if len(didDoc.Context) > 0 {
didDocBytes, err = ldcontext.BbsBlsSignature2020Normalize(&didDoc, &didDocProof)
if err != nil {
return err
}
} else {
didDocBytes = didDoc.GetSignBytes()
}

signature, err = hidnodecli.GetBbsBlsSignature2020(argPrivateKey, didDocBytes[:])
if err != nil {
return err
}
case types.BabyJubJubSignature2023:
signature, err = hidnodecli.GetBabyJubJubSignature2023(argPrivateKey, didDoc.GetSignBytes())
if err != nil {
return err
}
default:
panic("recieved unsupported signing-algo. Supported algorithms are: [Ed25519Signature2020, EcdsaSecp256k1Signature2019, EcdsaSecp256k1RecoverySignature2020, BbsBlsSignature2020, BabyJubJubSignature2023]")
signature, err := getDocumentSignature(&didDoc, &didDocProof, argPrivateKey)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), signature)
Expand All @@ -427,8 +359,8 @@ func signSchemaDocCmd() *cobra.Command {
}

// Unmarshal Schema Document
var schemaDoc types.CredentialSchemaDocument
err = clientCtx.Codec.UnmarshalJSON([]byte(argSchemaDoc), &schemaDoc)
var credSchemaDoc types.CredentialSchemaDocument
err = clientCtx.Codec.UnmarshalJSON([]byte(argSchemaDoc), &credSchemaDoc)
if err != nil {
return err
}
Expand All @@ -441,58 +373,9 @@ func signSchemaDocCmd() *cobra.Command {
}

// Sign Schema Document
var signature string
switch credSchemaDocProof.Type {
case types.Ed25519Signature2020:
credSchemaDocBytes, err := ldcontext.Ed25519Signature2020Normalize(&schemaDoc, &credSchemaDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEd25519Signature2020(argPrivateKey, credSchemaDocBytes)
if err != nil {
return err
}
case types.EcdsaSecp256k1Signature2019:
credSchemaDocBytes, err := ldcontext.EcdsaSecp256k1Signature2019Normalize(&schemaDoc, &credSchemaDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEcdsaSecp256k1Signature2019(argPrivateKey, credSchemaDocBytes)
if err != nil {
return err
}
case types.EcdsaSecp256k1RecoverySignature2020:
credSchemaDocBytes, err := ldcontext.EcdsaSecp256k1RecoverySignature2020Normalize(&schemaDoc, &credSchemaDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEcdsaSecp256k1RecoverySignature2020(argPrivateKey, credSchemaDocBytes)
if err != nil {
return err
}
case types.BbsBlsSignature2020:
credSchemaDocBytes, err := ldcontext.BbsBlsSignature2020Normalize(&schemaDoc, &credSchemaDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetBbsBlsSignature2020(argPrivateKey, credSchemaDocBytes)
if err != nil {
return err
}
case types.BabyJubJubSignature2023:
signature, err = hidnodecli.GetBabyJubJubSignature2023(argPrivateKey, schemaDoc.GetSignBytes())
if err != nil {
return err
}
default:
panic(fmt.Sprintf(
"recieved unsupported signing-algo '%v'. Supported algorithms are: [Ed25519Signature2020, EcdsaSecp256k1Signature2019, EcdsaSecp256k1RecoverySignature2020, BbsBlsSignature2020, BabyJubJubSignature2023]",
credSchemaDocProof.Type,
))
signature, err := getDocumentSignature(&credSchemaDoc, &credSchemaDocProof, argPrivateKey)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), signature)
Expand Down Expand Up @@ -532,55 +415,9 @@ func signCredStatusDocCmd() *cobra.Command {
}

// Sign Credential Status Document
var signature string
switch credStatusDocProof.Type {
case types.Ed25519Signature2020:
credStatusDocBytes, err := ldcontext.Ed25519Signature2020Normalize(&credStatusDoc, &credStatusDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEd25519Signature2020(argPrivateKey, credStatusDocBytes)
if err != nil {
return err
}
case types.EcdsaSecp256k1Signature2019:
credStatusDocBytes, err := ldcontext.EcdsaSecp256k1Signature2019Normalize(&credStatusDoc, &credStatusDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEcdsaSecp256k1Signature2019(argPrivateKey, credStatusDocBytes)
if err != nil {
return err
}
case types.EcdsaSecp256k1RecoverySignature2020:
credStatusDocBytes, err := ldcontext.EcdsaSecp256k1RecoverySignature2020Normalize(&credStatusDoc, &credStatusDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetEcdsaSecp256k1RecoverySignature2020(argPrivateKey, credStatusDocBytes)
if err != nil {
return err
}
case types.BbsBlsSignature2020:
credStatusDocBytes, err := ldcontext.BbsBlsSignature2020Normalize(&credStatusDoc, &credStatusDocProof)
if err != nil {
return err
}

signature, err = hidnodecli.GetBbsBlsSignature2020(argPrivateKey, credStatusDocBytes)
if err != nil {
return err
}
case types.BabyJubJubSignature2023:
signature, err = hidnodecli.GetBabyJubJubSignature2023(argPrivateKey, credStatusDoc.GetSignBytes())
if err != nil {
return err
}
default:
panic("recieved unsupported signing-algo. Supported algorithms are: [Ed25519Signature2020, EcdsaSecp256k1Signature2019, EcdsaSecp256k1RecoverySignature2020, BbsBlsSignature2020, BabyJubJubSignature2023]")
signature, err := getDocumentSignature(&credStatusDoc, &credStatusDocProof, argPrivateKey)
if err != nil {
return err
}

_, err = fmt.Fprintln(cmd.OutOrStdout(), signature)
Expand Down
70 changes: 70 additions & 0 deletions cmd/hid-noded/cmd/debug_extensions_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"golang.org/x/crypto/ripemd160" //nolint: staticcheck

bech32 "github.com/cosmos/cosmos-sdk/types/bech32"
hidnodecli "github.com/hypersign-protocol/hid-node/x/ssi/client/cli"
ldcontext "github.com/hypersign-protocol/hid-node/x/ssi/ld-context"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)

// publicKeyToBech32Address converts publicKey byteArray to Bech32 encoded blockchain address
Expand All @@ -29,3 +32,70 @@ func publicKeyToBech32Address(addressPrefix string, pubKeyBytes []byte) string {
}
return address
}

// getDocumentSignature returns signature for the input SSI Document
func getDocumentSignature(doc types.SsiMsg, docProof *types.DocumentProof, privateKey string) (string, error) {
var signature string

switch docProof.Type {
case types.Ed25519Signature2020:
var docBytes []byte
docBytes, err := ldcontext.Ed25519Signature2020Normalize(doc, docProof)
if err != nil {
return "", err
}

signature, err = hidnodecli.GetEd25519Signature2020(privateKey, docBytes)
if err != nil {
return "", err
}
case types.EcdsaSecp256k1Signature2019:
var docBytes []byte
docBytes, err := ldcontext.EcdsaSecp256k1Signature2019Normalize(doc, docProof)
if err != nil {
return "", err
}

signature, err = hidnodecli.GetEcdsaSecp256k1Signature2019(privateKey, docBytes)
if err != nil {
return "", err
}
case types.EcdsaSecp256k1RecoverySignature2020:
var docBytes []byte
docBytes, err := ldcontext.EcdsaSecp256k1RecoverySignature2020Normalize(doc, docProof)
if err != nil {
return "", err
}

signature, err = hidnodecli.GetEcdsaSecp256k1RecoverySignature2020(privateKey, docBytes)
if err != nil {
return "", err
}
case types.BbsBlsSignature2020:
var docBytes []byte
docBytes, err := ldcontext.BbsBlsSignature2020Normalize(doc, docProof)
if err != nil {
return "", err
}

signature, err = hidnodecli.GetBbsBlsSignature2020(privateKey, docBytes)
if err != nil {
return "", err
}
case types.BJJSignature2021:
var docBytes []byte
docBytes, err := ldcontext.BJJSignature2021Normalize(doc)
if err != nil {
return "", err
}

signature, err = hidnodecli.GetBJJSignature2021(privateKey, docBytes)
if err != nil {
return "", err
}
default:
panic("recieved unsupported signing-algo. Supported algorithms are: [Ed25519Signature2020, EcdsaSecp256k1Signature2019, EcdsaSecp256k1RecoverySignature2020, BbsBlsSignature2020, BJJSignature2021]")
}

return signature, nil
}
Loading

0 comments on commit a6d2c8c

Please sign in to comment.