Merge pull request #219 from hermeznetwork/feature/api-account-creation

Impl account creation auth endpoints
This commit is contained in:
Eduard S
2020-10-26 11:22:32 +01:00
committed by GitHub
10 changed files with 271 additions and 26 deletions

View File

@@ -1,11 +1,7 @@
package test
import (
"math/big"
"strconv"
"time"
ethCommon "github.com/ethereum/go-ethereum/common"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/hermeznetwork/hermez-node/common"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/jmoiron/sqlx"
@@ -84,13 +80,28 @@ func GenPoolTxs(n int, tokens []common.Token) []*common.PoolL2Tx {
func GenAuths(nAuths int) []*common.AccountCreationAuth {
auths := []*common.AccountCreationAuth{}
for i := 0; i < nAuths; i++ {
privK := babyjub.NewRandPrivKey()
auths = append(auths, &common.AccountCreationAuth{
EthAddr: ethCommon.BigToAddress(big.NewInt(int64(i))),
BJJ: privK.Public(),
Signature: []byte(strconv.Itoa(i)),
Timestamp: time.Now(),
})
// Generate keys
ethPrivK, err := ethCrypto.GenerateKey()
if err != nil {
panic(err)
}
bjjPrivK := babyjub.NewRandPrivKey()
// Generate auth
auth := &common.AccountCreationAuth{
EthAddr: ethCrypto.PubkeyToAddress(ethPrivK.PublicKey),
BJJ: bjjPrivK.Public(),
}
// Sign
h, err := auth.HashToSign()
if err != nil {
panic(err)
}
signature, err := ethCrypto.Sign(h, ethPrivK)
if err != nil {
panic(err)
}
auth.Signature = signature
auths = append(auths, auth)
}
return auths
}

15
test/l2db_test.go Normal file
View File

@@ -0,0 +1,15 @@
package test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenAuths(t *testing.T) {
const nAuths = 5
auths := GenAuths(nAuths)
for _, auth := range auths {
assert.True(t, auth.VerifySignature())
}
}