Skip to content

Commit

Permalink
crypto/rand usage for tests and query ids
Browse files Browse the repository at this point in the history
  • Loading branch information
xssnick committed Aug 17, 2023
1 parent b528152 commit 46d3d34
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 82 deletions.
6 changes: 3 additions & 3 deletions adnl/adnl.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ func (a *ADNL) GetAddressList() address.List {
}

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

Expand Down Expand Up @@ -639,7 +639,7 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
if !isResp {
packet.From = &PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)}
} else {
packet.FromIDShort, err = ToKeyID(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
packet.FromIDShort, err = tl.Hash(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -675,7 +675,7 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
return nil, err
}

enc, err := ToKeyID(PublicKeyED25519{Key: a.peerKey})
enc, err := tl.Hash(PublicKeyED25519{Key: a.peerKey})
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions adnl/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
c.encKey[(len(c.decKey)-1)-i] = c.decKey[i]
}

theirID, err := ToKeyID(PublicKeyED25519{c.adnl.peerKey})
theirID, err := tl.Hash(PublicKeyED25519{c.adnl.peerKey})
if err != nil {
return err
}

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

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

ctr.XORKeyStream(packetData, packetData)

enc, err := ToKeyID(PublicKeyAES{Key: c.encKey})
enc, err := tl.Hash(PublicKeyAES{Key: c.encKey})
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion adnl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func listenPacketsAsClient(a *ADNL, conn net.Conn) error {
a.Close()
}()

rootID, err := ToKeyID(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
rootID, err := tl.Hash(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})

Check failure on line 88 in adnl/client.go

View workflow job for this annotation

GitHub Actions / build

undefined: tl
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions adnl/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (g *Gateway) StartServer(listenAddr string) (err error) {
return err
}

rootId, err := ToKeyID(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
rootId, err := tl.Hash(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (g *Gateway) StartClient() (err error) {
return err
}

rootId, err := ToKeyID(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
rootId, err := tl.Hash(PublicKeyED25519{Key: g.key.Public().(ed25519.PublicKey)})
if err != nil {
return err
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (g *Gateway) listen(rootId []byte) {
continue
}

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

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

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

Expand Down
15 changes: 11 additions & 4 deletions adnl/rldp/raptorq/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package raptorq

import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"math/rand"
"testing"
)

Expand Down Expand Up @@ -58,11 +59,17 @@ func Test_EncodeDecode(t *testing.T) {
}

func Test_EncodeDecodeFuzz(t *testing.T) {
for n := 0; n < 100; n++ {
for n := 0; n < 1000; n++ {
str := make([]byte, 4096)
rand.Read(str)
_, _ = rand.Read(str)

buf := make([]byte, 4)
if _, err := rand.Read(buf); err != nil {
panic(err)
}
rnd := binary.LittleEndian.Uint32(buf)

symSz := (1 + (rand.Uint32() % 10)) * 10
symSz := (1 + (rnd % 10)) * 10
r := NewRaptorQ(symSz)
enc, err := r.CreateEncoder(str)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion example/send-to-contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func main() {
if balance.Nano().Uint64() >= 3000000 {
// create transaction body cell, depends on what contract needs, just random example here
body := cell.BeginCell().
MustStoreUInt(0x123abc55, 32). // op code
MustStoreUInt(0x123abc55, 32). // op code
MustStoreUInt(rand.Uint64(), 64). // query id
// payload:
MustStoreAddr(address.MustParseAddr("EQAbMQzuuGiCne0R7QEj9nrXsjM7gNjeVmrlBZouyC-SCLlO")).
Expand Down
12 changes: 9 additions & 3 deletions tlb/coins_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package tlb

import (
"crypto/rand"
"fmt"
"math/big"
"math/rand"
"strings"
"testing"
)
Expand Down Expand Up @@ -113,13 +113,19 @@ func TestCoins_Decimals(t *testing.T) {
t.Run("decimals "+fmt.Sprint(i), func(t *testing.T) {
for x := 0; x < 5000; x++ {
rnd := make([]byte, 64)
rand.Read(rnd)
_, _ = rand.Read(rnd)

lo := new(big.Int).Mod(new(big.Int).SetBytes(rnd), new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(i)), nil))
if i > 0 && strings.HasSuffix(lo.String(), "0") {
lo = lo.Add(lo, big.NewInt(1))
}
hi := big.NewInt(rand.Int63())

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
panic(err)
}

hi := new(big.Int).SetBytes(buf)

amt := new(big.Int).Mul(hi, new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(i)), nil))
amt = amt.Add(amt, lo)
Expand Down
9 changes: 7 additions & 2 deletions ton/dns/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package dns

import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"fmt"
"math/rand"
"strings"

"github.com/xssnick/tonutils-go/address"
Expand Down Expand Up @@ -37,7 +38,11 @@ type Client struct {
api TonApi
}

var randomizer = rand.Uint64
var randomizer = func() uint64 {
buf := make([]byte, 8)
_, _ = rand.Read(buf)
return binary.LittleEndian.Uint64(buf)
}

func RootContractAddr(api TonApi) (*address.Address, error) {
b, err := api.CurrentMasterchainInfo(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion ton/dns/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package dns

import (
"bytes"
"crypto/rand"
"crypto/sha256"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tvm/cell"
"math/rand"
"testing"
)

Expand Down
19 changes: 16 additions & 3 deletions ton/jetton/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package jetton

import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"math/big"
"math/rand"

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

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(TransferPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
Amount: amountCoins,
Destination: to,
ResponseDestination: to,
Expand All @@ -87,8 +94,14 @@ func (c *WalletClient) BuildTransferPayload(to *address.Address, amountCoins, am
}

func (c *WalletClient) BuildBurnPayload(amountCoins tlb.Coins, notifyAddr *address.Address) (*cell.Cell, error) {
buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(BurnPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
Amount: amountCoins,
ResponseDestination: notifyAddr,
CustomPayload: nil,
Expand Down
19 changes: 16 additions & 3 deletions ton/nft/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package nft

import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"math/big"
"math/rand"

"github.com/xssnick/tonutils-go/ton"

Expand Down Expand Up @@ -211,8 +212,14 @@ func (c *CollectionClient) BuildMintPayload(index *big.Int, owner *address.Addre

con = cell.BeginCell().MustStoreAddr(owner).MustStoreRef(con).EndCell()

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(ItemMintPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
Index: index,
TonAmount: amountForward,
Content: con,
Expand All @@ -232,8 +239,14 @@ func (c *CollectionClient) BuildMintEditablePayload(index *big.Int, owner, edito

con = cell.BeginCell().MustStoreAddr(owner).MustStoreRef(con).MustStoreAddr(editor).EndCell()

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(ItemMintPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
Index: index,
TonAmount: amountForward,
Content: con,
Expand Down
11 changes: 9 additions & 2 deletions ton/nft/item-editable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package nft

import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"github.com/xssnick/tonutils-go/ton"
"math/rand"

"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
Expand Down Expand Up @@ -68,8 +69,14 @@ func (c *ItemEditableClient) BuildEditPayload(content ContentAny) (*cell.Cell, e
}
}

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(ItemEditPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
Content: con,
})
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions ton/nft/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package nft

import (
"context"
"crypto/rand"
"encoding/binary"
"fmt"
"github.com/xssnick/tonutils-go/ton"
"math/big"
"math/rand"

"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
Expand Down Expand Up @@ -142,8 +143,14 @@ func (c *ItemClient) BuildTransferPayload(newOwner *address.Address, amountForwa
panic("only 1 response destination is allowed")
}

buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return nil, err
}
rnd := binary.LittleEndian.Uint64(buf)

body, err := tlb.ToCell(TransferPayload{
QueryID: rand.Uint64(),
QueryID: rnd,
NewOwner: newOwner,
ResponseDestination: respTo,
CustomPayload: nil,
Expand Down
2 changes: 1 addition & 1 deletion ton/payments/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"bytes"
"context"
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/tvm/cell"
"math/rand"
"time"
)

Expand Down
Loading

0 comments on commit 46d3d34

Please sign in to comment.