Reworked encode/decodes with fluent splicer
The best changes are when you are deleting more than you are adding :D
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -37,12 +38,11 @@ func (x *Layer) Insert(o types.Onion) {}
|
||||
func (x *Layer) Len() int { return Len }
|
||||
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ConfID[:])
|
||||
s := slice.NewUint64()
|
||||
slice.EncodeUint64(s, uint64(x.MilliSatoshi))
|
||||
copy(b[*c:c.Inc(slice.Uint64Len)], s)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
ID(x.ID).
|
||||
ID(x.ConfID).
|
||||
Uint64(uint64(x.MilliSatoshi))
|
||||
}
|
||||
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
@@ -50,9 +50,9 @@ func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len,
|
||||
string(Magic))
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
copy(x.ConfID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
x.MilliSatoshi = lnwire.MilliSatoshi(
|
||||
slice.DecodeUint64(b[*c:c.Inc(slice.Uint64Len)]))
|
||||
splice.Splice(b, c).
|
||||
ReadID(&x.ID).
|
||||
ReadID(&x.ConfID).
|
||||
ReadMilliSatoshi(&x.MilliSatoshi)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -47,19 +48,18 @@ type Layer struct {
|
||||
func (x *Layer) Insert(o types.Onion) {}
|
||||
func (x *Layer) Len() int { return Len }
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
// Copy in the ID.
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
b[*c] = x.Load
|
||||
c.Inc(1)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
ID(x.ID).
|
||||
Byte(x.Load)
|
||||
}
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]),
|
||||
Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
x.Load = b[*c]
|
||||
c.Inc(1)
|
||||
splice.Splice(b, c).
|
||||
ReadID(&x.ID).
|
||||
ReadByte(&x.Load)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/reverse"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -62,14 +63,11 @@ func (x *Layer) Len() int {
|
||||
return Len + x.Onion.Len()
|
||||
}
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonce[:])
|
||||
// Derive the cloaked key and copy it in.
|
||||
to := cloak.GetCloak(x.ToHeaderPub)
|
||||
copy(b[*c:c.Inc(cloak.Len)], to[:])
|
||||
// Derive the public key from the From key and copy in.
|
||||
pubKey := pub.Derive(x.From).ToBytes()
|
||||
copy(b[*c:c.Inc(pub.KeyLen)], pubKey[:])
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
IV(x.Nonce).
|
||||
Cloak(x.ToHeaderPub).
|
||||
Pubkey(x.From)
|
||||
start := int(*c)
|
||||
// Call the tree of onions to perform their encoding.
|
||||
x.Onion.Encode(b, c)
|
||||
@@ -99,11 +97,10 @@ func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len, "message")
|
||||
}
|
||||
copy(x.Nonce[:], b[*c:c.Inc(nonce.IVLen)])
|
||||
copy(x.Cloak[:], b[*c:c.Inc(cloak.Len)])
|
||||
if x.FromPub, e = pub.FromBytes(b[*c:c.Inc(pub.KeyLen)]); check(e) {
|
||||
return
|
||||
}
|
||||
splice.Splice(b, c).
|
||||
ReadIV(&x.Nonce).
|
||||
ReadCloak(&x.Cloak).
|
||||
ReadPubkey(&x.FromPub)
|
||||
// A further step is required which decrypts the remainder of the bytes
|
||||
// after finding the private key corresponding to the Cloak.
|
||||
return
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -32,17 +33,17 @@ func (x *Layer) Insert(_ types.Onion) {}
|
||||
func (x *Layer) Len() int { return Len }
|
||||
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
slice.EncodeUint64(b[*c:c.Inc(slice.Uint64Len)], uint64(x.Duration))
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
Uint64(uint64(x.Duration))
|
||||
x.Onion.Encode(b, c)
|
||||
}
|
||||
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len,
|
||||
string(Magic))
|
||||
return magicbytes.TooShort(len(b[*c:]),
|
||||
Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
x.Duration = time.Duration(
|
||||
slice.DecodeUint64(b[*c:c.Inc(slice.Uint64Len)]))
|
||||
splice.Splice(b, c).ReadDuration(&x.Duration)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -46,19 +47,18 @@ type Layer struct {
|
||||
func (x *Layer) Insert(o types.Onion) {}
|
||||
func (x *Layer) Len() int { return Len }
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
// Copy in the ID.
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
b[*c] = x.Load
|
||||
c.Inc(1)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
ID(x.ID).
|
||||
Byte(x.Load)
|
||||
}
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]),
|
||||
Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
x.Load = b[*c]
|
||||
c.Inc(1)
|
||||
splice.Splice(b, c).
|
||||
ReadID(&x.ID).
|
||||
ReadByte(&x.Load)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -46,7 +47,6 @@ type Layer struct {
|
||||
types.Onion
|
||||
}
|
||||
|
||||
//
|
||||
// func (x *Layer) String() string {
|
||||
// return spew.Sdump(x.Port, x.Ciphers, x.Nonces, x.Bytes.ToBytes())
|
||||
// }
|
||||
@@ -57,21 +57,13 @@ func (x *Layer) Len() int {
|
||||
}
|
||||
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
port := slice.NewUint16()
|
||||
slice.EncodeUint16(port, int(x.Port))
|
||||
copy(b[*c:c.Inc(slice.Uint16Len)], port)
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[0][:])
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[2][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[0][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[1][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[2][:])
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
bytesLen := slice.NewUint32()
|
||||
slice.EncodeUint32(bytesLen, len(x.Bytes))
|
||||
copy(b[*c:c.Inc(slice.Uint32Len)], bytesLen)
|
||||
copy(b[*c:c.Inc(len(x.Bytes))], x.Bytes)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
Uint16(x.Port).
|
||||
Hash(x.Ciphers[0]).Hash(x.Ciphers[1]).Hash(x.Ciphers[2]).
|
||||
IV(x.Nonces[0]).IV(x.Nonces[1]).IV(x.Nonces[2]).
|
||||
ID(x.ID).
|
||||
Bytes(x.Bytes)
|
||||
x.Onion.Encode(b, c)
|
||||
}
|
||||
|
||||
@@ -79,19 +71,11 @@ func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
x.Port = uint16(slice.DecodeUint16(b[*c:c.Inc(slice.Uint16Len)]))
|
||||
for i := range x.Ciphers {
|
||||
bytes := b[*c:c.Inc(sha256.Len)]
|
||||
copy(x.Ciphers[i][:], bytes)
|
||||
bytes.Zero()
|
||||
}
|
||||
for i := range x.Nonces {
|
||||
bytes := b[*c:c.Inc(nonce.IVLen)]
|
||||
copy(x.Nonces[i][:], bytes)
|
||||
bytes.Zero()
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
bytesLen := slice.DecodeUint32(b[*c:c.Inc(slice.Uint32Len)])
|
||||
x.Bytes = b[*c:c.Inc(bytesLen)]
|
||||
splice.Splice(b, c).
|
||||
ReadUint16(&x.Port).
|
||||
ReadHash(&x.Ciphers[0]).ReadHash(&x.Ciphers[1]).ReadHash(&x.Ciphers[2]).
|
||||
ReadIV(&x.Nonces[0]).ReadIV(&x.Nonces[1]).ReadIV(&x.Nonces[2]).
|
||||
ReadID(&x.ID).
|
||||
ReadBytes(&x.Bytes)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,13 +7,15 @@ import (
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
const (
|
||||
MagicString = "fw"
|
||||
Len = magicbytes.Len + 1 + net.IPv6len + 2
|
||||
AddrLen = net.IPv6len + 2
|
||||
Len = magicbytes.Len + 1 + AddrLen
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -38,25 +40,16 @@ type Layer struct {
|
||||
func (x *Layer) Insert(o types.Onion) { x.Onion = o }
|
||||
func (x *Layer) Len() int { return Len + x.Onion.Len() }
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
var ap []byte
|
||||
var e error
|
||||
if ap, e = x.AddrPort.MarshalBinary(); check(e) {
|
||||
return
|
||||
}
|
||||
b[*c] = byte(len(ap))
|
||||
copy(b[c.Inc(1):c.Inc(Len-magicbytes.Len-1)], ap)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
AddrPort(x.AddrPort)
|
||||
x.Onion.Encode(b, c)
|
||||
}
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
apLen := b[*c]
|
||||
apBytes := b[c.Inc(1):c.Inc(Len-magicbytes.Len-1)]
|
||||
x.AddrPort = &netip.AddrPort{}
|
||||
if e = x.AddrPort.UnmarshalBinary(apBytes[:apLen]); check(e) {
|
||||
return
|
||||
}
|
||||
splice.Splice(b, c).
|
||||
ReadAddrPort(&x.AddrPort)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -40,21 +41,18 @@ type Layer struct {
|
||||
// func (x *Layer) String() string {
|
||||
// return spew.Sdump(x.Ciphers, x.Nonces)
|
||||
// }
|
||||
|
||||
func (x *Layer) Insert(o types.Onion) { x.Onion = o }
|
||||
func (x *Layer) Len() int {
|
||||
return Len + x.Onion.Len()
|
||||
}
|
||||
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ConfID[:])
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[0][:])
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
|
||||
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[2][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[0][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[1][:])
|
||||
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonces[2][:])
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
ID(x.ID).ID(x.ConfID).
|
||||
Hash(x.Ciphers[0]).Hash(x.Ciphers[1]).Hash(x.Ciphers[2]).
|
||||
IV(x.Nonces[0]).IV(x.Nonces[1]).IV(x.Nonces[2])
|
||||
x.Onion.Encode(b, c)
|
||||
}
|
||||
|
||||
@@ -63,17 +61,10 @@ func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
return magicbytes.TooShort(len(b[*c:]), Len-magicbytes.Len,
|
||||
string(Magic))
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
copy(x.ConfID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
for i := range x.Ciphers {
|
||||
bytes := b[*c:c.Inc(sha256.Len)]
|
||||
copy(x.Ciphers[i][:], bytes)
|
||||
bytes.Zero()
|
||||
}
|
||||
for i := range x.Nonces {
|
||||
bytes := b[*c:c.Inc(nonce.IVLen)]
|
||||
copy(x.Nonces[i][:], bytes)
|
||||
bytes.Zero()
|
||||
}
|
||||
|
||||
splice.Splice(b, c).
|
||||
ReadID(&x.ID).ReadID(&x.ConfID).
|
||||
ReadHash(&x.Ciphers[0]).ReadHash(&x.Ciphers[1]).ReadHash(&x.Ciphers[2]).
|
||||
ReadIV(&x.Nonces[0]).ReadIV(&x.Nonces[1]).ReadIV(&x.Nonces[2])
|
||||
return
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
@@ -38,30 +39,22 @@ func New() *Layer {
|
||||
func (x *Layer) Insert(_ types.Onion) {}
|
||||
func (x *Layer) Len() int { return Len + len(x.Bytes) }
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
copy(b[*c:c.Inc(nonce.IDLen)], x.ID[:])
|
||||
port := slice.NewUint16()
|
||||
slice.EncodeUint16(port, int(x.Port))
|
||||
copy(b[*c:c.Inc(slice.Uint16Len)], port)
|
||||
b[*c] = x.Load
|
||||
c.Inc(1)
|
||||
bytesLen := slice.NewUint32()
|
||||
slice.EncodeUint32(bytesLen, len(x.Bytes))
|
||||
copy(b[*c:c.Inc(slice.Uint32Len)], bytesLen)
|
||||
copy(b[*c:c.Inc(len(x.Bytes))], x.Bytes)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
ID(x.ID).
|
||||
Uint16(x.Port).
|
||||
Byte(x.Load).
|
||||
Bytes(x.Bytes)
|
||||
}
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < Len-magicbytes.Len {
|
||||
return magicbytes.TooShort(len(b[*c:]),
|
||||
Len-magicbytes.Len, string(Magic))
|
||||
}
|
||||
copy(x.ID[:], b[*c:c.Inc(nonce.IDLen)])
|
||||
port := slice.DecodeUint16(b[*c:c.Inc(slice.Uint16Len)])
|
||||
x.Port = uint16(port)
|
||||
x.Load = b[*c]
|
||||
c.Inc(1)
|
||||
responseLen := slice.DecodeUint32(b[*c:c.Inc(slice.Uint32Len)])
|
||||
bb := b[*c:c.Inc(responseLen)]
|
||||
x.Bytes = bb
|
||||
splice.Splice(b, c).
|
||||
ReadID(&x.ID).
|
||||
ReadUint16(&x.Port).
|
||||
ReadByte(&x.Load).
|
||||
ReadBytes(&x.Bytes)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package reverse
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/splice"
|
||||
"git-indra.lan/indra-labs/indra/pkg/types"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
const (
|
||||
MagicString = "rv"
|
||||
AddrLen = net.IPv6len + 3
|
||||
Len = magicbytes.Len + 1 + AddrLen
|
||||
Len = magicbytes.Len + 1 + splice.AddrLen
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -40,25 +39,22 @@ type Layer struct {
|
||||
func (x *Layer) Insert(o types.Onion) { x.Onion = o }
|
||||
func (x *Layer) Len() int { return Len + x.Onion.Len() }
|
||||
func (x *Layer) Encode(b slice.Bytes, c *slice.Cursor) {
|
||||
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
|
||||
var ap []byte
|
||||
var e error
|
||||
if ap, e = x.AddrPort.MarshalBinary(); check(e) {
|
||||
return
|
||||
}
|
||||
b[*c] = byte(len(ap))
|
||||
copy(b[c.Inc(1):c.Inc(AddrLen)], ap)
|
||||
splice.Splice(b, c).
|
||||
Magic(Magic).
|
||||
AddrPort(x.AddrPort)
|
||||
x.Onion.Encode(b, c)
|
||||
}
|
||||
func (x *Layer) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
|
||||
if len(b[*c:]) < AddrLen {
|
||||
return magicbytes.TooShort(len(b[*c:]), AddrLen, string(Magic))
|
||||
}
|
||||
apLen := b[*c]
|
||||
apBytes := b[c.Inc(1):c.Inc(AddrLen)]
|
||||
x.AddrPort = &netip.AddrPort{}
|
||||
if e = x.AddrPort.UnmarshalBinary(apBytes[:apLen]); check(e) {
|
||||
return
|
||||
if len(b[*c:]) < splice.AddrLen {
|
||||
return magicbytes.TooShort(len(b[*c:]), splice.AddrLen, string(Magic))
|
||||
}
|
||||
splice.Splice(b, c).
|
||||
ReadAddrPort(&x.AddrPort)
|
||||
// apLen := b[*c]
|
||||
// apBytes := b[c.Inc(1):c.Inc(AddrLen)]
|
||||
// x.AddrPort = &netip.AddrPort{}
|
||||
// if e = x.AddrPort.UnmarshalBinary(apBytes[:apLen]); check(e) {
|
||||
// return
|
||||
// }
|
||||
return
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestOnionSkins_Exit(t *testing.T) {
|
||||
ciphers := GenCiphers(prvs, pubs)
|
||||
var msg slice.Bytes
|
||||
var hash sha256.Hash
|
||||
if msg, hash, e = tests.GenMessage(512, ""); check(e) {
|
||||
if msg, hash, e = tests.GenMessage(512, "aoeu"); check(e) {
|
||||
t.Error(e)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
180
pkg/splice/splice.go
Normal file
180
pkg/splice/splice.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package splice
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"git-indra.lan/indra-labs/lnd/lnd/lnwire"
|
||||
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/key/cloak"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/key/prv"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/key/pub"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/nonce"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
|
||||
"git-indra.lan/indra-labs/indra/pkg/onion/magicbytes"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
"git-indra.lan/indra-labs/indra/pkg/util/slice"
|
||||
)
|
||||
|
||||
var (
|
||||
log = log2.GetLogger(indra.PathBase)
|
||||
check = log.E.Chk
|
||||
)
|
||||
|
||||
const AddrLen = net.IPv6len + 2
|
||||
|
||||
type Splicer struct {
|
||||
b slice.Bytes
|
||||
c *slice.Cursor
|
||||
}
|
||||
|
||||
func Splice(b slice.Bytes, c *slice.Cursor) *Splicer {
|
||||
return &Splicer{b, c}
|
||||
}
|
||||
|
||||
func (s *Splicer) Magic(magic slice.Bytes) *Splicer {
|
||||
copy(s.b[*s.c:s.c.Inc(magicbytes.Len)], magic)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadID(id *nonce.ID) *Splicer {
|
||||
copy((*id)[:], s.b[*s.c:s.c.Inc(nonce.IDLen)])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ID(id nonce.ID) *Splicer {
|
||||
copy(s.b[*s.c:s.c.Inc(nonce.IDLen)], id[:])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) IV(iv nonce.IV) *Splicer {
|
||||
copy(s.b[*s.c:s.c.Inc(nonce.IVLen)], iv[:])
|
||||
return s
|
||||
}
|
||||
func (s *Splicer) ReadIV(iv *nonce.IV) *Splicer {
|
||||
copy((*iv)[:], s.b[*s.c:s.c.Inc(nonce.IVLen)])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadCloak(ck *cloak.PubKey) *Splicer {
|
||||
copy((*ck)[:], s.b[*s.c:s.c.Inc(cloak.Len)])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Cloak(pk *pub.Key) *Splicer {
|
||||
to := cloak.GetCloak(pk)
|
||||
copy(s.b[*s.c:s.c.Inc(cloak.Len)], to[:])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadPubkey(from **pub.Key) *Splicer {
|
||||
if f, e := pub.FromBytes(s.b[*s.c:s.c.Inc(pub.KeyLen)]); check(e) {
|
||||
return s
|
||||
} else {
|
||||
*from = f
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Pubkey(from *prv.Key) *Splicer {
|
||||
pubKey := pub.Derive(from).ToBytes()
|
||||
copy(s.b[*s.c:s.c.Inc(pub.KeyLen)], pubKey[:])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Prvkey(from *prv.Key) *Splicer {
|
||||
b := from.ToBytes()
|
||||
copy(s.b[*s.c:s.c.Inc(prv.KeyLen)], b[:])
|
||||
return s
|
||||
}
|
||||
func (s *Splicer) ReadMilliSatoshi(v *lnwire.MilliSatoshi) *Splicer {
|
||||
*v = lnwire.MilliSatoshi(slice.
|
||||
DecodeUint64(s.b[*s.c:s.c.Inc(slice.Uint64Len)]))
|
||||
return s
|
||||
}
|
||||
func (s *Splicer) ReadDuration(v *time.Duration) *Splicer {
|
||||
*v = time.Duration(slice.DecodeUint64(s.b[*s.c:s.c.Inc(slice.Uint64Len)]))
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Uint64(v uint64) *Splicer {
|
||||
slice.EncodeUint64(s.b[*s.c:s.c.Inc(slice.Uint64Len)], v)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Uint32(v uint32) *Splicer {
|
||||
slice.EncodeUint32(s.b[*s.c:s.c.Inc(slice.Uint32Len)], int(v))
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadUint16(v *uint16) *Splicer {
|
||||
*v = uint16(slice.DecodeUint16(s.b[*s.c:s.c.Inc(slice.Uint16Len)]))
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Uint16(v uint16) *Splicer {
|
||||
slice.EncodeUint16(s.b[*s.c:s.c.Inc(slice.Uint16Len)], int(v))
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadHash(h *sha256.Hash) *Splicer {
|
||||
copy((*h)[:], s.b[*s.c:s.c.Inc(sha256.Len)])
|
||||
zh := sha256.Hash{}
|
||||
copy(s.b[*s.c-sha256.Len:*s.c], zh[:])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Hash(h sha256.Hash) *Splicer {
|
||||
copy(s.b[*s.c:s.c.Inc(sha256.Len)], h[:])
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) AddrPort(a *netip.AddrPort) *Splicer {
|
||||
var ap []byte
|
||||
var e error
|
||||
if ap, e = a.MarshalBinary(); check(e) {
|
||||
return s
|
||||
}
|
||||
s.b[*s.c] = byte(len(ap))
|
||||
copy(s.b[s.c.Inc(1):s.c.Inc(AddrLen)], ap)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadAddrPort(ap **netip.AddrPort) *Splicer {
|
||||
apLen := s.b[*s.c]
|
||||
apBytes := s.b[s.c.Inc(1):s.c.Inc(AddrLen)]
|
||||
*ap = &netip.AddrPort{}
|
||||
if e := (*ap).UnmarshalBinary(apBytes[:apLen]); check(e) {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadByte(b *byte) *Splicer {
|
||||
*b = s.b[*s.c]
|
||||
s.c.Inc(1)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Byte(b byte) *Splicer {
|
||||
s.b[*s.c] = byte(b)
|
||||
s.c.Inc(1)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) ReadBytes(b *slice.Bytes) *Splicer {
|
||||
bytesLen := slice.DecodeUint32(s.b[*s.c:s.c.Inc(slice.Uint32Len)])
|
||||
*b = s.b[*s.c:s.c.Inc(bytesLen)]
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Bytes(b []byte) *Splicer {
|
||||
bytesLen := slice.NewUint32()
|
||||
slice.EncodeUint32(bytesLen, len(b))
|
||||
copy(s.b[*s.c:s.c.Inc(slice.Uint32Len)], bytesLen)
|
||||
copy(s.b[*s.c:s.c.Inc(len(b))], b)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Splicer) Done() {}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"net/netip"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
|
||||
"git-indra.lan/indra-labs/indra"
|
||||
"git-indra.lan/indra-labs/indra/pkg/crypto/sha256"
|
||||
log2 "git-indra.lan/indra-labs/indra/pkg/proc/log"
|
||||
|
||||
Reference in New Issue
Block a user