From 84722cb90bc7b50784ca981d9268bfc18b76731c Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Thu, 29 Aug 2024 23:43:58 -0300 Subject: [PATCH] chore: Refactor main.go to pad zeros in felt hex values --- examples/deployAccount/main.go | 5 ++--- examples/internal/setup.go | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/examples/deployAccount/main.go b/examples/deployAccount/main.go index 24152bf0..e135825f 100644 --- a/examples/deployAccount/main.go +++ b/examples/deployAccount/main.go @@ -77,8 +77,7 @@ func main() { if err != nil { panic(err) } - fmt.Println("PrecomputedAddress:", precomputedAddress) - + fmt.Println("PrecomputedAddress:", setup.PadZerosInFelt(precomputedAddress)) // Sign the transaction err = accnt.SignDeployAccountTransaction(context.Background(), &tx.DeployAccountTxn, precomputedAddress) if err != nil { @@ -115,7 +114,7 @@ func main() { // At this point you need to add funds to precomputed address to use it. var input string - fmt.Println("The `precomputedAddress` account needs to have enough ETH to perform a transaction.") + fmt.Println("\nThe `precomputedAddress` account needs to have enough ETH to perform a transaction.") fmt.Printf("Use the starknet faucet to send ETH to your `precomputedAddress`. You need aproximately %f ETH. \n", feeInETH) fmt.Println("When your account has been funded by the faucet, press any key, then `enter` to continue : ") fmt.Scan(&input) diff --git a/examples/internal/setup.go b/examples/internal/setup.go index 5342793c..912fee98 100644 --- a/examples/internal/setup.go +++ b/examples/internal/setup.go @@ -7,6 +7,7 @@ import ( "os" "strconv" + "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/starknet.go/rpc" "github.com/joho/godotenv" ) @@ -72,3 +73,23 @@ func getEnv(envName string) string { } return env } + +// PadZerosInFelt pads zeros to the left of a hex felt value to make it 64 characters long. +func PadZerosInFelt(hexFelt *felt.Felt) string { + length := 66 + hexStr := hexFelt.String() + + // Check if the hex value is already of the desired length + if len(hexStr) >= length { + return hexStr + } + + // Extract the hex value without the "0x" prefix + hexValue := hexStr[2:] + // Pad zeros after the "0x" prefix + paddedHexValue := fmt.Sprintf("%0*s", length-2, hexValue) + // Add back the "0x" prefix to the padded hex value + paddedHexStr := "0x" + paddedHexValue + + return paddedHexStr +}