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

Enhancement: Tx Unlocker #67

Merged
merged 9 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
ScriptTypePubKey = "pubkey"
ScriptTypePubKeyHash = "pubkeyhash"
ScriptTypeNonStandard = "nonstandard"
ScriptTypeEmpty = "empty"
ScriptTypeMultiSig = "multisig"
ScriptTypeNullData = "nulldata"
)
Expand Down Expand Up @@ -324,7 +325,7 @@ func (s *Script) PublicKeyHash() ([]byte, error) {
// ScriptType returns the type of script this is as a string.
func (s *Script) ScriptType() string {
if len(*s) == 0 {
return ScriptTypeNonStandard
return ScriptTypeEmpty
}
if s.IsP2PKH() {
return ScriptTypePubKeyHash
Expand Down
2 changes: 1 addition & 1 deletion examples/create_tx/create_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {

decodedWif, _ := wif.DecodeWIF("KznvCNc6Yf4iztSThoMH6oHWzH9EgjfodKxmeuUGPq5DEX5maspS")

if err := tx.SignAll(context.Background(), &bt.LocalSignerGetter{PrivateKey: decodedWif.PrivKey}); err != nil {
if err := tx.UnlockAll(context.Background(), &bt.LocalP2PKHUnlockerGetter{PrivateKey: decodedWif.PrivKey}); err != nil {
log.Fatal(err.Error())
}
log.Printf("tx: %s\n", tx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

decodedWif, _ := wif.DecodeWIF("L3VJH2hcRGYYG6YrbWGmsxQC1zyYixA82YjgEyrEUWDs4ALgk8Vu")

err := tx.SignAll(context.Background(), &bt.LocalSignerGetter{PrivateKey: decodedWif.PrivKey})
err := tx.UnlockAll(context.Background(), &bt.LocalP2PKHUnlockerGetter{PrivateKey: decodedWif.PrivKey})
if err != nil {
log.Fatal(err.Error())
}
Expand Down
56 changes: 0 additions & 56 deletions localsigner.go

This file was deleted.

53 changes: 53 additions & 0 deletions localunlocker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package bt

import (
"context"

"github.com/libsv/go-bk/bec"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bt/v2/sighash"
)

// LocalP2PKHUnlockerGetter implements the UnlockerGetter interface. It unlocks a Tx locally,
// using a bkec PrivateKey.
tigh-latte marked this conversation as resolved.
Show resolved Hide resolved
type LocalP2PKHUnlockerGetter struct {
PrivateKey *bec.PrivateKey
}

// Unlocker builds a new *bt.LocalP2PKHUnlocker with the same private key as the calling *bt.LocalP2PKHUnlockerGetter.
func (lg *LocalP2PKHUnlockerGetter) Unlocker(ctx context.Context, lockingScript *bscript.Script) (Unlocker, error) {
tigh-latte marked this conversation as resolved.
Show resolved Hide resolved
return &LocalP2PKHUnlocker{PrivateKey: lg.PrivateKey}, nil
}

// LocalP2PKHUnlocker implements the unlocker interface. It is used to unlock a tx locally using a
// bkec Private Key.
type LocalP2PKHUnlocker struct {
PrivateKey *bec.PrivateKey
}

// Unlock a transaction at a given input using the PrivateKey passed in through the LocalP2PKHUnlocker
// struct.
// Unlock generates and applies an ECDSA signature for the provided hash digest using the private key
// as well as the public key corresponding to the private key used. The produced
// signature is deterministic (same message and same key yield the same signature) and
// canonical in accordance with RFC6979 and BIP0062.
func (lu *LocalP2PKHUnlocker) Unlock(ctx context.Context, tx *Tx, idx uint32, shf sighash.Flag) error {
if shf == 0 {
shf = sighash.AllForkID
}

sh, err := tx.CalcInputSignatureHash(idx, shf)
if err != nil {
return err
}

sig, err := lu.PrivateKey.Sign(sh)
if err != nil {
return err
}

pubKey := lu.PrivateKey.PubKey().SerialiseCompressed()
signature := sig.Serialise()

return tx.ApplyP2PKHUnlockingScript(idx, pubKey, signature, shf)
}
Loading