-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathopt.go
41 lines (37 loc) · 1.05 KB
/
opt.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
package envelope
import (
"context"
"encoding/base64"
"fmt"
"strings"
"github.com/hyperledger-labs/cckit/gateway"
"google.golang.org/grpc/metadata"
)
// input opt for gateway to handle envelope with signature
func WithEnvelope() gateway.Opt {
return func(opts *gateway.Opts) {
opts.Input = append(opts.Input, func(ctx context.Context, input *gateway.ChaincodeInput) error {
// get envelop with signature from header and add as second arg
md, ok := metadata.FromIncomingContext(ctx)
if ok {
if v, ok := md[strings.ToLower(HeaderKey)]; ok {
envelope, err := DecodeEnvelope([]byte(v[0]))
if err != nil {
return fmt.Errorf(`invoke: %w`, err)
}
input.Args = append(input.Args, envelope)
}
}
return nil
})
}
}
// decode base64 envelop
func DecodeEnvelope(encEnvelope []byte) ([]byte, error) {
dst := make([]byte, base64.StdEncoding.DecodedLen(len(encEnvelope)))
n, err := base64.StdEncoding.Decode(dst, encEnvelope)
if err != nil {
return nil, fmt.Errorf("parse envelope: %w", err)
}
return dst[:n], nil
}