-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ordinal endpoint(ordinal service + unisat) and tests
- Loading branch information
1 parent
9a2bdd8
commit 5c4cdbf
Showing
33 changed files
with
1,279 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Ordinals in API system | ||
|
||
The Babylon Staking API allows for the option to deploy additional endpoints | ||
that check whether a UTXO contains an inscription or not, with the aim to help | ||
staking applications identify whether they should avoid spending a particular UTXO. | ||
This is accomplished through a connection to the | ||
[Ordinal Service](https://github.com/ordinals/ord) and | ||
a connection to the Unisat API. | ||
Due to Unisat being a payed service and applying rate limits, | ||
the API initially tries to get the status of a UTXO through the Ordinals Service, | ||
and if that fails, then contacts the Unisat API, | ||
effectively using it as a back-up mechanism to handle downtime from the Ordinals Service. | ||
|
||
NOTE: To enable the optional ordinal API endpoint, you will need to provide the | ||
`ordinal` and `unisat` configurations under `assets` | ||
|
||
## Ordinal service Client | ||
|
||
WIP | ||
|
||
## Unisat Service Client | ||
|
||
You can find more information about Unisat's Ordinal/BRC-20/Runes related endpoints at: | ||
https://docs.unisat.io/ | ||
|
||
In our service, we only utilize the following endpoint: | ||
- `/v1/indexer/address/{{address}}/inscription-utxo-data` | ||
|
||
### How to Use It | ||
|
||
1. Log in via https://developer.unisat.io/account/login (create an account if you don't have one). | ||
2. Copy the `API-Key`. | ||
3. Set the key as an environment variable named `UNISAT_TOKEN`. | ||
4. Configure the values for `unisat.host`, `limit`, `timeout`, etc. Refer to `config-docker.yml`. | ||
5. Ensure you also set up the `ordinals` configuration, as this is a dependency. | ||
6. Call the POST endpoint `/v1/ordinals/verify-utxos` as shown in the example below: | ||
7. The calls to unisat will only be triggered if the ordinal service is not responding or returning errors | ||
```POST | ||
{ | ||
"utxos": [ | ||
{ | ||
"txid": "143c33b4ff4450a60648aec6b4d086639322cb093195226c641ae4f0ae33c3f5", | ||
"vout": 2 | ||
}, | ||
{ | ||
"txid": "be3877c8dedd716f026cc77ef3f04f940b40b064d1928247cff5bb08ef1ba58e", | ||
"vout": 0 | ||
}, | ||
{ | ||
"txid": "d7f65a37f59088b3b4e4bc119727daa0a0dd8435a645c49e6a665affc109539d", | ||
"vout": 0 | ||
} | ||
], | ||
"address": "tb1pyqjxwcdv6pfcaj2l565ludclz2pwu2k5azs6uznz8kml74kkma6qm0gzlv" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/babylonchain/staking-api-service/internal/types" | ||
"github.com/babylonchain/staking-api-service/internal/utils" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
) | ||
|
||
type VerifyUTXOsRequestPayload struct { | ||
Address string `json:"address"` | ||
UTXOs []types.UTXOIdentifier `json:"utxos"` | ||
} | ||
|
||
func parseRequestPayload(request *http.Request, maxUTXOs uint32, netParam *chaincfg.Params) (*VerifyUTXOsRequestPayload, *types.Error) { | ||
var payload VerifyUTXOsRequestPayload | ||
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid input format") | ||
} | ||
utxos := payload.UTXOs | ||
if len(utxos) == 0 { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "empty UTXO array") | ||
} | ||
|
||
if uint32(len(utxos)) > maxUTXOs { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "too many UTXOs in the request") | ||
} | ||
|
||
for _, utxo := range utxos { | ||
if !utils.IsValidTxHash(utxo.Txid) { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid UTXO txid") | ||
} else if utxo.Vout < 0 { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, "invalid UTXO vout") | ||
} | ||
} | ||
|
||
if err := utils.IsValidBtcAddress(payload.Address, netParam); err != nil { | ||
return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, err.Error()) | ||
} | ||
return &payload, nil | ||
} | ||
|
||
func (h *Handler) VerifyUTXOs(request *http.Request) (*Result, *types.Error) { | ||
inputs, err := parseRequestPayload(request, h.config.Assets.MaxUTXOs, h.config.Server.BTCNetParam) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
results, err := h.services.VerifyUTXOs(request.Context(), inputs.UTXOs, inputs.Address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewResult(results), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.