-
Notifications
You must be signed in to change notification settings - Fork 76
/
wallet.go
827 lines (694 loc) · 23.4 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
package wallet
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"github.com/xssnick/tonutils-go/adnl"
"time"
"github.com/xssnick/tonutils-go/ton"
"github.com/xssnick/tonutils-go/address"
"github.com/xssnick/tonutils-go/tlb"
"github.com/xssnick/tonutils-go/tvm/cell"
)
type Version int
const (
V1R1 Version = 11
V1R2 Version = 12
V1R3 Version = 13
V2R1 Version = 21
V2R2 Version = 22
V3R1 Version = 31
V3R2 Version = 32
V3 = V3R2
V4R1 Version = 41
V4R2 Version = 42
V5R1 Version = 51
HighloadV2R2 Version = 122
HighloadV2Verified Version = 123
HighloadV3 Version = 300
Lockup Version = 200
Unknown Version = 0
)
const (
CarryAllRemainingBalance = 128
CarryAllRemainingIncomingValue = 64
DestroyAccountIfZero = 32
IgnoreErrors = 2
PayGasSeparately = 1
)
func (v Version) String() string {
if v == Unknown {
return "unknown"
}
switch v {
case HighloadV2R2:
return fmt.Sprintf("highload V2R2")
case HighloadV2Verified:
return fmt.Sprintf("highload V2R2 verified")
}
if v/100 == 2 {
return fmt.Sprintf("lockup")
}
if v/10 > 0 && v/10 < 10 {
return fmt.Sprintf("V%dR%d", v/10, v%10)
}
return fmt.Sprintf("%d", v)
}
var (
walletCodeHex = map[Version]string{
V1R1: _V1R1CodeHex, V1R2: _V1R2CodeHex, V1R3: _V1R3CodeHex,
V2R1: _V2R1CodeHex, V2R2: _V2R2CodeHex,
V3R1: _V3R1CodeHex, V3R2: _V3R2CodeHex,
V4R1: _V4R1CodeHex, V4R2: _V4R2CodeHex,
V5R1: _V5R1CodeHex,
HighloadV2R2: _HighloadV2R2CodeHex, HighloadV2Verified: _HighloadV2VerifiedCodeHex,
HighloadV3: _HighloadV3CodeHex,
Lockup: _LockupCodeHex,
}
walletCodeBOC = map[Version][]byte{}
walletCode = map[Version]*cell.Cell{}
)
func init() {
var err error
for ver, codeHex := range walletCodeHex {
walletCodeBOC[ver], err = hex.DecodeString(codeHex)
if err != nil {
panic(err)
}
walletCode[ver], err = cell.FromBOC(walletCodeBOC[ver])
if err != nil {
panic(err)
}
}
}
// defining some funcs this way to mock for tests
var randUint32 = func() uint32 {
buf := make([]byte, 4)
_, _ = rand.Read(buf)
return binary.LittleEndian.Uint32(buf)
}
var timeNow = time.Now
var (
ErrUnsupportedWalletVersion = errors.New("wallet version is not supported")
ErrTxWasNotConfirmed = errors.New("transaction was not confirmed in a given deadline, but it may still be confirmed later")
// Deprecated: use ton.ErrTxWasNotFound
ErrTxWasNotFound = errors.New("requested transaction is not found")
)
type TonAPI interface {
WaitForBlock(seqno uint32) ton.APIClientWrapped
Client() ton.LiteClient
CurrentMasterchainInfo(ctx context.Context) (*ton.BlockIDExt, error)
GetAccount(ctx context.Context, block *ton.BlockIDExt, addr *address.Address) (*tlb.Account, error)
SendExternalMessage(ctx context.Context, msg *tlb.ExternalMessage) error
RunGetMethod(ctx context.Context, blockInfo *ton.BlockIDExt, addr *address.Address, method string, params ...interface{}) (*ton.ExecutionResult, error)
ListTransactions(ctx context.Context, addr *address.Address, num uint32, lt uint64, txHash []byte) ([]*tlb.Transaction, error)
FindLastTransactionByInMsgHash(ctx context.Context, addr *address.Address, msgHash []byte, maxTxNumToScan ...int) (*tlb.Transaction, error)
FindLastTransactionByOutMsgHash(ctx context.Context, addr *address.Address, msgHash []byte, maxTxNumToScan ...int) (*tlb.Transaction, error)
}
type Message struct {
Mode uint8
InternalMessage *tlb.InternalMessage
}
type Wallet struct {
api TonAPI
key ed25519.PrivateKey
addr *address.Address
ver VersionConfig
// Can be used to operate multiple wallets with the same key and version.
// use GetSubwallet if you need it.
subwallet uint32
// Stores a pointer to implementation of the version related functionality
spec any
}
func FromPrivateKey(api TonAPI, key ed25519.PrivateKey, version VersionConfig) (*Wallet, error) {
var subwallet uint32 = DefaultSubwallet
// default subwallet depends on wallet type
switch version.(type) {
case ConfigV5R1:
subwallet = 0
}
addr, err := AddressFromPubKey(key.Public().(ed25519.PublicKey), version, subwallet)
if err != nil {
return nil, err
}
w := &Wallet{
api: api,
key: key,
addr: addr,
ver: version,
subwallet: subwallet,
}
w.spec, err = getSpec(w)
if err != nil {
return nil, err
}
return w, nil
}
func getSpec(w *Wallet) (any, error) {
switch v := w.ver.(type) {
case Version, ConfigV5R1:
regular := SpecRegular{
wallet: w,
messagesTTL: 60 * 3, // default ttl 3 min
}
seqnoFetcher := func(ctx context.Context, subWallet uint32) (uint32, error) {
block, err := w.api.CurrentMasterchainInfo(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get block: %w", err)
}
resp, err := w.api.WaitForBlock(block.SeqNo).RunGetMethod(ctx, block, w.addr, "seqno")
if err != nil {
if cErr, ok := err.(ton.ContractExecError); ok && cErr.Code == ton.ErrCodeContractNotInitialized {
return 0, nil
}
return 0, fmt.Errorf("get seqno err: %w", err)
}
iSeq, err := resp.Int(0)
if err != nil {
return 0, fmt.Errorf("failed to parse seqno: %w", err)
}
return uint32(iSeq.Uint64()), nil
}
switch x := w.ver.(type) {
case ConfigV5R1:
if x.NetworkGlobalID == 0 {
return nil, fmt.Errorf("NetworkGlobalID should be set in v5 config")
}
return &SpecV5R1{SpecRegular: regular, SpecSeqno: SpecSeqno{seqnoFetcher: seqnoFetcher}, config: x}, nil
}
switch v {
case V3R1, V3R2:
return &SpecV3{regular, SpecSeqno{seqnoFetcher: seqnoFetcher}}, nil
case V4R1, V4R2:
return &SpecV4R2{regular, SpecSeqno{seqnoFetcher: seqnoFetcher}}, nil
case HighloadV2R2, HighloadV2Verified:
return &SpecHighloadV2R2{regular, SpecQuery{}}, nil
case HighloadV3:
return nil, fmt.Errorf("use ConfigHighloadV3 for highload v3 spec")
case V5R1:
return nil, fmt.Errorf("use ConfigV5R1 for v5 spec")
}
case ConfigHighloadV3:
return &SpecHighloadV3{wallet: w, config: v}, nil
}
return nil, fmt.Errorf("cannot init spec: %w", ErrUnsupportedWalletVersion)
}
// Address - returns old (bounce) version of wallet address
// DEPRECATED: because of address reform, use WalletAddress,
// it will return UQ format
func (w *Wallet) Address() *address.Address {
return w.addr
}
// WalletAddress - returns new standard non bounce address
func (w *Wallet) WalletAddress() *address.Address {
return w.addr.Bounce(false)
}
func (w *Wallet) PrivateKey() ed25519.PrivateKey {
return w.key
}
func (w *Wallet) GetSubwallet(subwallet uint32) (*Wallet, error) {
addr, err := AddressFromPubKey(w.key.Public().(ed25519.PublicKey), w.ver, subwallet)
if err != nil {
return nil, err
}
sub := &Wallet{
api: w.api,
key: w.key,
addr: addr,
ver: w.ver,
subwallet: subwallet,
}
sub.spec, err = getSpec(sub)
if err != nil {
return nil, err
}
return sub, nil
}
func (w *Wallet) GetBalance(ctx context.Context, block *ton.BlockIDExt) (tlb.Coins, error) {
acc, err := w.api.WaitForBlock(block.SeqNo).GetAccount(ctx, block, w.addr)
if err != nil {
return tlb.Coins{}, fmt.Errorf("failed to get account state: %w", err)
}
if !acc.IsActive {
return tlb.Coins{}, nil
}
return acc.State.Balance, nil
}
func (w *Wallet) GetSpec() any {
return w.spec
}
func (w *Wallet) BuildExternalMessage(ctx context.Context, message *Message) (*tlb.ExternalMessage, error) {
return w.BuildExternalMessageForMany(ctx, []*Message{message})
}
// Deprecated: use BuildExternalMessageForMany
func (w *Wallet) BuildMessageForMany(ctx context.Context, messages []*Message) (*tlb.ExternalMessage, error) {
return w.BuildExternalMessageForMany(ctx, messages)
}
func (w *Wallet) BuildExternalMessageForMany(ctx context.Context, messages []*Message) (*tlb.ExternalMessage, error) {
block, err := w.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get block: %w", err)
}
acc, err := w.api.WaitForBlock(block.SeqNo).GetAccount(ctx, block, w.addr)
if err != nil {
return nil, fmt.Errorf("failed to get account state: %w", err)
}
initialized := acc.IsActive && acc.State.Status == tlb.AccountStatusActive
return w.PrepareExternalMessageForMany(ctx, !initialized, messages)
}
// PrepareExternalMessageForMany - Prepares external message for wallet
// can be used directly for offline signing but custom fetchers should be defined in this case
func (w *Wallet) PrepareExternalMessageForMany(ctx context.Context, withStateInit bool, messages []*Message) (_ *tlb.ExternalMessage, err error) {
var stateInit *tlb.StateInit
if withStateInit {
stateInit, err = GetStateInit(w.key.Public().(ed25519.PublicKey), w.ver, w.subwallet)
if err != nil {
return nil, fmt.Errorf("failed to get state init: %w", err)
}
}
var msg *cell.Cell
switch v := w.ver.(type) {
case Version, ConfigV5R1:
if _, ok := v.(ConfigV5R1); ok {
v = V5R1
}
switch v {
case V3R2, V3R1, V4R2, V4R1, V5R1:
msg, err = w.spec.(RegularBuilder).BuildMessage(ctx, !withStateInit, nil, messages)
if err != nil {
return nil, fmt.Errorf("build message err: %w", err)
}
case HighloadV2R2, HighloadV2Verified:
msg, err = w.spec.(*SpecHighloadV2R2).BuildMessage(ctx, messages)
if err != nil {
return nil, fmt.Errorf("build message err: %w", err)
}
case HighloadV3:
return nil, fmt.Errorf("use ConfigHighloadV3 for highload v3 spec")
default:
return nil, fmt.Errorf("send is not yet supported: %w", ErrUnsupportedWalletVersion)
}
case ConfigHighloadV3:
msg, err = w.spec.(*SpecHighloadV3).BuildMessage(ctx, messages)
if err != nil {
return nil, fmt.Errorf("build message err: %w", err)
}
default:
return nil, fmt.Errorf("send is not yet supported: %w", ErrUnsupportedWalletVersion)
}
return &tlb.ExternalMessage{
DstAddr: w.addr,
StateInit: stateInit,
Body: msg,
}, nil
}
func (w *Wallet) BuildTransfer(to *address.Address, amount tlb.Coins, bounce bool, comment string) (_ *Message, err error) {
var body *cell.Cell
if comment != "" {
body, err = CreateCommentCell(comment)
if err != nil {
return nil, err
}
}
return &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: bounce,
DstAddr: to,
Amount: amount,
Body: body,
},
}, nil
}
func (w *Wallet) BuildTransferEncrypted(ctx context.Context, to *address.Address, amount tlb.Coins, bounce bool, comment string) (_ *Message, err error) {
var body *cell.Cell
if comment != "" {
key, err := GetPublicKey(ctx, w.api, to)
if err != nil {
return nil, fmt.Errorf("failed to get destination contract (wallet) public key")
}
body, err = CreateEncryptedCommentCell(comment, w.WalletAddress(), w.key, key)
if err != nil {
return nil, err
}
}
return &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: bounce,
DstAddr: to,
Amount: amount,
Body: body,
},
}, nil
}
func (w *Wallet) Send(ctx context.Context, message *Message, waitConfirmation ...bool) error {
return w.SendMany(ctx, []*Message{message}, waitConfirmation...)
}
func (w *Wallet) SendMany(ctx context.Context, messages []*Message, waitConfirmation ...bool) error {
_, _, _, err := w.sendMany(ctx, messages, waitConfirmation...)
return err
}
// SendManyGetInMsgHash returns hash of external incoming message payload.
func (w *Wallet) SendManyGetInMsgHash(ctx context.Context, messages []*Message, waitConfirmation ...bool) ([]byte, error) {
_, _, inMsgHash, err := w.sendMany(ctx, messages, waitConfirmation...)
return inMsgHash, err
}
// SendManyWaitTxHash always waits for tx block confirmation and returns found tx hash in block.
func (w *Wallet) SendManyWaitTxHash(ctx context.Context, messages []*Message) ([]byte, error) {
tx, _, _, err := w.sendMany(ctx, messages, true)
if err != nil {
return nil, err
}
return tx.Hash, err
}
// SendManyWaitTransaction always waits for tx block confirmation and returns found tx.
func (w *Wallet) SendManyWaitTransaction(ctx context.Context, messages []*Message) (*tlb.Transaction, *ton.BlockIDExt, error) {
tx, block, _, err := w.sendMany(ctx, messages, true)
return tx, block, err
}
// SendWaitTransaction always waits for tx block confirmation and returns found tx.
func (w *Wallet) SendWaitTransaction(ctx context.Context, message *Message) (*tlb.Transaction, *ton.BlockIDExt, error) {
return w.SendManyWaitTransaction(ctx, []*Message{message})
}
func (w *Wallet) sendMany(ctx context.Context, messages []*Message, waitConfirmation ...bool) (tx *tlb.Transaction, block *ton.BlockIDExt, inMsgHash []byte, err error) {
block, err = w.api.CurrentMasterchainInfo(ctx)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get block: %w", err)
}
acc, err := w.api.WaitForBlock(block.SeqNo).GetAccount(ctx, block, w.addr)
if err != nil {
return nil, nil, nil, fmt.Errorf("failed to get account state: %w", err)
}
ext, err := w.BuildExternalMessageForMany(ctx, messages)
if err != nil {
return nil, nil, nil, err
}
inMsgHash = ext.Body.Hash()
if err = w.api.SendExternalMessage(ctx, ext); err != nil {
return nil, nil, nil, fmt.Errorf("failed to send message: %w", err)
}
if len(waitConfirmation) > 0 && waitConfirmation[0] {
tx, block, err = w.waitConfirmation(ctx, block, acc, ext)
if err != nil {
return nil, nil, nil, err
}
}
return tx, block, inMsgHash, nil
}
func (w *Wallet) waitConfirmation(ctx context.Context, block *ton.BlockIDExt, acc *tlb.Account, ext *tlb.ExternalMessage) (*tlb.Transaction, *ton.BlockIDExt, error) {
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
// fallback timeout to not stuck forever with background context
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), 180*time.Second)
defer cancel()
}
till, _ := ctx.Deadline()
ctx = w.api.Client().StickyContext(ctx)
for time.Now().Before(till) {
blockNew, err := w.api.WaitForBlock(block.SeqNo + 1).GetMasterchainInfo(ctx)
if err != nil {
continue
}
accNew, err := w.api.WaitForBlock(blockNew.SeqNo).GetAccount(ctx, blockNew, w.addr)
if err != nil {
continue
}
block = blockNew
if accNew.LastTxLT == acc.LastTxLT {
// if not in block, maybe LS lost our message, send it again
if err = w.api.SendExternalMessage(ctx, ext); err != nil {
continue
}
continue
}
lastLt, lastHash := accNew.LastTxLT, accNew.LastTxHash
// it is possible that > 5 new not related transactions will happen, and we should not lose our scan offset,
// to prevent this we will scan till we reach last seen offset.
for time.Now().Before(till) {
// we try to get last 5 transactions, and check if we have our new there.
txList, err := w.api.WaitForBlock(block.SeqNo).ListTransactions(ctx, w.addr, 5, lastLt, lastHash)
if err != nil {
continue
}
sawLastTx := false
for i, transaction := range txList {
if i == 0 {
// get previous of the oldest tx, in case if we need to scan deeper
lastLt, lastHash = txList[0].PrevTxLT, txList[0].PrevTxHash
}
if !sawLastTx && transaction.PrevTxLT == acc.LastTxLT &&
bytes.Equal(transaction.PrevTxHash, acc.LastTxHash) {
sawLastTx = true
}
if transaction.IO.In != nil && transaction.IO.In.MsgType == tlb.MsgTypeExternalIn {
extIn := transaction.IO.In.AsExternalIn()
if ext.StateInit != nil {
if extIn.StateInit == nil {
continue
}
if !bytes.Equal(ext.StateInit.Data.Hash(), extIn.StateInit.Data.Hash()) {
continue
}
if !bytes.Equal(ext.StateInit.Code.Hash(), extIn.StateInit.Code.Hash()) {
continue
}
}
if !bytes.Equal(extIn.Body.Hash(), ext.Body.Hash()) {
continue
}
return transaction, block, nil
}
}
if sawLastTx {
break
}
}
acc = accNew
}
return nil, nil, ErrTxWasNotConfirmed
}
// TransferNoBounce - can be used to transfer TON to not yet initialized contract/wallet
func (w *Wallet) TransferNoBounce(ctx context.Context, to *address.Address, amount tlb.Coins, comment string, waitConfirmation ...bool) error {
return w.transfer(ctx, to, amount, comment, false, waitConfirmation...)
}
// Transfer - safe transfer, in case of error on smart contract side, you will get coins back,
// cannot be used to transfer TON to not yet initialized contract/wallet
func (w *Wallet) Transfer(ctx context.Context, to *address.Address, amount tlb.Coins, comment string, waitConfirmation ...bool) error {
return w.transfer(ctx, to, amount, comment, true, waitConfirmation...)
}
// TransferWithEncryptedComment - same as Transfer but encrypts comment, throws error if target contract (address) has no get_public_key method.
func (w *Wallet) TransferWithEncryptedComment(ctx context.Context, to *address.Address, amount tlb.Coins, comment string, waitConfirmation ...bool) error {
transfer, err := w.BuildTransferEncrypted(ctx, to, amount, true, comment)
if err != nil {
return err
}
return w.Send(ctx, transfer, waitConfirmation...)
}
func CreateCommentCell(text string) (*cell.Cell, error) {
// comment ident
root := cell.BeginCell().MustStoreUInt(0, 32)
if err := root.StoreStringSnake(text); err != nil {
return nil, fmt.Errorf("failed to build comment: %w", err)
}
return root.EndCell(), nil
}
const EncryptedCommentOpcode = 0x2167da4b
func DecryptCommentCell(commentCell *cell.Cell, sender *address.Address, ourKey ed25519.PrivateKey, theirKey ed25519.PublicKey) ([]byte, error) {
slc := commentCell.BeginParse()
op, err := slc.LoadUInt(32)
if err != nil {
return nil, fmt.Errorf("failed to load op code: %w", err)
}
if op != EncryptedCommentOpcode {
return nil, fmt.Errorf("opcode not match encrypted comment")
}
xorKey, err := slc.LoadSlice(256)
if err != nil {
return nil, fmt.Errorf("failed to load xor key: %w", err)
}
for i := 0; i < 32; i++ {
xorKey[i] ^= theirKey[i]
}
if !bytes.Equal(xorKey, ourKey.Public().(ed25519.PublicKey)) {
return nil, fmt.Errorf("message was encrypted not for the given keys")
}
msgKey, err := slc.LoadSlice(128)
if err != nil {
return nil, fmt.Errorf("failed to load xor key: %w", err)
}
sharedKey, err := adnl.SharedKey(ourKey, theirKey)
if err != nil {
return nil, fmt.Errorf("failed to compute shared key: %w", err)
}
h := hmac.New(sha512.New, sharedKey)
h.Write(msgKey)
x := h.Sum(nil)
data, err := slc.LoadBinarySnake()
if err != nil {
return nil, fmt.Errorf("failed to load snake encrypted data: %w", err)
}
if len(data) < 32 || len(data)%16 != 0 {
return nil, fmt.Errorf("invalid data")
}
c, err := aes.NewCipher(x[:32])
if err != nil {
return nil, err
}
enc := cipher.NewCBCDecrypter(c, x[32:48])
enc.CryptBlocks(data, data)
if data[0] > 31 {
return nil, fmt.Errorf("invalid prefix size %d", data[0])
}
h = hmac.New(sha512.New, []byte(sender.String()))
h.Write(data)
if !bytes.Equal(msgKey, h.Sum(nil)[:16]) {
return nil, fmt.Errorf("incorrect msg key")
}
return data[data[0]:], nil
}
func CreateEncryptedCommentCell(text string, senderAddr *address.Address, ourKey ed25519.PrivateKey, theirKey ed25519.PublicKey) (*cell.Cell, error) {
// encrypted comment op code
root := cell.BeginCell().MustStoreUInt(EncryptedCommentOpcode, 32)
sharedKey, err := adnl.SharedKey(ourKey, theirKey)
if err != nil {
return nil, fmt.Errorf("failed to compute shared key: %w", err)
}
data := []byte(text)
pfxSz := 16
if len(data)%16 != 0 {
pfxSz += 16 - (len(data) % 16)
}
pfx := make([]byte, pfxSz)
pfx[0] = byte(len(pfx))
if _, err = rand.Read(pfx[1:]); err != nil {
return nil, fmt.Errorf("rand gen err: %w", err)
}
data = append(pfx, data...)
h := hmac.New(sha512.New, []byte(senderAddr.String()))
h.Write(data)
msgKey := h.Sum(nil)[:16]
h = hmac.New(sha512.New, sharedKey)
h.Write(msgKey)
x := h.Sum(nil)
c, err := aes.NewCipher(x[:32])
if err != nil {
return nil, err
}
enc := cipher.NewCBCEncrypter(c, x[32:48])
enc.CryptBlocks(data, data)
xorKey := ourKey.Public().(ed25519.PublicKey)
for i := 0; i < 32; i++ {
xorKey[i] ^= theirKey[i]
}
root.MustStoreSlice(xorKey, 256)
root.MustStoreSlice(msgKey, 128)
if err := root.StoreBinarySnake(data); err != nil {
return nil, fmt.Errorf("failed to build comment: %w", err)
}
return root.EndCell(), nil
}
func (w *Wallet) transfer(ctx context.Context, to *address.Address, amount tlb.Coins, comment string, bounce bool, waitConfirmation ...bool) (err error) {
transfer, err := w.BuildTransfer(to, amount, bounce, comment)
if err != nil {
return err
}
return w.Send(ctx, transfer, waitConfirmation...)
}
func (w *Wallet) DeployContractWaitTransaction(ctx context.Context, amount tlb.Coins, msgBody, contractCode, contractData *cell.Cell) (*address.Address, *tlb.Transaction, *ton.BlockIDExt, error) {
state := &tlb.StateInit{
Data: contractData,
Code: contractCode,
}
stateCell, err := tlb.ToCell(state)
if err != nil {
return nil, nil, nil, err
}
addr := address.NewAddress(0, 0, stateCell.Hash())
tx, block, err := w.SendWaitTransaction(ctx, &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: false,
DstAddr: addr,
Amount: amount,
Body: msgBody,
StateInit: state,
},
})
if err != nil {
return nil, nil, nil, err
}
return addr, tx, block, nil
}
// Deprecated: use DeployContractWaitTransaction
func (w *Wallet) DeployContract(ctx context.Context, amount tlb.Coins, msgBody, contractCode, contractData *cell.Cell, waitConfirmation ...bool) (*address.Address, error) {
state := &tlb.StateInit{
Data: contractData,
Code: contractCode,
}
stateCell, err := tlb.ToCell(state)
if err != nil {
return nil, err
}
addr := address.NewAddress(0, 0, stateCell.Hash())
if err = w.Send(ctx, &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: false,
DstAddr: addr,
Amount: amount,
Body: msgBody,
StateInit: state,
},
}, waitConfirmation...); err != nil {
return nil, err
}
return addr, nil
}
// Deprecated: use ton.FindLastTransactionByInMsgHash
// FindTransactionByInMsgHash returns transaction in wallet account with incoming message hash equal to msgHash.
func (w *Wallet) FindTransactionByInMsgHash(ctx context.Context, msgHash []byte, maxTxNumToScan ...int) (*tlb.Transaction, error) {
tx, err := w.api.FindLastTransactionByInMsgHash(ctx, w.addr, msgHash, maxTxNumToScan...)
if err != nil && errors.Is(err, ton.ErrTxWasNotFound) {
return nil, ErrTxWasNotFound
}
return tx, err
}
func SimpleMessage(to *address.Address, amount tlb.Coins, payload *cell.Cell) *Message {
return &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: true,
DstAddr: to,
Amount: amount,
Body: payload,
},
}
}
// SimpleMessageAutoBounce - will determine bounce flag from address
func SimpleMessageAutoBounce(to *address.Address, amount tlb.Coins, payload *cell.Cell) *Message {
return &Message{
Mode: PayGasSeparately + IgnoreErrors,
InternalMessage: &tlb.InternalMessage{
IHRDisabled: true,
Bounce: to.IsBounceable(),
DstAddr: to,
Amount: amount,
Body: payload,
},
}
}