-
Notifications
You must be signed in to change notification settings - Fork 664
/
signer.go
63 lines (53 loc) · 1.54 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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package signer
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/keychain"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/fx"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
stdcontext "context"
)
var _ Signer = (*txSigner)(nil)
type Signer interface {
// Sign 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.
Sign(ctx stdcontext.Context, tx *txs.Tx) error
}
type Backend interface {
GetUTXO(ctx stdcontext.Context, chainID, utxoID ids.ID) (*avax.UTXO, error)
GetSubnetOwner(ctx stdcontext.Context, subnetID ids.ID) (fx.Owner, error)
}
type txSigner struct {
kc keychain.Keychain
backend Backend
}
func New(kc keychain.Keychain, backend Backend) Signer {
return &txSigner{
kc: kc,
backend: backend,
}
}
func (s *txSigner) Sign(ctx stdcontext.Context, tx *txs.Tx) error {
return tx.Unsigned.Visit(&visitor{
kc: s.kc,
backend: s.backend,
ctx: ctx,
tx: tx,
})
}
func SignUnsigned(
ctx stdcontext.Context,
signer Signer,
utx txs.UnsignedTx,
) (*txs.Tx, error) {
tx := &txs.Tx{Unsigned: utx}
return tx, signer.Sign(ctx, tx)
}