Skip to content

Commit

Permalink
Add Signer interface to support multiple Signers
Browse files Browse the repository at this point in the history
In addition to the existing secp256k1 local signer,
we can allow any provider/api that has signature generation capability
to implement the Signer interface and use firefly-signer's transaction
signing

Signed-off-by: Vinod Damle <vinod.damle@kaleido.io>
  • Loading branch information
Vinod Damle committed Aug 10, 2022
1 parent 482aadf commit a2f3444
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/ethsigner/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (t *Transaction) Build1559(chainID int64) rlp.List {
// - If either of the new EIP-1559 fields are set, use EIP-1559
// - By default use EIP-155 signing
// Never picks legacy-legacy (non EIP-155), or EIP-2930
func (t *Transaction) Sign(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
func (t *Transaction) Sign(signer secp256k1.Signer, chainID int64) ([]byte, error) {
if t.MaxPriorityFeePerGas.BigInt().Sign() > 0 || t.MaxFeePerGas.BigInt().Sign() > 0 {
return t.SignEIP1559(signer, chainID)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func (t *Transaction) SignaturePayloadLegacyOriginal() *TransactionSignaturePayl
}

// SignLegacyOriginal uses legacy transaction structure, with legacy V value (27/28)
func (t *Transaction) SignLegacyOriginal(signer *secp256k1.KeyPair) ([]byte, error) {
func (t *Transaction) SignLegacyOriginal(signer secp256k1.Signer) ([]byte, error) {
signatureData := t.SignaturePayloadLegacyOriginal()
sig, err := signer.Sign(signatureData.data)
if err != nil {
Expand All @@ -148,7 +148,7 @@ func (t *Transaction) SignaturePayloadLegacyEIP155(chainID int64) *TransactionSi
}

// SignLegacyEIP155 uses legacy transaction structure, with EIP-155 signing V value (2*ChainID + 35 + Y-parity)
func (t *Transaction) SignLegacyEIP155(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
func (t *Transaction) SignLegacyEIP155(signer secp256k1.Signer, chainID int64) ([]byte, error) {

signaturePayload := t.SignaturePayloadLegacyEIP155(chainID)

Expand Down Expand Up @@ -178,7 +178,7 @@ func (t *Transaction) SignaturePayloadEIP1559(chainID int64) *TransactionSignatu
}

// SignEIP1559 uses EIP-1559 transaction structure (with EIP-2718 transaction type byte), with EIP-2930 V value (0 / 1 - direct parity-Y)
func (t *Transaction) SignEIP1559(signer *secp256k1.KeyPair, chainID int64) ([]byte, error) {
func (t *Transaction) SignEIP1559(signer secp256k1.Signer, chainID int64) ([]byte, error) {

signaturePayload := t.SignaturePayloadEIP1559(chainID)
sig, err := signer.Sign(signaturePayload.data)
Expand Down
2 changes: 1 addition & 1 deletion pkg/secp256k1/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k *KeyPair) PrivateKeyBytes() []byte {
}

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

Expand Down
5 changes: 5 additions & 0 deletions pkg/secp256k1/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type SignatureData struct {
S *big.Int
}

// Signer is the low level common interface that can be implemented by any module which provides signature capability
type Signer interface {
Sign(message []byte) (*SignatureData, error)
}

// getVNormalized returns the original 27/28 parity
func (s *SignatureData) getVNormalized(chainID int64) (byte, error) {
v := s.V.Int64()
Expand Down

0 comments on commit a2f3444

Please sign in to comment.