|
| 1 | +/* |
| 2 | + * Flow CLI |
| 3 | + * |
| 4 | + * Copyright 2019-2022 Dapper Labs, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package tools |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/onflow/flow-cli/internal/command" |
| 26 | + "github.com/onflow/flow-cli/pkg/flowkit" |
| 27 | + "github.com/onflow/flow-cli/pkg/flowkit/output" |
| 28 | + "github.com/onflow/flow-cli/pkg/flowkit/services" |
| 29 | + |
| 30 | + devWallet "github.com/onflow/fcl-dev-wallet" |
| 31 | + "github.com/spf13/cobra" |
| 32 | +) |
| 33 | + |
| 34 | +type FlagsWallet struct { |
| 35 | + Port uint `default:"8701" flag:"port" info:"Dev wallet port to listen on"` |
| 36 | +} |
| 37 | + |
| 38 | +var walletFlags = FlagsWallet{} |
| 39 | + |
| 40 | +var DevWallet = &command.Command{ |
| 41 | + Cmd: &cobra.Command{ |
| 42 | + Use: "dev-wallet", |
| 43 | + Short: "Starts a dev wallet", |
| 44 | + Example: "flow dev-wallet", |
| 45 | + Args: cobra.ExactArgs(0), |
| 46 | + }, |
| 47 | + Flags: &walletFlags, |
| 48 | + RunS: wallet, |
| 49 | +} |
| 50 | + |
| 51 | +func wallet( |
| 52 | + _ []string, |
| 53 | + _ flowkit.ReaderWriter, |
| 54 | + _ command.GlobalFlags, |
| 55 | + _ *services.Services, |
| 56 | + state *flowkit.State, |
| 57 | +) (command.Result, error) { |
| 58 | + service, err := state.EmulatorServiceAccount() |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + key := service.Key().ToConfig() |
| 64 | + conf := devWallet.Config{ |
| 65 | + Address: fmt.Sprintf("0x%s", service.Address().String()), |
| 66 | + PrivateKey: strings.TrimPrefix(key.PrivateKey.String(), "0x"), |
| 67 | + PublicKey: strings.TrimPrefix(key.PrivateKey.PublicKey().String(), "0x"), |
| 68 | + AccessNode: "http://localhost:8080", |
| 69 | + } |
| 70 | + |
| 71 | + srv, err := devWallet.NewHTTPServer(walletFlags.Port, &conf) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Printf("%s Starting dev wallet server on port %d\n", output.SuccessEmoji(), walletFlags.Port) |
| 77 | + fmt.Printf("%s Make sure the emulator is running\n", output.WarningEmoji()) |
| 78 | + |
| 79 | + err = srv.Start() |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + return nil, nil |
| 85 | +} |
0 commit comments