Skip to content

Commit

Permalink
Merge pull request #17 from kaleido-io/signer-interface-new
Browse files Browse the repository at this point in the history
Add Signer interface to support multiple Signers
  • Loading branch information
peterbroadhurst authored Aug 12, 2022
2 parents 482aadf + c996220 commit 9358fd2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
21 changes: 17 additions & 4 deletions pkg/ethsigner/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ethsigner

import (
"encoding/json"
"fmt"
"math/big"

"github.com/hyperledger/firefly-signer/pkg/ethtypes"
Expand Down Expand Up @@ -96,7 +97,10 @@ 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 signer == nil {
return nil, fmt.Errorf("invalid signer")
}
if t.MaxPriorityFeePerGas.BigInt().Sign() > 0 || t.MaxFeePerGas.BigInt().Sign() > 0 {
return t.SignEIP1559(signer, chainID)
}
Expand Down Expand Up @@ -124,7 +128,10 @@ 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) {
if signer == nil {
return nil, fmt.Errorf("invalid signer")
}
signatureData := t.SignaturePayloadLegacyOriginal()
sig, err := signer.Sign(signatureData.data)
if err != nil {
Expand All @@ -148,7 +155,10 @@ 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) {
if signer == nil {
return nil, fmt.Errorf("invalid signer")
}

signaturePayload := t.SignaturePayloadLegacyEIP155(chainID)

Expand Down Expand Up @@ -178,7 +188,10 @@ 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) {
if signer == nil {
return nil, fmt.Errorf("invalid signer")
}

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 9358fd2

Please sign in to comment.