package wirelesson import ( "bytes" "crypto/sha256" "encoding/binary" "errors" "io" ) const Mainnet uint32 = 0xd9b4bef9 const Testnet3 uint32 = 0x0709110b const MaxPayload = 4_000_000 // educational receive limit func CompactSize(n uint64) []byte { var b [9]byte switch { case n < 253: return []byte{byte(n)} case n <= 65535: b[0] = 253 binary.LittleEndian.PutUint16(b[1:], uint16(n)) return b[:3] case n <= 0xffffffff: b[0] = 254 binary.LittleEndian.PutUint32(b[1:], uint32(n)) return b[:5] default: b[0] = 255 binary.LittleEndian.PutUint64(b[1:], n) return b[:] } } func ReadCompactSize(r io.Reader) (uint64, error) { var b [8]byte if _, e := io.ReadFull(r, b[:1]); e != nil { return 0, e } p := b[0] size := 0 var min uint64 switch p { case 253: size = 2 min = 253 case 254: size = 4 min = 65536 case 255: size = 8 min = 0x100000000 default: return uint64(p), nil } b = [8]byte{} if _, e := io.ReadFull(r, b[:size]); e != nil { return 0, e } n := binary.LittleEndian.Uint64(b[:]) if n < min { return 0, errors.New("noncanonical CompactSize") } return n, nil } func Checksum(payload []byte) [4]byte { a := sha256.Sum256(payload) b := sha256.Sum256(a[:]) return [4]byte(b[:4]) } func validCommand(s string) bool { if len(s) == 0 || len(s) > 12 { return false } for _, c := range []byte(s) { if c < 32 || c > 126 { return false } } return true } func Encode(magic uint32, command string, payload []byte) ([]byte, error) { if !validCommand(command) || len(payload) > MaxPayload { return nil, errors.New("invalid message") } b := make([]byte, 24+len(payload)) binary.LittleEndian.PutUint32(b, magic) copy(b[4:16], command) binary.LittleEndian.PutUint32(b[16:20], uint32(len(payload))) c := Checksum(payload) copy(b[20:24], c[:]) copy(b[24:], payload) return b, nil } func Decode(r io.Reader, magic uint32) (string, []byte, error) { var h [24]byte if _, e := io.ReadFull(r, h[:]); e != nil { return "", nil, e } if binary.LittleEndian.Uint32(h[:4]) != magic { return "", nil, errors.New("wrong network") } cmd := string(bytes.TrimRight(h[4:16], "\x00")) if !validCommand(cmd) { return "", nil, errors.New("invalid command") } n := binary.LittleEndian.Uint32(h[16:20]) if n > MaxPayload { return "", nil, errors.New("payload limit") } p := make([]byte, int(n)) if _, e := io.ReadFull(r, p); e != nil { return "", nil, e } c := Checksum(p) if !bytes.Equal(c[:], h[20:24]) { return "", nil, errors.New("checksum mismatch") } return cmd, p, nil } func NoncePayload(n uint64) []byte { b := make([]byte, 8) binary.LittleEndian.PutUint64(b, n) return b } func ReadNonce(p []byte) (uint64, error) { if len(p) != 8 { return 0, errors.New("nonce must be eight bytes") } return binary.LittleEndian.Uint64(p), nil } // VersionPrefix decodes only the mandatory initial fields, not the full version message. func VersionPrefix(p []byte) (int32, uint64, int64, error) { if len(p) < 20 { return 0, 0, 0, io.ErrUnexpectedEOF } return int32(binary.LittleEndian.Uint32(p)), binary.LittleEndian.Uint64(p[4:]), int64(binary.LittleEndian.Uint64(p[12:])), nil }