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

Fix: Use a counter for path gen instead of a pathToScript map, in example #103

Merged
merged 1 commit into from Dec 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 5 additions & 9 deletions examples/unlocker_getter/account.go
Expand Up @@ -17,10 +17,10 @@ import (
type account struct {
// masterPrivKey of which all locking scripts and private keys are generated from.
masterPrivKey *bip32.ExtendedKey
// counter for deriving paths for public/private keys
counter uint64
// scriptToPathMap for mapping a locking script hex to its derivation path.
scriptToPathMap map[string]string
// pathToScriptMap for mapping a derivation path to its locking script.
pathToScriptMap map[string]*bscript.Script
}

func newAccount() *account {
Expand All @@ -37,16 +37,13 @@ func newAccount() *account {
return &account{
masterPrivKey: privKey,
scriptToPathMap: make(map[string]string, 0),
pathToScriptMap: make(map[string]*bscript.Script, 0),
}
}

func (a *account) createDestination() *bscript.Script {
var path string
// key generating a new path until unique one is found
for ok := true; ok; _, ok = a.pathToScriptMap[path] {
path = bip32.DerivePath(randUint64())
}
// generate a new path until and increment the counter.
path := bip32.DerivePath(a.counter)
a.counter++

// Derive a public key from the new derivation path.
pubKey, err := a.masterPrivKey.DerivePublicKeyFromPath(path)
Expand All @@ -61,7 +58,6 @@ func (a *account) createDestination() *bscript.Script {
}

// Store the locking script and its path for later use.
a.pathToScriptMap[path] = s
a.scriptToPathMap[s.String()] = path

return s
Expand Down