signer: enable signing of blob transactions

This commit is contained in:
Martin Holst Swende 2024-02-12 15:34:27 +01:00
parent 91fa8dfdac
commit a39e697b52
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
6 changed files with 137 additions and 6 deletions

View file

@ -1869,6 +1869,8 @@ type SignTransactionResult struct {
// The node needs to have the private key of the account corresponding with // The node needs to have the private key of the account corresponding with
// the given from address and it needs to be unlocked. // the given from address and it needs to be unlocked.
func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) { func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionArgs) (*SignTransactionResult, error) {
args.blobSidecarAllowed = true
if args.Gas == nil { if args.Gas == nil {
return nil, errors.New("gas not specified") return nil, errors.New("gas not specified")
} }

View file

@ -590,7 +590,10 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args apitypes.SendTxA
return nil, err return nil, err
} }
// Convert fields into a real transaction // Convert fields into a real transaction
var unsignedTx = result.Transaction.ToTransaction() unsignedTx, err := result.Transaction.ToTransaction()
if err != nil {
return nil, err
}
// Get the password for the transaction // Get the password for the transaction
pw, err := api.lookupOrQueryPassword(acc.Address, "Account password", pw, err := api.lookupOrQueryPassword(acc.Address, "Account password",
fmt.Sprintf("Please enter the password for account %s", acc.Address.String())) fmt.Sprintf("Please enter the password for account %s", acc.Address.String()))

View file

@ -28,6 +28,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"crypto/sha256"
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -131,14 +132,16 @@ func (args *SendTxArgs) data() []byte {
} }
// ToTransaction converts the arguments to a transaction. // ToTransaction converts the arguments to a transaction.
func (args *SendTxArgs) ToTransaction() *types.Transaction { func (args *SendTxArgs) ToTransaction() (*types.Transaction, error) {
// Add the To-field, if specified // Add the To-field, if specified
var to *common.Address var to *common.Address
if args.To != nil { if args.To != nil {
dstAddr := args.To.Address() dstAddr := args.To.Address()
to = &dstAddr to = &dstAddr
} }
if err := args.validateTxSidecar(); err != nil {
return nil, err
}
var data types.TxData var data types.TxData
switch { switch {
case args.BlobHashes != nil: case args.BlobHashes != nil:
@ -204,7 +207,78 @@ func (args *SendTxArgs) ToTransaction() *types.Transaction {
Data: args.data(), Data: args.data(),
} }
} }
return types.NewTx(data)
return types.NewTx(data), nil
}
// validateTxSidecar validates blob data, if present
func (args *SendTxArgs) validateTxSidecar() error {
// No blobs, we're done.
if args.Blobs == nil {
return nil
}
n := len(args.Blobs)
// Assume user provides either only blobs (w/o hashes), or
// blobs together with commitments and proofs.
if args.Commitments == nil && args.Proofs != nil {
return errors.New(`blob proofs provided while commitments were not`)
} else if args.Commitments != nil && args.Proofs == nil {
return errors.New(`blob commitments provided while proofs were not`)
}
// len(blobs) == len(commitments) == len(proofs) == len(hashes)
if args.Commitments != nil && len(args.Commitments) != n {
return fmt.Errorf("number of blobs and commitments mismatch (have=%d, want=%d)", len(args.Commitments), n)
}
if args.Proofs != nil && len(args.Proofs) != n {
return fmt.Errorf("number of blobs and proofs mismatch (have=%d, want=%d)", len(args.Proofs), n)
}
if args.BlobHashes != nil && len(args.BlobHashes) != n {
return fmt.Errorf("number of blobs and hashes mismatch (have=%d, want=%d)", len(args.BlobHashes), n)
}
if args.Commitments == nil {
// Generate commitment and proof.
commitments := make([]kzg4844.Commitment, n)
proofs := make([]kzg4844.Proof, n)
for i, b := range args.Blobs {
c, err := kzg4844.BlobToCommitment(b)
if err != nil {
return fmt.Errorf("blobs[%d]: error computing commitment: %v", i, err)
}
commitments[i] = c
p, err := kzg4844.ComputeBlobProof(b, c)
if err != nil {
return fmt.Errorf("blobs[%d]: error computing proof: %v", i, err)
}
proofs[i] = p
}
args.Commitments = commitments
args.Proofs = proofs
} else {
for i, b := range args.Blobs {
if err := kzg4844.VerifyBlobProof(b, args.Commitments[i], args.Proofs[i]); err != nil {
return fmt.Errorf("failed to verify blob proof: %v", err)
}
}
}
hashes := make([]common.Hash, n)
hasher := sha256.New()
for i, c := range args.Commitments {
hashes[i] = kzg4844.CalcBlobHashV1(hasher, &c)
}
if args.BlobHashes != nil {
for i, h := range hashes {
if h != args.BlobHashes[i] {
return fmt.Errorf("blob hash verification failed (have=%s, want=%s)", args.BlobHashes[i], h)
}
}
} else {
args.BlobHashes = hashes
}
return nil
} }
type SigFormat struct { type SigFormat struct {

View file

@ -20,8 +20,11 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"crypto/sha256"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/holiman/uint256"
) )
func TestIsPrimitive(t *testing.T) { func TestIsPrimitive(t *testing.T) {
@ -83,7 +86,9 @@ func TestTxArgs(t *testing.T) {
if err := json.Unmarshal(d2, &txArgs2); err != nil { if err := json.Unmarshal(d2, &txArgs2); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if have, want := txArgs.ToTransaction().Hash(), txArgs2.ToTransaction().Hash(); have != want { tx1, _ := txArgs.ToTransaction()
tx2, _ := txArgs2.ToTransaction()
if have, want := tx1.Hash(), tx2.Hash(); have != want {
t.Errorf("test %d: have %v, want %v", i, have, want) t.Errorf("test %d: have %v, want %v", i, have, want)
} }
} }
@ -98,3 +103,39 @@ func TestTxArgs(t *testing.T) {
> eth.signTransaction(tx) > eth.signTransaction(tx)
*/ */
} }
func TestBlobTxs(t *testing.T) {
blob := kzg4844.Blob{0x1}
committment, err := kzg4844.BlobToCommitment(blob)
if err != nil {
t.Fatal(err)
}
proof, err := kzg4844.ComputeBlobProof(blob, committment)
if err != nil {
t.Fatal(err)
}
hash := kzg4844.CalcBlobHashV1(sha256.New(), &committment)
b := &types.BlobTx{
ChainID: uint256.NewInt(6),
Nonce: 8,
GasTipCap: uint256.NewInt(500),
GasFeeCap: uint256.NewInt(600),
Gas: 21000,
BlobFeeCap: uint256.NewInt(700),
BlobHashes: []common.Hash{hash},
Value: uint256.NewInt(100),
Sidecar: &types.BlobTxSidecar{
Blobs: []kzg4844.Blob{blob},
Commitments: []kzg4844.Commitment{committment},
Proofs: []kzg4844.Proof{proof},
},
}
tx := types.NewTx(b)
data, err := json.Marshal(tx)
if err != nil {
t.Fatal(err)
}
t.Logf("tx %v", string(data))
}

View file

@ -128,7 +128,7 @@ func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, erro
fmt.Printf("chainid: %v\n", chainId) fmt.Printf("chainid: %v\n", chainId)
} }
if list := request.Transaction.AccessList; list != nil { if list := request.Transaction.AccessList; list != nil {
fmt.Printf("Accesslist\n") fmt.Printf("Accesslist:\n")
for i, el := range *list { for i, el := range *list {
fmt.Printf(" %d. %v\n", i, el.Address) fmt.Printf(" %d. %v\n", i, el.Address)
for j, slot := range el.StorageKeys { for j, slot := range el.StorageKeys {
@ -136,6 +136,12 @@ func (ui *CommandlineUI) ApproveTx(request *SignTxRequest) (SignTxResponse, erro
} }
} }
} }
if len(request.Transaction.BlobHashes) > 0 {
fmt.Printf("Blob hashes:\n")
for _, bh := range request.Transaction.BlobHashes {
fmt.Printf(" %v\n", bh)
}
}
if request.Transaction.Data != nil { if request.Transaction.Data != nil {
d := *request.Transaction.Data d := *request.Transaction.Data
if len(d) > 0 { if len(d) > 0 {

View file

@ -36,6 +36,11 @@ func (db *Database) ValidateTransaction(selector *string, tx *apitypes.SendTxArg
if tx.Data != nil && tx.Input != nil && !bytes.Equal(*tx.Data, *tx.Input) { if tx.Data != nil && tx.Input != nil && !bytes.Equal(*tx.Data, *tx.Input) {
return nil, errors.New(`ambiguous request: both "data" and "input" are set and are not identical`) return nil, errors.New(`ambiguous request: both "data" and "input" are set and are not identical`)
} }
// ToTransaction validates, among other things, that blob hashes match with blobs, and also
// populates the hashes if they were previously unset.
if _, err := tx.ToTransaction(); err != nil {
return nil, err
}
// Place data on 'data', and nil 'input' // Place data on 'data', and nil 'input'
var data []byte var data []byte
if tx.Input != nil { if tx.Input != nil {