-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # go.mod # go.sum
- Loading branch information
Showing
6 changed files
with
133 additions
and
4 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
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 | ||
} |
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,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 | ||
} |
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