/
main.go
71 lines (57 loc) · 1.81 KB
/
main.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
// Package main implements an example oasis-node signer plugin.
package main
import (
"crypto/rand"
"fmt"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
memorySigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/memory"
pluginSigner "github.com/oasisprotocol/oasis-core/go/common/crypto/signature/signers/plugin"
)
type examplePlugin struct {
roles []signature.SignerRole
inner map[signature.SignerRole]signature.Signer
}
func (pl *examplePlugin) Initialize(config string, roles ...signature.SignerRole) error {
// A real plugin will probably want to check to see if it has
// already been initialized.
pl.roles = roles
pl.inner = make(map[signature.SignerRole]signature.Signer)
return nil
}
func (pl *examplePlugin) Load(role signature.SignerRole, mustGenerate bool) error {
if signer := pl.inner[role]; signer != nil {
if mustGenerate {
return fmt.Errorf("example: key already exists")
}
return nil
}
if !mustGenerate {
return signature.ErrNotExist
}
signer, err := memorySigner.NewSigner(rand.Reader)
if err != nil {
return fmt.Errorf("example: failed to generate key: %w", err)
}
pl.inner[role] = signer
return nil
}
func (pl *examplePlugin) Public(role signature.SignerRole) (signature.PublicKey, error) {
signer := pl.inner[role]
if signer == nil {
return signature.PublicKey{}, signature.ErrNotExist
}
return signer.Public(), nil
}
func (pl *examplePlugin) ContextSign(role signature.SignerRole, rawContext signature.Context, message []byte) ([]byte, error) {
signer, ok := pl.inner[role]
if !ok {
return nil, signature.ErrNotExist
}
return signer.ContextSign(rawContext, message)
}
func main() {
// Signer plugins use raw contexts.
signature.UnsafeAllowUnregisteredContexts()
var impl examplePlugin
pluginSigner.Serve("example", &impl)
}