github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/verifiable/example_support_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package verifiable_test
     8  
     9  import (
    10  	"encoding/base64"
    11  	"encoding/json"
    12  	"strings"
    13  
    14  	"github.com/hyperledger/aries-framework-go/pkg/crypto/primitive/bbs12381g2pub"
    15  	"github.com/hyperledger/aries-framework-go/pkg/doc/ld"
    16  	"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
    17  	"github.com/hyperledger/aries-framework-go/pkg/internal/ldtestutil"
    18  )
    19  
    20  type UniversityDegree struct {
    21  	Type       string `json:"type,omitempty"`
    22  	University string `json:"university,omitempty"`
    23  }
    24  
    25  type UniversityDegreeSubject struct {
    26  	ID     string           `json:"id,omitempty"`
    27  	Name   string           `json:"name,omitempty"`
    28  	Spouse string           `json:"spouse,omitempty"`
    29  	Degree UniversityDegree `json:"degree,omitempty"`
    30  }
    31  
    32  type UniversityDegreeCredential struct {
    33  	*verifiable.Credential
    34  
    35  	ReferenceNumber int `json:"referenceNumber,omitempty"`
    36  }
    37  
    38  func (udc *UniversityDegreeCredential) MarshalJSON() ([]byte, error) {
    39  	// todo too complex! (https://github.com/hyperledger/aries-framework-go/issues/847)
    40  	c := udc.Credential
    41  	cp := *c
    42  
    43  	cp.CustomFields = map[string]interface{}{
    44  		"referenceNumber": udc.ReferenceNumber,
    45  	}
    46  
    47  	return json.Marshal(&cp)
    48  }
    49  
    50  func getJSONLDDocumentLoader() *ld.DocumentLoader {
    51  	loader, err := ldtestutil.DocumentLoader()
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  
    56  	return loader
    57  }
    58  
    59  type bbsSigner struct {
    60  	privKeyBytes []byte
    61  }
    62  
    63  func newBBSSigner(privKey *bbs12381g2pub.PrivateKey) (*bbsSigner, error) {
    64  	privKeyBytes, err := privKey.Marshal()
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return &bbsSigner{privKeyBytes: privKeyBytes}, nil
    70  }
    71  
    72  func (s *bbsSigner) Sign(data []byte) ([]byte, error) {
    73  	msgs := s.textToLines(string(data))
    74  
    75  	return bbs12381g2pub.New().Sign(msgs, s.privKeyBytes)
    76  }
    77  
    78  // Alg return alg.
    79  func (s *bbsSigner) Alg() string {
    80  	return ""
    81  }
    82  
    83  func (s *bbsSigner) textToLines(txt string) [][]byte {
    84  	lines := strings.Split(txt, "\n")
    85  	linesBytes := make([][]byte, 0, len(lines))
    86  
    87  	for i := range lines {
    88  		if strings.TrimSpace(lines[i]) != "" {
    89  			linesBytes = append(linesBytes, []byte(lines[i]))
    90  		}
    91  	}
    92  
    93  	return linesBytes
    94  }
    95  
    96  func loadBBSKeyPair(pubKeyB64, privKeyB64 string) (*bbs12381g2pub.PublicKey, *bbs12381g2pub.PrivateKey, error) {
    97  	pubKeyBytes, err := base64.RawStdEncoding.DecodeString(pubKeyB64)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  
   102  	pubKey, err := bbs12381g2pub.UnmarshalPublicKey(pubKeyBytes)
   103  	if err != nil {
   104  		return nil, nil, err
   105  	}
   106  
   107  	privKeyBytes, err := base64.RawStdEncoding.DecodeString(privKeyB64)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  
   112  	privKey, err := bbs12381g2pub.UnmarshalPrivateKey(privKeyBytes)
   113  	if err != nil {
   114  		return nil, nil, err
   115  	}
   116  
   117  	return pubKey, privKey, nil
   118  }