Skip to content

Commit

Permalink
TransactionHashInvoke support for v0 + found reference
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 22, 2023
1 parent f1f12e6 commit de96344
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func NewAccount(provider rpc.RpcProvider, version uint64, accountAddress *felt.F
// TransactionHash2 requires the callData to be compiled beforehand
func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt, error) {

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation
switch txn := tx.(type) {
case rpc.InvokeTxnV0:
if txn.Version == "" || len(txn.Calldata) == 0 || txn.MaxFee == nil {
if txn.Version == "" || len(txn.Calldata) == 0 || txn.MaxFee == nil || txn.EntryPointSelector == nil {
return nil, ErrNotAllParametersSet
}

Expand All @@ -89,25 +90,29 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
new(felt.Felt).SetBytes([]byte(TRANSACTION_PREFIX)),
txnVersionFelt,
txn.ContractAddress,
&felt.Zero,
txn.EntryPointSelector,
calldataHash,
txn.MaxFee,
account.ChainId,
[]*felt.Felt{},
)

case rpc.InvokeTxnV1:
if len(txn.Calldata) == 0 || txn.Nonce == nil || txn.MaxFee == nil {
if len(txn.Calldata) == 0 || txn.Nonce == nil || txn.MaxFee == nil || txn.SenderAddress == nil {
return nil, ErrNotAllParametersSet
}

calldataHash, err := computeHashOnElementsFelt(txn.Calldata)
if err != nil {
return nil, err
}
txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
return nil, err
}
return calculateTransactionHashCommon(
new(felt.Felt).SetBytes([]byte(TRANSACTION_PREFIX)),
new(felt.Felt).SetUint64(account.version),
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
calldataHash,
Expand Down Expand Up @@ -193,6 +198,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountTxn, co
return nil, err
}

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation
return calculateTransactionHashCommon(
Prefix_DEPLOY_ACCOUNT,
versionFelt,
Expand Down
6 changes: 4 additions & 2 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ func TestTransactionHashInvoke(t *testing.T) {
Calldata: []*felt.Felt{&felt.Zero},
},
TxDetails: rpc.TxDetails{
Nonce: &felt.Zero,
MaxFee: &felt.Zero,
Nonce: &felt.Zero,
MaxFee: &felt.Zero,
Version: rpc.TransactionV1,
},
},
{
Expand Down Expand Up @@ -144,6 +145,7 @@ func TestTransactionHashInvoke(t *testing.T) {
Nonce: test.TxDetails.Nonce,
MaxFee: test.TxDetails.MaxFee,
SenderAddress: account.AccountAddress,
Version: test.TxDetails.Version,
}
hash, err := account.TransactionHashInvoke(invokeTxn)
require.NoError(t, err, "error returned from account.TransactionHash()")
Expand Down

0 comments on commit de96344

Please sign in to comment.