arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/common/account.go
common/account.goRaw
  1package common
  2
  3import (
  4	"bytes"
  5	"encoding/binary"
  6	"fmt"
  7	"math/big"
  8
  9	eth "github.com/ethereum/go-ethereum/common"
 10	"github.com/iden3/go-iden3-crypto/babyjub"
 11	"github.com/iden3/go-iden3-crypto/poseidon"
 12	cryptoUtils "github.com/iden3/go-iden3-crypto/utils"
 13)
 14
 15const NLEAFELEMS = 4
 16
 17// Account is a struct that gives information of the holdings of an address and a specific token. Is the data structure that generates the Value stored in the leaf of the MerkleTree
 18type Account struct {
 19	TokenID   TokenID
 20	Nonce     uint64   // max of 40 bits used
 21	Balance   *big.Int // max of 192 bits used
 22	PublicKey *babyjub.PublicKey
 23	EthAddr   eth.Address
 24}
 25
 26func (a *Account) String() string {
 27	buf := bytes.NewBufferString("")
 28	fmt.Fprintf(buf, "PublicKey: %s..., ", a.PublicKey.String()[:10])
 29	fmt.Fprintf(buf, "EthAddr: %s..., ", a.EthAddr.String()[:10])
 30	fmt.Fprintf(buf, "TokenID: %v, ", a.TokenID)
 31	fmt.Fprintf(buf, "Nonce: %d, ", a.Nonce)
 32	fmt.Fprintf(buf, "Balance: %s, ", a.Balance.String())
 33	return buf.String()
 34}
 35
 36// Bytes returns the bytes representing the Account, in a way that each BigInt is represented by 32 bytes, in spite of the BigInt could be represented in less bytes (due a small big.Int), so in this way each BigInt is always 32 bytes and can be automatically parsed from a byte array.
 37func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
 38	var b [32 * NLEAFELEMS]byte
 39
 40	if a.Nonce > 0xffffffffff {
 41		return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
 42	}
 43	if len(a.Balance.Bytes()) > 24 {
 44		return b, fmt.Errorf("%s Balance", ErrNumOverflow)
 45	}
 46
 47	var nonceBytes [8]byte
 48	binary.LittleEndian.PutUint64(nonceBytes[:], a.Nonce)
 49
 50	copy(b[0:4], a.TokenID.Bytes())
 51	copy(b[4:9], nonceBytes[:])
 52	if babyjub.PointCoordSign(a.PublicKey.X) {
 53		b[10] = 1
 54	}
 55	copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
 56	copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
 57	copy(b[96:116], a.EthAddr.Bytes())
 58
 59	return b, nil
 60}
 61
 62// BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
 63func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
 64	e := [NLEAFELEMS]*big.Int{}
 65
 66	b, err := a.Bytes()
 67	if err != nil {
 68		return e, err
 69	}
 70
 71	e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
 72	e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
 73	e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
 74	e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
 75
 76	return e, nil
 77}
 78
 79// HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
 80func (a *Account) HashValue() (*big.Int, error) {
 81	b0 := big.NewInt(0)
 82	toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
 83	lBI, err := a.BigInts()
 84	if err != nil {
 85		return nil, err
 86	}
 87	copy(toHash[:], lBI[:])
 88
 89	v, err := poseidon.Hash(toHash)
 90	return v, err
 91}
 92
 93// AccountFromBigInts returns a Account from a [5]*big.Int
 94func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
 95	if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
 96		return nil, ErrNotInFF
 97	}
 98	var b [32 * NLEAFELEMS]byte
 99	copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
100	copy(b[32:64], SwapEndianness(e[1].Bytes()))
101	copy(b[64:96], SwapEndianness(e[2].Bytes()))
102	copy(b[96:128], SwapEndianness(e[3].Bytes()))
103
104	return AccountFromBytes(b)
105}
106
107// AccountFromBytes returns a Account from a byte array
108func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
109	tokenID := binary.LittleEndian.Uint32(b[0:4])
110	var nonceBytes [8]byte
111	copy(nonceBytes[:], b[4:9])
112	nonce := binary.LittleEndian.Uint64(nonceBytes[:])
113	sign := b[10] == 1
114	balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
115	if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
116		return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
117	}
118	ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
119	pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
120	if err != nil {
121		return nil, err
122	}
123	publicKey := babyjub.PublicKey(*pkPoint)
124	ethAddr := eth.BytesToAddress(b[96:116])
125
126	if !cryptoUtils.CheckBigIntInField(balance) {
127		return nil, ErrNotInFF
128	}
129	if !cryptoUtils.CheckBigIntInField(ay) {
130		return nil, ErrNotInFF
131	}
132
133	a := Account{
134		TokenID:   TokenID(tokenID),
135		Nonce:     nonce,
136		Balance:   balance,
137		PublicKey: &publicKey,
138		EthAddr:   ethAddr,
139	}
140	return &a, nil
141}