Skip to content

Commit

Permalink
Dep Replacement: libsv for go-bt
Browse files Browse the repository at this point in the history
Moving from libsv over to go-bt for all transaction related bitcoin stuff
  • Loading branch information
mrz1836 committed Nov 13, 2020
1 parent 158f8d6 commit 1ce9291
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ View the generated [documentation](https://pkg.go.dev/github.com/bitcoinschema/g
- [bitcoinsv/bsvd](https://github.com/bitcoinsv/bsvd)
- [bitcoinsv/bsvutil](https://github.com/bitcoinsv/bsvutil)
- [itchyny/base58-go](https://github.com/itchyny/base58-go)
- [libsv/libsv](https://github.com/libsv/libsv)
- [libsv/go-bt](https://github.com/libsv/go-bt)
- [piotrnar/gocoin](https://github.com/piotrnar/gocoin)
</details>

Expand Down
4 changes: 2 additions & 2 deletions examples/create_tx/create_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"

"github.com/bitcoinschema/go-bitcoin"
"github.com/libsv/libsv/transaction"
"github.com/libsv/go-bt"
)

func main() {
Expand Down Expand Up @@ -35,7 +35,7 @@ func main() {
}

// Generate the TX
var rawTx *transaction.Transaction
var rawTx *bt.Tx
rawTx, err = bitcoin.CreateTx(
[]*bitcoin.Utxo{utxo},
[]*bitcoin.PayToAddress{payTo},
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173
github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e // indirect
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9
github.com/libsv/libsv v0.0.11
github.com/libsv/go-bt v0.0.1
github.com/piotrnar/gocoin v0.0.0-20201027184336-0c389d7eb2c0
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9 // indirect
)
48 changes: 47 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,67 @@
bitbucket.org/simon_ordish/cryptolib v1.0.39/go.mod h1:7tkcAeq4Wg7wwJ/uN4kaGRPE6yBJA6mUyOTZnqSjsLc=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/bitcoin-sv/merchantapi-reference v1.1.0 h1:H8vUBMjzdm6eAJI+1bk7CA2DXwTlAkyJjFdy8i5gZXU=
github.com/bitcoin-sv/merchantapi-reference v1.1.0/go.mod h1:unZHsjGH7ChT0p5b5BSEBfZeo4E+eU+plMul2zYInJU=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173 h1:2yTIV9u7H0BhRDGXH5xrAwAz7XibWJtX2dNezMeNsUo=
github.com/bitcoinsv/bsvd v0.0.0-20190609155523-4c29707f7173/go.mod h1:BZ1UcC9+tmcDEcdVXgpt13hMczwJxWzpAn68wNs7zRA=
github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e h1:6f+gRvaPE/4h0g39dqTNPr9/P4mikw0aB+dhiExaWN8=
github.com/bitcoinsv/bsvlog v0.0.0-20181216181007-cb81b076bf2e/go.mod h1:WPrWor6cSeuGQZ15qPe+jqFmblJEFrJHYfr5cD7cmyk=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9 h1:hFI8rT84FCA0FFy3cFrkW5Nz4FyNKlIdCvEvvTNySKg=
github.com/bitcoinsv/bsvutil v0.0.0-20181216182056-1d77cf353ea9/go.mod h1:p44KuNKUH5BC8uX4ONEODaHUR4+ibC8todEAOGQEJAM=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-zeromq/goczmq/v4 v4.2.2/go.mod h1:Sm/lxrfxP/Oxqs0tnHD6WAhwkWrx+S+1MRrKzcxoaYE=
github.com/go-zeromq/zmq4 v0.9.0/go.mod h1:hCJ0OxYnL3Y3erSLQ025VLGi/W63zJjvr9i17oU2P24=
github.com/go-zeromq/zmq4 v0.10.0/go.mod h1:hCJ0OxYnL3Y3erSLQ025VLGi/W63zJjvr9i17oU2P24=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/libsv/go-bt v0.0.1 h1:kMRGGzHNvb70TrSgoLbkMlRBuGuW3xXeqE4BaepPlTw=
github.com/libsv/go-bt v0.0.1/go.mod h1:yo8F8kZaQnZ4GN7txXNs1ANGKLGjTyrHoNVu6ig3acA=
github.com/libsv/libsv v0.0.11 h1:tb2w/RFpsZLEfPNDGl4tlwk3haMSXYXKspta16+nyv4=
github.com/libsv/libsv v0.0.11/go.mod h1:jhXD2sLtYUNMWoD9mWmsQN0hoGZUK1wCvindT1Odijo=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/ordishs/go-bitcoin v1.0.33/go.mod h1:BMi/TpK6TKxA7K5ON3nrtGB0wuKsub3htiahSs3fJHA=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/piotrnar/gocoin v0.0.0-20201027184336-0c389d7eb2c0 h1:PuqJFsnjEnbgSk7b629KC4jyuWqr5tM09k80ZpyptHY=
github.com/piotrnar/gocoin v0.0.0-20201027184336-0c389d7eb2c0/go.mod h1:sW6i99ojgdRHcz53PCjyEeoTEDFh9dfP5iiEIiNfcaM=
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9 h1:umElSU9WZirRdgu2yFHY0ayQkEnKiOC1TtM3fWXFnoU=
golang.org/x/crypto v0.0.0-20201112155050-0c6587e931a9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6 changes: 3 additions & 3 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bitcoin
import (
"errors"

"github.com/libsv/libsv/script"
"github.com/libsv/go-bt"
)

// ScriptFromAddress will create an output P2PKH script from an address string
Expand All @@ -14,11 +14,11 @@ func ScriptFromAddress(address string) (string, error) {
}

// Generate a script from address
rawScript, err := script.NewP2PKHFromAddress(address)
rawScript, err := bt.NewP2PKHOutputFromAddress(address, 0)
if err != nil {
return "", err
}

// Return the string version
return rawScript.ToString(), nil
return rawScript.GetLockingScriptHexString(), nil
}
26 changes: 12 additions & 14 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"strings"

"github.com/bitcoinsv/bsvd/bsvec"
"github.com/libsv/libsv/transaction"
"github.com/libsv/libsv/transaction/output"
"github.com/libsv/libsv/transaction/signature"
"github.com/libsv/go-bt"
)

const (
Expand Down Expand Up @@ -56,16 +54,16 @@ type PayToAddress struct {
type OpReturnData [][]byte

// TxFromHex will return a libsv.tx from a raw hex string
func TxFromHex(rawHex string) (*transaction.Transaction, error) {
return transaction.NewFromString(rawHex)
func TxFromHex(rawHex string) (*bt.Tx, error) {
return bt.NewTxFromString(rawHex)
}

// CreateTxWithChange will automatically create the change output and calculate fees
//
// Use this if you don't want to figure out fees/change for a tx
func CreateTxWithChange(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns []OpReturnData,
changeAddress string, standardRate, dataRate *FeeAmount,
privateKey *bsvec.PrivateKey) (*transaction.Transaction, error) {
privateKey *bsvec.PrivateKey) (*bt.Tx, error) {

// Missing utxo(s) or change address
if len(utxos) == 0 {
Expand Down Expand Up @@ -139,7 +137,7 @@ func CreateTxWithChange(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns
//
// Use this if you don't want to figure out fees/change for a tx
func CreateTxWithChangeUsingWif(utxos []*Utxo, payToAddresses []*PayToAddress, opReturns []OpReturnData,
changeAddress string, standardRate, dataRate *FeeAmount, wif string) (*transaction.Transaction, error) {
changeAddress string, standardRate, dataRate *FeeAmount, wif string) (*bt.Tx, error) {

// Decode the WIF
privateKey, err := WifToPrivateKey(wif)
Expand All @@ -159,10 +157,10 @@ func CreateTxWithChangeUsingWif(utxos []*Utxo, payToAddresses []*PayToAddress, o
// Get the raw hex version: tx.ToString()
// Get the tx id: tx.GetTxID()
func CreateTx(utxos []*Utxo, addresses []*PayToAddress,
opReturns []OpReturnData, privateKey *bsvec.PrivateKey) (*transaction.Transaction, error) {
opReturns []OpReturnData, privateKey *bsvec.PrivateKey) (*bt.Tx, error) {

// Start creating a new transaction
tx := transaction.New()
tx := bt.NewTx()

// Accumulate the total satoshis from all utxo(s)
var totalSatoshis uint64
Expand All @@ -184,9 +182,9 @@ func CreateTx(utxos []*Utxo, addresses []*PayToAddress,
}

// Loop any op returns
var outPut *output.Output
var outPut *bt.Output
for _, op := range opReturns {
if outPut, err = output.NewOpReturnParts(op); err != nil {
if outPut, err = bt.NewOpReturnPartsOutput(op); err != nil {
return nil, err
}
tx.AddOutput(outPut)
Expand All @@ -205,7 +203,7 @@ func CreateTx(utxos []*Utxo, addresses []*PayToAddress,
// Sign the transaction
if privateKey != nil {

signer := signature.InternalSigner{PrivateKey: privateKey, SigHashFlag: 0}
signer := bt.InternalSigner{PrivateKey: privateKey, SigHashFlag: 0}
if err = tx.SignAuto(&signer); err != nil {
return nil, err
}
Expand All @@ -223,7 +221,7 @@ func CreateTx(utxos []*Utxo, addresses []*PayToAddress,
// Get the raw hex version: tx.ToString()
// Get the tx id: tx.GetTxID()
func CreateTxUsingWif(utxos []*Utxo, addresses []*PayToAddress,
opReturns []OpReturnData, wif string) (*transaction.Transaction, error) {
opReturns []OpReturnData, wif string) (*bt.Tx, error) {

// Decode the WIF
privateKey, err := WifToPrivateKey(wif)
Expand All @@ -241,7 +239,7 @@ func CreateTxUsingWif(utxos []*Utxo, addresses []*PayToAddress,
// Rate(s) can be derived from MinerAPI (default is DefaultDataRate and DefaultStandardRate)
// If rate is nil it will use default rates (0.5 sat per byte)
// Reference: https://tncpw.co/c215a75c
func CalculateFeeForTx(tx *transaction.Transaction, standardRate, dataRate *FeeAmount) uint64 {
func CalculateFeeForTx(tx *bt.Tx, standardRate, dataRate *FeeAmount) uint64 {

// Set the totals
var totalFee uint64
Expand Down
16 changes: 8 additions & 8 deletions transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/libsv/libsv/transaction"
"github.com/libsv/go-bt"
)

// TestTxFromHex will test the method TxFromHex()
Expand Down Expand Up @@ -62,7 +62,7 @@ func BenchmarkTxFromHex(b *testing.B) {
// TestCreateTx will test the method CreateTx()
func TestCreateTx(t *testing.T) {

// Example from: https://github.com/libsv/libsv
// Example from: https://github.com/libsv/go-bt

// Use a new UTXO
utxo := &Utxo{
Expand All @@ -89,7 +89,7 @@ func TestCreateTx(t *testing.T) {
}

// Generate the TX
var rawTx *transaction.Transaction
var rawTx *bt.Tx
rawTx, err = CreateTx(
[]*Utxo{utxo},
[]*PayToAddress{payTo},
Expand All @@ -106,7 +106,7 @@ func TestCreateTx(t *testing.T) {

func TestCreateEmptyTx(t *testing.T) {
// Generate the TX
var rawTx *transaction.Transaction
var rawTx *bt.Tx
rawTx, err := CreateTx(
nil,
nil,
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestCreateTxErrors(t *testing.T) {
// TestCreateTxUsingWif will test the method CreateTxUsingWif()
func TestCreateTxUsingWif(t *testing.T) {

// Example from: https://github.com/libsv/libsv
// Example from: https://github.com/libsv/go-bt

// Use a new UTXO
utxo := &Utxo{
Expand Down Expand Up @@ -674,7 +674,7 @@ func TestCreateTxWithChange(t *testing.T) {
}

// Generate the TX
var rawTx *transaction.Transaction
var rawTx *bt.Tx
rawTx, err = CreateTxWithChange(
[]*Utxo{utxo},
[]*PayToAddress{payTo},
Expand Down Expand Up @@ -908,7 +908,7 @@ func TestCreateTxWithChangeErrors(t *testing.T) {
}

// Run tests
var rawTx *transaction.Transaction
var rawTx *bt.Tx
for _, test := range tests {
privateKey, err := WifToPrivateKey(test.inputWif)
if err != nil && !test.expectedError {
Expand Down Expand Up @@ -958,7 +958,7 @@ func ExampleCreateTxWithChange() {
}

// Generate the TX
var rawTx *transaction.Transaction
var rawTx *bt.Tx
rawTx, err = CreateTxWithChange(
[]*Utxo{utxo},
[]*PayToAddress{payTo},
Expand Down

0 comments on commit 1ce9291

Please sign in to comment.