Skip to content

Commit

Permalink
Added ScriptFromAddress method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 1, 2020
1 parent 22cc91f commit b81297f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


<details>
Expand Down
30 changes: 30 additions & 0 deletions examples/script_from_address/script_from_address.go
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 24 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions script_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit b81297f

Please sign in to comment.