Refactor account creation auth endpoints

This commit is contained in:
Arnau B
2020-10-29 19:13:14 +01:00
parent 8c6f5f2950
commit 8c9d13ffa5
9 changed files with 316 additions and 165 deletions

View File

@@ -3,6 +3,7 @@ package apitypes
import (
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
@@ -155,6 +156,20 @@ func (s *StrHezEthAddr) UnmarshalText(text []byte) error {
return nil
}
// StrEthSignature is used to unmarshal EthSignature directly into an alias of []byte
type StrEthSignature []byte
// UnmarshalText unmarshals a StrEthSignature
func (s *StrEthSignature) UnmarshalText(text []byte) error {
without0x := strings.TrimPrefix(string(text), "0x")
signature, err := hex.DecodeString(without0x)
if err != nil {
return err
}
*s = signature
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
@@ -261,3 +276,38 @@ func (s *StrHezIdx) UnmarshalText(text []byte) error {
*s = StrHezIdx(common.Idx(idxInt))
return nil
}
// EthSignature is used to scan/value []byte representing an Ethereum signatue directly into strings from/to sql DBs.
type EthSignature string
// NewEthSignature creates a *EthSignature from []byte
// If the provided signature is nil the returned *EthSignature will also be nil
func NewEthSignature(signature []byte) *EthSignature {
if signature == nil {
return nil
}
ethSignature := EthSignature("0x" + hex.EncodeToString(signature))
return &ethSignature
}
// Scan implements Scanner for database/sql
func (e *EthSignature) Scan(src interface{}) error {
if srcStr, ok := src.(string); ok {
// src is a string
*e = *(NewEthSignature([]byte(srcStr)))
return nil
} else if srcBytes, ok := src.([]byte); ok {
// src is []byte
*e = *(NewEthSignature(srcBytes))
return nil
} else {
// unexpected src
return fmt.Errorf("can't scan %T into apitypes.EthSignature", src)
}
}
// Value implements valuer for database/sql
func (e EthSignature) Value() (driver.Value, error) {
without0x := strings.TrimPrefix(string(e), "0x")
return hex.DecodeString(without0x)
}

View File

@@ -332,3 +332,82 @@ func TestHezBJJ(t *testing.T) {
assert.NoError(t, err)
assert.Nil(t, toBJJNil.I)
}
func TestEthSignature(t *testing.T) {
// Clean DB
_, err := db.Exec("delete from test")
assert.NoError(t, err)
// Example structs
type ethSignStruct struct {
I []byte `meddler:"i"`
}
type hezEthSignStruct struct {
I EthSignature `meddler:"i"`
}
type hezEthSignStructNil struct {
I *EthSignature `meddler:"i"`
}
// Not nil case
// Insert into DB using []byte Scan/Value
s := "someRandomFooForYou"
fromEth := ethSignStruct{
I: []byte(s),
}
err = meddler.Insert(db, "test", &fromEth)
assert.NoError(t, err)
// Read from DB using EthSignature Scan/Value
toHezEth := hezEthSignStruct{}
err = meddler.QueryRow(db, &toHezEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, NewEthSignature(fromEth.I), &toHezEth.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using EthSignature Scan/Value
fromHezEth := hezEthSignStruct{
I: *NewEthSignature([]byte(s)),
}
err = meddler.Insert(db, "test", &fromHezEth)
assert.NoError(t, err)
// Read from DB using []byte Scan/Value
toEth := ethSignStruct{}
err = meddler.QueryRow(db, &toEth, "select * from test")
assert.NoError(t, err)
assert.Equal(t, &fromHezEth.I, NewEthSignature(toEth.I))
// Nil case
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using []byte Scan/Value
fromEthNil := ethSignStruct{
I: nil,
}
err = meddler.Insert(db, "test", &fromEthNil)
assert.NoError(t, err)
// Read from DB using EthSignature Scan/Value
foo := EthSignature("foo")
toHezEthNil := hezEthSignStructNil{
I: &foo, // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toHezEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toHezEthNil.I)
// Clean DB
_, err = db.Exec("delete from test")
assert.NoError(t, err)
// Insert into DB using EthSignature Scan/Value
fromHezEthNil := hezEthSignStructNil{
I: nil,
}
err = meddler.Insert(db, "test", &fromHezEthNil)
assert.NoError(t, err)
// Read from DB using []byte Scan/Value
toEthNil := ethSignStruct{
I: []byte(s), // check that this will be set to nil, not because of not being initialized
}
err = meddler.QueryRow(db, &toEthNil, "select * from test")
assert.NoError(t, err)
assert.Nil(t, toEthNil.I)
}