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