Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(adr-035): eots manager signing requests #184

Merged
merged 8 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#175](https://github.com/babylonlabs-io/finality-provider/pull/175) adds: `eotsd version` command
* [#179](https://github.com/babylonlabs-io/finality-provider/pull/179) Change `btc_pk` text to `eots_pk` in CLI
* [#182](https://github.com/babylonlabs-io/finality-provider/pull/182) Remove fp manager
* [#184](https://github.com/babylonlabs-io/finality-provider/pull/184) eots manager sign record store

### Bug Fixes

Expand Down
30 changes: 27 additions & 3 deletions eotsmanager/localmanager.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package eotsmanager

import (
"bytes"
"encoding/hex"
"fmt"
"github.com/babylonlabs-io/finality-provider/metrics"
"strings"
"sync"

"github.com/babylonlabs-io/finality-provider/metrics"

"github.com/babylonlabs-io/babylon/crypto/eots"
bbntypes "github.com/babylonlabs-io/babylon/types"
"github.com/btcsuite/btcd/btcec/v2"
Expand Down Expand Up @@ -194,6 +194,20 @@ func (lm *LocalEOTSManager) CreateRandomnessPairList(fpPk []byte, chainID []byte
}

func (lm *LocalEOTSManager) SignEOTS(fpPk []byte, chainID []byte, msg []byte, height uint64, passphrase string) (*btcec.ModNScalar, error) {
record, found, err := lm.es.GetSignRecord(height)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not safe to use data directly from db. Better to have a counter part of the db type to have validated data

if err != nil {
return nil, fmt.Errorf("error getting sign record: %w", err)
} else if found {
if bytes.Equal(msg, record.BlockHash) {
var s btcec.ModNScalar
s.SetByteSlice(record.Signature)

return &s, nil
}

return nil, eotstypes.ErrDoubleSign
}

privRand, _, err := lm.getRandomnessPair(fpPk, chainID, height, passphrase)
if err != nil {
return nil, fmt.Errorf("failed to get private randomness: %w", err)
Expand All @@ -208,7 +222,17 @@ func (lm *LocalEOTSManager) SignEOTS(fpPk []byte, chainID []byte, msg []byte, he
lm.metrics.IncrementEotsFpTotalEotsSignCounter(hex.EncodeToString(fpPk))
lm.metrics.SetEotsFpLastEotsSignHeight(hex.EncodeToString(fpPk), float64(height))

return eots.Sign(privKey, privRand, msg)
signedBytes, err := eots.Sign(privKey, privRand, msg)
if err != nil {
return nil, err
}

b := signedBytes.Bytes()
if err := lm.es.SaveSignRecord(height, msg, fpPk, b[:]); err != nil {
return nil, fmt.Errorf("failed to save signing record: %w", err)
}

return signedBytes, nil
}

func (lm *LocalEOTSManager) SignSchnorrSig(fpPk []byte, msg []byte, passphrase string) (*schnorr.Signature, error) {
Expand Down
45 changes: 45 additions & 0 deletions eotsmanager/localmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,48 @@ func FuzzCreateRandomnessPairList(f *testing.F) {
}
})
}

func FuzzSignRecord(f *testing.F) {
testutil.AddRandomSeedsToFuzzer(f, 10)
f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))

fpName := testutil.GenRandomHexStr(r, 4)
homeDir := filepath.Join(t.TempDir(), "eots-home")
eotsCfg := eotscfg.DefaultConfigWithHomePath(homeDir)
dbBackend, err := eotsCfg.DatabaseConfig.GetDBBackend()
defer func() {
dbBackend.Close()
err := os.RemoveAll(homeDir)
require.NoError(t, err)
}()
require.NoError(t, err)
lm, err := eotsmanager.NewLocalEOTSManager(homeDir, eotsCfg.KeyringBackend, dbBackend, zap.NewNop())
require.NoError(t, err)

fpPk, err := lm.CreateKey(fpName, passphrase, hdPath)
require.NoError(t, err)

chainID := datagen.GenRandomByteArray(r, 10)
startHeight := datagen.RandomInt(r, 100)
num := r.Intn(10) + 1
pubRandList, err := lm.CreateRandomnessPairList(fpPk, chainID, startHeight, uint32(num), passphrase)
require.NoError(t, err)
require.Len(t, pubRandList, num)

msg := datagen.GenRandomByteArray(r, 32)

sig, err := lm.SignEOTS(fpPk, chainID, msg, startHeight, passphrase)
require.NoError(t, err)
require.NotNil(t, sig)

// we expect return from db
sig2, err := lm.SignEOTS(fpPk, chainID, msg, startHeight, passphrase)
require.NoError(t, err)
require.Equal(t, sig, sig2)

// same height diff msg
_, err = lm.SignEOTS(fpPk, chainID, datagen.GenRandomByteArray(r, 32), startHeight, passphrase)
require.ErrorIs(t, err, types.ErrDoubleSign)
})
}
176 changes: 176 additions & 0 deletions eotsmanager/proto/signstore.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions eotsmanager/proto/signstore.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package proto;

option go_package = "github.com/babylonlabs-io/finality-provider/eotsmanager/proto";

// SigningRecord represents a record of a signing operation.
message SigningRecord {
bytes block_hash = 1; // The hash of the block.
bytes public_key = 2; // The public key used for signing.
bytes signature = 3; // The signature of the block.
int64 timestamp = 4; // The timestamp of the signing operation, in Unix seconds.
}
Loading
Loading