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

wallets/generate: generate in stable order #76

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions cmd/storjscan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ func generate(cmd *cobra.Command, args []string) (err error) {
return errs.Wrap(err)
}

for addr, info := range addresses {
err = w.Write([]string{addr.String(), info})
for _, addr := range addresses {
err = w.Write([]string{addr.Address.String(), addr.Info})
if err != nil {
return errs.Wrap(err)
}
Expand Down
19 changes: 6 additions & 13 deletions cmd/storjscan/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"sort"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

Expand Down Expand Up @@ -50,24 +49,18 @@ func TestImport(t *testing.T) {
addresses, err := wallets.Generate(ctx, "defaultkey", 0, 5, mnemonic)
require.NoError(t, err)

addressesSlice := []common.Address{}
for address := range addresses {
addressesSlice = append(addressesSlice, address)
}

// sort descending
sort.Slice(addressesSlice, func(i, j int) bool {
return bytes.Compare(addressesSlice[i].Bytes(), addressesSlice[j].Bytes()) > 0
sort.Slice(addresses, func(i, j int) bool {
return bytes.Compare(addresses[i].Address.Bytes(), addresses[j].Address.Bytes()) > 0
})

importFilePath := ctx.File("wallets.csv")
importFile, err := os.Create(importFilePath)
require.NoError(t, err)

fmt.Fprintln(importFile, "address,info")
for _, address := range addressesSlice {
info := addresses[address]
fmt.Fprintf(importFile, "%s,%s\n", address.String(), info)
for _, address := range addresses {
fmt.Fprintf(importFile, "%s,%s\n", address.Address.String(), address.Info)
}
require.NoError(t, importFile.Close())

Expand All @@ -77,11 +70,11 @@ func TestImport(t *testing.T) {
require.NoErrorf(t, err, "Error running test: %s", out)

// verify that addresses are claimed in the import order
for _, expectedAddress := range addressesSlice {
for _, expectedAddress := range addresses {
address, err := service.Claim(ctx, "eu1")
require.NoError(t, err)

require.Equal(t, expectedAddress, address)
require.Equal(t, expectedAddress.Address, address)
}

// no more wallets to claim
Expand Down
16 changes: 12 additions & 4 deletions wallets/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ func derive(masterKey *hdkeychain.ExtendedKey, path accounts.DerivationPath) (ac
}, nil
}

type GeneratedAddress struct {
Address common.Address
Info string
}

// Generate creates new HD wallet addresses.
func Generate(ctx context.Context, keysname string, min, max int, mnemonic string) (map[common.Address]string, error) {
addr := make(map[common.Address]string)
func Generate(ctx context.Context, keysname string, min, max int, mnemonic string) ([]GeneratedAddress, error) {
addrs := make([]GeneratedAddress, 0, max-min)

if mnemonic == "" {
return nil, errs.New("mnemonic is required")
Expand Down Expand Up @@ -80,7 +85,10 @@ func Generate(ctx context.Context, keysname string, min, max int, mnemonic strin
if err != nil {
return nil, err
}
addr[account.Address] = keysname + " " + path.String()
addrs = append(addrs, GeneratedAddress{
Address: account.Address,
Info: keysname + " " + path.String(),
})
}
return addr, nil
return addrs, nil
}
12 changes: 6 additions & 6 deletions wallets/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func TestGenerate(t *testing.T) {
client1 := wallets.NewClient("http://"+lis.Addr().String(), "eu1", "secret")

var inserts1 []wallets.InsertWallet
for address, info := range addresses1 {
for _, address := range addresses1 {
inserts1 = append(inserts1, wallets.InsertWallet{
Address: address,
Info: info,
Address: address.Address,
Info: address.Info,
})
}
err = client1.AddWallets(ctx, inserts1)
Expand All @@ -66,10 +66,10 @@ func TestGenerate(t *testing.T) {
client2 := wallets.NewClient("http://"+lis.Addr().String(), "eu1", "secret")

var inserts2 []wallets.InsertWallet
for address, info := range addresses2 {
for _, address := range addresses2 {
inserts2 = append(inserts2, wallets.InsertWallet{
Address: address,
Info: info,
Address: address.Address,
Info: address.Info,
})
}
err = client2.AddWallets(ctx, inserts2)
Expand Down