-
Notifications
You must be signed in to change notification settings - Fork 75
/
pass_reader.go
56 lines (46 loc) · 1.43 KB
/
pass_reader.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
package accounts
import (
"io"
"os"
"github.com/howeyc/gopass"
)
// PassPhraser is interface for retrieving
// pass phrase for Eth keys
//
// If you want to retrieve pass phrases reader different ways
// (e.g: from file, from env variables, interactively from terminal)
// you must implement PassPhraser reader a different way and pass it to
// KeyOpener instance
type PassPhraser interface {
GetPassPhrase() (string, error)
}
// interactivePassPhraser implements the PassPhrase which allows to
// read passphrase on terminal interactively
type interactivePassPhraser struct {
reader gopass.FdReader
writer io.Writer
}
func (pf *interactivePassPhraser) GetPassPhrase() (string, error) {
// TODO: pass prompt text as param, it will make prompt more obvious for user.
pw, err := gopass.GetPasswdPrompt("\r\nKey passphrase: ", false, pf.reader, pf.writer)
if err != nil {
return "", err
}
return string(pw), nil
}
// NewInteractivePassPhraser implements PassPhraser that prompts user for pass-phrase
// and read it from terminal's Stdin
func NewInteractivePassPhraser() PassPhraser {
return &interactivePassPhraser{
reader: os.Stdin,
writer: os.Stdout,
}
}
type staticPassPhraser struct {
p string
}
func (pf *staticPassPhraser) GetPassPhrase() (string, error) { return pf.p, nil }
// NewStaticPassPhraser inits pass phrase reader with pre-defined pass.
func NewStaticPassPhraser(p string) PassPhraser {
return &staticPassPhraser{p: p}
}