Skip to content

Commit

Permalink
Merge branch 'cid-pk' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
#	go.sum
  • Loading branch information
457813723 committed Mar 3, 2024
2 parents 23e9aa0 + 5b5942a commit f48f52c
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 4 deletions.
29 changes: 29 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [Transaction Status](#transaction-status)
* [Webauthn Verify](#webautn-verify)
* [Add Cid Info](#add-cid-info)
* [Store Cid Pk](#store-cid-pk)
## API LIST
test api url https://test-webauthn-api.did.id

Expand Down Expand Up @@ -350,6 +351,34 @@ prod api url https://webauthn-api.did.id
"notes":"bbb",
"device":"ios"

}
```
#### Response
```json
{
"err_no": 0,
"err_msg": "",
"data": true
}
```


### Store Cid Pk

#### Request
* path: /v1/webauthn/store-cid-pk
* params:
* sign_addr: sign address
* cid: cid
* msg: sign msg
* signature: signature
```json
{
"sign_addr":"ckt1qqexmutxu0c2jq9q4msy8cc6fh4q7q02xvr7dc347zw3ks3qka0m6qggql2ysqakcxwgzt6ugh7kp2c5stuyvdp7y5yq04zgqwmvr8yp9awytltq4v2g97zxxslz2aqmhst",
"cid":"d44803b6c19c812f5c45",
"msg":"",
"signature":""

}
```
#### Response
Expand Down
16 changes: 15 additions & 1 deletion dao/t_cid_pk.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package dao

import "das-multi-device/tables"
import (
"das-multi-device/tables"
"gorm.io/gorm/clause"
)

func (d *DbDao) GetCidPk(cid1 string) (cidPk tables.TableCidPk, err error) {
err = d.parserDb.Where("`cid`= ? ", cid1).Find(&cidPk).Error
return
}

func (d *DbDao) InsertCidPk(data tables.TableCidPk) (err error) {
if err := d.db.Clauses(clause.OnConflict{
DoUpdates: clause.AssignmentColumns([]string{
"OriginPk",
}),
}).Create(&data).Error; err != nil {
return err
}
return
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module das-multi-device
go 1.18

require (
github.com/dotbitHQ/das-lib v1.1.1-0.20231203091545-c3ae306378a6
github.com/dotbitHQ/das-lib v1.1.1-0.20240303123423-6ce9d59430a0
github.com/fsnotify/fsnotify v1.5.4
github.com/getsentry/sentry-go v0.25.0
github.com/gin-gonic/gin v1.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5O
github.com/docker/docker v1.4.2-0.20180625184442-8e610b2b55bf/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/dop251/goja v0.0.0-20211011172007-d99e4b8cbf48/go.mod h1:R9ET47fwRVRPZnOGvHxxhuZcbrMCuiqOz3Rlrh4KSnk=
github.com/dop251/goja_nodejs v0.0.0-20210225215109-d91c329300e7/go.mod h1:hn7BA7c8pLvoGndExHudxTDKZ84Pyvv+90pbBjbTz0Y=
github.com/dotbitHQ/das-lib v1.1.1-0.20231203091545-c3ae306378a6 h1:ej2gkqt4l0ps9VD+mYjKcSO1w4/c155hxulF0o2dNgw=
github.com/dotbitHQ/das-lib v1.1.1-0.20231203091545-c3ae306378a6/go.mod h1:CvkrfnYLnKjEFTmlz70wssdkRxGvTPnAR5fcAikfu7s=
github.com/dotbitHQ/das-lib v1.1.1-0.20240303123423-6ce9d59430a0 h1:yjZWBBLPFKTxxMmFgwuF4nSYqgmARaM0dFLjQSEoS8k=
github.com/dotbitHQ/das-lib v1.1.1-0.20240303123423-6ce9d59430a0/go.mod h1:CvkrfnYLnKjEFTmlz70wssdkRxGvTPnAR5fcAikfu7s=
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819 h1:RIB4cRk+lBqKK3Oy0r2gRX4ui7tuhiZq2SuTtTCi0/0=
Expand Down
85 changes: 85 additions & 0 deletions http_server/handle/store_cid_pk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package handle

import (
"das-multi-device/tables"
"encoding/hex"
"fmt"
"github.com/dotbitHQ/das-lib/common"
"github.com/dotbitHQ/das-lib/core"
"github.com/dotbitHQ/das-lib/http_api"
"github.com/dotbitHQ/das-lib/sign"
"github.com/gin-gonic/gin"
"github.com/scorpiotzh/toolib"
"net/http"
)

type ReqStoreCidPk struct {
Cid string `json:"cid" binding:"required"`
SignAddr string `json:"sign_addr" binding:"required"`
Msg string `json:"msg" binding:"required"`
Signature string `json:"signature" binding:"required"`
}

func (h *HttpHandle) StoreCidPk(ctx *gin.Context) {
var (
funcName = "StoreCidPk"
clientIp = GetClientIp(ctx)
req *ReqStoreCidPk
apiResp http_api.ApiResp
err error
)

if err := ctx.ShouldBindJSON(&req); err != nil {
log.Error("ShouldBindJSON err: ", err.Error(), funcName, clientIp, ctx)
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "params invalid")
ctx.JSON(http.StatusOK, apiResp)
return
}

log.Info("ApiReq:", funcName, clientIp, toolib.JsonString(req), ctx)

if err = h.doStoreCidPk(req, &apiResp); err != nil {
log.Error("doStoreCidPk err:", err.Error(), funcName, clientIp, ctx)
}

ctx.JSON(http.StatusOK, apiResp)
}

func (h *HttpHandle) doStoreCidPk(req *ReqStoreCidPk, apiResp *http_api.ApiResp) (err error) {
signType := common.DasAlgorithmIdWebauthn
signAddressHex, err := h.dasCore.Daf().NormalToHex(core.DasAddressNormal{
ChainType: common.ChainTypeWebauthn,
AddressNormal: req.SignAddr,
})
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "sign address NormalToHex err")
return err
}
h.dasCore.AddPkIndexForSignMsg(&req.Signature, 255)
signMsg := req.Msg
signature := req.Signature
address := signAddressHex.AddressHex
if address[20:] != hex.EncodeToString(common.CalculateCid1(req.Cid)) {
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "cid err")
return nil
}
verifyRes, _, err := http_api.VerifySignature(signType, signMsg, signature, address)
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeSignError, "VerifySignature err")
return fmt.Errorf("VerifySignature err: %s", err.Error())
}
if !verifyRes {
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "Validation failed")
return nil
}
var cidPk tables.TableCidPk
cidPk.OriginPk = sign.GetPkFromSignature(common.Hex2Bytes(signature))
cidPk.Pk = address[20:]
cidPk.Cid = req.Cid
if err := h.dbDao.InsertCidPk(cidPk); err != nil {
apiResp.ApiRespErr(http_api.ApiCodeDbError, "InsertCidPk err")
return fmt.Errorf("InsertCidPk err: %s", err.Error())
}
apiResp.ApiRespOK(true)
return
}
1 change: 1 addition & 0 deletions http_server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (h *HttpServer) initRouter() {
v1.POST("/webauthn/add-test-cid", api_code.DoMonitorLog("add-test-cid"), h.h.AddTestCid)
v1.POST("/webauthn/cover-cid", api_code.DoMonitorLog("cover-cid"), h.h.CoverCid)
v1.POST("/webauthn/verify", api_code.DoMonitorLog("cid-info"), h.h.VerifyWebauthnSign)
v1.POST("/webauthn/store-cid-pk", api_code.DoMonitorLog("store-cid-pk"), h.h.StoreCidPk)
v1.StaticFS("/webrtc/chatroom", http.FS(webrtc.WebRTC))
v1.GET("/webrtc/socket", h.h.WebRTCWebSocket)
}
Expand Down

0 comments on commit f48f52c

Please sign in to comment.