-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 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,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) | ||
} |
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,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 | ||
} |
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,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") | ||
} | ||
} |