Skip to content

Commit

Permalink
Merge branch 'main' into issue-590-4
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev authored Oct 2, 2024
2 parents 35aeb91 + 0ffc264 commit 163ad63
Show file tree
Hide file tree
Showing 81 changed files with 47,282 additions and 42,620 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CodeQL SAST
on:
push:
branches:
- main
pull_request:
schedule:
- cron: "23 9 * * 3"
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: ["go"]

steps:
- name: Checkout repository
uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938 #v4.2.0

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@5618c9fc1e675841ca52c1c6b1304f5255a905a0 #v2.19.0
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@5618c9fc1e675841ca52c1c6b1304f5255a905a0 #v2.19.0

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@5618c9fc1e675841ca52c1c6b1304f5255a905a0 #v2.19.0
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.56.2
version: v1.61
8 changes: 4 additions & 4 deletions .github/workflows/main_ci_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: (main) Auto CI run
on:
pull_request:
branches:
- 'main'
- "main"

jobs:
build_and_test:
Expand Down Expand Up @@ -40,8 +40,7 @@ jobs:

# Test rpc on testnet
- name: Test RPC on testnet
run: echo "Skip for now - need public endpoint that follows rpc spec"
#run: cd rpc && go test -timeout 1200s -v -env testnet .
run: cd rpc && go test -timeout 1200s -v -env testnet .
env:
TESTNET_ACCOUNT_PRIVATE_KEY: ${{ secrets.TESTNET_ACCOUNT_PRIVATE_KEY }}
INTEGRATION_BASE: "https://free-rpc.nethermind.io/sepolia-juno"
Expand All @@ -67,7 +66,8 @@ jobs:

# Build examples
- name: Build examples
run: |
run: |
cd examples/deployAccount && go build
cd ../simpleCall && go build
cd ../simpleInvoke && go build
cd ../deployContractUDC && go build
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ go run main.go

### RPC

`starknet.go` RPC implements the Starknet [RPC v0.7.0 spec](https://github.com/starkware-libs/starknet-specs/tree/v0.7.0-rc2)
`starknet.go` RPC implements the Starknet [RPC v0.7.1 spec](https://github.com/starkware-libs/starknet-specs/tree/v0.7.1)

| Method | Implemented (*) |
| ------------------------------------------ | ------------------ |
Expand Down
62 changes: 21 additions & 41 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/NethermindEth/juno/core/crypto"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/contracts"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/hash"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
Expand Down Expand Up @@ -180,10 +181,7 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
case rpc.DeployAccountTxn:
calldata := []*felt.Felt{txn.ClassHash, txn.ContractAddressSalt}
calldata = append(calldata, txn.ConstructorCalldata...)
calldataHash, err := hash.ComputeHashOnElementsFelt(calldata)
if err != nil {
return nil, err
}
calldataHash := curve.PedersenArray(calldata...)

versionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand All @@ -200,13 +198,11 @@ func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, c
txn.MaxFee,
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
), nil
case rpc.DeployAccountTxnV3:
if txn.Version == "" || txn.ResourceBounds == (rpc.ResourceBoundsMapping{}) || txn.Nonce == nil || txn.PayMasterData == nil {
return nil, ErrNotAllParametersSet
}
calldata := []*felt.Felt{txn.ClassHash, txn.ContractAddressSalt}
calldata = append(calldata, txn.ConstructorCalldata...) //nolint:all

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand Down Expand Up @@ -265,11 +261,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
return nil, ErrNotAllParametersSet
}

calldataHash, err := hash.ComputeHashOnElementsFelt(txn.Calldata)
if err != nil {
return nil, err
}

calldataHash := curve.PedersenArray(txn.Calldata...)
txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
return nil, err
Expand All @@ -283,17 +275,14 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
txn.MaxFee,
account.ChainId,
[]*felt.Felt{},
)
), nil

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

calldataHash, err := hash.ComputeHashOnElementsFelt(txn.Calldata)
if err != nil {
return nil, err
}
calldataHash := curve.PedersenArray(txn.Calldata...)
txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
return nil, err
Expand All @@ -307,7 +296,7 @@ func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt,
txn.MaxFee,
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
), nil
case rpc.InvokeTxnV3:
// 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 {
Expand Down Expand Up @@ -398,10 +387,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
return nil, ErrNotAllParametersSet
}

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

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand All @@ -416,16 +402,13 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
txn.MaxFee,
account.ChainId,
[]*felt.Felt{txn.Nonce},
)
), nil
case rpc.DeclareTxnV2:
if txn.CompiledClassHash == nil || 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
}
calldataHash := curve.PedersenArray(txn.ClassHash)

txnVersionFelt, err := new(felt.Felt).SetString(string(txn.Version))
if err != nil {
Expand All @@ -440,7 +423,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
txn.MaxFee,
account.ChainId,
[]*felt.Felt{txn.Nonce, txn.CompiledClassHash},
)
), nil
case rpc.DeclareTxnV3:
// 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 ||
Expand Down Expand Up @@ -495,10 +478,7 @@ func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Fel
// - *felt.Felt: the precomputed address as a *felt.Felt
// - error: an error if any
func (account *Account) PrecomputeAccountAddress(salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error) {
result, err := contracts.PrecomputeAddress(&felt.Zero, salt, classHash, constructorCalldata)
if err != nil {
return nil, err
}
result := contracts.PrecomputeAddress(&felt.Zero, salt, classHash, constructorCalldata)

return result, nil
}
Expand Down Expand Up @@ -702,9 +682,9 @@ func (account *Account) ClassHashAt(ctx context.Context, blockID rpc.BlockID, co
// - requests: An array of rpc.BroadcastTxn objects representing the requests to estimate the fee for.
// - blockID: The rpc.BlockID object representing the block ID for which to estimate the fee.
// Returns:
// - []rpc.FeeEstimate: An array of rpc.FeeEstimate objects representing the estimated fees.
// - []rpc.FeeEstimation: An array of rpc.FeeEstimation objects representing the estimated fees.
// - error: An error object if any error occurred during the estimation process.
func (account *Account) EstimateFee(ctx context.Context, requests []rpc.BroadcastTxn, simulationFlags []rpc.SimulationFlag, blockID rpc.BlockID) ([]rpc.FeeEstimate, error) {
func (account *Account) EstimateFee(ctx context.Context, requests []rpc.BroadcastTxn, simulationFlags []rpc.SimulationFlag, blockID rpc.BlockID) ([]rpc.FeeEstimation, error) {
return account.provider.EstimateFee(ctx, requests, simulationFlags, blockID)
}

Expand All @@ -715,9 +695,9 @@ func (account *Account) EstimateFee(ctx context.Context, requests []rpc.Broadcas
// - msg: The rpc.MsgFromL1 object representing the message.
// - blockID: The rpc.BlockID object representing the block ID.
// Returns:
// - *rpc.FeeEstimate: a pointer to rpc.FeeEstimate
// - *rpc.FeeEstimation: a pointer to rpc.FeeEstimation
// - error: an error if any.
func (account *Account) EstimateMessageFee(ctx context.Context, msg rpc.MsgFromL1, blockID rpc.BlockID) (*rpc.FeeEstimate, error) {
func (account *Account) EstimateMessageFee(ctx context.Context, msg rpc.MsgFromL1, blockID rpc.BlockID) (*rpc.FeeEstimation, error) {
return account.provider.EstimateMessageFee(ctx, msg, blockID)
}

Expand Down Expand Up @@ -755,7 +735,7 @@ func (account *Account) Nonce(ctx context.Context, blockID rpc.BlockID, contract
// Returns:
// - []rpc.SimulatedTransaction: a list of simulated transactions
// - error: an error, if any.
func (account *Account) SimulateTransactions(ctx context.Context, blockID rpc.BlockID, txns []rpc.Transaction, simulationFlags []rpc.SimulationFlag) ([]rpc.SimulatedTransaction, error) {
func (account *Account) SimulateTransactions(ctx context.Context, blockID rpc.BlockID, txns []rpc.BroadcastTxn, simulationFlags []rpc.SimulationFlag) ([]rpc.SimulatedTransaction, error) {
return account.provider.SimulateTransactions(ctx, blockID, txns, simulationFlags)
}

Expand Down Expand Up @@ -849,8 +829,8 @@ func (account *Account) TraceTransaction(ctx context.Context, transactionHash *f
// - blockID: The ID of the block.
// - index: The index of the transaction in the block.
// Returns:
// - rpc.Transaction: The transaction and an error, if any.
func (account *Account) TransactionByBlockIdAndIndex(ctx context.Context, blockID rpc.BlockID, index uint64) (rpc.Transaction, error) {
// - rpc.BlockTransaction: The transaction and an error, if any.
func (account *Account) TransactionByBlockIdAndIndex(ctx context.Context, blockID rpc.BlockID, index uint64) (*rpc.BlockTransaction, error) {
return account.provider.TransactionByBlockIdAndIndex(ctx, blockID, index)
}

Expand All @@ -860,9 +840,9 @@ func (account *Account) TransactionByBlockIdAndIndex(ctx context.Context, blockI
// - ctx: The context.Context
// - hash: The *felt.Felt hash as parameters.
// Returns:
// - rpc.Transaction
// - rpc.BlockTransaction
// - error
func (account *Account) TransactionByHash(ctx context.Context, hash *felt.Felt) (rpc.Transaction, error) {
func (account *Account) TransactionByHash(ctx context.Context, hash *felt.Felt) (*rpc.BlockTransaction, error) {
return account.provider.TransactionByHash(ctx, hash)
}

Expand Down
6 changes: 3 additions & 3 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"github.com/NethermindEth/starknet.go/mocks"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
"github.com/golang/mock/gomock"
"github.com/joho/godotenv"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

var (
Expand Down Expand Up @@ -1135,8 +1135,7 @@ func TestAddDeclareTxn(t *testing.T) {
var class rpc.ContractClass
err = json.Unmarshal(content, &class)
require.NoError(t, err)
classHash, err := hash.ClassHash(class)
require.NoError(t, err)
classHash := hash.ClassHash(class)

// Compiled Class Hash
content2, err := os.ReadFile("./tests/hello_world_compiled.casm.json")
Expand Down Expand Up @@ -1195,6 +1194,7 @@ func TestAddDeclareTxn(t *testing.T) {
// - []devnet.TestAccount: a slice of test accounts
// - error: an error, if any
func newDevnet(t *testing.T, url string) (*devnet.DevNet, []devnet.TestAccount, error) {
t.Helper()
devnet := devnet.NewDevNet(url)
acnts, err := devnet.Accounts()
return devnet, acnts, err
Expand Down
20 changes: 4 additions & 16 deletions contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/utils"
)

var PREFIX_CONTRACT_ADDRESS = new(felt.Felt).SetBytes([]byte("STARKNET_CONTRACT_ADDRESS"))
Expand Down Expand Up @@ -61,23 +60,12 @@ func UnmarshalCasmClass(filePath string) (*CasmClass, error) {
// - constructorCalldata: the constructor calldata
// Returns:
// - *felt.Felt: the precomputed address as a *felt.Felt
// - error: an error if any
func PrecomputeAddress(deployerAddress *felt.Felt, salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error) {

bigIntArr := utils.FeltArrToBigIntArr([]*felt.Felt{
func PrecomputeAddress(deployerAddress *felt.Felt, salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) *felt.Felt {
return curve.PedersenArray(
PREFIX_CONTRACT_ADDRESS,
deployerAddress,
salt,
classHash,
})

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

preBigInt, err := curve.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
return utils.BigIntToFelt(preBigInt), nil
curve.PedersenArray(constructorCalldata...),
)
}
8 changes: 3 additions & 5 deletions contracts/contracts_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package contracts_test
package contracts

import (
"encoding/json"
"os"
"testing"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/contracts"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -46,7 +45,7 @@ func TestUnmarshalContractClass(t *testing.T) {
//
// none
func TestUnmarshalCasmClass(t *testing.T) {
casmClass, err := contracts.UnmarshalCasmClass("./tests/hello_starknet_compiled.casm.json")
casmClass, err := UnmarshalCasmClass("./tests/hello_starknet_compiled.casm.json")
require.NoError(t, err)
assert.Equal(t, casmClass.Prime, "0x800000000000011000000000000000000000000000000000000000000000001")
assert.Equal(t, casmClass.Version, "2.1.0")
Expand Down Expand Up @@ -106,13 +105,12 @@ func TestPrecomputeAddress(t *testing.T) {
}

for _, test := range testSet {
precomputedAddress, err := contracts.PrecomputeAddress(
precomputedAddress := PrecomputeAddress(
utils.TestHexToFelt(t, test.DeployerAddress),
utils.TestHexToFelt(t, test.Salt),
utils.TestHexToFelt(t, test.ClassHash),
test.ConstructorCalldata,
)
require.NoError(t, err)
require.Equal(t, test.ExpectedPrecomputedAddress, precomputedAddress.String())
}
}
Loading

0 comments on commit 163ad63

Please sign in to comment.