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

chore::> Change input type of SignInvokeTransaction, SignDeployAccountTransaction, and SignDeclareTransaction #591

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 13 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
110 changes: 58 additions & 52 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package account
import (
"context"
"errors"
"fmt"
"time"

"github.com/NethermindEth/juno/core/crypto"
Expand All @@ -29,12 +30,12 @@ var (
//go:generate mockgen -destination=../mocks/mock_account.go -package=mocks -source=account.go AccountInterface
type AccountInterface interface {
Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt, error)
TransactionHashInvoke(invokeTxn rpc.InvokeTxnType) (*felt.Felt, error)
TransactionHashDeployAccount(tx rpc.DeployAccountType, contractAddress *felt.Felt) (*felt.Felt, error)
TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error)
SignInvokeTransaction(ctx context.Context, tx *rpc.InvokeTxnV1) error
SignDeployAccountTransaction(ctx context.Context, tx *rpc.DeployAccountTxn, precomputeAddress *felt.Felt) error
SignDeclareTransaction(ctx context.Context, tx *rpc.DeclareTxnV2) error
TransactionHashInvoke(invokeTxn rpc.BroadcastInvokeTxnType) (*felt.Felt, error)
TransactionHashDeployAccount(tx rpc.BroadcastAddDeployTxnType, contractAddress *felt.Felt) (*felt.Felt, error)
TransactionHashDeclare(tx rpc.BroadcastDeclareTxnType) (*felt.Felt, error)
SignInvokeTransaction(ctx context.Context, tx rpc.BroadcastInvokeTxnType) error
SignDeployAccountTransaction(ctx context.Context, tx rpc.BroadcastAddDeployTxnType, precomputeAddress *felt.Felt) error
SignDeclareTransaction(ctx context.Context, tx rpc.BroadcastDeclareTxnType) error
PrecomputeAccountAddress(salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error)
WaitForTransactionReceipt(ctx context.Context, transactionHash *felt.Felt, pollInterval time.Duration) (*rpc.TransactionReceiptWithBlockInfo, error)
}
Expand Down Expand Up @@ -108,17 +109,27 @@ func (account *Account) Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt,
// - invokeTx: the InvokeTxnV1 struct representing the transaction to be invoked.
// Returns:
// - error: an error if there was an error in the signing or invoking process
func (account *Account) SignInvokeTransaction(ctx context.Context, invokeTx *rpc.InvokeTxnV1) error {
func (account *Account) SignInvokeTransaction(ctx context.Context, invokeTx rpc.BroadcastInvokeTxnType) error {

txHash, err := account.TransactionHashInvoke(*invokeTx)
txHash, err := account.TransactionHashInvoke(invokeTx)
if err != nil {
return err
}
signature, err := account.Sign(ctx, txHash)
if err != nil {
return err
}
invokeTx.Signature = signature

switch tx := (invokeTx).(type) {
case rpc.BroadcastInvokev0Txn:
tx.Signature = signature
case rpc.BroadcastInvokev1Txn:
tx.Signature = signature
case rpc.BroadcastInvokev3Txn:
tx.Signature = signature
default:
return fmt.Errorf("unsupported (invoke) transaction type: %v", tx)
}
return nil
}

Expand All @@ -130,17 +141,25 @@ func (account *Account) SignInvokeTransaction(ctx context.Context, invokeTx *rpc
// - precomputeAddress: the precomputed address for the transaction
// Returns:
// - error: an error if any
func (account *Account) SignDeployAccountTransaction(ctx context.Context, tx *rpc.DeployAccountTxn, precomputeAddress *felt.Felt) error {
func (account *Account) SignDeployAccountTransaction(ctx context.Context, deployAccountTx rpc.BroadcastAddDeployTxnType, precomputeAddress *felt.Felt) error {

hash, err := account.TransactionHashDeployAccount(*tx, precomputeAddress)
hash, err := account.TransactionHashDeployAccount(deployAccountTx, precomputeAddress)
if err != nil {
return err
}
signature, err := account.Sign(ctx, hash)
if err != nil {
return err
}
tx.Signature = signature

switch tx := (deployAccountTx).(type) {
case rpc.BroadcastDeployAccountTxn:
tx.Signature = signature
case rpc.BroadcastDeployAccountTxnV3:
tx.Signature = signature
default:
return fmt.Errorf("unsupported (deploy account) transaction type: %v", tx)
}
return nil
}

Expand All @@ -151,17 +170,27 @@ func (account *Account) SignDeployAccountTransaction(ctx context.Context, tx *rp
// - tx: the *rpc.DeclareTxnV2
// Returns:
// - error: an error if any
func (account *Account) SignDeclareTransaction(ctx context.Context, tx *rpc.DeclareTxnV2) error {
func (account *Account) SignDeclareTransaction(ctx context.Context, declareTx rpc.BroadcastDeclareTxnType) error {

hash, err := account.TransactionHashDeclare(*tx)
hash, err := account.TransactionHashDeclare(declareTx)
if err != nil {
return err
}
signature, err := account.Sign(ctx, hash)
if err != nil {
return err
}
tx.Signature = signature

switch tx := (declareTx).(type) {
case rpc.BroadcastDeclareTxnV1:
tx.Signature = signature
case rpc.BroadcastDeclareTxnV2:
tx.Signature = signature
case rpc.BroadcastDeclareTxnV3:
tx.Signature = signature
default:
return fmt.Errorf("unsupported (declare) transaction type: %v", tx)
}
return nil
}

Expand All @@ -173,11 +202,11 @@ func (account *Account) SignDeclareTransaction(ctx context.Context, tx *rpc.Decl
// Returns:
// - *felt.Felt: the calculated transaction hash
// - error: an error if any
func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, contractAddress *felt.Felt) (*felt.Felt, error) {
func (account *Account) TransactionHashDeployAccount(tx rpc.BroadcastAddDeployTxnType, contractAddress *felt.Felt) (*felt.Felt, error) {

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_transaction
switch txn := tx.(type) {
case rpc.DeployAccountTxn:
case rpc.BroadcastDeployAccountTxn:
calldata := []*felt.Felt{txn.ClassHash, txn.ContractAddressSalt}
calldata = append(calldata, txn.ConstructorCalldata...)
calldataHash, err := hash.ComputeHashOnElementsFelt(calldata)
Expand All @@ -201,7 +230,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
case rpc.DeployAccountTxnV3:
case rpc.BroadcastDeployAccountTxnV3:
if txn.Version == "" || txn.ResourceBounds == (rpc.ResourceBoundsMapping{}) || txn.Nonce == nil || txn.PayMasterData == nil {
return nil, ErrNotAllParametersSet
}
Expand Down Expand Up @@ -256,11 +285,11 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
// - error: an error, if any

// If the transaction type is unsupported, the function returns an error.
func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt, error) {
func (account *Account) TransactionHashInvoke(tx rpc.BroadcastInvokeTxnType) (*felt.Felt, error) {

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#v0_hash_calculation
switch txn := tx.(type) {
case rpc.InvokeTxnV0:
case rpc.BroadcastInvokev0Txn:
if txn.Version == "" || len(txn.Calldata) == 0 || txn.MaxFee == nil || txn.EntryPointSelector == nil {
return nil, ErrNotAllParametersSet
}
Expand All @@ -285,7 +314,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
[]*felt.Felt{},
)

case rpc.InvokeTxnV1:
case rpc.BroadcastInvokev1Txn:
if txn.Version == "" || len(txn.Calldata) == 0 || txn.Nonce == nil || txn.MaxFee == nil || txn.SenderAddress == nil {
return nil, ErrNotAllParametersSet
}
Expand All @@ -308,7 +337,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
case rpc.InvokeTxnV3:
case rpc.BroadcastInvokev3Txn:
// https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-8.md#protocol-changes
if txn.Version == "" || txn.ResourceBounds == (rpc.ResourceBoundsMapping{}) || len(txn.Calldata) == 0 || txn.Nonce == nil || txn.SenderAddress == nil || txn.PayMasterData == nil || txn.AccountDeploymentData == nil {
return nil, ErrNotAllParametersSet
Expand Down Expand Up @@ -387,42 +416,19 @@ func dataAvailabilityMode(feeDAMode, nonceDAMode rpc.DataAvailabilityMode) (uint
// - error: an error, if any
//
// If the `tx` parameter is not one of the supported types, the function returns an error `ErrTxnTypeUnSupported`.
func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error) {
func (account *Account) TransactionHashDeclare(tx rpc.BroadcastDeclareTxnType) (*felt.Felt, error) {

switch txn := tx.(type) {
case rpc.DeclareTxnV0:
// Due to inconsistencies in version 0 hash calculation we don't calculate the hash
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why did you do that? The description says about the V0 and you removed the V0 and applied the comment to the V1, you also removed the V1 hash calculation. Let's put them back

case rpc.BroadcastDeclareTxnV1:
// Due to inconsistencies in version 1 hash calculation we don't calculate the hash
return nil, ErrTxnVersionUnSupported
case rpc.DeclareTxnV1:
if txn.SenderAddress == nil || txn.Version == "" || txn.ClassHash == nil || txn.MaxFee == nil || txn.Nonce == nil {
return nil, ErrNotAllParametersSet
}

calldataHash, err := hash.ComputeHashOnElementsFelt([]*felt.Felt{txn.ClassHash})
if err != nil {
return nil, err
}

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
return nil, err
}
return hash.CalculateTransactionHashCommon(
PREFIX_DECLARE,
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
calldataHash,
txn.MaxFee,
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
case rpc.DeclareTxnV2:
if txn.CompiledClassHash == nil || txn.SenderAddress == nil || txn.Version == "" || txn.ClassHash == nil || txn.MaxFee == nil || txn.Nonce == nil {
case rpc.BroadcastDeclareTxnV2:
if txn.CompiledClassHash == nil || txn.SenderAddress == nil || txn.Version == "" || txn.MaxFee == nil || txn.Nonce == nil {
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
return nil, ErrNotAllParametersSet
}

calldataHash, err := hash.ComputeHashOnElementsFelt([]*felt.Felt{txn.ClassHash})
calldataHash, err := hash.ComputeHashOnElementsFelt([]*felt.Felt{txn.CompiledClassHash})
PsychoPunkSage marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
Expand All @@ -441,7 +447,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
account.ChainId,
[]*felt.Felt{txn.Nonce, txn.CompiledClassHash},
)
case rpc.DeclareTxnV3:
case rpc.BroadcastDeclareTxnV3:
// https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-8.md#protocol-changes
if txn.Version == "" || txn.ResourceBounds == (rpc.ResourceBoundsMapping{}) || txn.Nonce == nil || txn.SenderAddress == nil || txn.PayMasterData == nil || txn.AccountDeploymentData == nil ||
txn.ClassHash == nil || txn.CompiledClassHash == nil {
Expand Down
Loading
Loading