Skip to content

Commit

Permalink
more helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Nov 18, 2024
1 parent c08e55e commit cb3c6eb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 50 deletions.
29 changes: 0 additions & 29 deletions boc/boc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package boc

import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"hash/crc32"
Expand Down Expand Up @@ -269,33 +267,6 @@ func DeserializeBoc(boc []byte) ([]*Cell, error) {
return rootCells, nil
}

func DeserializeBocBase64(boc string) ([]*Cell, error) {
bocData, err := base64.StdEncoding.DecodeString(boc)
if err != nil {
return nil, err
}
return DeserializeBoc(bocData)
}

func DeserializeSinglRootBase64(boc string) (*Cell, error) {
cells, err := DeserializeBocBase64(boc)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid boc roots number %v", len(cells))
}
return cells[0], nil
}

func DeserializeBocHex(boc string) ([]*Cell, error) {
bocData, err := hex.DecodeString(boc)
if err != nil {
return nil, err
}
return DeserializeBoc(bocData)
}

func SerializeBoc(cell *Cell, idx bool, hasCrc32 bool, cacheBits bool, flags uint) ([]byte, error) {
bag := newBagOfCells()
return bag.serializeBoc([]*Cell{cell}, idx, hasCrc32, cacheBits, flags)
Expand Down
80 changes: 80 additions & 0 deletions boc/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package boc

import (
"encoding/base64"
"encoding/hex"
"fmt"
)

func DeserializeSingleRootBoc(boc []byte) (*Cell, error) {
cells, err := DeserializeBoc(boc)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid boc roots number %v", len(cells))
}
return cells[0], nil
}

func DeserializeBocBase64(boc string) ([]*Cell, error) {
bocData, err := base64.StdEncoding.DecodeString(boc)
if err != nil {
return nil, err
}
return DeserializeBoc(bocData)
}

func DeserializeSinglRootBase64(boc string) (*Cell, error) {
cells, err := DeserializeBocBase64(boc)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid boc roots number %v", len(cells))
}
return cells[0], nil
}

func DeserializeBocHex(boc string) ([]*Cell, error) {
bocData, err := hex.DecodeString(boc)
if err != nil {
return nil, err
}
return DeserializeBoc(bocData)
}

func DeserializeSinglRootHex(boc string) (*Cell, error) {
cells, err := DeserializeBocHex(boc)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid boc roots number %v", len(cells))
}
return cells[0], nil
}

func MustDeserializeSinglRootHex(boc string) *Cell {
c, err := DeserializeSinglRootHex(boc)
if err != nil {
panic(err)
}
return c
}

func MustDeserializeSinglRootBase64(boc string) *Cell {
c, err := DeserializeSinglRootBase64(boc)
if err != nil {
panic(err)
}
return c
}

func MustBitStringFromFiftHex(hexRepr string) *BitString {
bs, err := BitStringFromFiftHex(hexRepr)
if err != nil {
panic(err)
}
return bs
}
10 changes: 0 additions & 10 deletions boc/utils.go

This file was deleted.

15 changes: 4 additions & 11 deletions utils/any_to_cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)
Expand All @@ -21,23 +20,17 @@ func AnyToCell(i any) (*boc.Cell, error) {
return nil, err
}
}
cells, err := boc.DeserializeBoc(b)
cell, err := boc.DeserializeSingleRootBoc(b)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid number of cells: %v", len(cells))
}
return cells[0], nil
return cell, nil
case []byte:
cells, err := boc.DeserializeBoc(v)
cell, err := boc.DeserializeSingleRootBoc(v)
if err != nil {
return nil, err
}
if len(cells) != 1 {
return nil, fmt.Errorf("invalid number of cells: %v", len(cells))
}
return cells[0], nil
return cell, nil
case *boc.Cell:
return v, nil
case boc.Cell:
Expand Down

0 comments on commit cb3c6eb

Please sign in to comment.