-
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.
- Loading branch information
goalongway
committed
Aug 1, 2024
1 parent
01f99c3
commit f3f2f84
Showing
11 changed files
with
228 additions
and
24 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
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,82 @@ | ||
package listener | ||
|
||
import ( | ||
"bsquared.network/b2-message-channel-serv/internal/enums" | ||
"bsquared.network/b2-message-channel-serv/internal/models" | ||
"bsquared.network/b2-message-channel-serv/internal/utils/message" | ||
"encoding/json" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func (l *Listener) validate() { | ||
duration := time.Millisecond * time.Duration(l.Blockchain.BlockInterval) | ||
for { | ||
list, err := l.pendingCallMessage(10) | ||
if err != nil { | ||
log.Errorf("Get pending call message err: %s\n", err) | ||
time.Sleep(duration) | ||
continue | ||
} | ||
if len(list) == 0 { | ||
log.Infof("Get pending call message length is 0\n") | ||
time.Sleep(duration) | ||
continue | ||
} | ||
|
||
var wg sync.WaitGroup | ||
for _, message := range list { | ||
wg.Add(1) | ||
go func(_wg *sync.WaitGroup, message models.Message) { | ||
defer _wg.Done() | ||
err = l.validateMessage(message) | ||
if err != nil { | ||
log.Errorf("Handle err: %v, %v\n", err, message) | ||
} | ||
}(&wg, message) | ||
} | ||
wg.Wait() | ||
} | ||
} | ||
|
||
func (l *Listener) validateMessage(msg models.Message) error { | ||
var signatures []string | ||
for _, validatorKey := range l.DataMap.ValidatorMap { | ||
_key, err := crypto.ToECDSA(common.FromHex(validatorKey)) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
signature, err := message.SignMessageSend(l.Blockchain.ChainId, l.Blockchain.MessageAddress, msg.FromChainId, msg.FromId, msg.FromSender, msg.ToChainId, msg.ToContractAddress, msg.ToBytes, _key) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
signatures = append(signatures, signature) | ||
} | ||
|
||
_signatures, err := json.Marshal(&signatures) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
err = l.Db.Where("`id`=? AND `status`=?", msg.Id, enums.MessageStatusValidating).Updates(map[string]interface{}{ | ||
"signatures": string(_signatures), | ||
"status": enums.MessageStatusPending, | ||
}).Error | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
return nil | ||
} | ||
|
||
func (l *Listener) validatingCallMessage(limit int) ([]models.Message, error) { | ||
var list []models.Message | ||
err := l.Db.Where("`to_chain_id`=? AND `type`=? AND `status`=?", l.Blockchain.ChainId, enums.MessageTypeCall, enums.MessageStatusValidating).Limit(limit).Find(&list).Error | ||
if err != nil { | ||
return nil, err | ||
} | ||
return list, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package message | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/signer/core/apitypes" | ||
"github.com/pkg/errors" | ||
"github.com/storyicon/sigverify" | ||
) | ||
|
||
const MessageSendTypedData = `{ | ||
"types":{ | ||
"EIP712Domain":[ | ||
{ | ||
"name":"name", | ||
"type":"string" | ||
}, | ||
{ | ||
"name":"version", | ||
"type":"string" | ||
}, | ||
{ | ||
"name":"chainId", | ||
"type":"uint256" | ||
}, | ||
{ | ||
"name":"verifyingContract", | ||
"type":"address" | ||
} | ||
], | ||
"Send":[ | ||
{ | ||
"name":"from_chain_id", | ||
"type":"uint256" | ||
}, | ||
{ | ||
"name":"from_id", | ||
"type":"uint256" | ||
}, | ||
{ | ||
"name":"from_sender", | ||
"type":"address" | ||
}, | ||
{ | ||
"name":"to_chain_id", | ||
"type":"uint256" | ||
}, | ||
{ | ||
"name":"contract_address", | ||
"type":"address" | ||
}, | ||
{ | ||
"name":"data", | ||
"type":"bytes" | ||
} | ||
] | ||
}, | ||
"domain":{ | ||
"name":"B2MessageBridge", | ||
"version":"1", | ||
"chainId":"%d", | ||
"verifyingContract":"%s" | ||
}, | ||
"primaryType":"Send", | ||
"message":{ | ||
"from_chain_id":"%s", | ||
"from_id":"%s", | ||
"from_sender":"%s", | ||
"to_chain_id":"%s", | ||
"contract_address":"%s", | ||
"data":%s | ||
} | ||
}` | ||
|
||
func SignMessageSend(chainId int64, messageContract string, fromChainId int64, fromId int64, fromSender string, toChainId int64, contractAddress string, data string, key *ecdsa.PrivateKey) (string, error) { | ||
_data := fmt.Sprintf(MessageSendTypedData, chainId, messageContract, fromChainId, fromId, fromSender, toChainId, contractAddress, data) | ||
var typedData apitypes.TypedData | ||
if err := json.Unmarshal([]byte(_data), &typedData); err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
_, originHash, err := sigverify.HashTypedData(typedData) | ||
fmt.Println("originHash", common.Bytes2Hex(originHash)) | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
sig, err := crypto.Sign(originHash, key) | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
return common.Bytes2Hex(sig), nil | ||
} |