forked from keybase/client
/
saltpack_sign.go
95 lines (80 loc) · 2.18 KB
/
saltpack_sign.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package engine
import (
"io"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
// SaltpackSign is an engine.
type SaltpackSign struct {
libkb.Contextified
arg *SaltpackSignArg
key libkb.NaclSigningKeyPair
}
type SaltpackSignArg struct {
Sink io.WriteCloser
Source io.ReadCloser
Opts keybase1.SaltpackSignOptions
}
// NewSaltpackSign creates a SaltpackSign engine.
func NewSaltpackSign(arg *SaltpackSignArg, g *libkb.GlobalContext) *SaltpackSign {
return &SaltpackSign{
arg: arg,
Contextified: libkb.NewContextified(g),
}
}
// Name is the unique engine name.
func (e *SaltpackSign) Name() string {
return "SaltpackSign"
}
// GetPrereqs returns the engine prereqs.
func (e *SaltpackSign) Prereqs() Prereqs {
return Prereqs{
Device: true,
}
}
// RequiredUIs returns the required UIs.
func (e *SaltpackSign) RequiredUIs() []libkb.UIKind {
return []libkb.UIKind{
libkb.SecretUIKind,
}
}
// SubConsumers returns the other UI consumers for this engine.
func (e *SaltpackSign) SubConsumers() []libkb.UIConsumer {
return nil
}
// Run starts the engine.
func (e *SaltpackSign) Run(ctx *Context) error {
if err := e.loadKey(ctx); err != nil {
return err
}
saltpackVersion, err := libkb.SaltpackVersionFromArg(e.arg.Opts.SaltpackVersion)
if err != nil {
return err
}
if e.arg.Opts.Detached {
return libkb.SaltpackSignDetached(e.G(), e.arg.Source, e.arg.Sink, e.key, e.arg.Opts.Binary, saltpackVersion)
}
return libkb.SaltpackSign(e.G(), e.arg.Source, e.arg.Sink, e.key, e.arg.Opts.Binary, saltpackVersion)
}
func (e *SaltpackSign) loadKey(ctx *Context) error {
me, err := libkb.LoadMe(libkb.NewLoadUserArg(e.G()))
if err != nil {
return err
}
ska := libkb.SecretKeyArg{
Me: me,
KeyType: libkb.DeviceSigningKeyType,
}
key, err := e.G().Keyrings.GetSecretKeyWithPrompt(ctx.SecretKeyPromptArg(ska, "signing a message/file"))
if err != nil {
return err
}
kp, ok := key.(libkb.NaclSigningKeyPair)
if !ok || kp.Private == nil {
return libkb.KeyCannotSignError{}
}
e.key = kp
return nil
}