arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/utils.go
common/utils.goRaw
 1package common
 2
 3import (
 4	"encoding/hex"
 5	"math/big"
 6
 7	ethCommon "github.com/ethereum/go-ethereum/common"
 8	"github.com/iden3/go-iden3-crypto/babyjub"
 9)
10
11// SwapEndianness swaps the order of the bytes in the slice.
12func SwapEndianness(b []byte) []byte {
13	o := make([]byte, len(b))
14	for i := range b {
15		o[len(b)-1-i] = b[i]
16	}
17	return o
18}
19
20// EthAddrToBigInt returns a *big.Int from a given ethereum common.Address.
21func EthAddrToBigInt(a ethCommon.Address) *big.Int {
22	return new(big.Int).SetBytes(a.Bytes())
23}
24
25// BJJFromStringWithChecksum parses a hex string in Hermez format (which has
26// the Hermez checksum at the last byte, and is encoded in BigEndian) and
27// returns the corresponding *babyjub.PublicKey. This method is not part of the
28// spec, is used for importing javascript test vectors data.
29func BJJFromStringWithChecksum(s string) (*babyjub.PublicKey, error) {
30	b, err := hex.DecodeString(s)
31	if err != nil {
32		return nil, err
33	}
34	pkBytes := SwapEndianness(b)
35	var pkComp babyjub.PublicKeyComp
36	copy(pkComp[:], pkBytes[:])
37	return pkComp.Decompress()
38}