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

Basic custom account create script #200

Merged
merged 2 commits into from
Nov 4, 2021
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
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
main

# Test binary, built with `go test -c`
*.test
Expand All @@ -25,3 +26,11 @@ flow/
api/
.github/
examples/

# Not required for build
flow/
api-test-scripts/
*.md
*.svg
*.html
*.yml
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ stop-emulator: emulator.pid

emulator.pid:
@cd flow && { flow emulator -b 100ms & echo $$! > ../$@; }

.PHONY: lint
lint:
@golangci-lint run
11 changes: 11 additions & 0 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package accounts
import (
"context"
"fmt"
"os"
"strings"

"github.com/flow-hydraulics/flow-wallet-api/configs"
Expand Down Expand Up @@ -214,6 +215,16 @@ func (s *Service) createAccount(ctx context.Context) (*Account, string, error) {
SetPayer(adminAuthorizer.Address).
SetGasLimit(maxGasLimit)

// Check if we want to use a custom account create script
if s.cfg.ScriptPathCreateAccount != "" {
bytes, err := os.ReadFile(s.cfg.ScriptPathCreateAccount)
if err != nil {
return nil, "", err
}
// Overwrite the existing script
flowTx.SetScript(bytes)
}

// Payer signs the envelope
if err := flowTx.SignEnvelope(adminAuthorizer.Address, adminAuthorizer.Key.Index, adminAuthorizer.Signer); err != nil {
return nil, "", err
Expand Down
3 changes: 2 additions & 1 deletion configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ type Config struct {

// -- Templates --

EnabledTokens []string `env:"FLOW_WALLET_ENABLED_TOKENS" envSeparator:","`
EnabledTokens []string `env:"FLOW_WALLET_ENABLED_TOKENS" envSeparator:","`
ScriptPathCreateAccount string `env:"FLOW_WALLET_SCRIPT_PATH_CREATE_ACCOUNT" envDefault:""`

// -- Workerpool --

Expand Down
5 changes: 5 additions & 0 deletions flow/cadence/transactions/custom_create_account.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
transaction(publicKeys: [String], contracts: {String: String}) {
prepare(signer: AuthAccount) {
panic("Account initialized with custom script")
}
}
55 changes: 53 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ var testLogger *log.Logger

type TestApp struct {
Config *configs.Config
FlowClient *client.Client
KeyManager keys.Manager
WorkerPool *jobs.WorkerPool

AccountService *accounts.Service
TemplateService *templates.Service
TokenService *tokens.Service
TransactionService *transactions.Service

JobStore jobs.Store
AccountStore accounts.Store
JobStore jobs.Store
}

type httpTestStep struct {
Expand All @@ -74,6 +77,20 @@ func fatal(t *testing.T, err error) {
}
}

func deepCopy(v interface{}) (interface{}, error) {
data, err := json.Marshal(v)
if err != nil {
return nil, err
}

vptr := reflect.New(reflect.TypeOf(v))
err = json.Unmarshal(data, vptr.Interface())
if err != nil {
return nil, err
}
return vptr.Elem().Interface(), err
}

func handleStepRequest(s httpTestStep, r *mux.Router, t *testing.T) *httptest.ResponseRecorder {
req, err := http.NewRequest(s.method, s.url, s.body)
fatal(t, err)
Expand Down Expand Up @@ -174,14 +191,17 @@ func getTestApp(t *testing.T) TestApp {

return TestApp{
Config: cfg,
FlowClient: fc,
KeyManager: km,
WorkerPool: wp,

AccountService: accountService,
TemplateService: templateService,
TokenService: tokenService,
TransactionService: transactionService,

JobStore: jobStore,
AccountStore: accountStore,
JobStore: jobStore,
}
}

Expand Down Expand Up @@ -264,6 +284,37 @@ func TestAccountServices(t *testing.T) {
t.Log("expected 503 'max capacity reached, try again later' but got no error")
}
})

t.Run("create with custom init script", func(t *testing.T) {
// Create a copy of config so we can isolate changes
cp, err := deepCopy(app.Config)
fatal(t, err)
cfg2 := cp.(*configs.Config)

// Set custom script path
cfg2.ScriptPathCreateAccount = "./flow/cadence/transactions/custom_create_account.cdc"

// Create a new service with new config
service2 := accounts.NewService(
cfg2,
app.AccountStore,
app.KeyManager,
app.FlowClient,
app.WorkerPool,
app.TransactionService,
)

// Use the new service to create an account
_, _, err = service2.Create(context.Background(), true)

if err == nil {
t.Fatal("expected an error")
}

if !strings.Contains(err.Error(), "Account initialized with custom script") {
t.Fatalf(`expected error to contain "Account initialized with custom script" got: %s`, err)
}
})
}

func TestAccountHandlers(t *testing.T) {
Expand Down