Skip to content

Commit 89cc955

Browse files
authored
Merge pull request #345 from xssnick/dev-14
v1.14
2 parents 8ec14d0 + 0fd9e9f commit 89cc955

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+570
-288
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
env:
3535
WALLET_SEED: ${{ secrets.WALLET_SEED }}
3636
run: |
37-
go test -v $(go list ./... | grep -v /example/) -covermode=count -coverprofile=coverage.out
37+
go test -v -failfast $(go list ./... | grep -v /example/) -covermode=count -coverprofile=coverage.out
3838
go tool cover -func=coverage.out -o=coverage.out
3939
4040
- name: Go Coverage Badge # Pass the `coverage.out` output to this action

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[![Based on TON][ton-svg]][ton]
66
[![Telegram Channel][tgc-svg]][tg-channel]
7-
![Coverage](https://img.shields.io/badge/Coverage-69.6%25-yellow)
7+
![Coverage](https://img.shields.io/badge/Coverage-69.7%25-yellow)
88

99
Golang library for interacting with TON blockchain.
1010

adnl/adnl.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/hex"
1010
"errors"
1111
"fmt"
12+
"github.com/xssnick/tonutils-go/adnl/keys"
1213
"reflect"
1314
"strings"
1415
"sync"
@@ -512,13 +513,16 @@ func (a *ADNL) Query(ctx context.Context, req, result tl.Serializable) error {
512513
a.activeQueries[reqID] = res
513514
a.mx.Unlock()
514515

516+
defer func() {
517+
a.mx.Lock()
518+
delete(a.activeQueries, reqID)
519+
a.mx.Unlock()
520+
}()
521+
515522
baseMTU := false
516523
reSplit:
517524
packets, err := a.buildRequestMaySplit(q, baseMTU)
518525
if err != nil {
519-
a.mx.Lock()
520-
delete(a.activeQueries, reqID)
521-
a.mx.Unlock()
522526
return fmt.Errorf("request failed: %w", err)
523527
}
524528

@@ -700,12 +704,12 @@ func decodePacket(key ed25519.PrivateKey, packet []byte) ([]byte, error) {
700704
checksum := packet[32:64]
701705
data := packet[64:]
702706

703-
key, err := SharedKey(key, pub)
707+
key, err := keys.SharedKey(key, pub)
704708
if err != nil {
705709
return nil, err
706710
}
707711

708-
ctr, err := BuildSharedCipher(key, checksum)
712+
ctr, err := keys.BuildSharedCipher(key, checksum)
709713
if err != nil {
710714
return nil, err
711715
}
@@ -731,7 +735,7 @@ func (a *ADNL) GetAddressList() address.List {
731735
}
732736

733737
func (a *ADNL) GetID() []byte {
734-
id, _ := tl.Hash(PublicKeyED25519{Key: a.peerKey})
738+
id, _ := tl.Hash(keys.PublicKeyED25519{Key: a.peerKey})
735739
return id
736740
}
737741

@@ -788,9 +792,9 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
788792
}
789793

790794
if !isResp {
791-
packet.From = &PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)}
795+
packet.From = &keys.PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)}
792796
} else {
793-
packet.FromIDShort, err = tl.Hash(PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
797+
packet.FromIDShort, err = tl.Hash(keys.PublicKeyED25519{Key: a.ourKey.Public().(ed25519.PublicKey)})
794798
if err != nil {
795799
return nil, err
796800
}
@@ -821,19 +825,19 @@ func (a *ADNL) createPacket(seqno int64, isResp bool, msgs ...any) ([]byte, erro
821825
hash := sha256.Sum256(packetData)
822826
checksum := hash[:]
823827

824-
key, err := SharedKey(a.ourKey, a.peerKey)
828+
key, err := keys.SharedKey(a.ourKey, a.peerKey)
825829
if err != nil {
826830
return nil, err
827831
}
828832

829-
ctr, err := BuildSharedCipher(key, checksum)
833+
ctr, err := keys.BuildSharedCipher(key, checksum)
830834
if err != nil {
831835
return nil, err
832836
}
833837

834838
ctr.XORKeyStream(packetData, packetData)
835839

836-
enc, err := tl.Hash(PublicKeyED25519{Key: a.peerKey})
840+
enc, err := tl.Hash(keys.PublicKeyED25519{Key: a.peerKey})
837841
if err != nil {
838842
return nil, err
839843
}

adnl/channel.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/rand"
88
"crypto/sha256"
99
"errors"
10+
"github.com/xssnick/tonutils-go/adnl/keys"
1011
"github.com/xssnick/tonutils-go/tl"
1112
"math/big"
1213
"sync/atomic"
@@ -36,7 +37,7 @@ func (c *Channel) decodePacket(packet []byte) ([]byte, error) {
3637
checksum := packet[0:32]
3738
data := packet[32:]
3839

39-
ctr, err := BuildSharedCipher(c.decKey, checksum)
40+
ctr, err := keys.BuildSharedCipher(c.decKey, checksum)
4041
if err != nil {
4142
return nil, err
4243
}
@@ -53,7 +54,7 @@ func (c *Channel) decodePacket(packet []byte) ([]byte, error) {
5354

5455
func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
5556
c.peerKey = theirKey
56-
c.decKey, err = SharedKey(c.key, c.peerKey)
57+
c.decKey, err = keys.SharedKey(c.key, c.peerKey)
5758
if err != nil {
5859
return err
5960
}
@@ -63,12 +64,12 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
6364
c.encKey[(len(c.decKey)-1)-i] = c.decKey[i]
6465
}
6566

66-
theirID, err := tl.Hash(PublicKeyED25519{c.adnl.peerKey})
67+
theirID, err := tl.Hash(keys.PublicKeyED25519{c.adnl.peerKey})
6768
if err != nil {
6869
return err
6970
}
7071

71-
ourID, err := tl.Hash(PublicKeyED25519{c.adnl.ourKey.Public().(ed25519.PublicKey)})
72+
ourID, err := tl.Hash(keys.PublicKeyED25519{c.adnl.ourKey.Public().(ed25519.PublicKey)})
7273
if err != nil {
7374
return err
7475
}
@@ -80,12 +81,12 @@ func (c *Channel) setup(theirKey ed25519.PublicKey) (err error) {
8081
c.encKey = c.decKey
8182
}
8283

83-
c.id, err = tl.Hash(PublicKeyAES{Key: c.decKey})
84+
c.id, err = tl.Hash(keys.PublicKeyAES{Key: c.decKey})
8485
if err != nil {
8586
return err
8687
}
8788

88-
c.idEnc, err = tl.Hash(PublicKeyAES{Key: c.encKey})
89+
c.idEnc, err = tl.Hash(keys.PublicKeyAES{Key: c.encKey})
8990
if err != nil {
9091
return err
9192
}
@@ -142,7 +143,7 @@ func (c *Channel) createPacket(seqno int64, msgs ...any) ([]byte, error) {
142143
hash := sha256.Sum256(packetBytes)
143144
checksum := hash[:]
144145

145-
ctr, err := BuildSharedCipher(c.encKey, checksum)
146+
ctr, err := keys.BuildSharedCipher(c.encKey, checksum)
146147
if err != nil {
147148
return nil, err
148149
}

adnl/dht/client.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"encoding/binary"
88
"errors"
99
"fmt"
10+
"github.com/xssnick/tonutils-go/adnl/keys"
11+
"github.com/xssnick/tonutils-go/liteclient"
1012
"net"
1113
"reflect"
1214
"sync"
@@ -16,7 +18,6 @@ import (
1618
"github.com/xssnick/tonutils-go/adnl"
1719
"github.com/xssnick/tonutils-go/adnl/address"
1820
"github.com/xssnick/tonutils-go/adnl/overlay"
19-
"github.com/xssnick/tonutils-go/liteclient"
2021
"github.com/xssnick/tonutils-go/tl"
2122
)
2223

@@ -81,7 +82,7 @@ func NewClientFromConfig(gateway Gateway, cfg *liteclient.GlobalConfig) (*Client
8182
}
8283

8384
n := &Node{
84-
ID: adnl.PublicKeyED25519{
85+
ID: keys.PublicKeyED25519{
8586
Key: key,
8687
},
8788
AddrList: &address.List{
@@ -145,7 +146,7 @@ func (c *Client) Close() {
145146
}
146147

147148
func (c *Client) addNode(node *Node) (_ *dhtNode, err error) {
148-
pub, ok := node.ID.(adnl.PublicKeyED25519)
149+
pub, ok := node.ID.(keys.PublicKeyED25519)
149150
if !ok {
150151
return nil, fmt.Errorf("unsupported id type %s", reflect.TypeOf(node.ID).String())
151152
}
@@ -182,7 +183,7 @@ func (c *Client) addNode(node *Node) (_ *dhtNode, err error) {
182183
}
183184

184185
func (c *Client) FindOverlayNodes(ctx context.Context, overlayKey []byte, continuation ...*Continuation) (*overlay.NodesList, *Continuation, error) {
185-
keyHash, err := tl.Hash(adnl.PublicKeyOverlay{
186+
keyHash, err := tl.Hash(keys.PublicKeyOverlay{
186187
Key: overlayKey,
187188
})
188189

@@ -227,7 +228,7 @@ func (c *Client) FindAddresses(ctx context.Context, key []byte) (*address.List,
227228
return nil, nil, fmt.Errorf("failed to parse address list: %w", err)
228229
}
229230

230-
keyID, ok := val.KeyDescription.ID.(adnl.PublicKeyED25519)
231+
keyID, ok := val.KeyDescription.ID.(keys.PublicKeyED25519)
231232
if !ok {
232233
return nil, nil, fmt.Errorf("unsupported key type %s", reflect.TypeOf(val.KeyDescription.ID))
233234
}
@@ -255,7 +256,7 @@ func (c *Client) StoreAddress(
255256
return 0, nil, err
256257
}
257258

258-
id := adnl.PublicKeyED25519{Key: ownerKey.Public().(ed25519.PublicKey)}
259+
id := keys.PublicKeyED25519{Key: ownerKey.Public().(ed25519.PublicKey)}
259260
return c.Store(ctx, id, []byte("address"), 0, data, UpdateRuleSignature{}, ttl, ownerKey, replicas)
260261
}
261262

@@ -282,7 +283,7 @@ func (c *Client) StoreOverlayNodes(
282283
return 0, nil, err
283284
}
284285

285-
id := adnl.PublicKeyOverlay{Key: overlayKey}
286+
id := keys.PublicKeyOverlay{Key: overlayKey}
286287
return c.Store(ctx, id, []byte("nodes"), 0, data, UpdateRuleOverlayNodes{}, ttl, nil, replicas)
287288
}
288289

adnl/dht/client_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"github.com/xssnick/tonutils-go/adnl"
1212
"github.com/xssnick/tonutils-go/adnl/address"
13+
"github.com/xssnick/tonutils-go/adnl/keys"
1314
"github.com/xssnick/tonutils-go/liteclient"
1415
"github.com/xssnick/tonutils-go/tl"
1516
"net"
@@ -170,7 +171,7 @@ func newCorrectNode(a byte, b byte, c byte, d byte, port int32) (*Node, error) {
170171
return nil, err
171172
}
172173
testNode := &Node{
173-
adnl.PublicKeyED25519{Key: tPubKey},
174+
keys.PublicKeyED25519{Key: tPubKey},
174175
&address.List{
175176
Addresses: []*address.UDP{
176177
{net.IPv4(a, b, c, d).To4(),
@@ -200,7 +201,7 @@ func correctValue(tAdnlAddr []byte) (*ValueFoundResult, error) {
200201
if err != nil {
201202
return nil, err
202203
}
203-
pubIdRes := adnl.PublicKeyED25519{pubId}
204+
pubIdRes := keys.PublicKeyED25519{pubId}
204205
sign, err := base64.StdEncoding.DecodeString("Zwj4eW/tMbgzF7kQtI8AF11E0q76h5/3+hkylzHuJzKDD2sDd7sw/FXIiVptjrrOIPze8kbbDEkq4K5O78KeDQ==")
205206
if err != nil {
206207
return nil, err
@@ -346,7 +347,7 @@ func TestClient_FindAddressesUnit(t *testing.T) {
346347
if err != nil {
347348
t.Fatal("failed creating pId of test value, err:", err)
348349
}
349-
tPubIdRes := adnl.PublicKeyED25519{Key: pubId}
350+
tPubIdRes := keys.PublicKeyED25519{Key: pubId}
350351

351352
t.Run("find addresses positive case", func(t *testing.T) {
352353
gateway := &MockGateway{}
@@ -422,7 +423,7 @@ func TestClient_FindAddressesIntegration(t *testing.T) {
422423
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
423424
defer cancel()
424425

425-
dhtClient, err := NewClientFromConfigUrl(ctx, gateway, "https://tonutils.com/global.config.json")
426+
dhtClient, err := NewClientFromConfigUrl(ctx, gateway, "https://ton-blockchain.github.io/global.config.json")
426427
if err != nil {
427428
t.Fatalf("failed to init DHT client: %s", err.Error())
428429
}
@@ -485,7 +486,7 @@ func TestClient_StoreAddressIntegration(t *testing.T) {
485486
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
486487
defer cancel()
487488

488-
dhtClient, err := NewClientFromConfigUrl(ctx, gateway, "https://tonutils.com/global.config.json")
489+
dhtClient, err := NewClientFromConfigUrl(ctx, gateway, "https://ton-blockchain.github.io/global.config.json")
489490
if err != nil {
490491
t.Fatalf("failed to init DHT client: %s", err.Error())
491492
}
@@ -520,7 +521,7 @@ func TestClient_StoreAddressIntegration(t *testing.T) {
520521
t.Fatal(err)
521522
}
522523

523-
kid, err := tl.Hash(adnl.PublicKeyED25519{
524+
kid, err := tl.Hash(keys.PublicKeyED25519{
524525
Key: pub,
525526
})
526527
if err != nil {

adnl/dht/dht.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"crypto/ed25519"
55
"encoding/hex"
66
"fmt"
7-
"github.com/xssnick/tonutils-go/adnl"
87
"github.com/xssnick/tonutils-go/adnl/address"
8+
"github.com/xssnick/tonutils-go/adnl/keys"
99
"github.com/xssnick/tonutils-go/tl"
1010
"reflect"
1111
)
@@ -105,7 +105,7 @@ type Pong struct {
105105
}
106106

107107
func (n *Node) CheckSignature() error {
108-
pub, ok := n.ID.(adnl.PublicKeyED25519)
108+
pub, ok := n.ID.(keys.PublicKeyED25519)
109109
if !ok {
110110
return fmt.Errorf("unsupported id type %s", reflect.TypeOf(n.ID).String())
111111
}

adnl/dht/node.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import (
66
"crypto/ed25519"
77
"encoding/hex"
88
"fmt"
9+
"github.com/xssnick/tonutils-go/adnl/keys"
910
"math/bits"
1011
"reflect"
1112
"sync"
1213
"sync/atomic"
1314
"time"
1415

15-
"github.com/xssnick/tonutils-go/adnl"
1616
"github.com/xssnick/tonutils-go/adnl/overlay"
1717
"github.com/xssnick/tonutils-go/tl"
1818
)
@@ -199,7 +199,7 @@ func checkValue(id []byte, value *Value) error {
199199
case UpdateRuleAnybody:
200200
// no checks
201201
case UpdateRuleSignature:
202-
pub, ok := value.KeyDescription.ID.(adnl.PublicKeyED25519)
202+
pub, ok := value.KeyDescription.ID.(keys.PublicKeyED25519)
203203
if !ok {
204204
return fmt.Errorf("unsupported value's key type: %s", reflect.ValueOf(value.KeyDescription.ID).String())
205205
}

adnl/dht/node_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/hex"
88
"fmt"
99
"github.com/xssnick/tonutils-go/adnl"
10+
"github.com/xssnick/tonutils-go/adnl/keys"
1011
"github.com/xssnick/tonutils-go/tl"
1112
"net"
1213
"reflect"
@@ -21,7 +22,7 @@ func newCorrectDhtNode(a byte, b byte, c byte, d byte, port string) (*dhtNode, e
2122
return nil, err
2223
}
2324

24-
kId, err := tl.Hash(adnl.PublicKeyED25519{Key: tPubKey})
25+
kId, err := tl.Hash(keys.PublicKeyED25519{Key: tPubKey})
2526
if err != nil {
2627
return nil, err
2728
}
@@ -43,7 +44,7 @@ func TestNode_findNodes(t *testing.T) {
4344
if err != nil {
4445
t.Fatal("failed to prepare test pub key, err: ", err)
4546
}
46-
pubKeyAdnl := adnl.PublicKeyED25519{Key: pubKey}
47+
pubKeyAdnl := keys.PublicKeyED25519{Key: pubKey}
4748

4849
idKey, err := tl.Hash(pubKeyAdnl)
4950
if err != nil {

0 commit comments

Comments
 (0)