Skip to content

Commit

Permalink
waddrmgr: refactor newWitnessScriptAddress to return interface
Browse files Browse the repository at this point in the history
To make integration of the upcoming taproot script address type a bit
easier, we allow returning a ManagedScriptAddress interface type in
newWitnessScriptAddress so we can re-use that function for the taproot
script address too.
  • Loading branch information
guggero committed Feb 21, 2022
1 parent ee6479e commit 2d5ee75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
17 changes: 17 additions & 0 deletions waddrmgr/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ func newManagedAddressFromExtKey(s *ScopedKeyManager,
return managedAddr, nil
}

// clearTextSetter is a non-exported interface to identify script types that
// allow their clear text script to be set.
type clearTextSetter interface {
// setClearText sets the unencrypted script on the struct after
// unlocking/decrypting it.
setClearText([]byte)
}

// baseScriptAddress represents the common fields of a pay-to-script-hash and
// a pay-to-witness-script-hash address.
type baseScriptAddress struct {
Expand All @@ -545,6 +553,8 @@ type baseScriptAddress struct {
scriptMutex sync.Mutex
}

var _ clearTextSetter = (*baseScriptAddress)(nil)

// unlock decrypts and stores the associated script. It will fail if the key is
// invalid or the encrypted script is not available. The returned clear text
// script will always be a copy that may be safely used by the caller without
Expand Down Expand Up @@ -603,6 +613,13 @@ func (a *baseScriptAddress) Internal() bool {
return false
}

// setClearText sets the unencrypted script on the struct after unlocking/
// decrypting it.
func (a *baseScriptAddress) setClearText(script []byte) {
a.scriptClearText = make([]byte, len(script))
copy(a.scriptClearText, script)
}

// scriptAddress represents a pay-to-script-hash address.
type scriptAddress struct {
baseScriptAddress
Expand Down
27 changes: 9 additions & 18 deletions waddrmgr/scoped_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2197,38 +2197,29 @@ func (s *ScopedKeyManager) importScriptAddress(ns walletdb.ReadWriteBucket,
// when not a watching-only address manager, make a copy of the script
// since it will be cleared on lock and the script the caller passed
// should not be cleared out from under the caller.
var (
managedAddr ManagedScriptAddress
baseScriptAddr *baseScriptAddress
)
var managedAddr ManagedScriptAddress
switch addrType {
case WitnessScript:
witnessAddr, err := newWitnessScriptAddress(
managedAddr, err = newWitnessScriptAddress(
s, ImportedAddrAccount, scriptIdent, encryptedScript,
witnessVersion, isSecretScript,
)
if err != nil {
return nil, err
}
managedAddr = witnessAddr
baseScriptAddr = &witnessAddr.baseScriptAddress

default:
scriptAddr, err := newScriptAddress(
managedAddr, err = newScriptAddress(
s, ImportedAddrAccount, scriptIdent, encryptedScript,
)
if err != nil {
return nil, err
}
managedAddr = scriptAddr
baseScriptAddr = &scriptAddr.baseScriptAddress
}
if err != nil {
return nil, err
}

// Even if the script is secret, we are currently unlocked, so we keep a
// clear text copy of the script around to avoid decrypting it on each
// access.
baseScriptAddr.scriptClearText = make([]byte, len(script))
copy(baseScriptAddr.scriptClearText, script)
if cts, ok := managedAddr.(clearTextSetter); ok {
cts.setClearText(script)
}

// Add the new managed address to the cache of recent addresses and
// return it.
Expand Down

0 comments on commit 2d5ee75

Please sign in to comment.