Skip to content

Commit

Permalink
issue(#4): Support init wallet before deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
lispking committed Jan 28, 2024
1 parent 5fc31bf commit 1b34b11
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
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
18 changes: 15 additions & 3 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,27 @@ 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"
name = "Aptos"
purl = "pkg:generic/aptos@2.4.0"
sha256 = "b77ce25ca98b1998ef0c9b1a4f45c96e68e49ceae27d98f5bcdc3c79e88106f1"
uri = "https://github.com/aptos-labs/aptos-core/releases/download/aptos-cli-v2.4.0/aptos-cli-2.4.0-Ubuntu-22.04-x86_64.zip"
# uri = "https://github.com/aptos-labs/aptos-core/releases/download/aptos-cli-v2.4.0/aptos-cli-2.4.0-Ubuntu-22.04-x86_64.zip"
uri = "https://gitee.com/lispking/aptos-core/releases/download/aptos-cli-v2.4.0/aptos-cli-2.4.0-Ubuntu-22.04-x86_64.zip"
stacks = ["*"]
version = "2.4.0"

Expand Down

0 comments on commit 1b34b11

Please sign in to comment.