Skip to content

Commit 9358fd2

Browse files
Merge pull request #17 from kaleido-io/signer-interface-new
Add Signer interface to support multiple Signers
2 parents 482aadf + c996220 commit 9358fd2

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

pkg/ethsigner/transaction.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package ethsigner
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"math/big"
2223

2324
"github.com/hyperledger/firefly-signer/pkg/ethtypes"
@@ -96,7 +97,10 @@ func (t *Transaction) Build1559(chainID int64) rlp.List {
9697
// - If either of the new EIP-1559 fields are set, use EIP-1559
9798
// - By default use EIP-155 signing
9899
// Never picks legacy-legacy (non EIP-155), or EIP-2930
99-
func (t *Transaction) Sign(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
100+
func (t *Transaction) Sign(signer secp256k1.Signer, chainID int64) ([]byte, error) {
101+
if signer == nil {
102+
return nil, fmt.Errorf("invalid signer")
103+
}
100104
if t.MaxPriorityFeePerGas.BigInt().Sign() > 0 || t.MaxFeePerGas.BigInt().Sign() > 0 {
101105
return t.SignEIP1559(signer, chainID)
102106
}
@@ -124,7 +128,10 @@ func (t *Transaction) SignaturePayloadLegacyOriginal() *TransactionSignaturePayl
124128
}
125129

126130
// SignLegacyOriginal uses legacy transaction structure, with legacy V value (27/28)
127-
func (t *Transaction) SignLegacyOriginal(signer *secp256k1.KeyPair) ([]byte, error) {
131+
func (t *Transaction) SignLegacyOriginal(signer secp256k1.Signer) ([]byte, error) {
132+
if signer == nil {
133+
return nil, fmt.Errorf("invalid signer")
134+
}
128135
signatureData := t.SignaturePayloadLegacyOriginal()
129136
sig, err := signer.Sign(signatureData.data)
130137
if err != nil {
@@ -148,7 +155,10 @@ func (t *Transaction) SignaturePayloadLegacyEIP155(chainID int64) *TransactionSi
148155
}
149156

150157
// SignLegacyEIP155 uses legacy transaction structure, with EIP-155 signing V value (2*ChainID + 35 + Y-parity)
151-
func (t *Transaction) SignLegacyEIP155(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
158+
func (t *Transaction) SignLegacyEIP155(signer secp256k1.Signer, chainID int64) ([]byte, error) {
159+
if signer == nil {
160+
return nil, fmt.Errorf("invalid signer")
161+
}
152162

153163
signaturePayload := t.SignaturePayloadLegacyEIP155(chainID)
154164

@@ -178,7 +188,10 @@ func (t *Transaction) SignaturePayloadEIP1559(chainID int64) *TransactionSignatu
178188
}
179189

180190
// SignEIP1559 uses EIP-1559 transaction structure (with EIP-2718 transaction type byte), with EIP-2930 V value (0 / 1 - direct parity-Y)
181-
func (t *Transaction) SignEIP1559(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
191+
func (t *Transaction) SignEIP1559(signer secp256k1.Signer, chainID int64) ([]byte, error) {
192+
if signer == nil {
193+
return nil, fmt.Errorf("invalid signer")
194+
}
182195

183196
signaturePayload := t.SignaturePayloadEIP1559(chainID)
184197
sig, err := signer.Sign(signaturePayload.data)

pkg/secp256k1/keypair.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (k *KeyPair) PrivateKeyBytes() []byte {
3535
}
3636

3737
func (k *KeyPair) PublicKeyBytes() []byte {
38-
// Remove the "04" Suffix byte when computing the address. This byte indicates that it is an uncompressed public key.
38+
// Remove the "04" Prefix byte when computing the address. This byte indicates that it is an uncompressed public key.
3939
return k.PublicKey.SerializeUncompressed()[1:]
4040
}
4141

pkg/secp256k1/signer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ type SignatureData struct {
3131
S *big.Int
3232
}
3333

34+
// Signer is the low level common interface that can be implemented by any module which provides signature capability
35+
type Signer interface {
36+
Sign(message []byte) (*SignatureData, error)
37+
}
38+
3439
// getVNormalized returns the original 27/28 parity
3540
func (s *SignatureData) getVNormalized(chainID int64) (byte, error) {
3641
v := s.V.Int64()

0 commit comments

Comments
 (0)