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

wip: ristretto transaction signing #1172

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 29 additions & 25 deletions core/types/external_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/crypto/sr25519"
)

type ExternalTx struct {
Expand All @@ -13,6 +14,7 @@ type ExternalTx struct {
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nilString"` // nil means contract creation
FromPubKey sr25519.PublicKey
Value *big.Int
Data []byte
AccessList AccessList
Expand Down Expand Up @@ -64,11 +66,12 @@ func (p *PendingEtxs) IsValid(hasher TrieHasher) bool {
// copy creates a deep copy of the transaction data and initializes all fields.
func (tx *ExternalTx) copy() TxData {
cpy := &ExternalTx{
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
Sender: tx.Sender,
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
FromPubKey: tx.FromPubKey,
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
Sender: tx.Sender,

// These are copied below.
AccessList: make(AccessList, len(tx.AccessList)),
Expand All @@ -94,29 +97,30 @@ func (tx *ExternalTx) copy() TxData {
}

// accessors for innerTx.
func (tx *ExternalTx) txType() byte { return ExternalTxType }
func (tx *ExternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *ExternalTx) protected() bool { return true }
func (tx *ExternalTx) accessList() AccessList { return tx.AccessList }
func (tx *ExternalTx) data() []byte { return tx.Data }
func (tx *ExternalTx) gas() uint64 { return tx.Gas }
func (tx *ExternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *ExternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *ExternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *ExternalTx) value() *big.Int { return tx.Value }
func (tx *ExternalTx) nonce() uint64 { return tx.Nonce }
func (tx *ExternalTx) to() *common.Address { return tx.To }
func (tx *ExternalTx) etxGasLimit() uint64 { panic("external TX does not have etxGasLimit") }
func (tx *ExternalTx) etxGasPrice() *big.Int { panic("external TX does not have etxGasPrice") }
func (tx *ExternalTx) etxGasTip() *big.Int { panic("external TX does not have etxGasTip") }
func (tx *ExternalTx) etxData() []byte { panic("external TX does not have etxData") }
func (tx *ExternalTx) etxAccessList() AccessList { panic("external TX does not have etxAccessList") }
func (tx *ExternalTx) txType() byte { return ExternalTxType }
func (tx *ExternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *ExternalTx) protected() bool { return true }
func (tx *ExternalTx) accessList() AccessList { return tx.AccessList }
func (tx *ExternalTx) data() []byte { return tx.Data }
func (tx *ExternalTx) gas() uint64 { return tx.Gas }
func (tx *ExternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *ExternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *ExternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *ExternalTx) value() *big.Int { return tx.Value }
func (tx *ExternalTx) nonce() uint64 { return tx.Nonce }
func (tx *ExternalTx) to() *common.Address { return tx.To }
func (tx *ExternalTx) fromPubKey() sr25519.PublicKey { return tx.FromPubKey }
func (tx *ExternalTx) etxGasLimit() uint64 { panic("external TX does not have etxGasLimit") }
func (tx *ExternalTx) etxGasPrice() *big.Int { panic("external TX does not have etxGasPrice") }
func (tx *ExternalTx) etxGasTip() *big.Int { panic("external TX does not have etxGasTip") }
func (tx *ExternalTx) etxData() []byte { panic("external TX does not have etxData") }
func (tx *ExternalTx) etxAccessList() AccessList { panic("external TX does not have etxAccessList") }

func (tx *ExternalTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *ExternalTx) rawSignatureValues() []byte {
// Signature values are ignored for external transactions
return nil, nil, nil
return nil
}

func (tx *ExternalTx) setSignatureValues(chainID, v, r, s *big.Int) {
func (tx *ExternalTx) setSignatureValues(chainID *big.Int, sig []byte) {
// Signature values are ignored for external transactions
}
73 changes: 32 additions & 41 deletions core/types/internal_to_external_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/crypto/sr25519"
)

type InternalToExternalTx struct {
Expand All @@ -28,7 +29,8 @@ type InternalToExternalTx struct {
GasTipCap *big.Int
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nilString"` // nil means contract creation
To *common.Address `rlp:"nilString"` // nil means contract creation
FromPubKey sr25519.PublicKey `rlp:"nilString"`
Value *big.Int
Data []byte // this probably is not applicable
AccessList AccessList // this probably is not applicable
Expand All @@ -40,18 +42,17 @@ type InternalToExternalTx struct {
ETXAccessList AccessList

// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
Signature []byte
}

// copy creates a deep copy of the transaction data and initializes all fields.
func (tx *InternalToExternalTx) copy() TxData {
cpy := &InternalToExternalTx{
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
FromPubKey: tx.FromPubKey,
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
// These are copied below.
AccessList: make(AccessList, len(tx.AccessList)),
Value: new(big.Int),
Expand All @@ -63,9 +64,7 @@ func (tx *InternalToExternalTx) copy() TxData {
ETXGasTip: new(big.Int),
ETXData: common.CopyBytes(tx.ETXData),
ETXAccessList: make(AccessList, len(tx.ETXAccessList)),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
Signature: common.CopyBytes(tx.Signature),
}
copy(cpy.AccessList, tx.AccessList)
copy(cpy.ETXAccessList, tx.ETXAccessList)
Expand All @@ -87,41 +86,33 @@ func (tx *InternalToExternalTx) copy() TxData {
if tx.ETXGasTip != nil {
cpy.ETXGasTip.Set(tx.ETXGasTip)
}
if tx.V != nil {
cpy.V.Set(tx.V)
}
if tx.R != nil {
cpy.R.Set(tx.R)
}
if tx.S != nil {
cpy.S.Set(tx.S)
}
return cpy
}

// accessors for innerTx.
func (tx *InternalToExternalTx) txType() byte { return InternalToExternalTxType }
func (tx *InternalToExternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *InternalToExternalTx) protected() bool { return true }
func (tx *InternalToExternalTx) accessList() AccessList { return tx.AccessList }
func (tx *InternalToExternalTx) data() []byte { return tx.Data }
func (tx *InternalToExternalTx) gas() uint64 { return tx.Gas }
func (tx *InternalToExternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *InternalToExternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *InternalToExternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *InternalToExternalTx) value() *big.Int { return tx.Value }
func (tx *InternalToExternalTx) nonce() uint64 { return tx.Nonce }
func (tx *InternalToExternalTx) to() *common.Address { return tx.To }
func (tx *InternalToExternalTx) etxGasLimit() uint64 { return tx.ETXGasLimit }
func (tx *InternalToExternalTx) etxGasPrice() *big.Int { return tx.ETXGasPrice }
func (tx *InternalToExternalTx) etxGasTip() *big.Int { return tx.ETXGasTip }
func (tx *InternalToExternalTx) etxData() []byte { return tx.ETXData }
func (tx *InternalToExternalTx) etxAccessList() AccessList { return tx.ETXAccessList }
func (tx *InternalToExternalTx) txType() byte { return InternalToExternalTxType }
func (tx *InternalToExternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *InternalToExternalTx) protected() bool { return true }
func (tx *InternalToExternalTx) accessList() AccessList { return tx.AccessList }
func (tx *InternalToExternalTx) data() []byte { return tx.Data }
func (tx *InternalToExternalTx) gas() uint64 { return tx.Gas }
func (tx *InternalToExternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *InternalToExternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *InternalToExternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *InternalToExternalTx) value() *big.Int { return tx.Value }
func (tx *InternalToExternalTx) nonce() uint64 { return tx.Nonce }
func (tx *InternalToExternalTx) to() *common.Address { return tx.To }
func (tx *InternalToExternalTx) fromPubKey() sr25519.PublicKey { return tx.FromPubKey }
func (tx *InternalToExternalTx) etxGasLimit() uint64 { return tx.ETXGasLimit }
func (tx *InternalToExternalTx) etxGasPrice() *big.Int { return tx.ETXGasPrice }
func (tx *InternalToExternalTx) etxGasTip() *big.Int { return tx.ETXGasTip }
func (tx *InternalToExternalTx) etxData() []byte { return tx.ETXData }
func (tx *InternalToExternalTx) etxAccessList() AccessList { return tx.ETXAccessList }

func (tx *InternalToExternalTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
func (tx *InternalToExternalTx) rawSignatureValues() []byte {
return tx.Signature
}

func (tx *InternalToExternalTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
func (tx *InternalToExternalTx) setSignatureValues(chainID *big.Int, sig []byte) {
tx.ChainID, tx.Signature = chainID, sig
}
71 changes: 31 additions & 40 deletions core/types/internal_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math/big"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/crypto/sr25519"
)

type InternalTx struct {
Expand All @@ -29,32 +30,30 @@ type InternalTx struct {
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nilString"` // nil means contract creation
FromPubKey sr25519.PublicKey
Value *big.Int
Data []byte
AccessList AccessList

// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
Signature []byte
}

// copy creates a deep copy of the transaction data and initializes all fields.
func (tx *InternalTx) copy() TxData {
cpy := &InternalTx{
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
FromPubKey: tx.FromPubKey,
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
// These are copied below.
AccessList: make(AccessList, len(tx.AccessList)),
Value: new(big.Int),
ChainID: new(big.Int),
GasTipCap: new(big.Int),
GasFeeCap: new(big.Int),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
Signature: common.CopyBytes(tx.Signature),
}
copy(cpy.AccessList, tx.AccessList)
if tx.Value != nil {
Expand All @@ -69,41 +68,33 @@ func (tx *InternalTx) copy() TxData {
if tx.GasFeeCap != nil {
cpy.GasFeeCap.Set(tx.GasFeeCap)
}
if tx.V != nil {
cpy.V.Set(tx.V)
}
if tx.R != nil {
cpy.R.Set(tx.R)
}
if tx.S != nil {
cpy.S.Set(tx.S)
}
return cpy
}

// accessors for innerTx.
func (tx *InternalTx) txType() byte { return InternalTxType }
func (tx *InternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *InternalTx) protected() bool { return true }
func (tx *InternalTx) accessList() AccessList { return tx.AccessList }
func (tx *InternalTx) data() []byte { return tx.Data }
func (tx *InternalTx) gas() uint64 { return tx.Gas }
func (tx *InternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *InternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *InternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *InternalTx) value() *big.Int { return tx.Value }
func (tx *InternalTx) nonce() uint64 { return tx.Nonce }
func (tx *InternalTx) to() *common.Address { return tx.To }
func (tx *InternalTx) etxGasLimit() uint64 { panic("internal TX does not have etxGasLimit") }
func (tx *InternalTx) etxGasPrice() *big.Int { panic("internal TX does not have etxGasPrice") }
func (tx *InternalTx) etxGasTip() *big.Int { panic("internal TX does not have etxGasTip") }
func (tx *InternalTx) etxData() []byte { panic("internal TX does not have etxData") }
func (tx *InternalTx) etxAccessList() AccessList { panic("internal TX does not have etxAccessList") }
func (tx *InternalTx) txType() byte { return InternalTxType }
func (tx *InternalTx) chainID() *big.Int { return tx.ChainID }
func (tx *InternalTx) protected() bool { return true }
func (tx *InternalTx) accessList() AccessList { return tx.AccessList }
func (tx *InternalTx) data() []byte { return tx.Data }
func (tx *InternalTx) gas() uint64 { return tx.Gas }
func (tx *InternalTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *InternalTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *InternalTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *InternalTx) value() *big.Int { return tx.Value }
func (tx *InternalTx) nonce() uint64 { return tx.Nonce }
func (tx *InternalTx) to() *common.Address { return tx.To }
func (tx *InternalTx) fromPubKey() sr25519.PublicKey { return tx.FromPubKey }
func (tx *InternalTx) etxGasLimit() uint64 { panic("internal TX does not have etxGasLimit") }
func (tx *InternalTx) etxGasPrice() *big.Int { panic("internal TX does not have etxGasPrice") }
func (tx *InternalTx) etxGasTip() *big.Int { panic("internal TX does not have etxGasTip") }
func (tx *InternalTx) etxData() []byte { panic("internal TX does not have etxData") }
func (tx *InternalTx) etxAccessList() AccessList { panic("internal TX does not have etxAccessList") }

func (tx *InternalTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
func (tx *InternalTx) rawSignatureValues() []byte {
return tx.Signature
}

func (tx *InternalTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
func (tx *InternalTx) setSignatureValues(chainID *big.Int, sig []byte) {
tx.ChainID, tx.Signature = chainID, sig
}
33 changes: 20 additions & 13 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/math"
"github.com/dominant-strategies/go-quai/crypto"
"github.com/dominant-strategies/go-quai/crypto/sr25519"
"github.com/dominant-strategies/go-quai/rlp"
)

Expand Down Expand Up @@ -84,14 +84,15 @@ type TxData interface {
value() *big.Int
nonce() uint64
to() *common.Address
fromPubKey() sr25519.PublicKey
etxGasLimit() uint64
etxGasPrice() *big.Int
etxGasTip() *big.Int
etxData() []byte
etxAccessList() AccessList

rawSignatureValues() (v, r, s *big.Int)
setSignatureValues(chainID, v, r, s *big.Int)
rawSignatureValues() []byte
setSignatureValues(chainID *big.Int, sig []byte)
}

// EncodeRLP implements rlp.Encoder
Expand Down Expand Up @@ -181,10 +182,10 @@ func (tx *Transaction) setDecoded(inner TxData, size int) {
}
}

func sanityCheckSignature(v *big.Int, r *big.Int, s *big.Int) error {
if !crypto.ValidateSignatureValues(byte(v.Uint64()), r, s) {
return ErrInvalidSig
}
func sanityCheckSignature(sig []byte) error {
// if !crypto.ValidateSignatureValues(byte(v.Uint64()), r, s) {
// return ErrInvalidSig
// }
return nil
}

Expand Down Expand Up @@ -276,6 +277,12 @@ func (tx *Transaction) To() *common.Address {
return &cpy
}

// To returns the recipient address of the transaction.
// For contract-creation transactions, To returns nil.
func (tx *Transaction) FromPubKey() sr25519.PublicKey {
return tx.inner.fromPubKey()
}

// Cost returns gas * gasPrice + value.
func (tx *Transaction) Cost() *big.Int {
total := new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.Gas()))
Expand All @@ -285,7 +292,7 @@ func (tx *Transaction) Cost() *big.Int {

// RawSignatureValues returns the V, R, S signature values of the transaction.
// The return values should not be modified by the caller.
func (tx *Transaction) RawSignatureValues() (v, r, s *big.Int) {
func (tx *Transaction) RawSignatureValues() []byte {
return tx.inner.rawSignatureValues()
}

Expand Down Expand Up @@ -409,12 +416,12 @@ func (tx *Transaction) Size() common.StorageSize {
// WithSignature returns a new transaction with the given signature.
// This signature needs to be in the [R || S || V] format where V is 0 or 1.
func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, error) {
r, s, v, err := signer.SignatureValues(tx, sig)
if err != nil {
return nil, err
}
// r, s, v, err := signer.SignatureValues(tx, sig)
// if err != nil {
// return nil, err
// }
cpy := tx.inner.copy()
cpy.setSignatureValues(signer.ChainID(), v, r, s)
cpy.setSignatureValues(signer.ChainID(), sig)
return &Transaction{inner: cpy, time: tx.time}, nil
}

Expand Down
Loading