github.com/s7techlab/cckit@v0.10.5/gateway/context.go (about) 1 package gateway 2 3 import ( 4 "context" 5 6 "github.com/hyperledger/fabric/msp" 7 ) 8 9 type contextKey string 10 11 func (c contextKey) String() string { 12 return string(c) 13 } 14 15 const ( 16 CtxTransientKey = contextKey(`TransientMap`) 17 CtxSignerKey = contextKey(`SigningIdentity`) 18 CtxTxWaiterKey = contextKey(`TxWaiter`) 19 ) 20 21 func ContextWithTransientMap(ctx context.Context, transient map[string][]byte) context.Context { 22 return context.WithValue(ctx, CtxTransientKey, transient) 23 } 24 25 func ContextWithTransientValue(ctx context.Context, key string, value []byte) context.Context { 26 transient, ok := ctx.Value(CtxTransientKey).(map[string][]byte) 27 if !ok { 28 transient = make(map[string][]byte) 29 } 30 transient[key] = value 31 return context.WithValue(ctx, CtxTransientKey, transient) 32 } 33 34 func TransientFromContext(ctx context.Context) (map[string][]byte, error) { 35 if transient, ok := ctx.Value(CtxTransientKey).(map[string][]byte); !ok { 36 return nil, nil 37 } else { 38 return transient, nil 39 } 40 } 41 42 func ContextWithDefaultSigner(ctx context.Context, defaultSigner msp.SigningIdentity) context.Context { 43 if _, err := SignerFromContext(ctx); err != nil { 44 return ContextWithSigner(ctx, defaultSigner) 45 } else { 46 return ctx 47 } 48 } 49 50 func ContextWithSigner(ctx context.Context, signer msp.SigningIdentity) context.Context { 51 return context.WithValue(ctx, CtxSignerKey, signer) 52 } 53 54 func SignerFromContext(ctx context.Context) (msp.SigningIdentity, error) { 55 if signer, ok := ctx.Value(CtxSignerKey).(msp.SigningIdentity); !ok { 56 return nil, ErrSignerNotDefinedInContext 57 } else { 58 return signer, nil 59 } 60 } 61 62 func ContextWithTxWaiter(ctx context.Context, txWaiterType string) context.Context { 63 return context.WithValue(ctx, CtxTxWaiterKey, txWaiterType) 64 } 65 66 // TxWaiterFromContext - fetch 'txWaiterType' param which identify transaction waiting policy 67 // what params you'll have depends on your implementation 68 // for example, in hlf-sdk: 69 // available: 'self'(wait for one peer of endorser org), 'all'(wait for each organizations from endorsement policy) 70 // default is 'self'(even if you pass empty string) 71 func TxWaiterFromContext(ctx context.Context) string { 72 txWaiter, _ := ctx.Value(CtxTxWaiterKey).(string) 73 return txWaiter 74 }