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	ethCommon "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 (
 16	NLEAFELEMS = 4
 17	// maxNonceValue is the maximum value that the Account.Nonce can have (40 bits: maxNonceValue=2**40-1)
 18	maxNonceValue = 0xffffffffff
 19	// maxBalanceBytes is the maximum bytes that can use the Account.Balance *big.Int
 20	maxBalanceBytes = 24
 21)
 22
 23// 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
 24type Account struct {
 25	TokenID   TokenID
 26	Nonce     Nonce    // max of 40 bits used
 27	Balance   *big.Int // max of 192 bits used
 28	PublicKey *babyjub.PublicKey
 29	EthAddr   ethCommon.Address
 30}
 31
 32func (a *Account) String() string {
 33	buf := bytes.NewBufferString("")
 34	fmt.Fprintf(buf, "PublicKey: %s..., ", a.PublicKey.String()[:10])
 35	fmt.Fprintf(buf, "EthAddr: %s..., ", a.EthAddr.String()[:10])
 36	fmt.Fprintf(buf, "TokenID: %v, ", a.TokenID)
 37	fmt.Fprintf(buf, "Nonce: %d, ", a.Nonce)
 38	fmt.Fprintf(buf, "Balance: %s, ", a.Balance.String())
 39	return buf.String()
 40}
 41
 42// 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.
 43func (a *Account) Bytes() ([32 * NLEAFELEMS]byte, error) {
 44	var b [32 * NLEAFELEMS]byte
 45
 46	if a.Nonce > maxNonceValue {
 47		return b, fmt.Errorf("%s Nonce", ErrNumOverflow)
 48	}
 49	if len(a.Balance.Bytes()) > maxBalanceBytes {
 50		return b, fmt.Errorf("%s Balance", ErrNumOverflow)
 51	}
 52
 53	nonceBytes, err := a.Nonce.Bytes()
 54	if err != nil {
 55		return b, err
 56	}
 57
 58	copy(b[0:4], a.TokenID.Bytes())
 59	copy(b[4:9], nonceBytes[:])
 60	if babyjub.PointCoordSign(a.PublicKey.X) {
 61		b[10] = 1
 62	}
 63	copy(b[32:64], SwapEndianness(a.Balance.Bytes())) // SwapEndianness, as big.Int uses BigEndian
 64	copy(b[64:96], SwapEndianness(a.PublicKey.Y.Bytes()))
 65	copy(b[96:116], a.EthAddr.Bytes())
 66
 67	return b, nil
 68}
 69
 70// BigInts returns the [5]*big.Int, where each *big.Int is inside the Finite Field
 71func (a *Account) BigInts() ([NLEAFELEMS]*big.Int, error) {
 72	e := [NLEAFELEMS]*big.Int{}
 73
 74	b, err := a.Bytes()
 75	if err != nil {
 76		return e, err
 77	}
 78
 79	e[0] = new(big.Int).SetBytes(SwapEndianness(b[0:32]))
 80	e[1] = new(big.Int).SetBytes(SwapEndianness(b[32:64]))
 81	e[2] = new(big.Int).SetBytes(SwapEndianness(b[64:96]))
 82	e[3] = new(big.Int).SetBytes(SwapEndianness(b[96:128]))
 83
 84	return e, nil
 85}
 86
 87// HashValue returns the value of the Account, which is the Poseidon hash of its *big.Int representation
 88func (a *Account) HashValue() (*big.Int, error) {
 89	b0 := big.NewInt(0)
 90	toHash := []*big.Int{b0, b0, b0, b0, b0, b0}
 91	lBI, err := a.BigInts()
 92	if err != nil {
 93		return nil, err
 94	}
 95	copy(toHash[:], lBI[:])
 96
 97	v, err := poseidon.Hash(toHash)
 98	return v, err
 99}
100
101// AccountFromBigInts returns a Account from a [5]*big.Int
102func AccountFromBigInts(e [NLEAFELEMS]*big.Int) (*Account, error) {
103	if !cryptoUtils.CheckBigIntArrayInField(e[:]) {
104		return nil, ErrNotInFF
105	}
106	var b [32 * NLEAFELEMS]byte
107	copy(b[0:32], SwapEndianness(e[0].Bytes())) // SwapEndianness, as big.Int uses BigEndian
108	copy(b[32:64], SwapEndianness(e[1].Bytes()))
109	copy(b[64:96], SwapEndianness(e[2].Bytes()))
110	copy(b[96:128], SwapEndianness(e[3].Bytes()))
111
112	return AccountFromBytes(b)
113}
114
115// AccountFromBytes returns a Account from a byte array
116func AccountFromBytes(b [32 * NLEAFELEMS]byte) (*Account, error) {
117	tokenID := binary.LittleEndian.Uint32(b[0:4])
118	var nonceBytes5 [5]byte
119	copy(nonceBytes5[:], b[4:9])
120	nonce := NonceFromBytes(nonceBytes5)
121	sign := b[10] == 1
122	balance := new(big.Int).SetBytes(SwapEndianness(b[32:56])) // b[32:56], as Balance is 192 bits (24 bytes)
123	if !bytes.Equal(b[56:64], []byte{0, 0, 0, 0, 0, 0, 0, 0}) {
124		return nil, fmt.Errorf("%s Balance", ErrNumOverflow)
125	}
126	ay := new(big.Int).SetBytes(SwapEndianness(b[64:96]))
127	pkPoint, err := babyjub.PointFromSignAndY(sign, ay)
128	if err != nil {
129		return nil, err
130	}
131	publicKey := babyjub.PublicKey(*pkPoint)
132	ethAddr := ethCommon.BytesToAddress(b[96:116])
133
134	if !cryptoUtils.CheckBigIntInField(balance) {
135		return nil, ErrNotInFF
136	}
137	if !cryptoUtils.CheckBigIntInField(ay) {
138		return nil, ErrNotInFF
139	}
140
141	a := Account{
142		TokenID:   TokenID(tokenID),
143		Nonce:     nonce,
144		Balance:   balance,
145		PublicKey: &publicKey,
146		EthAddr:   ethAddr,
147	}
148	return &a, nil
149}