mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Add PoolL2Tx verify signature needed methods
Implements the hash to be signed from PoolL2Tx, compatible with javascript version. Implements the signature verification for a given PoolL2Tx.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -73,3 +74,49 @@ func TestTxCompressedData(t *testing.T) {
|
||||
assert.Equal(t, "10c000000000b0000000a0009000000000008000000000007", hex.EncodeToString(txCompressedData.Bytes())[1:])
|
||||
|
||||
}
|
||||
|
||||
func TestHashToSign(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
|
||||
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 2,
|
||||
ToIdx: 3,
|
||||
Amount: big.NewInt(4),
|
||||
TokenID: 5,
|
||||
Nonce: 6,
|
||||
ToBJJ: sk.Public(),
|
||||
RqToEthAddr: ethAddr,
|
||||
RqToBJJ: sk.Public(),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
|
||||
}
|
||||
|
||||
func TestVerifyTxSignature(t *testing.T) {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
assert.Nil(t, err)
|
||||
ethAddr := ethCommon.HexToAddress("0xc58d29fA6e86E4FAe04DDcEd660d45BCf3Cb2370")
|
||||
|
||||
tx := PoolL2Tx{
|
||||
FromIdx: 2,
|
||||
ToIdx: 3,
|
||||
Amount: big.NewInt(4),
|
||||
TokenID: 5,
|
||||
Nonce: 6,
|
||||
ToBJJ: sk.Public(),
|
||||
RqToEthAddr: ethAddr,
|
||||
RqToBJJ: sk.Public(),
|
||||
}
|
||||
toSign, err := tx.HashToSign()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "14526446928649310956370997581245770629723313742905751117262272426489782809503", toSign.String())
|
||||
|
||||
sig := sk.SignPoseidon(toSign)
|
||||
tx.Signature = sig
|
||||
assert.True(t, tx.VerifySignature(sk.Public()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user