/
signer_impl_v1.go
112 lines (99 loc) · 3.39 KB
/
signer_impl_v1.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package signer
/*
* Copyright 2022 ConsenSys Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
import (
"context"
"fmt"
"math/big"
"time"
"github.com/consensys/gpact/services/relayer/internal/logging"
"github.com/ethereum/go-ethereum/common"
datastore "github.com/ipfs/go-datastore"
badgerds "github.com/ipfs/go-ds-badger"
)
const (
dsTimeout = 3 * time.Second
)
// SignerImplV1 implements the Signer interface.
type SignerImplV1 struct {
path string
ds datastore.Datastore
}
// NewSignerImplV1 creates a new Signer.
func NewSignerImplV1(path string) Signer {
return &SignerImplV1{path: path}
}
// Start starts the signer's routine.
func (s *SignerImplV1) Start() error {
var err error
if s.ds == nil {
dsopts := badgerds.DefaultOptions
dsopts.SyncWrites = false
dsopts.Truncate = true
s.ds, err = badgerds.NewDatastore(s.path, &dsopts)
}
return err
}
// Stop stops the signer's routine.
func (s *SignerImplV1) Stop() {
if s.ds != nil {
if err := s.ds.Close(); err != nil {
logging.Error("Error in closing the db %v", err.Error())
}
}
}
// SetKey sets a signing key for given chain and a given contract addr.
func (s *SignerImplV1) SetKey(chainID *big.Int, contractAddr common.Address, keyType byte, key []byte) error {
_, ok := supportedKeyTypes[keyType]
if !ok {
return fmt.Errorf("key type %v not supported", keyType)
}
ctx, cancel := context.WithTimeout(context.Background(), dsTimeout)
defer cancel()
logging.Info("Signing key set for: Chain: %v, Contract: %v", chainID, contractAddr)
return s.ds.Put(ctx, dsKey(chainID, contractAddr), append([]byte{keyType}, key...))
}
// GetAddr gets the associated address for a signing key linked with given chain and given contract addr.
func (s *SignerImplV1) GetAddr(chainID *big.Int, contractAddr common.Address) (byte, common.Address, error) {
ctx, cancel := context.WithTimeout(context.Background(), dsTimeout)
defer cancel()
val, err := s.ds.Get(ctx, dsKey(chainID, contractAddr))
if err != nil {
return 0, common.Address{}, err
}
addr, err := keyToAddr(val[0], val[1:])
if err != nil {
return 0, common.Address{}, err
}
return val[0], addr, nil
}
// Sign signs given message with internal stored key correspdoing to given chainID and given contract addr.
func (s *SignerImplV1) Sign(chainID *big.Int, contractAddr common.Address, msg []byte) (byte, []byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), dsTimeout)
defer cancel()
val, err := s.ds.Get(ctx, dsKey(chainID, contractAddr))
if err != nil {
return 0, nil, err
}
sig, err := sign(val[0], val[1:], msg)
if err != nil {
return 0, nil, err
}
return val[0], sig, nil
}
// dsKey gets the datastore key from given chainID and contract address.
func dsKey(chainID *big.Int, contractAddr common.Address) datastore.Key {
return datastore.NewKey(fmt.Sprintf("%v-%v", chainID.String(), contractAddr.String()))
}