-
Notifications
You must be signed in to change notification settings - Fork 646
/
signer.go
229 lines (197 loc) · 6.57 KB
/
signer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package c
import (
"errors"
"fmt"
"github.com/ava-labs/coreth/plugin/evm"
"github.com/ethereum/go-ethereum/common"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
stdcontext "context"
)
const version = 0
var (
_ Signer = (*txSigner)(nil)
errUnknownInputType = errors.New("unknown input type")
errUnknownCredentialType = errors.New("unknown credential type")
errUnknownOutputType = errors.New("unknown output type")
errInvalidUTXOSigIndex = errors.New("invalid UTXO signature index")
emptySig [secp256k1.SignatureLen]byte
)
type Signer interface {
// SignAtomic adds as many missing signatures as possible to the provided
// transaction.
//
// If there are already some signatures on the transaction, those signatures
// will not be removed.
//
// If the signer doesn't have the ability to provide a required signature,
// the signature slot will be skipped without reporting an error.
SignAtomic(ctx stdcontext.Context, tx *evm.Tx) error
}
type EthKeychain interface {
// The returned Signer can provide a signature for [addr]
GetEth(addr common.Address) (keychain.Signer, bool)
// Returns the set of addresses for which the accessor keeps an associated
// signer
EthAddresses() set.Set[common.Address]
}
type SignerBackend interface {
GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
}
type txSigner struct {
avaxKC keychain.Keychain
ethKC EthKeychain
backend SignerBackend
}
func NewSigner(avaxKC keychain.Keychain, ethKC EthKeychain, backend SignerBackend) Signer {
return &txSigner{
avaxKC: avaxKC,
ethKC: ethKC,
backend: backend,
}
}
func (s *txSigner) SignAtomic(ctx stdcontext.Context, tx *evm.Tx) error {
switch utx := tx.UnsignedAtomicTx.(type) {
case *evm.UnsignedImportTx:
signers, err := s.getImportSigners(ctx, utx.SourceChain, utx.ImportedInputs)
if err != nil {
return err
}
return sign(tx, true, signers)
case *evm.UnsignedExportTx:
signers := s.getExportSigners(utx.Ins)
return sign(tx, true, signers)
default:
return fmt.Errorf("%w: %T", errUnknownTxType, tx)
}
}
func (s *txSigner) getImportSigners(ctx stdcontext.Context, sourceChainID ids.ID, ins []*avax.TransferableInput) ([][]keychain.Signer, error) {
txSigners := make([][]keychain.Signer, len(ins))
for credIndex, transferInput := range ins {
input, ok := transferInput.In.(*secp256k1fx.TransferInput)
if !ok {
return nil, errUnknownInputType
}
inputSigners := make([]keychain.Signer, len(input.SigIndices))
txSigners[credIndex] = inputSigners
utxoID := transferInput.InputID()
utxo, err := s.backend.GetUTXO(ctx, sourceChainID, utxoID)
if err == database.ErrNotFound {
// If we don't have access to the UTXO, then we can't sign this
// transaction. However, we can attempt to partially sign it.
continue
}
if err != nil {
return nil, err
}
out, ok := utxo.Out.(*secp256k1fx.TransferOutput)
if !ok {
return nil, errUnknownOutputType
}
for sigIndex, addrIndex := range input.SigIndices {
if addrIndex >= uint32(len(out.Addrs)) {
return nil, errInvalidUTXOSigIndex
}
addr := out.Addrs[addrIndex]
key, ok := s.avaxKC.Get(addr)
if !ok {
// If we don't have access to the key, then we can't sign this
// transaction. However, we can attempt to partially sign it.
continue
}
inputSigners[sigIndex] = key
}
}
return txSigners, nil
}
func (s *txSigner) getExportSigners(ins []evm.EVMInput) [][]keychain.Signer {
txSigners := make([][]keychain.Signer, len(ins))
for credIndex, input := range ins {
inputSigners := make([]keychain.Signer, 1)
txSigners[credIndex] = inputSigners
key, ok := s.ethKC.GetEth(input.Address)
if !ok {
// If we don't have access to the key, then we can't sign this
// transaction. However, we can attempt to partially sign it.
continue
}
inputSigners[0] = key
}
return txSigners
}
func SignUnsignedAtomic(ctx stdcontext.Context, signer Signer, utx evm.UnsignedAtomicTx) (*evm.Tx, error) {
tx := &evm.Tx{UnsignedAtomicTx: utx}
return tx, signer.SignAtomic(ctx, tx)
}
// TODO: remove [signHash] after the ledger supports signing all transactions.
func sign(tx *evm.Tx, signHash bool, txSigners [][]keychain.Signer) error {
unsignedBytes, err := evm.Codec.Marshal(version, &tx.UnsignedAtomicTx)
if err != nil {
return fmt.Errorf("couldn't marshal unsigned tx: %w", err)
}
unsignedHash := hashing.ComputeHash256(unsignedBytes)
if expectedLen := len(txSigners); expectedLen != len(tx.Creds) {
tx.Creds = make([]verify.Verifiable, expectedLen)
}
sigCache := make(map[ids.ShortID][secp256k1.SignatureLen]byte)
for credIndex, inputSigners := range txSigners {
credIntf := tx.Creds[credIndex]
if credIntf == nil {
credIntf = &secp256k1fx.Credential{}
tx.Creds[credIndex] = credIntf
}
cred, ok := credIntf.(*secp256k1fx.Credential)
if !ok {
return errUnknownCredentialType
}
if expectedLen := len(inputSigners); expectedLen != len(cred.Sigs) {
cred.Sigs = make([][secp256k1.SignatureLen]byte, expectedLen)
}
for sigIndex, signer := range inputSigners {
if signer == nil {
// If we don't have access to the key, then we can't sign this
// transaction. However, we can attempt to partially sign it.
continue
}
addr := signer.Address()
if sig := cred.Sigs[sigIndex]; sig != emptySig {
// If this signature has already been populated, we can just
// copy the needed signature for the future.
sigCache[addr] = sig
continue
}
if sig, exists := sigCache[addr]; exists {
// If this key has already produced a signature, we can just
// copy the previous signature.
cred.Sigs[sigIndex] = sig
continue
}
var sig []byte
if signHash {
sig, err = signer.SignHash(unsignedHash)
} else {
sig, err = signer.Sign(unsignedBytes)
}
if err != nil {
return fmt.Errorf("problem signing tx: %w", err)
}
copy(cred.Sigs[sigIndex][:], sig)
sigCache[addr] = cred.Sigs[sigIndex]
}
}
signedBytes, err := evm.Codec.Marshal(version, tx)
if err != nil {
return fmt.Errorf("couldn't marshal tx: %w", err)
}
tx.Initialize(unsignedBytes, signedBytes)
return nil
}