-
Notifications
You must be signed in to change notification settings - Fork 41
/
wallet_v3.go
88 lines (77 loc) · 2.15 KB
/
wallet_v3.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
package wallet
import (
"crypto/ed25519"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
)
type DataV3 struct {
Seqno uint32
SubWalletId uint32
PublicKey tlb.Bits256
}
type walletV3 struct {
version Version
publicKey ed25519.PublicKey
workchain int
subWalletID uint32
}
var _ wallet = &walletV3{}
func newWalletV3(ver Version, key ed25519.PublicKey, options Options) *walletV3 {
workchain := defaultOr(options.Workchain, 0)
subWalletID := defaultOr(options.SubWalletID, uint32(DefaultSubWallet+workchain))
return &walletV3{
version: ver,
publicKey: key,
workchain: workchain,
subWalletID: subWalletID,
}
}
func (w *walletV3) generateAddress() (ton.AccountID, error) {
stateInit, err := w.generateStateInit()
if err != nil {
return ton.AccountID{}, err
}
return generateAddress(w.workchain, *stateInit)
}
func (w *walletV3) generateStateInit() (*tlb.StateInit, error) {
data := DataV3{
Seqno: 0,
SubWalletId: w.subWalletID,
PublicKey: publicKeyToBits(w.publicKey),
}
return generateStateInit(w.version, data)
}
func (w *walletV3) maxMessageNumber() int {
return 4
}
func (w *walletV3) createSignedMsgBodyCell(privateKey ed25519.PrivateKey, internalMessages []RawMessage, msgConfig MessageConfig) (*boc.Cell, error) {
body := MessageV3{
SubWalletId: w.subWalletID,
ValidUntil: uint32(msgConfig.ValidUntil.Unix()),
Seqno: msgConfig.Seqno,
RawMessages: PayloadV1toV4(internalMessages),
}
bodyCell := boc.NewCell()
if err := tlb.Marshal(bodyCell, body); err != nil {
return nil, err
}
return signBodyCell(*bodyCell, privateKey)
}
func (w *walletV3) nextMessageParams(state tlb.ShardAccount) (nextMsgParams, error) {
if state.Account.Status() == tlb.AccountActive {
var data DataV3
cell := boc.Cell(state.Account.Account.Storage.State.AccountActive.StateInit.Data.Value.Value)
if err := tlb.Unmarshal(&cell, &data); err != nil {
return nextMsgParams{}, err
}
return nextMsgParams{
Seqno: data.Seqno,
}, nil
}
init, err := w.generateStateInit()
if err != nil {
return nextMsgParams{}, err
}
return nextMsgParams{Init: init}, nil
}