Skip to content

Commit

Permalink
feat(core/btc): add runeId string & unmarshal from str
Browse files Browse the repository at this point in the history
  • Loading branch information
Gkirito committed Apr 14, 2024
1 parent dd28b59 commit 12d5462
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion core/btc/runes/rune_id.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package runes

import "math/big"
import (
"errors"
"fmt"
"math/big"
"strconv"
"strings"
)

var (
ErrIllegalRuneIdString = errors.New("illegal rune id string")
)

type RuneId struct {
Block uint64
Expand All @@ -20,3 +30,23 @@ func (r *RuneId) Next(block big.Int, tx big.Int) *RuneId {
}
return &RuneId{newBlock, newTx}
}

func (r *RuneId) String() string {
return fmt.Sprintf("%d:%d", r.Block, r.Tx)
}

func NewRuneIdFromStr(runeIdStr string) (*RuneId, error) {
runeIdData := strings.Split(runeIdStr, ":")
if len(runeIdData) != 2 {
return nil, ErrIllegalRuneIdString
}
block, err := strconv.ParseUint(runeIdData[0], 10, 64)
if err != nil {
return nil, err
}
tx, err := strconv.ParseUint(runeIdData[1], 10, 64)
if err != nil {
return nil, err
}
return &RuneId{block, uint32(tx)}, nil
}

0 comments on commit 12d5462

Please sign in to comment.