Skip to content

Commit

Permalink
Minor changes, polish
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 29, 2020
1 parent 2d67bfd commit 73d4096
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 50 deletions.
23 changes: 13 additions & 10 deletions b.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Package b is a library for working with B transactions (Bitcoin OP_RETURN protocol) in Go
//
// If you have any suggestions or comments, please feel free to open an issue on
// this GitHub repository!
//
// By BitcoinSchema Organization (https://bitcoinschema.org)
package b

import (
"encoding/base64"
"fmt"
"log"
"strings"

"github.com/bitcoinschema/go-bob"
Expand Down Expand Up @@ -34,7 +39,7 @@ func New() *B {
// FromTape takes a BOB Tape and returns a B data structure
func (b *B) FromTape(tape bob.Tape) error {
if len(tape.Cell) < 4 || tape.Cell[0].S != Prefix {
return fmt.Errorf("Invalid B tx Only %d pushdatas", len(tape.Cell))
return fmt.Errorf("invalid B tx Only %d pushdatas", len(tape.Cell))
}

b.MediaType = tape.Cell[2].S
Expand All @@ -47,7 +52,6 @@ func (b *B) FromTape(tape bob.Tape) error {
// Decode base64 data
data, err := base64.StdEncoding.DecodeString(tape.Cell[1].B)
if err != nil {
log.Println("Failed to decode b64 signature", err)
return err
}
b.Data.Bytes = data
Expand All @@ -64,14 +68,13 @@ func (b *B) FromTape(tape bob.Tape) error {
return nil
}

// BitFsURL is a helper to create a bitfs url to fetch the content over http
func BitFsURL(txid string, outIndex int, scriptChunk int) string {
return fmt.Sprintf("https://x.bitfs.network/%s.out.%d.%d", txid, outIndex, scriptChunk)
}

// DataURI returns a b64 encoded image that can be set directly. Ex: <img src="b64data" />
func (b *B) DataURI() string {
// encode raw bytes to b64
s := base64.StdEncoding.EncodeToString(b.Data.Bytes)
return fmt.Sprintf("data:%s;base64,%s", b.Encoding, s)
return fmt.Sprintf("data:%s;base64,%s", b.Encoding, base64.StdEncoding.EncodeToString(b.Data.Bytes))
}

// BitFsURL is a helper to create a bitfs url to fetch the content over http
func BitFsURL(txid string, outIndex int, scriptChunk int) string {
return fmt.Sprintf("https://x.bitfs.network/%s.out.%d.%d", txid, outIndex, scriptChunk)
}
Loading

0 comments on commit 73d4096

Please sign in to comment.