Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wallet backup #553

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading