Skip to content

Commit

Permalink
replacing python3 tests with golang tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arnabghose997 committed Mar 13, 2024
1 parent 583b9fd commit d8f7e84
Show file tree
Hide file tree
Showing 22 changed files with 2,030 additions and 0 deletions.
57 changes: 57 additions & 0 deletions x/ssi/tests/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tests

import (
"context"
"strings"

testcrypto "github.com/hypersign-protocol/hid-node/x/ssi/tests/crypto"
testconstants "github.com/hypersign-protocol/hid-node/x/ssi/tests/constants"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)


func GenerateSchemaDocumentRPCElements(keyPair testcrypto.IKeyPair, authorId string, verficationMethodId string) *types.MsgRegisterCredentialSchema {
var schemaId string = "sch:" + testconstants.DidMethod + ":" + testconstants.ChainNamespace + ":" + strings.Split(authorId, ":")[3] + ":1.0"
var schemaDocument *types.CredentialSchemaDocument = &types.CredentialSchemaDocument{
Type: "https://w3c-ccg.github.io/vc-json-schemas/schema/1.0/schema.json",
ModelVersion: "v1.0",
Name: "HS Credential",
Author: authorId,
Id: schemaId,
Authored: "2022-04-10T04:07:12Z",
Schema: &types.CredentialSchemaProperty{
Schema: "https://json-schema.org/draft-07/schema#",
Description: "test",
Type: "Object",
Properties: "{myString:{type:string}}",
Required: []string{"myString"},
AdditionalProperties: false,
},
}

var schemaProof *types.DocumentProof = &types.DocumentProof{
Created: "2022-04-10T04:07:12Z",
VerificationMethod: verficationMethodId,
ProofPurpose: "assertionMethod",
}

var schemaDocumentSignature string = testcrypto.SignGeneric(keyPair, schemaDocument, schemaProof)

schemaProof.ProofValue = schemaDocumentSignature

return &types.MsgRegisterCredentialSchema{
CredentialSchemaDocument: schemaDocument,
CredentialSchemaProof: schemaProof,
TxAuthor: testconstants.Creator,
}
}


func UpdateDidTx(msgServer types.MsgServer, ctx context.Context, rpcElements *types.MsgUpdateDID, versionId string) error {
_, err := msgServer.UpdateDID(ctx, rpcElements)
if err != nil {
return err
}
return nil
}

3 changes: 3 additions & 0 deletions x/ssi/tests/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tests

const errExpectedToFail = "the tx was expected to fail, but it didn't"
5 changes: 5 additions & 0 deletions x/ssi/tests/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const Creator = "hid1kxqk5ejca8nfpw8pg47484rppv359xh7qcasy4"
const DidMethod = "hid"
const ChainNamespace = "devnet"
38 changes: 38 additions & 0 deletions x/ssi/tests/crypto/babyJubJub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package crypto

import (
"encoding/hex"

"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/multiformats/go-multibase"
)

func GenerateBabyJubJubKeyPair() *BabyJubJubKeyPair {
// Generate Key Pair
babyJubjubPrivKey := babyjub.NewRandPrivKey()
babyJubjubPubKey := babyJubjubPrivKey.Public()

// Prepare Priv key
var privKeyBytes [32]byte = babyJubjubPrivKey
var privKeyHex string = hex.EncodeToString(privKeyBytes[:])

// Prepare Public Key
var pubKeyHex string = babyJubjubPubKey.Compress().String()

// Prepare Multibase Public Key
pubKeyBytes, err := hex.DecodeString(pubKeyHex)
if err != nil {
panic(err)
}
pubKeyMultibase, err := multibase.Encode(multibase.Base58BTC, pubKeyBytes)
if err != nil {
panic(err)
}

return &BabyJubJubKeyPair{
Type: types.BabyJubJubKey2021,
PublicKey: pubKeyMultibase,
PrivateKey: privKeyHex,
}
}
40 changes: 40 additions & 0 deletions x/ssi/tests/crypto/bbs_bls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package crypto

import (
"crypto/sha256"
"encoding/base64"

bbs "github.com/hyperledger/aries-framework-go/component/kmscrypto/crypto/primitive/bbs12381g2pub"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/multiformats/go-multibase"
)

func GenerateBbsBlsKeyPair() *BbsBlsKeyPair {
pubKey, privKey, err := bbs.GenerateKeyPair(sha256.New, nil)
if err != nil {
panic(err)
}

// Convert Public Key Object to Multibase
pubKeyBytes, err := pubKey.Marshal()
if err != nil {
panic(err)
}

publicKeyMultibase, err := multibase.Encode(multibase.Base58BTC, pubKeyBytes)
if err != nil {
panic(err)
}

// Convert Private Object to Bytes
privKeyBytes, err := privKey.Marshal()
if err != nil {
panic(err)
}

return &BbsBlsKeyPair{
Type: types.Bls12381G2Key2020,
PublicKey: publicKeyMultibase,
PrivateKey: base64.StdEncoding.EncodeToString(privKeyBytes),
}
}
33 changes: 33 additions & 0 deletions x/ssi/tests/crypto/ed25519.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package crypto

import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"

"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/multiformats/go-multibase"
)

func GenerateEd25519KeyPair() *Ed25519KeyPair {
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}

var publicKeyWithHeader []byte
publicKeyWithHeader = append(publicKeyWithHeader, append([]byte{0xed, 0x01}, publicKey...)...)

publicKeyMultibase, err := multibase.Encode(multibase.Base58BTC, publicKeyWithHeader)
if err != nil {
panic("Error while encoding multibase string")
}

privKeyBase64String := base64.StdEncoding.EncodeToString(privateKey)

return &Ed25519KeyPair{
Type: types.Ed25519VerificationKey2020,
PublicKey: publicKeyMultibase,
PrivateKey: privKeyBase64String,
}
}
48 changes: 48 additions & 0 deletions x/ssi/tests/crypto/secp256k1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package crypto

import (
"encoding/base64"
"encoding/hex"

"github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
"github.com/multiformats/go-multibase"

ethercrypto "github.com/ethereum/go-ethereum/crypto"
)

func GenerateSecp256k1KeyPair() *Secp256k1Pair {
privateKey := secp256k1.GenPrivKey()

publicKey := privateKey.PubKey().Bytes()

publicKeyMultibase, err := multibase.Encode(multibase.Base58BTC, publicKey)
if err != nil {
panic("Error while encoding multibase string")
}
return &Secp256k1Pair{
Type: types.EcdsaSecp256k1VerificationKey2019,
PublicKey: publicKeyMultibase,
PrivateKey: base64.StdEncoding.EncodeToString(privateKey),
}
}

func GenerateSecp256k1RecoveryKeyPair() *Secp256k1RecoveryPair {
privateKeyObj := secp256k1.GenPrivKey()
privateKey := privateKeyObj.Bytes()

publicKeyCompressed := privateKeyObj.PubKey().Bytes()

publicKeyUncompressed, err := ethercrypto.DecompressPubkey(publicKeyCompressed)
if err != nil {
panic(err)
}
ethereumAddress := ethercrypto.PubkeyToAddress(*publicKeyUncompressed).Hex()

return &Secp256k1RecoveryPair{
Type: types.EcdsaSecp256k1RecoveryMethod2020,
PublicKey: hex.EncodeToString(publicKeyCompressed),
PrivateKey: hex.EncodeToString(privateKey),
OptionalID: ethereumAddress,
}
}
87 changes: 87 additions & 0 deletions x/ssi/tests/crypto/signature.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package crypto

import (
ldcontext "github.com/hypersign-protocol/hid-node/x/ssi/ld-context"
cli "github.com/hypersign-protocol/hid-node/x/ssi/client/cli"
"github.com/hypersign-protocol/hid-node/x/ssi/types"
)

func SignGeneric(keyPair IKeyPair, doc types.SsiMsg, docProof *types.DocumentProof) string {
docProof.Type = GetSignatureTypeFromVmType(keyPair.GetType())

signature, err := GetDocumentSignature(doc, docProof, keyPair.GetPrivateKey())
if err != nil {
panic(err)
}
return signature
}

func GetPublicKeyAndOptionalID(keyPair IKeyPair) (string, string) {
return keyPair.GetPublicKey(), keyPair.GetOptionalID()
}

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 = cli.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 = cli.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 = cli.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 = cli.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 = cli.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 d8f7e84

Please sign in to comment.