Skip to content

Commit edf5e1c

Browse files
authored
Merge pull request #131 from xssnick/fix-dict
Fix Dict & Fix Decrypt comment & crypto/rand for tests
2 parents 4bfa6a6 + 32b89cb commit edf5e1c

File tree

22 files changed

+227
-80
lines changed

22 files changed

+227
-80
lines changed

adnl/adnl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func (a *ADNL) GetAddressList() address.List {
598598
}
599599

600600
func (a *ADNL) GetID() []byte {
601-
id, _ := ToKeyID(PublicKeyED25519{Key: a.peerKey})
601+
id, _ := tl.Hash(PublicKeyED25519{Key: a.peerKey})
602602
return id
603603
}
604604

@@ -639,7 +639,7 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
639639
if !isResp {
640640
packet.From = &PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)}
641641
} else {
642-
packet.FromIDShort, err = ToKeyID(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
642+
packet.FromIDShort, err = tl.Hash(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
643643
if err != nil {
644644
return nil, err
645645
}
@@ -675,7 +675,7 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
675675
return nil, err
676676
}
677677

678-
enc, err := ToKeyID(PublicKeyED25519{Key: a.peerKey})
678+
enc, err := tl.Hash(PublicKeyED25519{Key: a.peerKey})
679679
if err != nil {
680680
return nil, err
681681
}

adnl/channel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
6161
c.encKey[(len(c.decKey)-1)-i] = c.decKey[i]
6262
}
6363

64-
theirID, err := ToKeyID(PublicKeyED25519{c.adnl.peerKey})
64+
theirID, err := tl.Hash(PublicKeyED25519{c.adnl.peerKey})
6565
if err != nil {
6666
return err
6767
}
6868

69-
ourID, err := ToKeyID(PublicKeyED25519{c.adnl.ourKey.Public().(ed25519.PublicKey)})
69+
ourID, err := tl.Hash(PublicKeyED25519{c.adnl.ourKey.Public().(ed25519.PublicKey)})
7070
if err != nil {
7171
return err
7272
}
@@ -78,7 +78,7 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
7878
c.encKey = c.decKey
7979
}
8080

81-
c.id, err = ToKeyID(PublicKeyAES{Key: c.decKey})
81+
c.id, err = tl.Hash(PublicKeyAES{Key: c.decKey})
8282
if err != nil {
8383
return err
8484
}
@@ -129,7 +129,7 @@ func (c *Channel) createPacket(seqno int64, msgs ...any) ([]byte, error) {
129129

130130
ctr.XORKeyStream(packetData, packetData)
131131

132-
enc, err := ToKeyID(PublicKeyAES{Key: c.encKey})
132+
enc, err := tl.Hash(PublicKeyAES{Key: c.encKey})
133133
if err != nil {
134134
return nil, err
135135
}

adnl/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/ed25519"
77
"encoding/hex"
88
"fmt"
9+
"github.com/xssnick/tonutils-go/tl"
910
"net"
1011
"sync"
1112
"time"
@@ -85,7 +86,7 @@ func listenPacketsAsClient(a *ADNL, conn net.Conn) error {
8586
a.Close()
8687
}()
8788

88-
rootID, err := ToKeyID(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
89+
rootID, err := tl.Hash(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
8990
if err != nil {
9091
return err
9192
}

adnl/gateway.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (g *Gateway) StartServer(listenAddr string) (err error) {
134134
return err
135135
}
136136

137-
rootId, err := ToKeyID(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
137+
rootId, err := tl.Hash(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
138138
if err != nil {
139139
return err
140140
}
@@ -168,7 +168,7 @@ func (g *Gateway) StartClient() (err error) {
168168
return err
169169
}
170170

171-
rootId, err := ToKeyID(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
171+
rootId, err := tl.Hash(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
172172
if err != nil {
173173
return err
174174
}
@@ -236,7 +236,7 @@ func (g *Gateway) listen(rootId []byte) {
236236
continue
237237
}
238238

239-
peerId, err = ToKeyID(PublicKeyED25519{Key: packet.From.Key})
239+
peerId, err = tl.Hash(PublicKeyED25519{Key: packet.From.Key})
240240
if err != nil {
241241
// invalid packet
242242
continue
@@ -402,7 +402,7 @@ func (g *Gateway) RegisterClient(addr string, key ed25519.PublicKey) (Peer, erro
402402
}
403403
udpAddr := net.UDPAddrFromAddrPort(pAddr)
404404

405-
clientId, err := ToKeyID(PublicKeyED25519{Key: key})
405+
clientId, err := tl.Hash(PublicKeyED25519{Key: key})
406406
if err != nil {
407407
return nil, err
408408
}
@@ -449,7 +449,7 @@ func (g *Gateway) write(deadline time.Time, addr net.Addr, buf []byte) error {
449449
}
450450

451451
func (g *Gateway) GetID() []byte {
452-
id, _ := ToKeyID(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
452+
id, _ := tl.Hash(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
453453
return id
454454
}
455455

adnl/rldp/raptorq/solver_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package raptorq
22

33
import (
44
"bytes"
5+
"crypto/rand"
6+
"encoding/binary"
57
"encoding/hex"
6-
"math/rand"
78
"testing"
89
)
910

@@ -58,11 +59,17 @@ func Test_EncodeDecode(t *testing.T) {
5859
}
5960

6061
func Test_EncodeDecodeFuzz(t *testing.T) {
61-
for n := 0; n < 100; n++ {
62+
for n := 0; n < 1000; n++ {
6263
str := make([]byte, 4096)
63-
rand.Read(str)
64+
_, _ = rand.Read(str)
65+
66+
buf := make([]byte, 4)
67+
if _, err := rand.Read(buf); err != nil {
68+
panic(err)
69+
}
70+
rnd := binary.LittleEndian.Uint32(buf)
6471

65-
symSz := (1 + (rand.Uint32() % 10)) * 10
72+
symSz := (1 + (rnd % 10)) * 10
6673
r := NewRaptorQ(symSz)
6774
enc, err := r.CreateEncoder(str)
6875
if err != nil {

example/send-to-contract/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
if balance.Nano().Uint64() >= 3000000 {
5454
// create transaction body cell, depends on what contract needs, just random example here
5555
body := cell.BeginCell().
56-
MustStoreUInt(0x123abc55, 32). // op code
56+
MustStoreUInt(0x123abc55, 32). // op code
5757
MustStoreUInt(rand.Uint64(), 64). // query id
5858
// payload:
5959
MustStoreAddr(address.MustParseAddr("EQAbMQzuuGiCne0R7QEj9nrXsjM7gNjeVmrlBZouyC-SCLlO")).

tlb/coins_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package tlb
22

33
import (
4+
"crypto/rand"
45
"fmt"
56
"math/big"
6-
"math/rand"
77
"strings"
88
"testing"
99
)
@@ -113,13 +113,19 @@ func TestCoins_Decimals(t *testing.T) {
113113
t.Run("decimals "+fmt.Sprint(i), func(t *testing.T) {
114114
for x := 0; x < 5000; x++ {
115115
rnd := make([]byte, 64)
116-
rand.Read(rnd)
116+
_, _ = rand.Read(rnd)
117117

118118
lo := new(big.Int).Mod(new(big.Int).SetBytes(rnd), new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(i)), nil))
119119
if i > 0 && strings.HasSuffix(lo.String(), "0") {
120120
lo = lo.Add(lo, big.NewInt(1))
121121
}
122-
hi := big.NewInt(rand.Int63())
122+
123+
buf := make([]byte, 8)
124+
if _, err := rand.Read(buf); err != nil {
125+
panic(err)
126+
}
127+
128+
hi := new(big.Int).SetBytes(buf)
123129

124130
amt := new(big.Int).Mul(hi, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(i)), nil))
125131
amt = amt.Add(amt, lo)

ton/dns/resolve.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package dns
22

33
import (
44
"context"
5+
"crypto/rand"
56
"crypto/sha256"
7+
"encoding/binary"
68
"fmt"
7-
"math/rand"
89
"strings"
910

1011
"github.com/xssnick/tonutils-go/address"
@@ -37,7 +38,11 @@ type Client struct {
3738
api TonApi
3839
}
3940

40-
var randomizer = rand.Uint64
41+
var randomizer = func() uint64 {
42+
buf := make([]byte, 8)
43+
_, _ = rand.Read(buf)
44+
return binary.LittleEndian.Uint64(buf)
45+
}
4146

4247
func RootContractAddr(api TonApi) (*address.Address, error) {
4348
b, err := api.CurrentMasterchainInfo(context.Background())

ton/dns/resolve_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package dns
22

33
import (
44
"bytes"
5+
"crypto/rand"
56
"crypto/sha256"
67
"github.com/xssnick/tonutils-go/address"
78
"github.com/xssnick/tonutils-go/tvm/cell"
8-
"math/rand"
99
"testing"
1010
)
1111

ton/jetton/wallet.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package jetton
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"encoding/binary"
57
"fmt"
68
"math/big"
7-
"math/rand"
89

910
"github.com/xssnick/tonutils-go/address"
1011
"github.com/xssnick/tonutils-go/tlb"
@@ -70,8 +71,14 @@ func (c *WalletClient) BuildTransferPayload(to *address.Address, amountCoins, am
7071
payloadForward = cell.BeginCell().EndCell()
7172
}
7273

74+
buf := make([]byte, 8)
75+
if _, err := rand.Read(buf); err != nil {
76+
return nil, err
77+
}
78+
rnd := binary.LittleEndian.Uint64(buf)
79+
7380
body, err := tlb.ToCell(TransferPayload{
74-
QueryID: rand.Uint64(),
81+
QueryID: rnd,
7582
Amount: amountCoins,
7683
Destination: to,
7784
ResponseDestination: to,
@@ -87,8 +94,14 @@ func (c *WalletClient) BuildTransferPayload(to *address.Address, amountCoins, am
8794
}
8895

8996
func (c *WalletClient) BuildBurnPayload(amountCoins tlb.Coins, notifyAddr *address.Address) (*cell.Cell, error) {
97+
buf := make([]byte, 8)
98+
if _, err := rand.Read(buf); err != nil {
99+
return nil, err
100+
}
101+
rnd := binary.LittleEndian.Uint64(buf)
102+
90103
body, err := tlb.ToCell(BurnPayload{
91-
QueryID: rand.Uint64(),
104+
QueryID: rnd,
92105
Amount: amountCoins,
93106
ResponseDestination: notifyAddr,
94107
CustomPayload: nil,

0 commit comments

Comments
 (0)