diff --git a/buildpack.toml b/buildpack.toml index 564b25f..8fe5e44 100644 --- a/buildpack.toml +++ b/buildpack.toml @@ -71,6 +71,12 @@ api = "0.8" description = "Configure the rpc for Starknet deploy" name = "BP_STARKNET_DEPLOY_RPC" + [[metadata.configurations]] + build = true + default = "" + description = "Configure the args for Starknet deploy" + name = "BP_STARKNET_DEPLOY_ARGS" + [[metadata.dependencies]] id = "starkli-gnu" name = "Starkli (GNU libc)" diff --git a/go.mod b/go.mod index 81f92e2..fab87c5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/buildpacks/libcnb v1.30.3 github.com/paketo-buildpacks/libpak v1.70.0 + github.com/mattn/go-shellwords v1.0.12 ) require ( diff --git a/go.sum b/go.sum index ff9a033..c62df37 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-shellwords v1.0.12 h1:M2zGm7EW6UQJvDeQxo4T51eKPurbeFbe8WtebGE2xrk= +github.com/mattn/go-shellwords v1.0.12/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y= github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk= diff --git a/scripts/build.sh b/scripts/build.sh index 11e6bd6..b5136f0 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -16,4 +16,4 @@ fi ln -fs main linux/amd64/bin/build ln -fs main linux/arm64/bin/build ln -fs main linux/amd64/bin/detect -ln -fs main linux/arm64/bin/detect \ No newline at end of file +ln -fs main linux/arm64/bin/detect diff --git a/starknet/starknet.go b/starknet/starknet.go index c3ff635..6ca6fd7 100644 --- a/starknet/starknet.go +++ b/starknet/starknet.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/buildpacks/libcnb" + "github.com/mattn/go-shellwords" "github.com/paketo-buildpacks/libpak" "github.com/paketo-buildpacks/libpak/bard" "github.com/paketo-buildpacks/libpak/crush" @@ -36,6 +37,11 @@ type Starknet struct { Executor effect.Executor } +const ( + classHashFile = "/workspace/class_hash.txt" + compileDir = "/workspace/target/dev" +) + func NewStarknet(dependency libpak.BuildpackDependency, cache libpak.DependencyCache, configResolver libpak.ConfigurationResolver) Starknet { contributor := libpak.NewDependencyLayerContributor(dependency, cache, libcnb.LayerTypes{ Cache: true, @@ -92,6 +98,16 @@ func (r Starknet) Contribute(layer libcnb.Layer) (libcnb.Layer, error) { layer.LaunchEnvironment.Default("STARKNET_PRIVATE_KEY", deployPrivateKey) layer.LaunchEnvironment.Default("STARKNET_ACCOUNT", deployAccount) layer.LaunchEnvironment.Default("STARKNET_RPC", deployRpc) + + os.Setenv("STARKNET_PRIVATE_KEY", deployPrivateKey) + os.Setenv("STARKNET_ACCOUNT", deployAccount) + os.Setenv("STARKNET_RPC", deployRpc) + + classHash, err := r.DeclareContract() + if err != nil { + return libcnb.Layer{}, err + } + layer.LaunchEnvironment.Default("STARKNET_CLASS_HASH", classHash) return layer, nil }) } @@ -119,15 +135,22 @@ func (r Starknet) BuildProcessTypes(cr libpak.ConfigurationResolver, app libcnb. return processes, fmt.Errorf("BP_STARKNET_DEPLOY_PRIVATE_KEY must be specified") } - if classHash, err := r.DeclareContract(); err != nil { - deployWalletAddress, _ := r.configResolver.Resolve("BP_STARKNET_DEPLOY_WALLET_ADDRESS") - processes = append(processes, libcnb.Process{ - Type: PlanEntryStarkli, - Command: PlanEntryStarkli, - Arguments: []string{"deploy", classHash, deployWalletAddress}, - Default: true, - }) + deployArgsRaw, _ := cr.Resolve("BP_STARKNET_DEPLOY_ARGS") + deployArgs, err := shellwords.Parse(deployArgsRaw) + if err != nil { + return processes, fmt.Errorf("unable to parse BP_STARKNET_DEPLOY_ARGS=%q\n%w", deployArgsRaw, err) } + + args := []string{"deploy", "--strk", "$STARKNET_CLASS_HASH"} + args = append(args, deployArgs...) + r.Logger.Bodyf("Deploying contract with args: %s", args) + + processes = append(processes, libcnb.Process{ + Type: PlanEntryStarkli, + Command: PlanEntryStarkli, + Arguments: args, + Default: true, + }) } return processes, nil } @@ -155,7 +178,7 @@ func (r Starknet) InitializeWallet() (bool, error) { deployRpc, _ := r.configResolver.Resolve("BP_STARKNET_DEPLOY_RPC") accountDir := filepath.Dir(deployAccount) - r.Logger.Bodyf("Initializing deploy wallet and save to dir:", accountDir) + r.Logger.Bodyf("Initializing deploy wallet and save to dir: %s", accountDir) os.MkdirAll(accountDir, os.ModePerm) args := []string{ @@ -175,17 +198,35 @@ func (r Starknet) InitializeWallet() (bool, error) { } func (r Starknet) DeclareContract() (string, error) { - r.Logger.Bodyf("Declaring contract") - args := []string{ - "declare", - "target/dev/*.contract_class.json", + file, err := r.ReadContractClass() + if err != nil { + return "", fmt.Errorf("unable to read contract class\n%w", err) } + args := []string{ + "class-hash", + filepath.Join(compileDir, file), + } buf, err := r.Execute(PlanEntryStarkli, args) if err != nil { return "", fmt.Errorf("unable to declaring contract\n%w", err) } - classHash := strings.TrimSpace(buf.String()) + r.Logger.Bodyf("Writing class hash: %s to file: %s", classHash, classHashFile) + os.WriteFile(classHashFile, []byte(classHash), 0644) return classHash, nil } + +func (r Starknet) ReadContractClass() (string, error) { + files, err := os.ReadDir(compileDir) + if err != nil { + return "", fmt.Errorf("unable to read contract class\n%w", err) + } + + for _, file := range files { + if !file.IsDir() && strings.HasSuffix(file.Name(), ".contract_class.json") { + return file.Name(), nil + } + } + return "", fmt.Errorf("unable to find contract class") +}