diff --git a/README.md b/README.md index fa26587..49edeb1 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g - [Address from Script](address.go) - [Create PrivateKey](private_key.go) - [Create PubKey from PrivateKey](pubkey.go) +- [Script from Address](script.go)
diff --git a/examples/script_from_address/script_from_address.go b/examples/script_from_address/script_from_address.go new file mode 100644 index 0000000..568ef5b --- /dev/null +++ b/examples/script_from_address/script_from_address.go @@ -0,0 +1,30 @@ +package main + +import ( + "log" + + "github.com/bitcoinschema/go-bitcoin" +) + +func main() { + // Start with a private key + privateKey, err := bitcoin.CreatePrivateKeyString() + if err != nil { + log.Fatalf("error occurred: %s", err.Error()) + } + + // Get an address + var address string + if address, err = bitcoin.AddressFromPrivateKey(privateKey); err != nil { + log.Fatalf("error occurred: %s", err.Error()) + } + + // Get the script + var script string + if script, err = bitcoin.ScriptFromAddress(address); err != nil { + log.Fatalf("error occurred: %s", err.Error()) + } + + // Success! + log.Printf("generated script: %s from address: %s", script, address) +} diff --git a/script.go b/script.go new file mode 100644 index 0000000..11d91ae --- /dev/null +++ b/script.go @@ -0,0 +1,24 @@ +package bitcoin + +import ( + "errors" + + "github.com/libsv/libsv/script" +) + +// ScriptFromAddress will create an output P2PKH script from an address string +func ScriptFromAddress(address string) (string, error) { + // Missing address? + if len(address) == 0 { + return "", errors.New("missing address") + } + + // Generate a script from address + rawScript, err := script.NewP2PKHFromAddress(address) + if err != nil { + return "", err + } + + // Return the string version + return rawScript.ToString(), nil +} diff --git a/script_test.go b/script_test.go new file mode 100644 index 0000000..4ef806f --- /dev/null +++ b/script_test.go @@ -0,0 +1,53 @@ +package bitcoin + +import ( + "fmt" + "testing" +) + +// TestScriptFromAddress will test the method ScriptFromAddress() +func TestScriptFromAddress(t *testing.T) { + t.Parallel() + + // Create the list of tests + var tests = []struct { + inputAddress string + expectedScript string + expectedError bool + }{ + {"", "", true}, + {"0", "", true}, + {"1234567", "", true}, + {"1HRVqUGDzpZSMVuNSZxJVaB9xjneEShfA7", "76a914b424110292f4ea2ac92beb9e83cf5e6f0fa2996388ac", false}, + {"13Rj7G3pn2GgG8KE6SFXLc7dCJdLNnNK7M", "76a9141a9d62736746f85ca872dc555ff51b1fed2471e288ac", false}, + } + + // Run tests + for _, test := range tests { + if script, err := ScriptFromAddress(test.inputAddress); err != nil && !test.expectedError { + t.Errorf("%s Failed: [%v] inputted and error not expected but got: %s", t.Name(), test.inputAddress, err.Error()) + } else if err == nil && test.expectedError { + t.Errorf("%s Failed: [%v] inputted and error was expected", t.Name(), test.inputAddress) + } else if script != test.expectedScript { + t.Errorf("%s Failed: [%v] inputted [%s] expected but failed comparison of scripts, got: %s", t.Name(), test.inputAddress, test.expectedScript, script) + } + } +} + +// ExampleScriptFromAddress example using ScriptFromAddress() +func ExampleScriptFromAddress() { + script, err := ScriptFromAddress("1HRVqUGDzpZSMVuNSZxJVaB9xjneEShfA7") + if err != nil { + fmt.Printf("error occurred: %s", err.Error()) + return + } + fmt.Printf("script generated: %s", script) + // Output:script generated: 76a914b424110292f4ea2ac92beb9e83cf5e6f0fa2996388ac +} + +// BenchmarkScriptFromAddress benchmarks the method ScriptFromAddress() +func BenchmarkScriptFromAddress(b *testing.B) { + for i := 0; i < b.N; i++ { + _, _ = ScriptFromAddress("1HRVqUGDzpZSMVuNSZxJVaB9xjneEShfA7") + } +}