-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added generic tx unlocker interface * made unlocker a general signature script unlocker * fixed linter * Unlocker.Unlock return the data used to build the unlocking script * reverted back to the unlocker just returning an error * fix doc string comment * change switch statement to if/else * fixed test names and doc comments
- Loading branch information
1 parent
2f85c3e
commit fc06c54
Showing
17 changed files
with
367 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package bt | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/libsv/go-bk/bec" | ||
"github.com/libsv/go-bt/v2/bscript" | ||
"github.com/libsv/go-bt/v2/sighash" | ||
) | ||
|
||
// LocalUnlockerGetter implements the UnlockerGetter interface. It unlocks a Tx locally, | ||
// using a bec PrivateKey. | ||
type LocalUnlockerGetter struct { | ||
PrivateKey *bec.PrivateKey | ||
} | ||
|
||
// Unlocker builds a new *bt.LocalUnlocker with the same private key | ||
// as the calling *bt.LocalUnlockerGetter. | ||
func (lg *LocalUnlockerGetter) Unlocker(ctx context.Context, lockingScript *bscript.Script) (Unlocker, error) { | ||
return &LocalUnlocker{PrivateKey: lg.PrivateKey}, nil | ||
} | ||
|
||
// LocalUnlocker implements the unlocker interface. It is used to unlock a tx locally using a | ||
// bec Private Key. | ||
type LocalUnlocker struct { | ||
PrivateKey *bec.PrivateKey | ||
} | ||
|
||
// Unlock a transaction at a given input using the PrivateKey passed in through the LocalUnlocker | ||
// struct. | ||
// Unlock generates, applies, and returns 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 *LocalUnlocker) 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() | ||
|
||
switch tx.Inputs[idx].PreviousTxScript.ScriptType() { | ||
case bscript.ScriptTypePubKeyHash: | ||
return tx.ApplyP2PKHUnlockingScript(idx, pubKey, signature, shf) | ||
} | ||
|
||
return errors.New("currently only p2pkh supported") | ||
} |
Oops, something went wrong.