Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[![Based on TON][ton-svg]][ton]
[![Telegram Channel][tgc-svg]][tg-channel]
![Coverage](https://img.shields.io/badge/Coverage-69.7%25-yellow)
![Coverage](https://img.shields.io/badge/Coverage-70.4%25-brightgreen)

Golang library for interacting with TON blockchain.

Expand Down
30 changes: 16 additions & 14 deletions adnl/adnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type ADNL struct {
recvPriorityAddrVer int32
ourAddrVerOnPeerSide int32

peerID []byte
sharedKey []byte
peerKey ed25519.PublicKey
ourAddresses unsafe.Pointer

Expand Down Expand Up @@ -175,6 +177,16 @@ func (a *ADNL) processPacket(packet *PacketContent, fromChannel bool) (err error
if !fromChannel && packet.From != nil {
a.mx.Lock()
if a.peerKey == nil {
a.sharedKey, err = keys.SharedKey(a.ourKey, packet.From.Key)
if err != nil {
return err
}

a.peerID, err = tl.Hash(keys.PublicKeyED25519{Key: packet.From.Key})
if err != nil {
return err
}

a.peerKey = packet.From.Key
}
a.mx.Unlock()
Expand Down Expand Up @@ -735,12 +747,11 @@ func (a *ADNL) GetAddressList() address.List {
}

func (a *ADNL) GetID() []byte {
id, _ := tl.Hash(keys.PublicKeyED25519{Key: a.peerKey})
return id
return append([]byte{}, a.peerID...)
}

func (a *ADNL) GetPubKey() ed25519.PublicKey {
return a.peerKey
return append(ed25519.PublicKey{}, a.peerKey...)
}

func (a *ADNL) Reinit() {
Expand Down Expand Up @@ -825,23 +836,14 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
hash := sha256.Sum256(packetData)
checksum := hash[:]

key, err := keys.SharedKey(a.ourKey, a.peerKey)
if err != nil {
return nil, err
}

ctr, err := keys.BuildSharedCipher(key, checksum)
ctr, err := keys.BuildSharedCipher(a.sharedKey, checksum)
if err != nil {
return nil, err
}

ctr.XORKeyStream(packetData, packetData)

enc, err := tl.Hash(keys.PublicKeyED25519{Key: a.peerKey})
if err != nil {
return nil, err
}
copy(bufData, enc)
copy(bufData, a.peerID)
copy(bufData[32:], a.ourKey.Public().(ed25519.PublicKey))
copy(bufData[64:], checksum)

Expand Down
17 changes: 16 additions & 1 deletion adnl/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,23 @@ func (g *Gateway) registerClient(addr net.Addr, key ed25519.PublicKey, id string
addrList.Version = addrList.ReinitDate

a := g.initADNL()
a.SetAddresses(addrList)

sharedKey, err := keys.SharedKey(a.ourKey, key)
if err != nil {
return nil, err
}

peerId, err := tl.Hash(keys.PublicKeyED25519{Key: key})
if err != nil {
return nil, err
}

a.peerID = peerId
a.sharedKey = sharedKey
a.peerKey = key

a.SetAddresses(addrList)

a.addr = addr.String()
a.writer = newWriter(func(p []byte, deadline time.Time) (err error) {
currentAddr := *(*net.Addr)(atomic.LoadPointer(&peer.addr))
Expand Down
6 changes: 3 additions & 3 deletions adnl/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (p *PacketContent) Serialize(buf *bytes.Buffer) (int, error) {
binary.LittleEndian.PutUint32(tmp, _PacketContentID)
buf.Write(tmp)

tl.ToBytesToBuffer(buf, p.Rand1)
_ = tl.ToBytesToBuffer(buf, p.Rand1)

var flags uint32
if p.Seqno != nil {
Expand Down Expand Up @@ -314,10 +314,10 @@ func (p *PacketContent) Serialize(buf *bytes.Buffer) (int, error) {
}

if p.Signature != nil {
tl.ToBytesToBuffer(buf, p.Signature)
_ = tl.ToBytesToBuffer(buf, p.Signature)
}

tl.ToBytesToBuffer(buf, p.Rand2)
_ = tl.ToBytesToBuffer(buf, p.Rand2)

return payloadLen, nil
}
Expand Down
Loading