Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/remove-wallet-backup'
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Nov 28, 2024
2 parents 987ea6d + 2f880fd commit 082cbd9
Show file tree
Hide file tree
Showing 12 changed files with 1 addition and 875 deletions.
82 changes: 0 additions & 82 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,6 @@
},
"description": "a list of account ids"
},
"Backup": {
"content": {
"application/octet-stream": {
"schema": {
"format": "binary",
"type": "string"
}
}
},
"description": "Information for saving backup",
"required": true
},
"BatchBoc": {
"content": {
"application/json": {
Expand Down Expand Up @@ -10467,76 +10455,6 @@
]
}
},
"/v2/wallet/backup": {
"get": {
"description": "Get backup info",
"operationId": "getWalletBackup",
"parameters": [
{
"in": "header",
"name": "X-TonConnect-Auth",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"dump": {
"type": "string"
}
},
"required": [
"dump"
],
"type": "object"
}
}
},
"description": "get wallet dump"
},
"default": {
"$ref": "#/components/responses/Error"
}
},
"tags": [
"Wallet"
]
},
"put": {
"description": "Set backup info",
"operationId": "setWalletBackup",
"parameters": [
{
"in": "header",
"name": "X-TonConnect-Auth",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"$ref": "#/components/requestBodies/Backup"
},
"responses": {
"200": {
"description": "success"
},
"default": {
"$ref": "#/components/responses/Error"
}
},
"tags": [
"Wallet"
]
}
},
"/v2/wallet/emulate": {
"post": {
"description": "Emulate sending message to blockchain",
Expand Down
53 changes: 0 additions & 53 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1931,51 +1931,6 @@ paths:
$ref: '#/components/schemas/AccountInfoByStateInit'
'default':
$ref: '#/components/responses/Error'

/v2/wallet/backup:
get:
description: Get backup info
operationId: getWalletBackup
tags:
- Wallet
parameters:
- in: header
name: X-TonConnect-Auth
schema:
type: string
required: true
responses:
'200':
description: get wallet dump
content:
application/json:
schema:
type: object
required:
- dump
properties:
dump:
type: string
'default':
$ref: '#/components/responses/Error'
put:
description: Set backup info
operationId: setWalletBackup
tags:
- Wallet
parameters:
- in: header
name: X-TonConnect-Auth
schema:
type: string
required: true
requestBody:
$ref: "#/components/requestBodies/Backup"
responses:
'200':
description: success
'default':
$ref: '#/components/responses/Error'
/v2/wallet/auth/proof:
post:
description: Account verification and token issuance
Expand Down Expand Up @@ -3337,14 +3292,6 @@ components:
state_init:
type: string
format: cell-base64
Backup:
description: "Information for saving backup"
required: true
content:
application/octet-stream:
schema:
type: string
format: binary
TonConnectStateInit:
description: "Data that is expected"
required: true
Expand Down
112 changes: 1 addition & 111 deletions pkg/api/wallet_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@ package api

import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/tonkeeper/opentonapi/pkg/core"

"github.com/tonkeeper/tongo/ton"
"net/http"

"github.com/tonkeeper/opentonapi/pkg/oas"
"github.com/tonkeeper/opentonapi/pkg/wallet"
Expand All @@ -25,107 +16,6 @@ import (
tongoWallet "github.com/tonkeeper/tongo/wallet"
)

func (h *Handler) SetWalletBackup(ctx context.Context, request oas.SetWalletBackupReq, params oas.SetWalletBackupParams) error {
pubKey, verify, err := checkTonConnectToken(params.XTonConnectAuth, h.tonConnect.GetSecret())
if err != nil {
return toError(http.StatusBadRequest, err)
}
if !verify {
return toError(http.StatusBadRequest, fmt.Errorf("failed verify"))
}

walletBalance, err := getTotalBalances(ctx, h.storage, pubKey)
if err != nil {
return toError(http.StatusInternalServerError, err)
}
if walletBalance < int64(ton.OneTON) {
return toError(http.StatusBadRequest, fmt.Errorf("wallet must have more than 1 TON"))
}

fileName := fmt.Sprintf("%x.dump", pubKey)
tempFileName := fileName + fmt.Sprintf(".temp%v", time.Now().Nanosecond()+time.Now().Second())
file, err := os.Create(tempFileName)
if err != nil {
return toError(http.StatusInternalServerError, err)
}
defer file.Close()
_, err = io.Copy(file, io.LimitReader(request.Data, 640*1024)) //640K ought to be enough for anybody
if err != nil {
return toError(http.StatusInternalServerError, err)
}
file.Close()
err = os.Rename(tempFileName, fileName)
if err != nil {
return toError(http.StatusInternalServerError, err)
}
return nil
}

func (h *Handler) GetWalletBackup(ctx context.Context, params oas.GetWalletBackupParams) (*oas.GetWalletBackupOK, error) {
pubKey, verify, err := checkTonConnectToken(params.XTonConnectAuth, h.tonConnect.GetSecret())
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
if !verify {
return nil, toError(http.StatusBadRequest, fmt.Errorf("failed verify"))
}

dump, err := os.ReadFile(fmt.Sprintf("%v.dump", hex.EncodeToString(pubKey)))
if err != nil {
return nil, toError(http.StatusInternalServerError, err)
}

return &oas.GetWalletBackupOK{Dump: string(dump)}, nil
}

func checkTonConnectToken(authToken, secret string) ([]byte, bool, error) {
decodedData, err := base64.URLEncoding.DecodeString(authToken)
if err != nil {
return nil, false, err
}
if len(decodedData) <= 32 {
return nil, false, fmt.Errorf("invalid payload length")
}
pubKey := decodedData[:32]
signature := decodedData[32:]

hmacHash := hmac.New(sha256.New, []byte(secret))
hmacHash.Write(pubKey)
computedSignature := hmacHash.Sum(nil)
if !hmac.Equal(signature, computedSignature) {
return nil, false, nil
}

return pubKey, true, nil
}

func getTotalBalances(ctx context.Context, storage storage, pubKey []byte) (int64, error) {
var balance int64
versions := []tongoWallet.Version{
tongoWallet.V1R1, tongoWallet.V1R2, tongoWallet.V1R3,
tongoWallet.V2R1, tongoWallet.V2R2,
tongoWallet.V3R1, tongoWallet.V3R2,
tongoWallet.V4R1, tongoWallet.V4R2,
tongoWallet.V5Beta,
}
var walletAddresses []tongo.AccountID
for _, version := range versions {
walletAddress, err := tongoWallet.GenerateWalletAddress(pubKey, version, nil, 0, nil)
if err != nil {
continue
}
walletAddresses = append(walletAddresses, walletAddress)
}
for _, address := range walletAddresses {
account, err := storage.GetRawAccount(ctx, address)
if err != nil {
continue
}
balance += account.TonBalance
}
return balance, nil
}

func (h *Handler) GetWalletsByPublicKey(ctx context.Context, params oas.GetWalletsByPublicKeyParams) (*oas.Accounts, error) {
publicKey, err := hex.DecodeString(params.PublicKey)
if err != nil {
Expand Down
Loading

0 comments on commit 082cbd9

Please sign in to comment.