Skip to content

Commit

Permalink
add address parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Nov 3, 2023
1 parent 821985e commit b6b855c
Show file tree
Hide file tree
Showing 16 changed files with 2,214 additions and 561 deletions.
78 changes: 78 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5684,6 +5684,84 @@
]
}
},
"/v2/address/{account_id}/parse": {
"get": {
"description": "parse address and display in all formats",
"operationId": "addressParse",
"parameters": [
{
"$ref": "#/components/parameters/accountIDParameter"
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"bounceable": {
"properties": {
"b64": {
"type": "string"
},
"b64url": {
"type": "string"
}
},
"required": [
"b64",
"b64url"
],
"type": "object"
},
"given_type": {
"type": "string"
},
"non_bounceable": {
"properties": {
"b64": {
"type": "string"
},
"b64url": {
"type": "string"
}
},
"required": [
"b64",
"b64url"
],
"type": "object"
},
"raw_form": {
"example": "0:6e731f2e28b73539a7f85ac47ca104d5840b229351189977bb6151d36b5e3f5e",
"type": "string"
},
"test_only": {
"type": "boolean"
}
},
"required": [
"raw_form",
"bounceable",
"non_bounceable",
"given_type",
"test_only"
],
"type": "object"
}
}
},
"description": "all forms and info"
},
"default": {
"$ref": "#/components/responses/Error"
}
},
"tags": [
"Accounts"
]
}
},
"/v2/blockchain/accounts/{account_id}": {
"get": {
"description": "Get low-level information about an account taken directly from the blockchain.",
Expand Down
56 changes: 55 additions & 1 deletion api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,61 @@ paths:
$ref: '#/components/schemas/BlockchainAccountInspect'
'default':
$ref: '#/components/responses/Error'

/v2/address/{account_id}/parse:
get:
description: parse address and display in all formats
operationId: addressParse
tags:
- Accounts
parameters:
- $ref: '#/components/parameters/accountIDParameter'
responses:
'200':
description: all forms and info
content:
application/json:
schema:
type: object
required:
- raw_form
- bounceable
- non_bounceable
- given_type
- test_only
properties:
raw_form:
type: string
example: "0:6e731f2e28b73539a7f85ac47ca104d5840b229351189977bb6151d36b5e3f5e"
bounceable:
required:
- b64
- b64url
type: object
properties:
b64:
type: string
b64url:
type: string
non_bounceable:
required:
- b64
- b64url
type: object
properties:
b64:
type: string
b64url:
type: string
given_type:
type: string
test_only:
type: boolean

default:
$ref: '#/components/responses/Error'



/v2/events/emulate:
post:
description: Emulate sending message to blockchain
Expand Down
27 changes: 27 additions & 0 deletions pkg/api/account_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package api

import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
"strings"

"github.com/tonkeeper/opentonapi/internal/g"

Expand Down Expand Up @@ -439,3 +441,28 @@ func pubkeyFromCodeData(code, data []byte) ([]byte, error) {
return nil, fmt.Errorf("unknown wallet version")
}
}

func (h *Handler) AddressParse(ctx context.Context, params oas.AddressParseParams) (*oas.AddressParseOK, error) {
address, err := tongo.ParseAddress(params.AccountID)
if err != nil {
return nil, toError(http.StatusBadRequest, err)
}
var res oas.AddressParseOK
res.RawForm = address.ID.ToRaw()
res.Bounceable.B64url = address.ID.ToHuman(true, false)
res.NonBounceable.B64url = address.ID.ToHuman(false, false)
b, _ := base64.URLEncoding.DecodeString(res.Bounceable.B64url)
res.Bounceable.B64 = base64.StdEncoding.EncodeToString(b)
b, _ = base64.URLEncoding.DecodeString(res.NonBounceable.B64url)
res.NonBounceable.B64 = base64.StdEncoding.EncodeToString(b)
if strings.Contains(params.AccountID, ":") {
res.GivenType = "raw_form"
} else if strings.Contains(params.AccountID, ".") {
res.GivenType = "dns"
} else if address.Bounce {
res.GivenType = "friendly_bounceable"
} else {
res.GivenType = "friendly_non_bounceable"
}
return &res, nil //todo: add testnet_only
}
117 changes: 117 additions & 0 deletions pkg/oas/oas_handlers_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b6b855c

Please sign in to comment.