generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring(spv-790) addressing leatest review changes
- Loading branch information
Showing
9 changed files
with
287 additions
and
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pike_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/libsv/go-bk/bec" | ||
|
||
"github.com/bitcoin-sv/spv-wallet/engine/pike" | ||
) | ||
|
||
func Example_generateLockingScripts() { | ||
// Example sender's public key (replace with actual sender's public key) | ||
senderPublicKeyHex := "034252e5359a1de3b8ec08e6c29b80594e88fb47e6ae9ce65ee5a94f0d371d2cde" | ||
senderPublicKeyBytes, err := hex.DecodeString(senderPublicKeyHex) | ||
if err != nil { | ||
panic(err) | ||
} | ||
senderPubKey, err := bec.ParsePubKey(senderPublicKeyBytes, bec.S256()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
receiverPublicKeyHex := "027c1404c3ecb034053e6dd90bc68f7933284559c7d0763367584195a8796d9b0e" | ||
receiverPublicKeyBytes, err := hex.DecodeString(receiverPublicKeyHex) | ||
if err != nil { | ||
panic(err) | ||
} | ||
receiverPubKey, err := bec.ParsePubKey(receiverPublicKeyBytes, bec.S256()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Example usage of GenerateOutputsTemplate | ||
outputsTemplate, err := pike.GenerateOutputsTemplate(10000) | ||
if err != nil { | ||
panic(fmt.Errorf("Error generating outputs template - %w", err)) | ||
} | ||
|
||
// Example usage of GenerateLockingScriptsFromTemplates | ||
lockingScripts, err := pike.GenerateLockingScriptsFromTemplates(outputsTemplate, senderPubKey, receiverPubKey, "reference") | ||
if err != nil { | ||
panic(fmt.Errorf("Error generating locking scripts - %w", err)) | ||
} | ||
|
||
for _, script := range lockingScripts { | ||
fmt.Println("Locking Script:", script) | ||
} | ||
|
||
// Output: | ||
// Locking Script: 76a9147327490be831259f38b0f9ab019413e51d1b40c688ac | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package template | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/libsv/go-bk/bec" | ||
"github.com/libsv/go-bk/crypto" | ||
"github.com/libsv/go-bt/v2/bscript" | ||
"github.com/libsv/go-bt/v2/bscript/interpreter" | ||
) | ||
|
||
// Evaluate processes a given Bitcoin script by parsing it, replacing certain opcodes | ||
// with the public key hash, and returning the resulting script as a byte array. | ||
// Will replace any OP_PUBKEYHASH or OP_PUBKEY | ||
// | ||
// Parameters: | ||
// - script: A byte array representing the input script. | ||
// - pubKey: A pointer to a bec.PublicKey which provides the dedicated public key to be used in the evaluation. | ||
// | ||
// Returns: | ||
// - A byte array representing the evaluated script, or nil if an error occurs. | ||
func Evaluate(script []byte, pubKey *bec.PublicKey) ([]byte, error) { | ||
s := bscript.Script(script) | ||
|
||
parser := interpreter.DefaultOpcodeParser{} | ||
parsedScript, err := parser.Parse(&s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Validate parsed opcodes | ||
for _, op := range parsedScript { | ||
if op.Value() == 0xFF { | ||
return nil, errors.New("invalid opcode") | ||
} | ||
} | ||
|
||
// Serialize the public key to compressed format | ||
dPKBytes := pubKey.SerialiseCompressed() | ||
|
||
// Apply Hash160 (SHA-256 followed by RIPEMD-160) to the compressed public key | ||
dPKHash := crypto.Hash160(dPKBytes) | ||
|
||
// Create a new script with the public key hash | ||
newScript := new(bscript.Script) | ||
if err := newScript.AppendPushData(dPKHash); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Parse the public key hash script | ||
pkhParsed, err := parser.Parse(newScript) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Replace OP_PUBKEYHASH with the actual public key hash | ||
evaluated := make([]interpreter.ParsedOpcode, 0, len(parsedScript)) | ||
for _, op := range parsedScript { | ||
switch op.Value() { | ||
case bscript.OpPUBKEYHASH: | ||
evaluated = append(evaluated, pkhParsed...) | ||
case bscript.OpPUBKEY: | ||
return nil, errors.New("OP_PUBKEY not supported yet") | ||
default: | ||
evaluated = append(evaluated, op) | ||
} | ||
} | ||
|
||
// Unparse the evaluated opcodes back into a script | ||
finalScript, err := parser.Unparse(evaluated) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Cast *bscript.Script back to []byte | ||
return []byte(*finalScript), nil | ||
} |
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
Oops, something went wrong.