Skip to content

Commit 111ea53

Browse files
authored
Merge pull request #4 from libsv/fix/derivation_counter
fixing up bit shifting and sorting tests
2 parents 023994c + 211b23b commit 111ea53

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

bip32/derivationpaths.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
// We split the seed bits into 3 sections: (b63-b32|b32-b1|b1-b0)
1414
// Each section is then added onto 2^31 and concatenated together which will give us the final path.
1515
func DerivePath(i uint64) string {
16-
path := fmt.Sprintf("%d/", i>>32|1<<31)
17-
path += fmt.Sprintf("%d/", ((i<<32)>>34)|1<<31)
16+
path := fmt.Sprintf("%d/", i>>33|1<<31)
17+
path += fmt.Sprintf("%d/", ((i<<31)>>33)|1<<31)
1818
path += fmt.Sprintf("%d", (i&3)|1<<31)
1919
return path
2020
}
@@ -31,7 +31,7 @@ func DeriveNumber(path string) (uint64, error) {
3131
if err != nil {
3232
return 0, err
3333
}
34-
seed := (d1 - 1<<31) << 32
34+
seed := (d1 - 1<<31) << 33
3535
d2, err := strconv.ParseUint(ss[1], 10, 32)
3636
if err != nil {
3737
return 0, err

bip32/derivationpaths_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ func Test_DerivePathAndDeriveSeed(t *testing.T) {
3131
}, "max int * 2 should return 1 with root path": {
3232
counter: (math.MaxInt32 * 10000) + 172732732,
3333
startingPath: "",
34-
exp: "2147488648/2190664331/2147483648",
34+
exp: "2147486148/2190664331/2147483648",
3535
}, "max int squared should return 0/0 path": {
3636
counter: (math.MaxInt32 * math.MaxInt32) + 172732732,
3737
startingPath: "",
38-
exp: "3221225471/2190666831/2147483649",
38+
exp: "2684354559/3264408655/2147483649",
3939
}, "max int squared + 100 should return correct path": {
4040
counter: (math.MaxInt32*math.MaxInt32 + (math.MaxInt32 * 100)) + 172732732,
4141
startingPath: "",
42-
exp: "3221225521/2190666806/2147483649",
42+
exp: "2684354584/3264408630/2147483649",
4343
}, "max int squared plus two int32 should return correct path": {
4444
counter: ((math.MaxInt32 * math.MaxInt32 * 1) + (math.MaxInt32 * 2)) + 172732732,
4545
startingPath: "",
46-
exp: "3221225472/2190666830/2147483651",
46+
exp: "2684354560/2190666830/2147483651",
4747
},
4848
}
4949
for name, test := range tests {

0 commit comments

Comments
 (0)