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