Skip to content

Commit

Permalink
fix broken files after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Oct 5, 2023
1 parent 7594e4c commit fd18018
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
44 changes: 16 additions & 28 deletions examples/deployAccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"

"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
ethrpc "github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -70,7 +70,7 @@ func main() {
panic(err)
}
fmt.Println("Transaction hash:", hash)
x, y, err := starknetgo.Curve.SignFelt(hash, priv)
x, y, err := curve.Curve.SignFelt(hash, priv)
if err != nil {
panic(err)
}
Expand All @@ -87,24 +87,19 @@ func main() {
}

func getRandomKeys() (*felt.Felt, *felt.Felt) {
privateKey, err := starknetgo.Curve.GetRandomPrivateKey()
privateKey, err := curve.Curve.GetRandomPrivateKey()
if err != nil {
fmt.Println("can't get random private key:", err)
os.Exit(1)
}
pubX, _, err := starknetgo.Curve.PrivateToPoint(privateKey)
pubX, _, err := curve.Curve.PrivateToPoint(privateKey)
if err != nil {
fmt.Println("can't generate public key:", err)
os.Exit(1)
}
privFelt, err := utils.BigIntToFelt(privateKey)
if err != nil {
panic(err)
}
pubFelt, err := utils.BigIntToFelt(pubX)
if err != nil {
panic(err)
}
privFelt := utils.BigIntToFelt(privateKey)
pubFelt := utils.BigIntToFelt(pubX)

return pubFelt, privFelt
}

Expand All @@ -114,38 +109,31 @@ func getRandomKeys() (*felt.Felt, *felt.Felt) {
func precomputeAddress(deployerAddress *felt.Felt, salt *felt.Felt, classHash *felt.Felt, constructorCalldata []*felt.Felt) (*felt.Felt, error) {
CONTRACT_ADDRESS_PREFIX := new(felt.Felt).SetBytes([]byte("STARKNET_CONTRACT_ADDRESS"))

bigIntArr, err := utils.FeltArrToBigIntArr([]*felt.Felt{
bigIntArr := utils.FeltArrToBigIntArr([]*felt.Felt{
CONTRACT_ADDRESS_PREFIX,
deployerAddress,
salt,
classHash,
})
if err != nil {
return nil, err
}

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

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

}

func computeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
bigIntArr, err := utils.FeltArrToBigIntArr(feltArr)
if err != nil {
return nil, err
}
hash, err := starknetgo.Curve.ComputeHashOnElements(*bigIntArr)
bigIntArr := utils.FeltArrToBigIntArr(feltArr)
hash, err := curve.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
return utils.BigIntToFelt(hash)
return utils.BigIntToFelt(hash), nil
}

// calculateDeployAccountTransactionHash computes the transaction hash for deployAccount transactions
Expand Down
20 changes: 10 additions & 10 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package hash

import (
"github.com/NethermindEth/juno/core/felt"
starknetgo "github.com/NethermindEth/starknet.go"
"github.com/NethermindEth/starknet.go/contracts"
"github.com/NethermindEth/starknet.go/curve"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
)
Expand All @@ -12,7 +12,7 @@ import (
func ComputeHashOnElementsFelt(feltArr []*felt.Felt) (*felt.Felt, error) {
bigIntArr := utils.FeltArrToBigIntArr(feltArr)

hash, err := starknetgo.Curve.ComputeHashOnElements(bigIntArr)
hash, err := curve.Curve.ComputeHashOnElements(bigIntArr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -52,33 +52,33 @@ func ClassHash(contract rpc.ContractClass) (*felt.Felt, error) {
ConstructorHash := hashEntryPointByType(contract.EntryPointsByType.Constructor)
ExternalHash := hashEntryPointByType(contract.EntryPointsByType.External)
L1HandleHash := hashEntryPointByType(contract.EntryPointsByType.L1Handler)
SierraProgamHash := starknetgo.Curve.PoseidonArray(contract.SierraProgram...)
ABIHash, err := starknetgo.Curve.StarknetKeccak([]byte(contract.ABI))
SierraProgamHash := curve.Curve.PoseidonArray(contract.SierraProgram...)
ABIHash, err := curve.Curve.StarknetKeccak([]byte(contract.ABI))
if err != nil {
return nil, err
}

// https://docs.starknet.io/documentation/architecture_and_concepts/Network_Architecture/transactions/#deploy_account_hash_calculation
return starknetgo.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ABIHash, SierraProgamHash), nil
return curve.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ABIHash, SierraProgamHash), nil
}

func hashEntryPointByType(entryPoint []rpc.SierraEntryPoint) *felt.Felt {
flattened := make([]*felt.Felt, 0, len(entryPoint))
for _, elt := range entryPoint {
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.FunctionIdx)))
}
return starknetgo.Curve.PoseidonArray(flattened...)
return curve.Curve.PoseidonArray(flattened...)
}

func CompiledClassHash(casmClass contracts.CasmClass) *felt.Felt {
ContractClassVersionHash := new(felt.Felt).SetBytes([]byte("COMPILED_CLASS_V1"))
ExternalHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.External)
L1HandleHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.L1Handler)
ConstructorHash := hashCasmClassEntryPointByType(casmClass.EntryPointByType.Constructor)
ByteCodeHasH := starknetgo.Curve.PoseidonArray(casmClass.ByteCode...)
ByteCodeHasH := curve.Curve.PoseidonArray(casmClass.ByteCode...)

// https://github.com/software-mansion/starknet.py/blob/development/starknet_py/hash/casm_class_hash.py#L10
return starknetgo.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ByteCodeHasH)
return curve.Curve.PoseidonArray(ContractClassVersionHash, ExternalHash, L1HandleHash, ConstructorHash, ByteCodeHasH)
}

func hashCasmClassEntryPointByType(entryPoint []contracts.CasmClassEntryPoint) *felt.Felt {
Expand All @@ -88,8 +88,8 @@ func hashCasmClassEntryPointByType(entryPoint []contracts.CasmClassEntryPoint) *
for _, builtIn := range elt.Builtins {
builtInFlat = append(builtInFlat, new(felt.Felt).SetBytes([]byte(builtIn)))
}
builtInHash := starknetgo.Curve.PoseidonArray(builtInFlat...)
builtInHash := curve.Curve.PoseidonArray(builtInFlat...)
flattened = append(flattened, elt.Selector, new(felt.Felt).SetUint64(uint64(elt.Offset)), builtInHash)
}
return starknetgo.Curve.PoseidonArray(flattened...)
return curve.Curve.PoseidonArray(flattened...)
}

0 comments on commit fd18018

Please sign in to comment.