-
-
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.
Creating a basic example of making a Tx
- Loading branch information
Showing
2 changed files
with
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package bitcoin | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/bitcoinsv/bsvutil" | ||
"github.com/libsv/libsv/transaction" | ||
"github.com/libsv/libsv/transaction/output" | ||
"github.com/libsv/libsv/transaction/signature" | ||
) | ||
|
||
// Utxo is an unspent transaction output | ||
type Utxo struct { | ||
Satoshis uint64 `json:"satoshis"` | ||
ScriptSig string `json:"string"` | ||
TxID string `json:"tx_id"` | ||
Vout uint32 `json:"vout"` | ||
} | ||
|
||
// PayToAddress is the pay-to-address | ||
type PayToAddress struct { | ||
Address string `json:"address"` | ||
Satoshis uint64 `json:"satoshis"` | ||
} | ||
|
||
// OpReturnData is the op return data to include in the tx | ||
type OpReturnData struct { | ||
Data string `json:"data"` | ||
} | ||
|
||
// CreateTx will create a basic transaction | ||
func CreateTx(utxos []*Utxo, addresses []*PayToAddress, opReturns []*OpReturnData, wif string) (string, error) { | ||
|
||
// Missing utxos | ||
if len(utxos) == 0 { | ||
return "", errors.New("utxos are required to create a tx") | ||
} | ||
|
||
// Start creating a new transaction | ||
tx := transaction.New() | ||
|
||
// Loop all utxos and add to the transaction | ||
var err error | ||
for _, utxo := range utxos { | ||
if err = tx.From(utxo.TxID, utxo.Vout, utxo.ScriptSig, utxo.Satoshis); err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
// Loop any pay addresses | ||
for _, address := range addresses { | ||
if err = tx.PayTo(address.Address, address.Satoshis); err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
// Loop any op returns | ||
var outPut *output.Output | ||
for _, op := range opReturns { | ||
if outPut, err = output.NewOpReturn([]byte(op.Data)); err != nil { | ||
return "", err | ||
} | ||
tx.AddOutput(outPut) | ||
} | ||
|
||
// Decode the WIF | ||
var decodedWif *bsvutil.WIF | ||
if decodedWif, err = bsvutil.DecodeWIF(wif); err != nil { | ||
return "", err | ||
} | ||
|
||
// Sign the transaction | ||
signer := signature.InternalSigner{PrivateKey: decodedWif.PrivKey, SigHashFlag: 0} | ||
if err = tx.SignAuto(&signer); err != nil { | ||
return "", err | ||
} | ||
|
||
// Return the transaction as a raw string | ||
return tx.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,35 @@ | ||
package bitcoin | ||
|
||
import "testing" | ||
|
||
// TestCreateTx will test the method CreateTx() | ||
func TestCreateTx(t *testing.T) { | ||
|
||
// Example from: https://github.com/libsv/libsv | ||
|
||
// Use a new UTXO | ||
utxo := &Utxo{ | ||
TxID: "b7b0650a7c3a1bd4716369783876348b59f5404784970192cec1996e86950576", | ||
Vout: 0, | ||
ScriptSig: "76a9149cbe9f5e72fa286ac8a38052d1d5337aa363ea7f88ac", | ||
Satoshis: 1000, | ||
} | ||
|
||
// Add a pay-to address | ||
payTo := &PayToAddress{ | ||
Address: "1C8bzHM8XFBHZ2ZZVvFy2NSoAZbwCXAicL", | ||
Satoshis: 500, | ||
} | ||
|
||
// Add some op return data | ||
opReturns := &OpReturnData{Data: "This is the example data!"} | ||
|
||
// Generate the TX | ||
rawTx, err := CreateTx([]*Utxo{utxo}, []*PayToAddress{payTo}, []*OpReturnData{opReturns}, "L3VJH2hcRGYYG6YrbWGmsxQC1zyYixA82YjgEyrEUWDs4ALgk8Vu") | ||
if err != nil { | ||
t.Fatalf("error occurred: %s", err.Error()) | ||
} | ||
|
||
// Show the results | ||
t.Logf("created tx: %s", rawTx) | ||
} |