github.com/s7techlab/cckit@v0.10.5/examples/payment/cc_enc_manual_required_without_mapping.go (about)

     1  package payment
     2  
     3  import (
     4  	"github.com/s7techlab/cckit/examples/payment/schema"
     5  	"github.com/s7techlab/cckit/extensions/debug"
     6  	"github.com/s7techlab/cckit/extensions/encryption"
     7  	"github.com/s7techlab/cckit/router"
     8  	p "github.com/s7techlab/cckit/router/param"
     9  	"github.com/s7techlab/cckit/state"
    10  	m "github.com/s7techlab/cckit/state/mapping"
    11  )
    12  
    13  // Chaincode with required encrypting (encrypting key must be provided in transient map)
    14  // WITHOUT mapping
    15  func NewEncryptPaymentCC() *router.Chaincode {
    16  	r := router.New(`encrypted`).
    17  		Pre(encryption.ArgsDecrypt). //  encrypted args required - key must be provided in transient map
    18  		Init(router.EmptyContextHandler)
    19  
    20  	r.Use(m.MapStates(StateMappings)) // use state mapping
    21  
    22  	debug.AddHandlers(r, `debug`)
    23  
    24  	// same as NewEncryptOnDemandPaymentCC
    25  	r.Group(`payment`).
    26  		Invoke(`Create`, invokePaymentCreateManualEncryptWithoutMapping, p.String(`type`), p.String(`id`), p.Int(`amount`)).
    27  		Query(`List`, queryPaymentsWithoutMapping, p.String(`type`)).
    28  		Query(`Get`, queryPaymentWithoutMapping, p.String(`type`), p.String(`id`))
    29  
    30  	return router.NewChaincode(r)
    31  }
    32  
    33  // Payment creation chaincode function handler - can be used in encryption and no encryption mode
    34  func invokePaymentCreateManualEncryptWithoutMapping(c router.Context) (interface{}, error) {
    35  	var (
    36  		paymentType   = c.ParamString(`type`)
    37  		paymentId     = c.ParamString(`id`)
    38  		paymentAmount = c.ParamInt(`amount`)
    39  		s             state.State
    40  		err           error
    41  		returnVal     []byte
    42  	)
    43  
    44  	// Explicit encrypted state wrapper with key from transient map if key provided
    45  	if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	returnVal = []byte(paymentId)
    50  	// return encrypted val if key provided
    51  	if key, _ := encryption.KeyFromTransient(c); key != nil {
    52  		returnVal, err = encryption.Encrypt(key, paymentId)
    53  	}
    54  
    55  	// manually set key
    56  	return returnVal, s.Put(
    57  		[]string{`PaymentManual`, paymentType, paymentId},
    58  		&schema.Payment{
    59  			Type:   paymentType,
    60  			Id:     paymentId,
    61  			Amount: int32(paymentAmount)},
    62  	)
    63  }
    64  
    65  func queryPaymentsWithoutMapping(c router.Context) (interface{}, error) {
    66  	var (
    67  		//paymentType = c.ParamString(`type`)
    68  		s   state.State
    69  		err error
    70  	)
    71  	// Explicit encrypted state wrapper with key from transient map if key provided
    72  	if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
    73  		return nil, err
    74  	}
    75  	// manually set key
    76  	return s.List(`Payment`)
    77  }
    78  
    79  // Payment query chaincode function handler - can be used in encryption and no encryption mode
    80  func queryPaymentWithoutMapping(c router.Context) (interface{}, error) {
    81  	var (
    82  		paymentType = c.ParamString(`type`)
    83  		paymentId   = c.ParamString(`id`)
    84  		s           state.State
    85  		err         error
    86  	)
    87  	if s, err = encryption.StateWithTransientKeyIfProvided(c); err != nil {
    88  		return nil, err
    89  	}
    90  	// manually set key
    91  	return s.Get([]string{`Payment`, paymentType, paymentId})
    92  }