Skip to content

Commit b9c03d1

Browse files
committed
add tostring and test
1 parent 2092c09 commit b9c03d1

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ type Tx struct {
3939
// BOB from libsv.transaction
4040
bobData := bob.New()
4141
err = bobData.FromTx(tx)
42-
if err != nil {
43-
return err
44-
}
4542

4643
```
4744

@@ -51,9 +48,6 @@ if err != nil {
5148
// BOB from libsv.transaction
5249
bobData := bob.New()
5350
tx, err = bobData.ToTx()
54-
if err != nil {
55-
return err
56-
}
5751

5852
```
5953

@@ -62,7 +56,7 @@ if err != nil {
6256
```go
6357
// BOB formatted JSON string
6458
bobData := bob.New()
65-
tx = bobData.ToString()
59+
tx, err = bobData.ToString()
6660

6761
```
6862

@@ -72,12 +66,14 @@ tx = bobData.ToString()
7266
// BOB to raw tx string
7367
bobData := bob.New()
7468
tx, err = bobData.ToRawTxString()
75-
if err != nil {
76-
return err
77-
}
7869

7970
```
8071

81-
## ToDo
72+
## BOB from raw tx string
8273

83-
- FromRawTx
74+
```go
75+
// BOB from raw tx string
76+
bobTx := New()
77+
err := bobTx.FromRawTxString(rawTxString)
78+
79+
```

bob.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ func (t *BobTx) FromString(line string) error {
9494
return nil
9595
}
9696

97+
// FromRawTxString takes a hex encoded tx string
98+
func (t *BobTx) FromRawTxString(rawTxString string) error {
99+
100+
tx, err := transaction.NewFromString(rawTxString)
101+
if err != nil {
102+
return err
103+
}
104+
return t.FromTx(tx)
105+
}
106+
97107
// FromTx takes a libsv.Transaction
98108
func (t *BobTx) FromTx(tx *transaction.Transaction) error {
99109

@@ -183,6 +193,16 @@ func (t *BobTx) FromTx(tx *transaction.Transaction) error {
183193
return nil
184194
}
185195

196+
// ToString returns a json string of bobTx
197+
func (t *BobTx) ToString() (string, error) {
198+
// Create JSON from the instance data.
199+
// ... Ignore errors.
200+
b, err := json.Marshal(t)
201+
// Convert bytes to string.
202+
return string(b), err
203+
204+
}
205+
186206
// ToTx returns a transaction.Transaction
187207
func (t *BobTx) ToTx() (*transaction.Transaction, error) {
188208
tx := transaction.New()
@@ -278,11 +298,7 @@ func (t *BobTx) FromBytes(line []byte) error {
278298
for cellIdx, cell := range tape.Cell {
279299
if len(cell.H) == 0 && len(cell.B) > 0 {
280300
// base 64 decode cell.B and encode it to hex string
281-
cellBytes, e := base64.StdEncoding.DecodeString(cell.B)
282-
if e != nil {
283-
fmt.Println(e)
284-
}
285-
log.Println("Replacing!", hex.EncodeToString(cellBytes))
301+
cellBytes, _ := base64.StdEncoding.DecodeString(cell.B)
286302
t.Out[outIdx].Tape[tapeIdx].Cell[cellIdx].H = hex.EncodeToString(cellBytes)
287303
}
288304
}
@@ -293,11 +309,7 @@ func (t *BobTx) FromBytes(line []byte) error {
293309
for cellIdx, cell := range tape.Cell {
294310
if len(cell.H) == 0 && len(cell.B) > 0 {
295311
// base 64 decode cell.B and encode it to hex string
296-
cellBytes, e := base64.StdEncoding.DecodeString(cell.B)
297-
if e != nil {
298-
fmt.Println(e)
299-
}
300-
log.Println("Replacing!", hex.EncodeToString(cellBytes))
312+
cellBytes, _ := base64.StdEncoding.DecodeString(cell.B)
301313
t.In[inIdx].Tape[tapeIdx].Cell[cellIdx].H = hex.EncodeToString(cellBytes)
302314
}
303315
}

bob_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,45 @@ func TestToRawTxString(t *testing.T) {
204204
}
205205
}
206206

207+
func TestToString(t *testing.T) {
208+
rawTxString := "0100000001f15a9d3c550c14e12ca066ad09edff31432f1e9f45894ecff5b70c8354c81f3d010000006b483045022100f012c3bd3781091aa8e53cab2ffcb90acced8c65500b41086fd225e48c98c1d702200b8ff117b8ecd2b2d7e95551bc5a1b3bbcca8049864479a28bed9dc842a86804412103ef5bb22964d529c0af748d9a6381432f05298e7a66ed2fe22e7975b1502528a7ffffffff0200000000000000001f006a15e4b880e781afe883bde999a4e58d83e5b9b4e69a970635386135393733b30100000000001976a9149c63715c6d1fa6c61b31d2911516e1c3db3bdfa888ac00000000"
209+
210+
bobTx := New()
211+
err := bobTx.FromRawTxString(rawTxString)
212+
if err != nil {
213+
t.Errorf("Error %s", err)
214+
}
215+
216+
// to string
217+
txString, err := bobTx.ToString()
218+
if err != nil {
219+
t.Errorf("Error %s", err)
220+
}
221+
222+
// from string
223+
otherBob := New()
224+
err = otherBob.FromString(txString)
225+
if err != nil {
226+
t.Errorf("Error %s", err)
227+
}
228+
229+
// check txid match
230+
if bobTx.Tx.H != otherBob.Tx.H {
231+
t.Errorf("TXIDS do not match!")
232+
}
233+
}
234+
235+
func TestFromRawTx(t *testing.T) {
236+
237+
rawTxString := "0100000001f15a9d3c550c14e12ca066ad09edff31432f1e9f45894ecff5b70c8354c81f3d010000006b483045022100f012c3bd3781091aa8e53cab2ffcb90acced8c65500b41086fd225e48c98c1d702200b8ff117b8ecd2b2d7e95551bc5a1b3bbcca8049864479a28bed9dc842a86804412103ef5bb22964d529c0af748d9a6381432f05298e7a66ed2fe22e7975b1502528a7ffffffff0200000000000000001f006a15e4b880e781afe883bde999a4e58d83e5b9b4e69a970635386135393733b30100000000001976a9149c63715c6d1fa6c61b31d2911516e1c3db3bdfa888ac00000000"
238+
239+
bobTx := New()
240+
err := bobTx.FromRawTxString(rawTxString)
241+
if err != nil {
242+
t.Errorf("Error %s", err)
243+
}
244+
}
245+
207246
func TestFromBadString(t *testing.T) {
208247
bobBadStrings := New()
209248
err := bobBadStrings.FromString(sampleBobTxBadStrings)

0 commit comments

Comments
 (0)