github.com/storacha/go-ucanto@v0.7.2/principal/multiformat/multiformat.go (about) 1 package multiformat 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/multiformats/go-varint" 8 ) 9 10 func TagWith(code uint64, bytes []byte) []byte { 11 offset := varint.UvarintSize(code) 12 tagged := make([]byte, len(bytes)+offset) 13 varint.PutUvarint(tagged, code) 14 copy(tagged[offset:], bytes) 15 return tagged 16 } 17 18 func UntagWith(code uint64, source []byte, offset int) ([]byte, error) { 19 b := source 20 if offset != 0 { 21 b = source[offset:] 22 } 23 24 tag, err := varint.ReadUvarint(bytes.NewReader(b)) 25 if err != nil { 26 return nil, err 27 } 28 29 if tag != code { 30 return nil, fmt.Errorf("expected multiformat with 0x%x tag instead got 0x%x", code, tag) 31 } 32 33 size := varint.UvarintSize(code) 34 return b[size:], nil 35 }