-
Notifications
You must be signed in to change notification settings - Fork 13
/
tonapi.go
51 lines (45 loc) · 1.46 KB
/
tonapi.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
package main
import (
"context"
"crypto/ed25519"
"fmt"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/abi"
"github.com/tonkeeper/tongo/liteapi"
"github.com/tonkeeper/tonproof/datatype"
"math/big"
)
const (
GetWalletPath = "/v1/wallet/getWalletPublicKey"
GetAccountInfoPath = "/v1/account/getInfo"
)
var networks = map[string]*liteapi.Client{}
func GetAccountInfo(ctx context.Context, address tongo.AccountID, net *liteapi.Client) (*datatype.AccountInfo, error) {
account, err := net.GetAccountState(ctx, address)
if err != nil {
return nil, err
}
accountInfo := datatype.AccountInfo{
Balance: int64(account.Account.Account.Storage.Balance.Grams),
Status: string(account.Account.Status()),
}
accountInfo.Address.Raw = address.ToRaw()
accountInfo.Address.Bounceable = address.ToHuman(true, false)
accountInfo.Address.NonBounceable = address.ToHuman(false, false)
return &accountInfo, nil
}
func GetWalletPubKey(ctx context.Context, address tongo.AccountID, net *liteapi.Client) (ed25519.PublicKey, error) {
_, result, err := abi.GetPublicKey(ctx, net, address)
if err != nil {
return nil, err
}
if r, ok := result.(abi.GetPublicKeyResult); ok {
i := big.Int(r.PublicKey)
b := i.Bytes()
if len(b) < 24 || len(b) > 32 { //govno kakoe-to
return nil, fmt.Errorf("invalid publock key")
}
return append(make([]byte, 32-len(b)), b...), nil //make padding if first bytes are empty
}
return nil, fmt.Errorf("can't get publick key")
}