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

Das extra signing key #867

Merged
merged 15 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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 arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,7 @@ func SetUpDataAvailability(
config.KeyConfig,
_seqInboxCaller,
topLevelStorageService,
config.ExtraSignatureCheckingPublicKey,
)
if err != nil {
return nil, nil, err
Expand Down
58 changes: 45 additions & 13 deletions cmd/datool/datool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/base64"
"fmt"
"io/ioutil"
Expand All @@ -15,6 +16,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/offchainlabs/nitro/cmd/genericconf"

"github.com/offchainlabs/nitro/cmd/util"
Expand Down Expand Up @@ -73,17 +75,18 @@ func startClient(args []string) error {
// datool client rpc store

type ClientStoreConfig struct {
URL string `koanf:"url"`
Message string `koanf:"message"`
DASRetentionPeriod time.Duration `koanf:"das-retention-period"`
// TODO ECDSA private key to sign message with
ConfConfig genericconf.ConfConfig `koanf:"conf"`
URL string `koanf:"url"`
Message string `koanf:"message"`
DASRetentionPeriod time.Duration `koanf:"das-retention-period"`
SigningKey string `koanf:"signing-key"`
ConfConfig genericconf.ConfConfig `koanf:"conf"`
}

func parseClientStoreConfig(args []string) (*ClientStoreConfig, error) {
f := flag.NewFlagSet("datool client store", flag.ContinueOnError)
f.String("url", "", "URL of DAS server to connect to.")
f.String("message", "", "Message to send.")
f.String("url", "", "URL of DAS server to connect to")
f.String("message", "", "message to send")
f.String("signing-key", "", "ecdsa private key to sign the message with, treated as a hex string if prefixed with 0x otherise treated as a file; if not specified the message is not signed")
f.Duration("das-retention-period", 24*time.Hour, "The period which DASes are requested to retain the stored batches.")
genericconf.ConfConfigAddOptions("conf", f)

Expand All @@ -110,8 +113,31 @@ func startClientStore(args []string) error {
return err
}

// var privateKey *ecdsa.PrivateKey
var dasClient das.DataAvailabilityService = client
if config.SigningKey != "" {
var privateKey *ecdsa.PrivateKey
if config.SigningKey[:2] == "0x" {
privateKey, err = crypto.HexToECDSA(config.SigningKey[2:])
if err != nil {
return err
}
} else {
privateKey, err = crypto.LoadECDSA(config.SigningKey)
if err != nil {
return err
}
}
signer := das.DasSignerFromPrivateKey(privateKey)

dasClient, err = das.NewStoreSigningDAS(dasClient, signer)
if err != nil {
return err
}
}

ctx := context.Background()
cert, err := client.Store(ctx, []byte(config.Message), uint64(time.Now().Add(config.DASRetentionPeriod).Unix()), []byte{})
cert, err := dasClient.Store(ctx, []byte(config.Message), uint64(time.Now().Add(config.DASRetentionPeriod).Unix()), []byte{})
if err != nil {
return err
}
Expand Down Expand Up @@ -249,11 +275,13 @@ func startRESTClientGetByHash(args []string) error {
type KeyGenConfig struct {
Dir string
ConfConfig genericconf.ConfConfig `koanf:"conf"`
ECDSAMode bool `koanf:"ecdsa"`
}

func parseKeyGenConfig(args []string) (*KeyGenConfig, error) {
f := flag.NewFlagSet("datool keygen", flag.ContinueOnError)
f.String("dir", "", "The directory to generate the keys in")
f.String("dir", "", "the directory to generate the keys in")
f.Bool("ecdsa", false, "generate an ECDSA keypair instead of BLS")
genericconf.ConfConfigAddOptions("conf", f)

k, err := util.BeginCommonParse(f, args)
Expand All @@ -274,9 +302,13 @@ func startKeyGen(args []string) error {
return err
}

_, _, err = das.GenerateAndStoreKeys(config.Dir)
if err != nil {
return err
if !config.ECDSAMode {
_, _, err = das.GenerateAndStoreKeys(config.Dir)
if err != nil {
return err
}
return nil
}
return nil

return das.GenerateAndStoreECDSAKeys(config.Dir)
}
8 changes: 5 additions & 3 deletions das/das.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ type DataAvailabilityConfig struct {
AggregatorConfig AggregatorConfig `koanf:"rpc-aggregator"`
RestfulClientAggregatorConfig RestfulClientAggregatorConfig `koanf:"rest-aggregator"`

L1NodeURL string `koanf:"l1-node-url"`
L1ConnectionAttempts int `koanf:"l1-connection-attempts"`
SequencerInboxAddress string `koanf:"sequencer-inbox-address"`
L1NodeURL string `koanf:"l1-node-url"`
L1ConnectionAttempts int `koanf:"l1-connection-attempts"`
SequencerInboxAddress string `koanf:"sequencer-inbox-address"`
ExtraSignatureCheckingPublicKey string `koanf:"extra-signature-checking-public-key"`

PanicOnError bool `koanf:"panic-on-error"`
DisableSignatureChecking bool `koanf:"disable-signature-checking"`
Expand Down Expand Up @@ -104,6 +105,7 @@ func DataAvailabilityConfigAddOptions(prefix string, f *flag.FlagSet) {
f.String(prefix+".l1-node-url", DefaultDataAvailabilityConfig.L1NodeURL, "URL for L1 node, only used in standalone daserver; when running as part of a node that node's L1 configuration is used")
f.Int(prefix+".l1-connection-attempts", DefaultDataAvailabilityConfig.L1ConnectionAttempts, "layer 1 RPC connection attempts (spaced out at least 1 second per attempt, 0 to retry infinitely), only used in standalone daserver; when running as part of a node that node's L1 configuration is used")
f.String(prefix+".sequencer-inbox-address", DefaultDataAvailabilityConfig.SequencerInboxAddress, "L1 address of SequencerInbox contract")
f.String(prefix+".extra-signature-checking-public-key", DefaultDataAvailabilityConfig.ExtraSignatureCheckingPublicKey, "public key to use to validate Data Availability Store requests in addition to the Sequencer's public key determined using sequencer-inbox-address, can be a file or the hex-encoded public key beginning with 0x; useful for testing")
}

func Serialize(c *arbstate.DataAvailabilityCertificate) []byte {
Expand Down
2 changes: 1 addition & 1 deletion das/dasrpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestRPC(t *testing.T) {
storageService, lifecycleManager, err := das.CreatePersistentStorageService(ctx, &config)
testhelpers.RequireImpl(t, err)
defer lifecycleManager.StopAndWaitUntil(time.Second)
localDas, err := das.NewSignAfterStoreDASWithSeqInboxCaller(ctx, config.KeyConfig, nil, storageService)
localDas, err := das.NewSignAfterStoreDASWithSeqInboxCaller(ctx, config.KeyConfig, nil, storageService, "")
testhelpers.RequireImpl(t, err)
dasServer, err := StartDASRPCServerOnListener(ctx, lis, genericconf.HTTPServerTimeoutConfigDefault, localDas)
defer func() {
Expand Down
116 changes: 116 additions & 0 deletions das/extra_signature_checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

package das

import (
"bytes"
"context"
"encoding/hex"
"errors"
"io/ioutil"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/offchainlabs/nitro/arbstate"
)

type StubSignatureCheckDAS struct {
keyDir string
}

func (s *StubSignatureCheckDAS) Store(ctx context.Context, message []byte, timeout uint64, sig []byte) (*arbstate.DataAvailabilityCertificate, error) {
pubkeyEncoded, err := ioutil.ReadFile(s.keyDir + "/ecdsa.pub")
if err != nil {
return nil, err
}
pubkey, err := hex.DecodeString(string(pubkeyEncoded))
if err != nil {
return nil, err
}

verified := crypto.VerifySignature(pubkey, dasStoreHash(message, timeout), sig[:64])
if !verified {
return nil, errors.New("signature verification failed")
}
return nil, nil
}

func (s *StubSignatureCheckDAS) ExpirationPolicy(ctx context.Context) (arbstate.ExpirationPolicy, error) {
return arbstate.KeepForever, nil
}

func (s *StubSignatureCheckDAS) GetByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
return []byte{}, nil
}

func (s *StubSignatureCheckDAS) HealthCheck(ctx context.Context) error {
return nil
}

func (s *StubSignatureCheckDAS) String() string {
return "StubSignatureCheckDAS"
}

func TestExtraSignatureCheck(t *testing.T) {
keyDir := t.TempDir()
err := GenerateAndStoreECDSAKeys(keyDir)
Require(t, err)

privateKey, err := crypto.LoadECDSA(keyDir + "/ecdsa")
Require(t, err)
signer := DasSignerFromPrivateKey(privateKey)

var da DataAvailabilityService = &StubSignatureCheckDAS{keyDir}
da, err = NewStoreSigningDAS(da, signer)
Require(t, err)

_, err = da.Store(context.Background(), []byte("Hello world"), 1234, []byte{})
Require(t, err)
}

func TestSimpleSignatureCheck(t *testing.T) {
keyDir := t.TempDir()
err := GenerateAndStoreECDSAKeys(keyDir)
Require(t, err)
privateKey, err := crypto.LoadECDSA(keyDir + "/ecdsa")
Require(t, err)

data := []byte("Hello World")
dataHash := crypto.Keccak256(data)
sig, err := crypto.Sign(dataHash, privateKey)
Require(t, err)

pubkeyEncoded, err := ioutil.ReadFile(keyDir + "/ecdsa.pub")
Require(t, err)

pubkey, err := hex.DecodeString(string(pubkeyEncoded))
Require(t, err)

verified := crypto.VerifySignature(pubkey, dataHash, sig[:64])
if !verified {
Fail(t, "Signature not verified")
}
}

func TestEvenSimplerSignatureCheck(t *testing.T) {
privateKey, err := crypto.GenerateKey()
Require(t, err)

data := []byte("Hello World")
dataHash := crypto.Keccak256(data)
sig, err := crypto.Sign(dataHash, privateKey)
Require(t, err)

pubkey, err := crypto.SigToPub(dataHash, sig)
Require(t, err)
if !bytes.Equal(crypto.FromECDSAPub(pubkey), crypto.FromECDSAPub(&privateKey.PublicKey)) {
Fail(t, "Derived pubkey doesn't match pubkey")
}

verified := crypto.VerifySignature(crypto.FromECDSAPub(&privateKey.PublicKey), dataHash, sig[:64])
if !verified {
Fail(t, "Signature not verified")
}
}
16 changes: 16 additions & 0 deletions das/key_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package das
import (
"bytes"
"encoding/base64"
"encoding/hex"
"io/ioutil"
"os"

"github.com/ethereum/go-ethereum/crypto"
"github.com/offchainlabs/nitro/blsSignatures"
)

Expand Down Expand Up @@ -106,3 +108,17 @@ func ReadPrivKeyFromFile(privKeyPath string) (*blsSignatures.PrivateKey, error)
}
return privKey, nil
}

func GenerateAndStoreECDSAKeys(dir string) error {
privateKey, err := crypto.GenerateKey()
if err != nil {
return err
}

err = crypto.SaveECDSA(dir+"/ecdsa", privateKey)
if err != nil {
return err
}
encodedPubKey := hex.EncodeToString(crypto.FromECDSAPub(&privateKey.PublicKey))
return os.WriteFile(dir+"/ecdsa.pub", []byte(encodedPubKey), 0600)
}
Loading