Skip to content

Instantly share code, notes, and snippets.

@losh11
Created April 13, 2023 00:57
Show Gist options
  • Save losh11/89a89190138a1d330ce2d20aad4bee94 to your computer and use it in GitHub Desktop.
Save losh11/89a89190138a1d330ce2d20aad4bee94 to your computer and use it in GitHub Desktop.
// FORMATTED FOR LTCSUITE/LTCD
// readMWTX
func (msg *MsgTx) readMWTX(r io.Reader) ([]byte, bool, error) {
var tee = new(bytes.Buffer)
haveMWTX, err := binarySerializer.Uint8(r) //d.readByte()
if err != nil {
return nil, false, err
}
if haveMWTX == 0 {
return nil, true, nil // HogEx - that's all folks
}
if err = discardBytes(r, tee, 64); err != nil {
return nil, false, err
}
// TxBody
kern0, err := msg.readMWTXBody(r, tee)
if err != nil {
return nil, false, err
}
return kern0, false, nil
}
// readMWTXBody
func (msg *MsgTx) readMWTXBody(r io.Reader, tee *bytes.Buffer) ([]byte, error) {
numIn, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
// inputs
for i := 0; i < int(numIn); i++ {
feats, err := binarySerializer.Uint8(r)
if err != nil {
return nil, err
}
if err = discardBytes(r, tee, 32+33+33); err != nil { // outputID, commitment, outputPubKey
return nil, err
}
if feats&0x1 != 0 { // input pubkey
if err = discardBytes(r, tee, 33); err != nil {
return nil, err
}
}
if feats&0x2 != 0 { // extraData
if err = discardVect(r, tee); err != nil {
return nil, err
}
}
if err = discardBytes(r, tee, 64); err != nil { // sig
return nil, err
}
}
// outputs
numOut, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
for i := 0; i < int(numOut); i++ {
if err = discardBytes(r, tee, 33+33+33); err != nil { // commitment, sender pk, receiver pk
return nil, err
}
feats, err := binarySerializer.Uint8(r)
if err != nil {
return nil, err
}
if feats&0x1 != 0 { // pubkey | view_tag uint8_t | masked_value uint64_t | nonce 16-bytes
if err = discardBytes(r, tee, 33+1+8+16); err != nil {
return nil, err
}
}
if feats&0x2 != 0 { // extraData
if err = discardVect(r, tee); err != nil {
return nil, err
}
}
// RangeProof "The proof itself, at most 675 bytes long."
// std::vector<uint8_t> m_bytes; -- except it's actually used like a [675]byte...
if err = discardBytes(r, tee, 675+64); err != nil { // proof + sig
return nil, err
}
}
// kernels
numKerns, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
var kern0 []byte
tee = new(bytes.Buffer)
for i := 0; i < int(numKerns); i++ {
feats, err := binarySerializer.Uint8(r)
if err != nil {
return nil, err
}
if feats&0x1 != 0 { // fee
_, err = readVLQ(r) // vlq for amount? in the wire protocol?!?
if err != nil {
return nil, err
}
}
if feats&0x2 != 0 { // pegin amt
_, err = readVLQ(r)
if err != nil {
return nil, err
}
}
if feats&0x4 != 0 { // pegouts vector
sz, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
for i := uint64(0); i < sz; i++ {
_, err = readVLQ(r) // pegout amt
if err != nil {
return nil, err
}
if err = discardVect(r, tee); err != nil { // pkScript
return nil, err
}
}
}
if feats&0x8 != 0 { // lockHeight
_, err = readVLQ(r)
if err != nil {
return nil, err
}
}
if feats&0x10 != 0 { // stealth_excess pubkey
if err = discardBytes(r, tee, 33); err != nil {
return nil, err
}
}
if feats&0x20 != 0 { // extraData vector
if err = discardVect(r, tee); err != nil {
return nil, err
}
}
// "excess" commitment and signature
if err = discardBytes(r, tee, 33+64); err != nil {
return nil, err
}
if i == 0 {
kern0 = tee.Bytes()
tee = nil
}
}
return kern0, nil
}
// common
func discardBytes(r io.Reader, tee *bytes.Buffer, n int64) error {
w := io.Discard
if tee != nil {
w = tee
}
m, err := io.CopyN(w, r, n)
if err != nil {
return err
}
if m != n {
return fmt.Errorf("only discarded %d of %d bytes", m, n)
}
return nil
}
func discardVect(r io.Reader, tee *bytes.Buffer) error {
sz, err := ReadVarInt(r, 0)
if err != nil {
return err
}
return discardBytes(r, tee, int64(sz))
}
func readVLQ(r io.Reader) (uint64, error) {
var n uint64
for {
val, err := binarySerializer.Uint8(r)
if err != nil {
return 0, err
}
n = (n << 7) | uint64(val&0x7f)
if val&0x80 != 0x80 {
break
}
n++
}
return n, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment