Skip to content

Commit

Permalink
Wpkh descriptor parsing bug (#193)
Browse files Browse the repository at this point in the history
* added factory method FromPublicKeys for creating multiscript payment

* fix wpkh desc parsing

* fix wpkh desc, no fingerprint, no children
  • Loading branch information
sekulicd authored May 13, 2022
1 parent b50117b commit 4bc7702
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
42 changes: 42 additions & 0 deletions descriptor/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,48 @@ func TestParseScriptExpressionWpkh(t *testing.T) {
},
wantErr: false,
},
{
name: "wpkh_xpub_no_fingerprint",
args: args{
descriptor: "wpkh(xpub661MyMwAqRbcFFzgbS7PZzrLLXNYmno5FN7aYMceX7HcRgot6DUnPWn8z8C2EAcqiQ9QBmsWkVmhvMjsrwsMexwiqcW1mdyMZDspQqv6SUQ/1/*)",
topLevel: true,
},
validate: func(wallet Wallet) error {
scriptHexExpected := "0014881e461e5a3d4114011aedb9242fa4b05d0a9142"
scripts, err := wallet.Script(nil)
if err != nil {
return err
}

if hex.EncodeToString(scripts[0].Script) != scriptHexExpected {
return errors.New("unexpected script gen")
}

return nil
},
wantErr: false,
},
{
name: "wpkh_xpub_no_fingerprint_no_children",
args: args{
descriptor: "wpkh(xpub661MyMwAqRbcFFzgbS7PZzrLLXNYmno5FN7aYMceX7HcRgot6DUnPWn8z8C2EAcqiQ9QBmsWkVmhvMjsrwsMexwiqcW1mdyMZDspQqv6SUQ)",
topLevel: true,
},
validate: func(wallet Wallet) error {
scriptHexExpected := "0014032ef494864b63614b5dc5c1ee23bb620e74cf7f"
scripts, err := wallet.Script(nil)
if err != nil {
return err
}

if hex.EncodeToString(scripts[0].Script) != scriptHexExpected {
return errors.New("unexpected script gen")
}

return nil
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
27 changes: 24 additions & 3 deletions descriptor/wpkh.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ func (w WpkhWallet) Script(opts *ScriptOpts) ([]ScriptResponse, error) {
Script: script,
})
}
} else {
pubKey, err := masterExtKey.ECPubKey()
if err != nil {
return nil, err
}

script, err := wpkhScriptFromBytes(pubKey.SerializeCompressed())
if err != nil {
return nil, err
}

response = append(response, ScriptResponse{
DerivationPath: w.derivationPath(index),
Script: script,
})
}

return response, nil
Expand All @@ -159,9 +174,15 @@ func (w WpkhWallet) Script(opts *ScriptOpts) ([]ScriptResponse, error) {

func (w WpkhWallet) derivationPath(index uint32) []uint32 {
derivationPath := make([]uint32, 0)
derivationPath = append(derivationPath, w.keyInfo.keyOrigin.masterKeyFingerprint)
derivationPath = append(derivationPath, w.keyInfo.keyOrigin.path...)
derivationPath = append(derivationPath, w.keyInfo.extendedKeyInfo.path...)
if w.keyInfo.keyOrigin != nil {
derivationPath = append(derivationPath, w.keyInfo.keyOrigin.masterKeyFingerprint)
derivationPath = append(derivationPath, w.keyInfo.keyOrigin.path...)
}
if w.keyInfo.extendedKeyInfo != nil {
if len(w.keyInfo.extendedKeyInfo.path) > 0 {
derivationPath = append(derivationPath, w.keyInfo.extendedKeyInfo.path...)
}
}
derivationPath = append(derivationPath, index)

return derivationPath
Expand Down

0 comments on commit 4bc7702

Please sign in to comment.