Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Sep 29, 2023
1 parent b66cab6 commit 5855255
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 110 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_account.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
- uses: actions/setup-go[@v4.1.0](https://github.com/v4.1.0)
with:
go-version: 1.18
go-version-file: go.mod

- name: wait for devnet to be up...
run: ./.bin/devnet.sh
Expand Down
30 changes: 10 additions & 20 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,12 @@ func (account *Account) prepFunctionInvokeRPC(ctx context.Context, messageType s
if err != nil {
return nil, err
}
nonceFelt, err := utils.BigIntToFelt(nonce)
if err != nil {
return nil, err
}
s1Felt, err := utils.BigIntToFelt(s1)
if err != nil {
return nil, err
}
s2Felt, err := utils.BigIntToFelt(s2)
if err != nil {
return nil, err
}
nonceFelt := utils.BigIntToFelt(nonce)

s1Felt := utils.BigIntToFelt(s1)

s2Felt := utils.BigIntToFelt(s2)

return &rpc.InvokeTxnV1{
MaxFee: maxFeeFelt,
Version: version,
Expand Down Expand Up @@ -620,14 +614,10 @@ func (account *Account) Deploy(ctx context.Context, classHash string, details ty
if err != nil {
return nil, err
}
saltFelt, err := utils.BigIntToFelt(salt)
if err != nil {
return nil, err
}
uniqueIntFelt, err := utils.BigIntToFelt(uniqueInt)
if err != nil {
return nil, err
}
saltFelt := utils.BigIntToFelt(salt)

uniqueIntFelt := utils.BigIntToFelt(uniqueInt)

lenCalldataFelt, err := utils.HexToFelt(fmt.Sprintf("0x%x", len(calldata)))
if err != nil {
return nil, err
Expand Down
44 changes: 19 additions & 25 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ var (
)

var (
TRANSACTION_PREFIX = new(felt.Felt).SetBytes([]byte("invoke"))
DECLARE_PREFIX = new(felt.Felt).SetBytes([]byte("declare"))
CONTRACT_ADDRESS_PREFIX = new(felt.Felt).SetBytes([]byte("STARKNET_CONTRACT_ADDRESS"))
PREFIX_TRANSACTION = new(felt.Felt).SetBytes([]byte("invoke"))
PREFIX_DECLARE = new(felt.Felt).SetBytes([]byte("declare"))
PREFIX_CONTRACT_ADDRESS = new(felt.Felt).SetBytes([]byte("STARKNET_CONTRACT_ADDRESS"))
PREFIX_DEPLOY_ACCOUNT = new(felt.Felt).SetBytes([]byte("deploy_account"))
)

//go:generate mockgen -destination=../mocks/mock_account.go -package=mocks -source=account.go AccountInterface
Expand Down Expand Up @@ -66,16 +67,14 @@ func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey s

func (account *Account) Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt, error) {

msgBig, ok := utils.FeltToBigInt(msg)
if ok != true {
return nil, ErrFeltToBigInt
}
msgBig := utils.FeltToBigInt(msg)

s1, s2, err := account.ks.Sign(ctx, account.publicKey, msgBig)
if err != nil {
return nil, err
}
s1Felt, _ := utils.BigIntToFelt(s1)
s2Felt, _ := utils.BigIntToFelt(s2)
s1Felt := utils.BigIntToFelt(s1)
s2Felt := utils.BigIntToFelt(s2)

return []*felt.Felt{s1Felt, s2Felt}, nil
}
Expand Down Expand Up @@ -131,9 +130,6 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountTxn, co
if tx.Version != rpc.TransactionV1 {
return nil, ErrTxnTypeUnSupported
}

Prefix_DEPLOY_ACCOUNT := new(felt.Felt).SetBytes([]byte("deploy_account"))

calldata := []*felt.Felt{tx.ClassHash, tx.ContractAddressSalt}
calldata = append(calldata, tx.ConstructorCalldata...)
calldataHash, err := computeHashOnElementsFelt(calldata)
Expand All @@ -148,7 +144,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountTxn, co

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation
return calculateTransactionHashCommon(
Prefix_DEPLOY_ACCOUNT,
PREFIX_DEPLOY_ACCOUNT,
versionFelt,
contractAddress,
&felt.Zero,
Expand All @@ -161,7 +157,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountTxn, co

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
// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#v0_hash_calculation
switch txn := tx.(type) {
case rpc.InvokeTxnV0:
if txn.Version == "" || len(txn.Calldata) == 0 || txn.MaxFee == nil || txn.EntryPointSelector == nil {
Expand All @@ -178,7 +174,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
return nil, err
}
return calculateTransactionHashCommon(
TRANSACTION_PREFIX,
PREFIX_TRANSACTION,
txnVersionFelt,
txn.ContractAddress,
txn.EntryPointSelector,
Expand All @@ -202,7 +198,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
return nil, err
}
return calculateTransactionHashCommon(
TRANSACTION_PREFIX,
PREFIX_TRANSACTION,
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
Expand All @@ -217,8 +213,6 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,

func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error) {

Prefix_DECLARE := new(felt.Felt).SetBytes([]byte("declare"))

switch txn := tx.(type) {
case rpc.DeclareTxnV0:
// Due to inconsistencies in version 0 hash calculation we don't calculate the hash
Expand All @@ -238,7 +232,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
return nil, err
}
return calculateTransactionHashCommon(
Prefix_DECLARE,
PREFIX_DECLARE,
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
Expand All @@ -262,7 +256,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
return nil, err
}
return calculateTransactionHashCommon(
Prefix_DECLARE,
PREFIX_DECLARE,
txnVersionFelt,
txn.SenderAddress,
&felt.Zero,
Expand All @@ -281,7 +275,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
func (account *Account) PrecomputeAddress(deployerAddress *felt.Felt, salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error) {

bigIntArr, err := utils.FeltArrToBigIntArr([]*felt.Felt{
CONTRACT_ADDRESS_PREFIX,
PREFIX_CONTRACT_ADDRESS,
deployerAddress,
salt,
classHash,
Expand All @@ -291,14 +285,14 @@ func (account *Account) PrecomputeAddress(deployerAddress *felt.Felt, salt *felt
}

constructorCalldataBigIntArr, err := utils.FeltArrToBigIntArr(constructorCalldata)
constructorCallDataHashInt, _ := starknetgo.Curve.ComputeHashOnElements(*constructorCalldataBigIntArr)
*bigIntArr = append(*bigIntArr, constructorCallDataHashInt)
constructorCallDataHashInt, _ := starknetgo.Curve.ComputeHashOnElements(constructorCalldataBigIntArr)
bigIntArr = append(bigIntArr, constructorCallDataHashInt)

preBigInt, err := starknetgo.Curve.ComputeHashOnElements(*bigIntArr)
preBigInt, err := starknetgo.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
return utils.BigIntToFelt(preBigInt)
return utils.BigIntToFelt(preBigInt), nil

}

Expand Down
71 changes: 35 additions & 36 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ func TestTransactionHashInvoke(t *testing.T) {
PubKey: "0x043784df59268c02b716e20bf77797bd96c68c2f100b2a634e448c35e3ad363e",
ChainID: "SN_GOERLI",
FnCall: rpc.FunctionCall{
Calldata: []*felt.Felt{
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
utils.TestHexToFelt(t, "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e"),
utils.TestHexToFelt(t, "0x0"),
utils.TestHexToFelt(t, "0x3"),
utils.TestHexToFelt(t, "0x3"),
utils.TestHexToFelt(t, "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x0"),
},
Calldata: utils.TestHexArrToFelt(t, []string{
"0x1",
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e",
"0x0",
"0x3",
"0x3",
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x1",
"0x0",
}),
},
TxDetails: rpc.TxDetails{
Nonce: utils.TestHexToFelt(t, "0x2"),
Expand Down Expand Up @@ -106,15 +106,15 @@ func TestTransactionHashInvoke(t *testing.T) {
AccountAddress: utils.TestHexToFelt(t, "0x59cd166e363be0a921e42dd5cfca0049aedcf2093a707ef90b5c6e46d4555a8"),
ChainID: "SN_MAIN",
FnCall: rpc.FunctionCall{
Calldata: []*felt.Felt{
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x5dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af"),
utils.TestHexToFelt(t, "0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354"),
utils.TestHexToFelt(t, "0x0"),
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x52884ee3f"),
},
Calldata: utils.TestHexArrToFelt(t, []string{
"0x1",
"0x5dbdedc203e92749e2e746e2d40a768d966bd243df04a6b712e222bc040a9af",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x0",
"0x1",
"0x1",
"0x52884ee3f",
}),
},
TxDetails: rpc.TxDetails{
Nonce: utils.TestHexToFelt(t, "0x1"),
Expand Down Expand Up @@ -167,22 +167,21 @@ func TestFmtCallData(t *testing.T) {
FnCall: rpc.FunctionCall{
ContractAddress: utils.TestHexToFelt(t, "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
EntryPointSelector: types.GetSelectorFromNameFelt("transfer"),
Calldata: []*felt.Felt{
utils.TestHexToFelt(t, "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
utils.TestHexToFelt(t, "0x1"),
},
},
ExpectedCallData: []*felt.Felt{
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
utils.TestHexToFelt(t, "0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e"),
utils.TestHexToFelt(t, "0x0"),
utils.TestHexToFelt(t, "0x3"),
utils.TestHexToFelt(t, "0x3"),
utils.TestHexToFelt(t, "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"),
utils.TestHexToFelt(t, "0x1"),
utils.TestHexToFelt(t, "0x0"),
Calldata: utils.TestHexArrToFelt(t, []string{
"0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x1"}),
},
ExpectedCallData: utils.TestHexArrToFelt(t, []string{
"0x1",
"0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x83afd3f4caedc6eebf44246fe54e38c95e3179a5ec9ea81740eca5b482d12e",
"0x0",
"0x3",
"0x3",
"0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"0x1",
"0x0",
}),
},
},
"testnet": {},
Expand Down Expand Up @@ -402,7 +401,7 @@ func TestAddInvoke(t *testing.T) {
require.Equal(t, txHash.String(), test.ExpectedHash.String())

resp, err := acnt.AddInvokeTransaction(context.Background(), test.InvokeTx)
require.Equal(t, err.Error(), test.ExpectedError)
require.Equal(t, err.Error(), test.ExpectedError.Error())
require.Nil(t, resp)
}
}
Expand Down
8 changes: 3 additions & 5 deletions account/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func computeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
if err != nil {
return nil, err
}
hash, err := starknetgo.Curve.ComputeHashOnElements(*bigIntArr)
hash, err := starknetgo.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
return utils.BigIntToFelt(hash)
return utils.BigIntToFelt(hash), nil
}

// calculateTransactionHashCommon [specification] calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
Expand Down Expand Up @@ -61,9 +61,7 @@ func FmtCalldata(fnCalls []rpc.FunctionCall) []*felt.Felt {
}

callData = append(callData, new(felt.Felt).SetUint64(uint64(len(callArray))), new(felt.Felt).SetUint64(uint64(len(tx.Calldata))+1))
for _, cd := range tx.Calldata {
callArray = append(callArray, cd)
}
callArray = append(callArray, tx.Calldata...)
}
callData = append(callData, new(felt.Felt).SetUint64(uint64(len(callArray)+1)))
callData = append(callData, callArray...)
Expand Down
27 changes: 12 additions & 15 deletions utils/Felt.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"errors"
"math/big"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -16,32 +15,30 @@ func HexToFelt(hex string) (*felt.Felt, error) {
}

func HexArrToFelt(hexArr []string) ([]*felt.Felt, error) {
var qwe []*felt.Felt
var feltArr []*felt.Felt
for _, hex := range hexArr {
felt, err := HexToFelt(hex)
if err != nil {
return nil, err
}
qwe = append(qwe, felt)
feltArr = append(feltArr, felt)
}
return qwe, nil
return feltArr, nil
}

func FeltToBigInt(f *felt.Felt) (*big.Int, bool) {
return new(big.Int).SetString(f.String(), 0)
func FeltToBigInt(f *felt.Felt) *big.Int {
tmp := f.Bytes()
return new(big.Int).SetBytes(tmp[:])
}

func BigIntToFelt(big *big.Int) (*felt.Felt, error) {
return new(felt.Felt).SetBytes(big.Bytes()), nil
func BigIntToFelt(big *big.Int) *felt.Felt {
return new(felt.Felt).SetBytes(big.Bytes())
}
func FeltArrToBigIntArr(f []*felt.Felt) (*[]*big.Int, error) {

func FeltArrToBigIntArr(f []*felt.Felt) ([]*big.Int, error) {
var bigArr []*big.Int
for _, felt := range f {
bint, ok := FeltToBigInt(felt)
if !ok {
return nil, errors.New("Failed to convert felt to big.Int")
}
bigArr = append(bigArr, bint)
bigArr = append(bigArr, FeltToBigInt(felt))
}
return &bigArr, nil
return bigArr, nil
}
7 changes: 0 additions & 7 deletions utils/Test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"math/big"
"testing"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -15,12 +14,6 @@ func TestHexToFelt(t testing.TB, hex string) *felt.Felt {
return f
}

func TestBigIntToFelt(t testing.TB, big *big.Int) (*felt.Felt, error) {
t.Helper()
felt, _ := BigIntToFelt(big)
return felt, nil
}

func TestHexArrToFelt(t testing.TB, hexArr []string) []*felt.Felt {
t.Helper()
feltArr, err := HexArrToFelt(hexArr)
Expand Down

0 comments on commit 5855255

Please sign in to comment.