Merge pull request #833 from touhonoob/embed-wallet-ini

[wallet] embed wallet.ini into wallet binary (#830)
pull/838/head
Leo Chen 6 years ago committed by GitHub
commit 7fa822cb66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      cmd/client/wallet/generated_wallet.ini.go
  2. 15
      cmd/client/wallet/main.go
  3. 1
      go.mod
  4. 4
      internal/utils/configfile.go
  5. 10
      internal/utils/configfile_test.go
  6. 64
      internal/utils/file_embed.go
  7. 2
      scripts/go_executable_build.sh
  8. 8
      scripts/wallet_embed_ini_files.go

@ -0,0 +1,44 @@
package main
const (
defaultWalletIni = `[default]
bootnode = /ip4/100.26.90.187/tcp/9876/p2p/QmZJJx6AdaoEkGLrYG4JeLCKeCKDjnFz2wfHNHxAqFSGA9
bootnode = /ip4/54.213.43.194/tcp/9876/p2p/QmQayinFSgMMw5cSpDUiD9pQ2WeP6WNmGxpZ6ou3mdVFJX
shards = 1
[default.shard0.rpc]
rpc = 34.217.179.222:14555
rpc = 18.209.247.105:14555
rpc = 100.25.248.42:14555
rpc = 3.80.164.193:14555
rpc = 54.87.237.93:14555
[local]
bootnode = /ip4/127.0.0.1/tcp/19876/p2p/Qmc1V6W7BwX8Ugb42Ti8RnXF1rY5PF7nnZ6bKBryCgi6cv
shards = 1
[local.shard0.rpc]
rpc = 127.0.0.1:14555
rpc = 127.0.0.1:14556
[devnet]
bootnode = /ip4/100.26.90.187/tcp/9871/p2p/Qmdfjtk6hPoyrH1zVD9PEH4zfWLo38dP2mDvvKXfh3tnEv
bootnode = /ip4/54.213.43.194/tcp/9871/p2p/QmRVbTpEYup8dSaURZfF6ByrMTSKa4UyUzJhSjahFzRqNj
shards = 3
[devnet.shard0.rpc]
rpc = 13.57.196.136:14555
rpc = 35.175.103.144:14555
rpc = 54.245.176.36:14555
[devnet.shard1.rpc]
rpc = 35.163.188.234:14555
rpc = 54.215.251.123:14555
rpc = 54.153.11.146:14555
[devnet.shard2.rpc]
rpc = 52.201.246.212:14555
rpc = 3.81.26.139:14555
rpc = 18.237.42.209:14555
`
)

@ -196,10 +196,23 @@ ARG:
}
}
//go:generate go run ../../../scripts/wallet_embed_ini_files.go
func readProfile(profile string) {
fmt.Printf("Using %s profile for wallet\n", profile)
// try to load .hmy/wallet.ini from filesystem
// use default_wallet_ini if .hmy/wallet.ini doesn't exist
var err error
walletProfile, err = utils.ReadWalletProfile(defaultConfigFile, profile)
var iniBytes []byte
iniBytes, err = ioutil.ReadFile(defaultConfigFile)
if err != nil {
log.Debug(fmt.Sprintf("%s doesn't exist, using default ini\n", defaultConfigFile))
iniBytes = []byte(defaultWalletIni)
}
walletProfile, err = utils.ReadWalletProfile(iniBytes, profile)
if err != nil {
fmt.Printf("Read wallet profile error: %v\nExiting ...\n", err)
os.Exit(2)

@ -24,6 +24,7 @@ require (
github.com/harmony-ek/gencodec v0.0.0-20190215044613-e6740dbdd846
github.com/harmony-one/bls v0.0.1
github.com/hashicorp/golang-lru v0.5.1
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
github.com/ipfs/go-datastore v0.0.1
github.com/ipfs/go-log v0.0.1
github.com/karalabe/hid v0.0.0-20181128192157-d815e0c1a2e2 // indirect

@ -18,8 +18,8 @@ type WalletProfile struct {
}
// ReadWalletProfile reads an ini file and return WalletProfile
func ReadWalletProfile(fn string, profile string) (*WalletProfile, error) {
cfg, err := ini.ShadowLoad(fn)
func ReadWalletProfile(iniBytes []byte, profile string) (*WalletProfile, error) {
cfg, err := ini.ShadowLoad(iniBytes)
if err != nil {
return nil, err
}

@ -1,6 +1,7 @@
package utils
import (
"io/ioutil"
"reflect"
"testing"
@ -95,14 +96,19 @@ func TestReadWalletProfile(t *testing.T) {
},
}
config1, err := ReadWalletProfile("test.ini", "default")
testIniBytes, err := ioutil.ReadFile("test.ini")
if err != nil {
t.Fatalf("Failed to read test.ini: %v", err)
}
config1, err := ReadWalletProfile(testIniBytes, "default")
if err != nil {
t.Fatalf("ReadWalletProfile Error: %v", err)
}
if !reflect.DeepEqual(config[0], config1) {
t.Errorf("Got: %v\nExpect: %v\n", config1, config[0])
}
config2, err := ReadWalletProfile("test.ini", "testnet")
config2, err := ReadWalletProfile(testIniBytes, "testnet")
if err != nil {
t.Fatalf("ReadWalletProfile Error: %v", err)
}

@ -0,0 +1,64 @@
package utils
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/iancoleman/strcase"
)
// EmbedFile Text file embed script for go:generate.
// This script embeds a text file located at filePath into a string constant
// named as constName defined in a golang source file located at the current
// go:generate path.
func EmbedFile(filePath string, constName string) {
// validate inputs
if _, err := os.Stat(filePath); os.IsNotExist(err) {
panic(fmt.Sprintf("File %s does not exist", filePath))
}
if strcase.ToLowerCamel(constName) != constName {
panic(fmt.Sprintf("constName %s is not in lower camel-case", constName))
}
// generate go file
var err error
fileName := filepath.Base(filePath)
generatedFileName := "generated_" + strcase.ToSnake(fileName) + ".go"
out, err := os.Create(generatedFileName)
if err != nil {
panic(err)
}
_, err = out.Write([]byte(fmt.Sprintf("package %s\n\nconst (\n", os.Getenv("GOPACKAGE"))))
if err != nil {
panic(err)
}
_, err = out.Write([]byte("\t" + constName + " = `"))
if err != nil {
panic(err)
}
f, err := os.Open(filePath)
if err != nil {
panic(err)
}
_, err = io.Copy(out, f)
if err != nil {
panic(err)
}
_, err = out.Write([]byte("`\n"))
if err != nil {
panic(err)
}
_, err = out.Write([]byte(")\n"))
if err != nil {
panic(err)
}
}

@ -6,7 +6,7 @@ declare -A SRC
SRC[harmony]=cmd/harmony/main.go
SRC[txgen]=cmd/client/txgen/main.go
SRC[bootnode]=cmd/bootnode/main.go
SRC[wallet]=cmd/client/wallet/main.go
SRC[wallet]="cmd/client/wallet/main.go cmd/client/wallet/generated_wallet.ini.go"
BINDIR=bin
BUCKET=unique-bucket-bin

@ -0,0 +1,8 @@
package main
import "github.com/harmony-one/harmony/internal/utils"
// Embed the default wallet.ini file into defaultWalletIni string literal constant
func main() {
utils.EmbedFile("../../../.hmy/wallet.ini", "defaultWalletIni")
}
Loading…
Cancel
Save