-
Notifications
You must be signed in to change notification settings - Fork 12
/
state_init.go
75 lines (68 loc) · 1.71 KB
/
state_init.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
package main
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/wallet"
)
var knownHashes = make(map[string]wallet.Version)
func init() {
for i := wallet.Version(0); i <= wallet.V4R2; i++ {
ver := wallet.GetCodeHashByVer(i)
knownHashes[hex.EncodeToString(ver[:])] = i
}
}
func ParseStateInit(stateInit string) ([]byte, error) {
cells, err := boc.DeserializeBocBase64(stateInit)
if err != nil || len(cells) != 1 {
return nil, err
}
var state tlb.StateInit
err = tlb.Unmarshal(cells[0], &state)
if err != nil {
return nil, err
}
if !state.Data.Exists || !state.Code.Exists {
return nil, fmt.Errorf("empty init state")
}
codeHash, err := state.Code.Value.Value.HashString()
if err != nil {
return nil, err
}
version, prs := knownHashes[codeHash]
if !prs {
return nil, fmt.Errorf("unknown code hash")
}
var pubKey tlb.Bits256
switch version {
case wallet.V1R1, wallet.V1R2, wallet.V1R3, wallet.V2R1, wallet.V2R2:
var data wallet.DataV1V2
err = tlb.Unmarshal(&state.Data.Value.Value, &data)
if err != nil {
return nil, err
}
pubKey = data.PublicKey
case wallet.V3R1, wallet.V3R2, wallet.V4R1, wallet.V4R2:
var data wallet.DataV3
err = tlb.Unmarshal(&state.Data.Value.Value, &data)
if err != nil {
return nil, err
}
pubKey = data.PublicKey
}
return pubKey[:], nil
}
func CompareStateInitWithAddress(a tongo.AccountID, stateInit string) (bool, error) {
cells, err := boc.DeserializeBocBase64(stateInit)
if err != nil || len(cells) != 1 {
return false, err
}
h, err := cells[0].Hash()
if err != nil {
return false, err
}
return bytes.Equal(h, a.Address[:]), nil
}