Skip to content

Commit

Permalink
add bt.Tx to parse config object
Browse files Browse the repository at this point in the history
  • Loading branch information
rohenaz committed Mar 18, 2023
1 parent ddc97bf commit fc70ed6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ var splitConfig = []SplitConfig{
},
}

bpuTx, err := Parse(ParseConfig{RawTxHex: sampleTx, SplitConfig: splitConfig})
bpuTx, err := Parse(ParseConfig{RawTxHex: &sampleTx, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
```

## Transaction Source

You can either use SplitConfig.RawTxHex and set by hex string, or use SplitConfig.Tx to set by bt.Tx.

## Transform Function

You can pass an optional Transform function to bpu.Parse. Function should look something like this:
Expand Down
23 changes: 14 additions & 9 deletions bpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Parse is the main transformation function for the bpu package
func Parse(config ParseConfig) (bpuTx *BpuTx, err error) {
bpuTx = new(BpuTx)
err = bpuTx.fromTx(config)
err = bpuTx.fromConfig(config)
if err != nil {
return nil, err
}
Expand All @@ -28,14 +28,19 @@ var defaultTransform Transform = func(r Cell, c string) (to *Cell, err error) {
}

// convert a raw tx to a bpu tx
func (b *BpuTx) fromTx(config ParseConfig) (err error) {
if len(config.RawTxHex) == 0 {
return errors.New("raw tx must be set")
}

gene, err := bt.NewTxFromString(config.RawTxHex)
if err != nil {
return fmt.Errorf("failed to parse tx: %e", err)
func (b *BpuTx) fromConfig(config ParseConfig) (err error) {
var gene *bt.Tx
if config.Tx != nil {
gene = config.Tx
} else {
if config.RawTxHex == nil || len(*config.RawTxHex) == 0 {
return errors.New("raw tx must be set")
} else {
gene, err = bt.NewTxFromString(*config.RawTxHex)
if err != nil {
return fmt.Errorf("failed to parse tx: %e", err)
}
}
}

var inXputs []XPut
Expand Down
10 changes: 5 additions & 5 deletions bpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var splitTransform Transform = func(o Cell, c string) (to *Cell, e error) {

func TestTransform(t *testing.T) {
t.Run("bpu.Transform", func(t *testing.T) {
bpuTx, err := Parse(ParseConfig{RawTxHex: sampleTx, SplitConfig: splitConfig, Transform: &splitTransform})
bpuTx, err := Parse(ParseConfig{RawTxHex: &sampleTx, SplitConfig: splitConfig, Transform: &splitTransform})
if err != nil {
fmt.Println(err)
}
Expand All @@ -79,7 +79,7 @@ func TestBpu(t *testing.T) {

t.Run("bpu.Parse", func(t *testing.T) {

bpuTx, err := Parse(ParseConfig{RawTxHex: sampleTx, SplitConfig: splitConfig})
bpuTx, err := Parse(ParseConfig{RawTxHex: &sampleTx, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestUnicode(t *testing.T) {
func TestBpuBitchat(t *testing.T) {

t.Run("bpu.Parse bitchat", func(t *testing.T) {
bpuTx, err := Parse(ParseConfig{RawTxHex: bitchatTx, SplitConfig: splitConfig})
bpuTx, err := Parse(ParseConfig{RawTxHex: &bitchatTx, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
Expand All @@ -152,7 +152,7 @@ func TestBpuBitchat(t *testing.T) {
func TestBpuBuster(t *testing.T) {

t.Run("bpu.Parse bpu buster", func(t *testing.T) {
bpuTx, err := Parse(ParseConfig{RawTxHex: bpuBuster, SplitConfig: splitConfig})
bpuTx, err := Parse(ParseConfig{RawTxHex: &bpuBuster, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
Expand All @@ -174,7 +174,7 @@ func TestBpuBuster(t *testing.T) {
func TestBoost(t *testing.T) {

t.Run("bpu.Parse boost", func(t *testing.T) {
bpuTx, err := Parse(ParseConfig{RawTxHex: boostTx, SplitConfig: splitConfig})
bpuTx, err := Parse(ParseConfig{RawTxHex: &boostTx, SplitConfig: splitConfig})
if err != nil {
fmt.Println(err)
}
Expand Down
5 changes: 4 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bpu

import "github.com/libsv/go-bt/v2"

type IncludeType string

const (
Expand All @@ -23,7 +25,8 @@ type SplitConfig struct {
}

type ParseConfig struct {
RawTxHex string `json:"tx" bson:"tx"`
Tx *bt.Tx `json:"tx" bson:"tx"`
RawTxHex *string `json:"rawTx" bson:"rawTx"`
SplitConfig []SplitConfig `json:"split,omitempty" bson:"split,omitempty"`
Transform *Transform `json:"transform,omitempty" bson:"transform,omitempty"`
}
Expand Down

0 comments on commit fc70ed6

Please sign in to comment.