From 3e173f141e6ec82c103ce3328c656422f2d7b850 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Fri, 31 Jan 2025 17:04:50 +0000 Subject: [PATCH] Order deposit data from HD wallet accounts by path. --- CHANGELOG.md | 1 + cmd/validator/depositdata/output.go | 1 + cmd/validator/depositdata/process.go | 49 +++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f7eb2b..b620dcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ 1.36.6: - allow specification of blockid for validator info + - validator depositdata orders deposits from an HD wallet by path 1.36.5: - avoid corner case mnemonic derivation with 25th word diff --git a/cmd/validator/depositdata/output.go b/cmd/validator/depositdata/output.go index ba8f26f..a5ccdc2 100644 --- a/cmd/validator/depositdata/output.go +++ b/cmd/validator/depositdata/output.go @@ -24,6 +24,7 @@ import ( type dataOut struct { format string account string + path string validatorPubKey *spec.BLSPubKey withdrawalCredentials []byte amount spec.Gwei diff --git a/cmd/validator/depositdata/process.go b/cmd/validator/depositdata/process.go index dd29776..a9f5ef4 100644 --- a/cmd/validator/depositdata/process.go +++ b/cmd/validator/depositdata/process.go @@ -17,6 +17,8 @@ import ( "context" "encoding/hex" "fmt" + "sort" + "strconv" "strings" spec "github.com/attestantio/go-eth2-client/spec/phase0" @@ -80,7 +82,7 @@ func process(data *dataIn) ([]*dataOut, error) { copy(depositDataRoot[:], root[:]) validatorWallet := validatorAccount.(e2wtypes.AccountWalletProvider).Wallet() - results = append(results, &dataOut{ + result := &dataOut{ format: data.format, account: fmt.Sprintf("%s/%s", validatorWallet.Name(), validatorAccount.Name()), validatorPubKey: &pubKey, @@ -90,8 +92,53 @@ func process(data *dataIn) ([]*dataOut, error) { forkVersion: data.forkVersion, depositMessageRoot: &depositMessageRoot, depositDataRoot: &depositDataRoot, + } + if pathProvider, isPathProvider := validatorAccount.(e2wtypes.AccountPathProvider); isPathProvider { + result.path = pathProvider.Path() + } + results = append(results, result) + } + if len(results) == 0 { + return results, nil + } + + // Order the results + if results[0].path != "" { + // Order accounts by their path components. + sort.Slice(results, func(i int, j int) bool { + iBits := strings.Split(results[i].path, "/") + jBits := strings.Split(results[j].path, "/") + for index := range iBits { + if iBits[index] == "m" && jBits[index] == "m" { + continue + } + if len(jBits) <= index { + return false + } + iBit, err := strconv.ParseUint(iBits[index], 10, 64) + if err != nil { + return true + } + jBit, err := strconv.ParseUint(jBits[index], 10, 64) + if err != nil { + return false + } + if iBit < jBit { + return true + } + if iBit > jBit { + return false + } + } + return len(jBits) > len(iBits) + }) + } else { + // Order accounts by their name. + sort.Slice(results, func(i int, j int) bool { + return strings.Compare(results[i].account, results[j].account) < 0 }) } + return results, nil }