mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Refactor api txs
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
@@ -71,6 +72,19 @@ func (b BigIntStr) Value() (driver.Value, error) {
|
||||
return base64.StdEncoding.EncodeToString(bigInt.Bytes()), nil
|
||||
}
|
||||
|
||||
// StrBigInt is used to unmarshal BigIntStr directly into an alias of big.Int
|
||||
type StrBigInt big.Int
|
||||
|
||||
// UnmarshalText unmarshals a StrBigInt
|
||||
func (s *StrBigInt) UnmarshalText(text []byte) error {
|
||||
bi, ok := (*big.Int)(s).SetString(string(text), 10)
|
||||
if !ok {
|
||||
return fmt.Errorf("could not unmarshal %s into a StrBigInt", text)
|
||||
}
|
||||
*s = StrBigInt(*bi)
|
||||
return nil
|
||||
}
|
||||
|
||||
// CollectedFees is used to retrieve common.batch.CollectedFee from the DB
|
||||
type CollectedFees map[common.TokenID]BigIntStr
|
||||
|
||||
@@ -127,6 +141,20 @@ func (a HezEthAddr) Value() (driver.Value, error) {
|
||||
return ethAddr.Value()
|
||||
}
|
||||
|
||||
// StrHezEthAddr is used to unmarshal HezEthAddr directly into an alias of ethCommon.Address
|
||||
type StrHezEthAddr ethCommon.Address
|
||||
|
||||
// UnmarshalText unmarshals a StrHezEthAddr
|
||||
func (s *StrHezEthAddr) UnmarshalText(text []byte) error {
|
||||
withoutHez := strings.TrimPrefix(string(text), "hez:")
|
||||
var addr ethCommon.Address
|
||||
if err := addr.UnmarshalText([]byte(withoutHez)); err != nil {
|
||||
return err
|
||||
}
|
||||
*s = StrHezEthAddr(addr)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HezBJJ is used to scan/value *babyjub.PublicKey directly into strings that follow the BJJ public key hez fotmat (^hez:[A-Za-z0-9_-]{44}$) from/to sql DBs.
|
||||
// It assumes that *babyjub.PublicKey are inserted/fetched to/from the DB using the default Scan/Value interface
|
||||
type HezBJJ string
|
||||
@@ -143,12 +171,11 @@ func NewHezBJJ(bjj *babyjub.PublicKey) HezBJJ {
|
||||
return HezBJJ("hez:" + base64.RawURLEncoding.EncodeToString(bjjSum))
|
||||
}
|
||||
|
||||
// ToBJJ returns a *babyjub.PublicKey created from HezBJJ
|
||||
func (b HezBJJ) ToBJJ() (*babyjub.PublicKey, error) {
|
||||
func hezStrToBJJ(s string) (*babyjub.PublicKey, error) {
|
||||
const decodedLen = 33
|
||||
const encodedLen = 44
|
||||
formatErr := errors.New("invalid BJJ format. Must follow this regex: ^hez:[A-Za-z0-9_-]{44}$")
|
||||
encoded := strings.TrimPrefix(string(b), "hez:")
|
||||
encoded := strings.TrimPrefix(s, "hez:")
|
||||
if len(encoded) != encodedLen {
|
||||
return nil, formatErr
|
||||
}
|
||||
@@ -172,6 +199,11 @@ func (b HezBJJ) ToBJJ() (*babyjub.PublicKey, error) {
|
||||
return bjjComp.Decompress()
|
||||
}
|
||||
|
||||
// ToBJJ returns a *babyjub.PublicKey created from HezBJJ
|
||||
func (b HezBJJ) ToBJJ() (*babyjub.PublicKey, error) {
|
||||
return hezStrToBJJ(string(b))
|
||||
}
|
||||
|
||||
// Scan implements Scanner for database/sql
|
||||
func (b *HezBJJ) Scan(src interface{}) error {
|
||||
bjj := &babyjub.PublicKey{}
|
||||
@@ -193,3 +225,39 @@ func (b HezBJJ) Value() (driver.Value, error) {
|
||||
}
|
||||
return bjj.Value()
|
||||
}
|
||||
|
||||
// StrHezBJJ is used to unmarshal HezBJJ directly into an alias of babyjub.PublicKey
|
||||
type StrHezBJJ babyjub.PublicKey
|
||||
|
||||
// UnmarshalText unmarshals a StrHezBJJ
|
||||
func (s *StrHezBJJ) UnmarshalText(text []byte) error {
|
||||
bjj, err := hezStrToBJJ(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*s = StrHezBJJ(*bjj)
|
||||
return nil
|
||||
}
|
||||
|
||||
// HezIdx is used to value common.Idx directly into strings that follow the Idx key hez fotmat (hez:tokenSymbol:idx) to sql DBs.
|
||||
// Note that this can only be used to insert to DB since there is no way to automaticaly read from the DB since it needs the tokenSymbol
|
||||
type HezIdx string
|
||||
|
||||
// StrHezIdx is used to unmarshal HezIdx directly into an alias of common.Idx
|
||||
type StrHezIdx common.Idx
|
||||
|
||||
// UnmarshalText unmarshals a StrHezIdx
|
||||
func (s *StrHezIdx) UnmarshalText(text []byte) error {
|
||||
withoutHez := strings.TrimPrefix(string(text), "hez:")
|
||||
splitted := strings.Split(withoutHez, ":")
|
||||
const expectedLen = 2
|
||||
if len(splitted) != expectedLen {
|
||||
return fmt.Errorf("can not unmarshal %s into StrHezIdx", text)
|
||||
}
|
||||
idxInt, err := strconv.Atoi(splitted[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*s = StrHezIdx(common.Idx(idxInt))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,12 +2,14 @@ package apitypes
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
_ "github.com/mattn/go-sqlite3" //nolint sqlite driver
|
||||
@@ -123,6 +125,53 @@ func TestBigIntStrScannerValuer(t *testing.T) {
|
||||
assert.Nil(t, toMeddlerNil.I)
|
||||
}
|
||||
|
||||
func TestStrBigInt(t *testing.T) {
|
||||
type testStrBigInt struct {
|
||||
I StrBigInt
|
||||
}
|
||||
from := []byte(`{"I":"4"}`)
|
||||
to := &testStrBigInt{}
|
||||
assert.NoError(t, json.Unmarshal(from, to))
|
||||
assert.Equal(t, big.NewInt(4), (*big.Int)(&to.I))
|
||||
}
|
||||
|
||||
func TestStrHezEthAddr(t *testing.T) {
|
||||
type testStrHezEthAddr struct {
|
||||
I StrHezEthAddr
|
||||
}
|
||||
withoutHez := "0xaa942cfcd25ad4d90a62358b0dd84f33b398262a"
|
||||
from := []byte(`{"I":"hez:` + withoutHez + `"}`)
|
||||
var addr ethCommon.Address
|
||||
if err := addr.UnmarshalText([]byte(withoutHez)); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
to := &testStrHezEthAddr{}
|
||||
assert.NoError(t, json.Unmarshal(from, to))
|
||||
assert.Equal(t, addr, ethCommon.Address(to.I))
|
||||
}
|
||||
|
||||
func TestStrHezBJJ(t *testing.T) {
|
||||
type testStrHezBJJ struct {
|
||||
I StrHezBJJ
|
||||
}
|
||||
priv := babyjub.NewRandPrivKey()
|
||||
hezBjj := NewHezBJJ(priv.Public())
|
||||
from := []byte(`{"I":"` + hezBjj + `"}`)
|
||||
to := &testStrHezBJJ{}
|
||||
assert.NoError(t, json.Unmarshal(from, to))
|
||||
assert.Equal(t, priv.Public(), (*babyjub.PublicKey)(&to.I))
|
||||
}
|
||||
|
||||
func TestStrHezIdx(t *testing.T) {
|
||||
type testStrHezIdx struct {
|
||||
I StrHezIdx
|
||||
}
|
||||
from := []byte(`{"I":"hez:foo:4"}`)
|
||||
to := &testStrHezIdx{}
|
||||
assert.NoError(t, json.Unmarshal(from, to))
|
||||
assert.Equal(t, common.Idx(4), common.Idx(to.I))
|
||||
}
|
||||
|
||||
func TestHezEthAddr(t *testing.T) {
|
||||
// Clean DB
|
||||
_, err := db.Exec("delete from test")
|
||||
|
||||
Reference in New Issue
Block a user