Skip to content

Commit

Permalink
chore: Refactor main.go to pad zeros in felt hex values
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Aug 30, 2024
1 parent f2fa504 commit 84722cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
5 changes: 2 additions & 3 deletions examples/deployAccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions examples/internal/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strconv"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/joho/godotenv"
)
Expand Down Expand Up @@ -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
}

0 comments on commit 84722cb

Please sign in to comment.