Skip to content

Commit

Permalink
Keccak (umbracle#121)
Browse files Browse the repository at this point in the history
* Add public keccak function

* Replace keccak in wallet with new one
  • Loading branch information
ferranbt authored Dec 8, 2021
1 parent 0cb44c2 commit a62dc1e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
12 changes: 12 additions & 0 deletions keccak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package web3

import "golang.org/x/crypto/sha3"

// Keccak256 calculates the Keccak256
func Keccak256(v ...[]byte) []byte {
h := sha3.NewLegacyKeccak256()
for _, i := range v {
h.Write(i)
}
return h.Sum(nil)
}
16 changes: 3 additions & 13 deletions wallet/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/btcsuite/btcd/btcec"
"github.com/umbracle/go-web3"
"golang.org/x/crypto/sha3"
)

// S256 is the secp256k1 elliptic curve
Expand All @@ -31,7 +30,7 @@ func (k *Key) MarshallPrivateKey() ([]byte, error) {
}

func (k *Key) SignMsg(msg []byte) ([]byte, error) {
return k.Sign(keccak256(msg))
return k.Sign(web3.Keccak256(msg))
}

func (k *Key) Sign(hash []byte) ([]byte, error) {
Expand All @@ -56,7 +55,7 @@ func NewKey(priv *ecdsa.PrivateKey) *Key {
}

func pubKeyToAddress(pub *ecdsa.PublicKey) (addr web3.Address) {
b := keccak256(elliptic.Marshal(S256, pub.X, pub.Y)[1:])
b := web3.Keccak256(elliptic.Marshal(S256, pub.X, pub.Y)[1:])
copy(addr[:], b[12:])
return
}
Expand All @@ -71,7 +70,7 @@ func GenerateKey() (*Key, error) {
}

func EcrecoverMsg(msg, signature []byte) (web3.Address, error) {
return Ecrecover(keccak256(msg), signature)
return Ecrecover(web3.Keccak256(msg), signature)
}

func Ecrecover(hash, signature []byte) (web3.Address, error) {
Expand All @@ -96,12 +95,3 @@ func RecoverPubkey(signature, hash []byte) (*ecdsa.PublicKey, error) {
}
return pub.ToECDSA(), nil
}

func keccak256(buf ...[]byte) []byte {
h := sha3.NewLegacyKeccak256()
for _, i := range buf {
h.Write(i)
}
b := h.Sum(nil)
return b
}
2 changes: 1 addition & 1 deletion wallet/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func signHash(tx *web3.Transaction, chainID uint64) []byte {
v.Set(a.NewUint(0))
}

hash := keccak256(v.MarshalTo(nil))
hash := web3.Keccak256(v.MarshalTo(nil))
fastrlp.DefaultArenaPool.Put(a)
return hash
}
Expand Down
3 changes: 2 additions & 1 deletion wallet/wallet_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io/ioutil"
"strings"

"github.com/umbracle/go-web3"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
)
Expand Down Expand Up @@ -126,7 +127,7 @@ func (c *cryptoEncoding) getKDF(password []byte) ([]byte, error) {
}

// validate mac
mac := keccak256(key[16:32], c.CipherText)
mac := web3.Keccak256(key[16:32], c.CipherText)
if !bytes.Equal(mac, c.Mac) {
return nil, fmt.Errorf("incorrect mac")
}
Expand Down

0 comments on commit a62dc1e

Please sign in to comment.