mv of babyjub.PublicKey to babyjub.PublicKeyComp

Update usage of `*babyjub.PublicKey` to `babyjub.PublicKeyComp`
- when the key is not defined, internally is used `babyjub.EmptyBJJComp`, which is a `[32]byte` of zeroes of type `babyjub.PublicKeyComp`
- the API continues returning `nil` when the key is not defined
This commit is contained in:
arnaucube
2020-12-18 17:19:07 +01:00
parent b9943182b8
commit 4b10549822
47 changed files with 761 additions and 733 deletions

View File

@@ -61,7 +61,7 @@ type receivedAuth struct {
func accountCreationAuthAPIToCommon(apiAuth *receivedAuth) *common.AccountCreationAuth {
return &common.AccountCreationAuth{
EthAddr: ethCommon.Address(apiAuth.EthAddr),
BJJ: (*babyjub.PublicKey)(&apiAuth.BJJ),
BJJ: (babyjub.PublicKeyComp)(apiAuth.BJJ),
Signature: []byte(apiAuth.Signature),
Timestamp: apiAuth.Timestamp,
}

View File

@@ -87,7 +87,7 @@ func parseQueryHezEthAddr(c querier) (*ethCommon.Address, error) {
return hezStringToEthAddr(addrStr, name)
}
func parseQueryBJJ(c querier) (*babyjub.PublicKey, error) {
func parseQueryBJJ(c querier) (*babyjub.PublicKeyComp, error) {
const name = "BJJ"
bjjStr := c.Query(name)
if bjjStr == "" {
@@ -146,7 +146,7 @@ func parseIdx(c querier) (*common.Idx, error) {
return stringToIdx(idxStr, name)
}
func parseExitFilters(c querier) (*common.TokenID, *ethCommon.Address, *babyjub.PublicKey, *common.Idx, error) {
func parseExitFilters(c querier) (*common.TokenID, *ethCommon.Address, *babyjub.PublicKeyComp, *common.Idx, error) {
// TokenID
tid, err := parseQueryUint("tokenId", nil, 0, maxUint32, c)
if err != nil {
@@ -237,7 +237,7 @@ func parseSlotFilters(c querier) (*int64, *int64, *ethCommon.Address, *bool, err
return minSlotNum, maxSlotNum, wonByEthereumAddress, finishedAuction, nil
}
func parseAccountFilters(c querier) ([]common.TokenID, *ethCommon.Address, *babyjub.PublicKey, error) {
func parseAccountFilters(c querier) ([]common.TokenID, *ethCommon.Address, *babyjub.PublicKeyComp, error) {
// TokenID
idsStr := c.Query("tokenIds")
var tokenIDs []common.TokenID
@@ -365,7 +365,7 @@ func hezStringToEthAddr(addrStr, name string) (*ethCommon.Address, error) {
return &addr, tracerr.Wrap(err)
}
func hezStringToBJJ(bjjStr, name string) (*babyjub.PublicKey, error) {
func hezStringToBJJ(bjjStr, name string) (*babyjub.PublicKeyComp, error) {
const decodedLen = 33
splitted := strings.Split(bjjStr, "hez:")
if len(splitted) != 2 || len(splitted[1]) != 44 {
@@ -395,13 +395,7 @@ func hezStringToBJJ(bjjStr, name string) (*babyjub.PublicKey, error) {
name))
}
bjjComp := babyjub.PublicKeyComp(bjjBytes)
bjj, err := bjjComp.Decompress()
if err != nil {
return nil, tracerr.Wrap(fmt.Errorf(
"invalid %s, error decompressing public key: %s",
name, err.Error()))
}
return bjj, nil
return &bjjComp, nil
}
func parseQueryEthAddr(name string, c querier) (*ethCommon.Address, error) {
@@ -436,8 +430,8 @@ type errorMsg struct {
Message string
}
func bjjToString(bjj *babyjub.PublicKey) string {
pkComp := [32]byte(bjj.Compress())
func bjjToString(bjj babyjub.PublicKeyComp) string {
pkComp := [32]byte(bjj)
sum := pkComp[0]
for i := 1; i < len(pkComp); i++ {
sum += pkComp[i]

View File

@@ -214,10 +214,10 @@ func TestParseQueryBJJ(t *testing.T) {
assert.NoError(t, err)
assert.Nil(t, res)
// Correct
c.m[name] = bjjToString(pubK)
c.m[name] = bjjToString(pubK.Compress())
res, err = parseQueryBJJ(c)
assert.NoError(t, err)
assert.Equal(t, *pubK, *res)
assert.Equal(t, pubK.Compress(), *res)
}
func TestParseQueryTxType(t *testing.T) {

View File

@@ -83,7 +83,7 @@ func (tx *receivedPoolTx) toPoolL2TxWrite() *l2db.PoolL2TxWrite {
FromIdx: common.Idx(tx.FromIdx),
ToIdx: (*common.Idx)(tx.ToIdx),
ToEthAddr: (*ethCommon.Address)(tx.ToEthAddr),
ToBJJ: (*babyjub.PublicKey)(tx.ToBJJ),
ToBJJ: (*babyjub.PublicKeyComp)(tx.ToBJJ),
TokenID: tx.TokenID,
Amount: (*big.Int)(&tx.Amount),
AmountFloat: amountF,
@@ -94,7 +94,7 @@ func (tx *receivedPoolTx) toPoolL2TxWrite() *l2db.PoolL2TxWrite {
RqFromIdx: (*common.Idx)(tx.RqFromIdx),
RqToIdx: (*common.Idx)(tx.RqToIdx),
RqToEthAddr: (*ethCommon.Address)(tx.RqToEthAddr),
RqToBJJ: (*babyjub.PublicKey)(tx.RqToBJJ),
RqToBJJ: (*babyjub.PublicKeyComp)(tx.RqToBJJ),
RqTokenID: tx.RqTokenID,
RqAmount: (*big.Int)(tx.RqAmount),
RqFee: tx.RqFee,
@@ -107,14 +107,12 @@ func (a *API) verifyPoolL2TxWrite(txw *l2db.PoolL2TxWrite) error {
poolTx := common.PoolL2Tx{
TxID: txw.TxID,
FromIdx: txw.FromIdx,
ToBJJ: txw.ToBJJ,
TokenID: txw.TokenID,
Amount: txw.Amount,
Fee: txw.Fee,
Nonce: txw.Nonce,
// State: txw.State,
Signature: txw.Signature,
RqToBJJ: txw.RqToBJJ,
RqAmount: txw.RqAmount,
Type: txw.Type,
}
@@ -128,6 +126,12 @@ func (a *API) verifyPoolL2TxWrite(txw *l2db.PoolL2TxWrite) error {
} else {
poolTx.ToEthAddr = *txw.ToEthAddr
}
// ToBJJ
if txw.ToBJJ == nil {
poolTx.ToBJJ = common.EmptyBJJComp
} else {
poolTx.ToBJJ = *txw.ToBJJ
}
// RqFromIdx
if txw.RqFromIdx != nil {
poolTx.RqFromIdx = *txw.RqFromIdx
@@ -142,6 +146,12 @@ func (a *API) verifyPoolL2TxWrite(txw *l2db.PoolL2TxWrite) error {
} else {
poolTx.RqToEthAddr = *txw.RqToEthAddr
}
// RqToBJJ
if txw.RqToBJJ == nil {
poolTx.RqToBJJ = common.EmptyBJJComp
} else {
poolTx.RqToBJJ = *txw.RqToBJJ
}
// RqTokenID
if txw.RqTokenID != nil {
poolTx.RqTokenID = *txw.RqTokenID

View File

@@ -122,7 +122,7 @@ func genTestPoolTxs(
addr := ethAddrToHez(acc.EthAddr)
genReceiveTx.ToEthAddr = &addr
}
if poolTx.ToBJJ != nil {
if poolTx.ToBJJ != common.EmptyBJJComp {
toBJJ := bjjToString(poolTx.ToBJJ)
genSendTx.ToBJJ = &toBJJ
genReceiveTx.ToBJJ = &toBJJ
@@ -151,12 +151,13 @@ func genTestPoolTxs(
genSendTx.RqToEthAddr = &rqToEth
genReceiveTx.RqToEthAddr = &rqToEth
}
if poolTx.RqToBJJ != nil {
if poolTx.RqToBJJ != common.EmptyBJJComp {
rqToBJJ := bjjToString(poolTx.RqToBJJ)
genSendTx.RqToBJJ = &rqToBJJ
genReceiveTx.RqToBJJ = &rqToBJJ
}
}
poolTxsToSend = append(poolTxsToSend, genSendTx)
poolTxsToReceive = append(poolTxsToReceive, genReceiveTx)
}