Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue(#4): Support init wallet before deploy #5

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ A Cloud Native Buildpack that provides the Aptos Tool Suite
| Environment Variable | Description |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$BP_APTOS_VERSION` | Configure the version of Aptos to install. It can be a specific version or a wildcard like `1.*`. It defaults to the latest `2.4.0` version. |
| `$BP_ENABLE_APTOS_PROCESS` | Enable the Aptos run process. It defaults to `aptos move publish --skip-fetch-latest-git-deps --assume-yes`. |

| `$BP_ENABLE_APTOS_DEPLOY` | Enable the Aptos deploy. It defaults to `aptos move publish --skip-fetch-latest-git-deps --assume-yes`. |
| `$BP_APTOS_DEPLOY_PRIVATE_KEY` | Configure the wallet private key for Aptos deploy. `It defaults to must be specified.` |
| `$BP_APTOS_DEPLOY_NETWORK` | Configure the network for Aptos deploy. It defaults to `devnet`. |

## Usage

Expand Down
51 changes: 48 additions & 3 deletions aptos/aptos.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ import (

type Aptos struct {
LayerContributor libpak.DependencyLayerContributor
configResolver libpak.ConfigurationResolver
Logger bard.Logger
Executor effect.Executor
}

func NewAptos(dependency libpak.BuildpackDependency, cache libpak.DependencyCache) Aptos {
func NewAptos(dependency libpak.BuildpackDependency, cache libpak.DependencyCache, configResolver libpak.ConfigurationResolver) Aptos {
contributor := libpak.NewDependencyLayerContributor(dependency, cache, libcnb.LayerTypes{
Build: true,
Cache: true,
Launch: true,
})
return Aptos{
LayerContributor: contributor,
configResolver: configResolver,
Executor: effect.NewExecutor(),
}
}
Expand Down Expand Up @@ -87,11 +89,16 @@ func (r Aptos) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {

// compile contract
args := []string{"move", "compile"}
r.Logger.Bodyf("Compiling contracts by '%s %s'", PlanEntryAptos, args)
r.Logger.Bodyf("Compiling contracts")
if _, err := r.Execute(PlanEntryAptos, args); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to compile contract\n%w", err)
}

// initialize wallet for deploy
if ok, err := r.InitializeDeployWallet(); !ok {
return libcnb.Layer{}, fmt.Errorf("unable to initialize deploy wallet\n%w", err)
}

layer.LaunchEnvironment.Append("PATH", ":", bin)
layer.LaunchEnvironment.Default("MOVE_HOME", moveHome)
return layer, nil
Expand All @@ -114,8 +121,14 @@ func (r Aptos) Execute(command string, args []string) (*bytes.Buffer, error) {
func (r Aptos) BuildProcessTypes(cr libpak.ConfigurationResolver, app libcnb.Application) ([]libcnb.Process, error) {
processes := []libcnb.Process{}

enableDeploy := cr.ResolveBool("BP_ENABLE_APTOS_PROCESS")
enableDeploy := cr.ResolveBool("BP_ENABLE_APTOS_DEPLOY")
if enableDeploy {
deployPrivateKey, _ := r.configResolver.Resolve("BP_APTOS_DEPLOY_PRIVATE_KEY")
if deployPrivateKey == "" {
return processes, fmt.Errorf("BP_APTOS_DEPLOY_PRIVATE_KEY must be specified")
}

// publish module
processes = append(processes, libcnb.Process{
Type: PlanEntryAptos,
Command: PlanEntryAptos,
Expand All @@ -126,6 +139,38 @@ func (r Aptos) BuildProcessTypes(cr libpak.ConfigurationResolver, app libcnb.App
return processes, nil
}

func (r Aptos) InitializeDeployWallet() (bool, error) {
enableDeploy := r.configResolver.ResolveBool("BP_ENABLE_APTOS_DEPLOY")
if enableDeploy {
deployPrivateKey, _ := r.configResolver.Resolve("BP_APTOS_DEPLOY_PRIVATE_KEY")
deployNetwork, _ := r.configResolver.Resolve("BP_APTOS_DEPLOY_NETWORK")
ok, err := r.InitializeWallet(deployPrivateKey, deployNetwork)
if !ok {
return false, fmt.Errorf("unable to initialize %s wallet\n%w", PlanEntryAptos, err)
}
}
return true, nil
}

func (r Aptos) InitializeWallet(deployPrivateKey, deployNetwork string) (bool, error) {
// init wallet
args := []string{"init", "--private-key", deployPrivateKey, "--assume-yes", "--network", deployNetwork}
r.Logger.Bodyf("Initializing %s wallet", PlanEntryAptos)
if _, err := r.Execute(PlanEntryAptos, args); err != nil {
return false, fmt.Errorf("unable to initialize wallet\n%w", err)
}

// Get faucet for devnet
if deployNetwork == "devnet" {
r.Logger.Bodyf("Getting faucet")
args = []string{"account", "fund-with-faucet", "--account", "default"}
if _, err := r.Execute(PlanEntryAptos, args); err != nil {
return false, fmt.Errorf("unable to get faucet\n%w", err)
}
}
return true, nil
}

func (r Aptos) Name() string {
return r.LayerContributor.LayerName()
}
2 changes: 1 addition & 1 deletion aptos/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}

aptosLayer := NewAptos(dependency, dc)
aptosLayer := NewAptos(dependency, dc, cr)
aptosLayer.Logger = b.Logger

result.Processes, err = aptosLayer.BuildProcessTypes(cr, context.Application)
Expand Down
15 changes: 13 additions & 2 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,19 @@ api = "0.8"
[[metadata.configurations]]
build = true
default = "true"
description = "Enable the Aptos run process"
name = "BP_ENABLE_APTOS_PROCESS"
description = "Enable the Aptos deploy"
name = "BP_ENABLE_APTOS_DEPLOY"

[[metadata.configurations]]
build = true
description = "Configure the wallet private key for Aptos deploy"
name = "BP_APTOS_DEPLOY_PRIVATE_KEY"

[[metadata.configurations]]
build = true
default = "devnet"
description = "Configure the network for Aptos deploy"
name = "BP_APTOS_DEPLOY_NETWORK"

[[metadata.dependencies]]
id = "aptos"
Expand Down
Loading