Skip to content

Commit

Permalink
[refactor] Cleanup switch statements (#71)
Browse files Browse the repository at this point in the history
Taking additional suggestion from #65
  • Loading branch information
gregnazario authored Jun 28, 2024
1 parent a08b1be commit 55131ba
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crypto/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type Ed25519PublicKey struct {
// Implements:
// - [VerifyingKey]
func (key *Ed25519PublicKey) Verify(msg []byte, sig Signature) bool {
switch sig.(type) {
switch sig := sig.(type) {
case *Ed25519Signature:
return ed25519.Verify(key.Inner, msg, sig.Bytes())
default:
Expand Down
3 changes: 1 addition & 2 deletions crypto/multiEd25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ type MultiEd25519PublicKey struct {
// Implements:
// - [VerifyingKey]
func (key *MultiEd25519PublicKey) Verify(msg []byte, signature Signature) bool {
switch signature.(type) {
switch sig := signature.(type) {
case *MultiEd25519Signature:
sig := signature.(*MultiEd25519Signature)
verified := uint8(0)
// TODO: Verify with bitmap, and check that this works properly
for i, pubKey := range key.PubKeys {
Expand Down
3 changes: 1 addition & 2 deletions crypto/multiKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ type MultiKey struct {
// Implements:
// - [VerifyingKey]
func (key *MultiKey) Verify(msg []byte, signature Signature) bool {
switch signature.(type) {
switch sig := signature.(type) {
case *MultiKeySignature:
sig := signature.(*MultiKeySignature)
verified := uint8(0)
for i, pub := range key.PubKeys {
if pub.Verify(msg, sig.Signatures[i]) {
Expand Down
6 changes: 2 additions & 4 deletions crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,11 @@ type Secp256k1PublicKey struct {
// Implements:
// - [VerifyingKey]
func (key *Secp256k1PublicKey) Verify(msg []byte, sig Signature) bool {
switch sig.(type) {
switch sig := sig.(type) {
case *Secp256k1Signature:
typedSig := sig.(*Secp256k1Signature)

// Verification requires to pass the SHA-256 hash of the message
msg = util.Sha3256Hash([][]byte{msg})
return ethCrypto.VerifySignature(key.Bytes(), msg, typedSig.Bytes())
return ethCrypto.VerifySignature(key.Bytes(), msg, sig.Bytes())
default:
return false
}
Expand Down
5 changes: 2 additions & 3 deletions crypto/singleKey.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ func ToAnyPublicKey(key VerifyingKey) (*AnyPublicKey, error) {
// Implements:
// - [VerifyingKey]
func (key *AnyPublicKey) Verify(msg []byte, sig Signature) bool {
switch sig.(type) {
switch sig := sig.(type) {
case *AnySignature:
anySig := sig.(*AnySignature)
return key.PubKey.Verify(msg, anySig.Signature)
return key.PubKey.Verify(msg, sig.Signature)
default:
return false
}
Expand Down
7 changes: 3 additions & 4 deletions transactionSubmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,11 @@ func (rc *NodeClient) BuildSignAndSubmitTransactions(
buildOptions ...any,
) {
singleSigner := func(rawTxn RawTransactionImpl) (*SignedTransaction, error) {
switch rawTxn.(type) {
switch rawTxn := rawTxn.(type) {
case *RawTransaction:
return rawTxn.(*RawTransaction).SignedTransaction(sender)
return rawTxn.SignedTransaction(sender)
case *RawTransactionWithData:
rawTxnWithData := rawTxn.(*RawTransactionWithData)
switch rawTxnWithData.Variant {
switch rawTxn.Variant {
case MultiAgentRawTransactionWithDataVariant:
return nil, fmt.Errorf("multi agent not supported, please provide a signer function")
case MultiAgentWithFeePayerRawTransactionWithDataVariant:
Expand Down

0 comments on commit 55131ba

Please sign in to comment.