Skip to content

Commit

Permalink
Merge branch 'anylock' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpiotzh committed Jun 3, 2024
2 parents fd1474a + 3091597 commit 09e8cf1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
12 changes: 10 additions & 2 deletions dao/t_reverse_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,16 @@ func (d *DbDao) DeleteReverseInfo(outpoints []string) error {
return d.db.Where(" outpoint IN (?) ", outpoints).Delete(&tables.TableReverseInfo{}).Error
}

func (d *DbDao) FindLatestReverseRecord(chainType common.ChainType, address string) (r tables.TableReverseInfo, err error) {
err = d.db.Where(" chain_type=? AND address=? ", chainType, address).Order(" block_number DESC,outpoint DESC ").Limit(1).Find(&r).Error
func (d *DbDao) FindLatestReverseRecord(chainType common.ChainType, address, btcAddr string) (r tables.TableReverseInfo, err error) {
if btcAddr != "" {
err = d.db.Where("chain_type=? AND (p2sh_p2wpkh=? OR p2tr=?)",
chainType, btcAddr, btcAddr).
Order(" block_number DESC, outpoint DESC ").Limit(1).Find(&r).Error
} else {
err = d.db.Where(" chain_type=? AND address=? ", chainType, address).
Order(" block_number DESC,outpoint DESC ").Limit(1).Find(&r).Error
}

return
}

Expand Down
2 changes: 1 addition & 1 deletion http_server/handle/batch_reverse_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (h *HttpHandle) doBatchReverseRecord(req *ReqBatchReverseRecord, apiResp *c
}

func (h *HttpHandle) checkReverse(chainType common.ChainType, addressHex string, apiResp *code.ApiResp) (account, errMsg string) {
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex)
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex, "")
if err != nil {
log.Error("FindLatestReverseRecord err: ", err.Error(), addressHex)
apiResp.ApiRespErr(http_api.ApiCodeDbError, "find reverse record err")
Expand Down
37 changes: 31 additions & 6 deletions http_server/handle/batch_reverse_record_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,43 @@ func (h *HttpHandle) doBatchReverseRecordV2(req *ReqBatchReverseRecordV2, apiRes

// check params
var listKeyInfo []*core.DasAddressHex
var listBtcAddr []string
for _, v := range req.BatchKeyInfo {
addrHex, err := v.FormatChainTypeAddress(config.Cfg.Server.Net, false)
if err != nil {
log.Warn("FormatChainTypeAddress err: %s", err.Error())
listKeyInfo = append(listKeyInfo, nil)
listBtcAddr = append(listBtcAddr, "")
continue
} else if addrHex.DasAlgorithmId == common.DasAlgorithmIdAnyLock {
}
switch addrHex.DasAlgorithmId {
case common.DasAlgorithmIdAnyLock:
anyLockAddrHex, err := addrHex.FormatAnyLock()
if err != nil {
log.Warn("FormatAnyLock err: %s", err.Error())
listKeyInfo = append(listKeyInfo, nil)
listBtcAddr = append(listBtcAddr, "")
continue
}
listKeyInfo = append(listKeyInfo, anyLockAddrHex)
} else {
listBtcAddr = append(listBtcAddr, "")
case common.DasAlgorithmIdEth, common.DasAlgorithmIdTron,
common.DasAlgorithmIdDogeChain, common.DasAlgorithmIdWebauthn:
listKeyInfo = append(listKeyInfo, addrHex)
listBtcAddr = append(listBtcAddr, "")
case common.DasAlgorithmIdBitcoin:
log.Info("doReverseInfoV2:", addrHex.DasAlgorithmId, addrHex.DasSubAlgorithmId, addrHex.AddressHex)
switch addrHex.DasSubAlgorithmId {
case common.DasSubAlgorithmIdBitcoinP2PKH, common.DasSubAlgorithmIdBitcoinP2WPKH:
listKeyInfo = append(listKeyInfo, addrHex)
listBtcAddr = append(listBtcAddr, "")
default:
listKeyInfo = append(listKeyInfo, addrHex)
listBtcAddr = append(listBtcAddr, v.KeyInfo.Key)
}
default:
listKeyInfo = append(listKeyInfo, nil)
listBtcAddr = append(listBtcAddr, "")
}
}

Expand All @@ -108,7 +129,7 @@ func (h *HttpHandle) doBatchReverseRecordV2(req *ReqBatchReverseRecordV2, apiRes
if v == nil {
tmp.ErrMsg = "address is invalid"
} else {
account, errMsg := h.checkReverseV2(v.ChainType, v.AddressHex, req.BatchKeyInfo[i].KeyInfo.Key, apiResp)
account, errMsg := h.checkReverseV2(v.ChainType, v.AddressHex, req.BatchKeyInfo[i].KeyInfo.Key, listBtcAddr[i], apiResp)
if apiResp.ErrNo != http_api.ApiCodeSuccess {
return nil
}
Expand All @@ -126,9 +147,9 @@ func (h *HttpHandle) doBatchReverseRecordV2(req *ReqBatchReverseRecordV2, apiRes
return nil
}

func (h *HttpHandle) checkReverseV2(chainType common.ChainType, addressHex, reqKey string, apiResp *http_api.ApiResp) (account, errMsg string) {
func (h *HttpHandle) checkReverseV2(chainType common.ChainType, addressHex, reqKey, btcAddr string, apiResp *http_api.ApiResp) (account, errMsg string) {
// reverse
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex)
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex, btcAddr)
if err != nil {
log.Error("FindAccountInfoByAccountId err: ", err.Error(), reverse.Account)
apiResp.ApiRespErr(http_api.ApiCodeDbError, "find reverse record err")
Expand All @@ -138,6 +159,10 @@ func (h *HttpHandle) checkReverseV2(chainType common.ChainType, addressHex, reqK
return
}

if btcAddr != "" {
addressHex = reverse.Address
}

// check account
var owner, manager string
accountId := common.Bytes2Hex(common.GetAccountIdByAccount(reverse.Account))
Expand Down Expand Up @@ -165,7 +190,7 @@ func (h *HttpHandle) checkReverseV2(chainType common.ChainType, addressHex, reqK
owner = accountInfo.Owner
manager = accountInfo.Manager
}
log.Info("owner manager:", owner, manager)
log.Info("owner manager:", owner, manager, addressHex)

if strings.EqualFold(addressHex, owner) || strings.EqualFold(addressHex, manager) {
account = accountInfo.Account
Expand Down
2 changes: 1 addition & 1 deletion http_server/handle/reverse_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (h *HttpHandle) doReverseRecord(req *ReqReverseRecord, apiResp *code.ApiRes
return nil
}

reverse, err := h.DbDao.FindLatestReverseRecord(res.ChainType, res.AddressHex)
reverse, err := h.DbDao.FindLatestReverseRecord(res.ChainType, res.AddressHex, "")
if err != nil {
log.Error("FindLatestReverseRecord err:", err.Error(), res.ChainType, res.AddressHex)
apiResp.ApiRespErr(http_api.ApiCodeDbError, "find reverse record err")
Expand Down
36 changes: 30 additions & 6 deletions http_server/handle/reverse_record_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ func (h *HttpHandle) doReverseRecordV2(req *ReqReverseRecordV2, apiResp *http_ap
var resp RespReverseRecordV2
var chainType common.ChainType
var addressHex string
var btcAddr string

addrHex, err := req.FormatChainTypeAddress(h.DasCore.NetType(), false)
if err != nil {
log.Error("FormatChainTypeAddress err:", req.KeyInfo.Key)
apiResp.ApiRespErr(http_api.ApiCodeParamsInvalid, "params invalid")
return err
} else if addrHex.DasAlgorithmId == common.DasAlgorithmIdAnyLock {
apiResp.ApiRespOK(resp)
return nil
}

switch addrHex.DasAlgorithmId {
case common.DasAlgorithmIdAnyLock:
res, err := addrHex.FormatAnyLock()
if err != nil {
log.Error("addrHex.FormatAnyLock err: %s", err.Error())
Expand All @@ -86,21 +90,41 @@ func (h *HttpHandle) doReverseRecordV2(req *ReqReverseRecordV2, apiResp *http_ap
}
chainType = res.ChainType
addressHex = res.AddressHex
} else {
case common.DasAlgorithmIdEth, common.DasAlgorithmIdTron,
common.DasAlgorithmIdDogeChain, common.DasAlgorithmIdWebauthn:
chainType = addrHex.ChainType
addressHex = addrHex.AddressHex
case common.DasAlgorithmIdBitcoin:
log.Info("doReverseRecordV2:", addrHex.DasAlgorithmId, addrHex.DasSubAlgorithmId, addrHex.AddressHex)
switch addrHex.DasSubAlgorithmId {
case common.DasSubAlgorithmIdBitcoinP2PKH, common.DasSubAlgorithmIdBitcoinP2WPKH:
chainType = addrHex.ChainType
addressHex = addrHex.AddressHex
default:
chainType = addrHex.ChainType
addressHex = addrHex.AddressHex
btcAddr = req.KeyInfo.Key
}
default:
log.Error("default address invalid")
apiResp.ApiRespOK(resp)
return nil
}
log.Info("doReverseInfoV2:", chainType, addressHex, req.KeyInfo.Key)

log.Info("doReverseRecordV2:", chainType, addressHex, req.KeyInfo.Key, btcAddr)

// reverse
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex)
reverse, err := h.DbDao.FindLatestReverseRecord(chainType, addressHex, btcAddr)
if err != nil {
apiResp.ApiRespErr(http_api.ApiCodeDbError, "find reverse record err")
return fmt.Errorf("FindLatestReverseRecord err: %s", err.Error())
} else if reverse.Id == 0 {
apiResp.ApiRespOK(resp)
return nil
}
if btcAddr != "" {
addressHex = reverse.Address
}

// check account
var owner, manager string
Expand Down

0 comments on commit 09e8cf1

Please sign in to comment.