Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
goalongway committed Sep 29, 2024
1 parent db7a89a commit 8c865d5
Show file tree
Hide file tree
Showing 84 changed files with 7,403 additions and 3,963 deletions.
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# b2-message-channel
# b2-message-sharing

Message-channel is a blockchain cross-chain messaging mechanism.
Message-sharing is a blockchain cross-chain messaging mechanism.

## 1. Contracts

### 1.1 Contracts interface

#### 1.1.1 Message Bridge Interface
#### 1.1.1 Message Sharing Interface

```
interface IB2MessageBridge {
interface IB2MessageSharing {
/**
* Get the validator role for a specific chain
Expand All @@ -24,24 +24,24 @@ interface IB2MessageBridge {
* @param from_id The ID of the cross-chain message, used to uniquely identify the message.
* @param from_sender The address of the sender on the originating chain.
* @param to_chain_id The ID of the target chain, where the message will be sent.
* @param contract_address The address of the target contract that will receive the cross-chain message.
* @param data The input data for the target contract's cross-chain call.
* @param to_business_contract The address of the target contract that will receive the cross-chain message.
* @param to_message The input data for the target contract's cross-chain call.
* @return bytes32 The generated message hash, used for subsequent verification and processing.
*/
function SendHash(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address contract_address, bytes calldata data) external view returns (bytes32);
function SendHash(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address to_business_contract, bytes calldata to_message) external view returns (bytes32);
/**
* Verify the legitimacy of a message
* @param from_chain_id The ID of the originating chain, used to validate the source of the message.
* @param from_id The ID of the cross-chain message, used to check if the message has already been processed.
* @param from_sender The address of the sender on the originating chain, used to verify the sender's legitimacy.
* @param to_chain_id The ID of the target chain, indicating where the message will be sent.
* @param contract_address The address of the target contract that will receive the cross-chain message.
* @param data The input data for the target contract's cross-chain call.
* @param to_business_contract The address of the target contract that will receive the cross-chain message.
* @param to_message The input data for the target contract's cross-chain call.
* @param signature The signature of the message, used to verify its legitimacy and integrity.
* @return bool Returns true if the verification succeeds, and false if it fails.
*/
function verify(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address contract_address, bytes calldata data, bytes calldata signature) external view returns (bool);
function verify(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address to_business_contract, bytes calldata to_message, bytes calldata signature) external view returns (bool);
/**
* Set the weight for message processing
Expand All @@ -53,22 +53,22 @@ interface IB2MessageBridge {
/**
* Request cross-chain message data
* @param to_chain_id The ID of the target chain, specifying where the message will be sent.
* @param contract_address The address of the target contract that will receive the cross-chain message.
* @param data The input data for the target contract's cross-chain call.
* @param to_business_contract The address of the target contract that will receive the cross-chain message.
* @param to_message The input data for the target contract's cross-chain call.
* @return from_id The ID of the cross-chain message, returning a unique identifier to track the request.
*/
function call(uint256 to_chain_id, address contract_address, bytes calldata data) external returns (uint256 from_id);
function call(uint256 to_chain_id, address to_business_contract, bytes calldata to_message) external returns (uint256 from_id);
/**
* Confirm cross-chain message data
* @param from_chain_id The ID of the originating chain, used to validate the source of the message.
* @param from_id The ID of the cross-chain message, used to check if the message has already been processed.
* @param from_sender The address of the sender on the originating chain (msg.sender), used to determine the sender's security based on business needs.
* @param contract_address The address of the target contract, indicating where the message will be sent (can be a contract on the target chain or the current chain).
* @param data The input data for the target contract's cross-chain call.
* @param to_business_contract The address of the target contract, indicating where the message will be sent (can be a contract on the target chain or the current chain).
* @param to_message The input data for the target contract's cross-chain call.
* @param signatures An array of signatures used to verify the legitimacy of the message, ensuring only authorized senders can send the message.
*/
function send(uint256 from_chain_id, uint256 from_id, address from_sender, address contract_address, bytes calldata data, bytes[] calldata signatures) external;
function send(uint256 from_chain_id, uint256 from_id, address from_sender, address to_business_contract, bytes calldata to_message, bytes[] calldata signatures) external;
/**
* Set the validator role for a specific chain
Expand All @@ -81,8 +81,8 @@ interface IB2MessageBridge {
// Event declarations
event SetWeight(uint256 chain_id, uint256 weight); // Event emitted when weight is set
event SetValidatorRole(uint256 chain_id, address account, bool valid); // Event emitted when validator role is set
event Send(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address contract_address, bytes data); // Event emitted when a message is sent
event Call(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address contract_address, bytes data); // Event emitted when a message call is made
event Send(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address to_business_contract, bytes to_message); // Event emitted when a message is sent
event Call(uint256 from_chain_id, uint256 from_id, address from_sender, uint256 to_chain_id, address to_business_contract, bytes to_message); // Event emitted when a message call is made
}
```

Expand All @@ -95,23 +95,22 @@ interface IBusinessContract {
* @param from_chain_id The ID of the originating chain, used to validate the source of the message.
* @param from_id The ID of the cross-chain message, used to check if the message has already been processed to prevent duplication.
* @param from_sender The address of the sender on the originating chain, used to verify the sender's legitimacy (business needs may dictate whether verification is necessary).
* @param data The input data for processing the cross-chain message, which may need to be decoded based on byte encoding rules.
* @param message The input data for processing the cross-chain message, which may need to be decoded based on byte encoding rules.
* @return success Indicates whether the message processing was successful, returning true for success and false for failure.
*/
function send(uint256 from_chain_id, uint256 from_id, address from_sender, bytes calldata data) external returns (bool success);
function send(uint256 from_chain_id, uint256 from_id, address from_sender, bytes calldata message) external returns (bool success);
}
```

### 1.2 Contracts code

#### 1.2.1 Message Bridge
#### 1.2.1 Message Sharing

[MessageBridge.sol](./contracts/contracts/message/MessageBridge.sol)
[MessageSharing.sol](./contracts/contracts/message/MessageSharing.sol)

#### 1.2.2 Business Contract Example

[BusinessContractExample.sol](./contracts/contracts/business/BusinessContractExample.sol)
[BusinessContractExample.sol](contracts/contracts/business/example/BusinessContractExample.sol)

### 1.3 Deploy

Expand Down Expand Up @@ -183,14 +182,14 @@ yarn hardhat run scripts/business/revoke_role.js --network b2dev
#### 1.4.1 Bsquared testnet

```
B2MessageBridge: 0xe55c8D6D7Ed466f66D136f29434bDB6714d8E3a5
B2MessageSharing: 0xDf5b12f094cf9b12eb2523cC43a62Dd6787D7AB8
BusinessContract: 0x804641e29f5F63a037022f0eE90A493541cCb869
```

#### 1.4.2 Arbitrum sepolia

```
B2MessageBridge: 0x2A82058E46151E337Baba56620133FC39BD5B71F
B2MessageSharing: 0x2A82058E46151E337Baba56620133FC39BD5B71F
BusinessContract: 0x8Ac2C830532d7203a12C4C32C0BE4d3d15917534
```

Expand Down
8 changes: 4 additions & 4 deletions applications/cmd/builder/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/initiates"
"bsquared.network/b2-message-channel-applications/internal/serves/builder"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/initiates"
"bsquared.network/b2-message-sharing-applications/internal/serves/builder"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"encoding/json"
"flag"
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions applications/cmd/listener/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/initiates"
"bsquared.network/b2-message-channel-applications/internal/serves/listener/bitcoin"
"bsquared.network/b2-message-channel-applications/internal/serves/listener/ethereum"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/initiates"
"bsquared.network/b2-message-sharing-applications/internal/serves/listener/bitcoin"
"bsquared.network/b2-message-sharing-applications/internal/serves/listener/ethereum"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"encoding/json"
"flag"
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions applications/cmd/proposer/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/initiates"
"bsquared.network/b2-message-channel-applications/internal/serves/proposer"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-channel-applications/internal/vo"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/initiates"
"bsquared.network/b2-message-sharing-applications/internal/serves/proposer"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/vo"
"encoding/json"
"flag"
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions applications/cmd/validator/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/initiates"
"bsquared.network/b2-message-channel-applications/internal/serves/validator"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-channel-applications/internal/vo"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/initiates"
"bsquared.network/b2-message-sharing-applications/internal/serves/validator"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/vo"
"encoding/json"
"flag"
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion applications/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module bsquared.network/b2-message-channel-applications
module bsquared.network/b2-message-sharing-applications

go 1.22

Expand Down
2 changes: 1 addition & 1 deletion applications/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion applications/internal/initiates/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package initiates

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/config"
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
Expand Down
2 changes: 1 addition & 1 deletion applications/internal/models/deposit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models

import (
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion applications/internal/models/messages.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models

import "bsquared.network/b2-message-channel-applications/internal/enums"
import "bsquared.network/b2-message-sharing-applications/internal/enums"

type Message struct {
Base
Expand Down
2 changes: 1 addition & 1 deletion applications/internal/models/signatures.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models

import (
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"github.com/shopspring/decimal"
)

Expand Down
2 changes: 1 addition & 1 deletion applications/internal/models/sync_tasks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models

import "bsquared.network/b2-message-channel-applications/internal/enums"
import "bsquared.network/b2-message-sharing-applications/internal/enums"

type SyncTask struct {
Base
Expand Down
10 changes: 5 additions & 5 deletions applications/internal/serves/builder/builder.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package builder

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-channel-applications/internal/models"
msg "bsquared.network/b2-message-channel-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/models"
msg "bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bytes"
"context"
"crypto/ecdsa"
Expand Down
14 changes: 7 additions & 7 deletions applications/internal/serves/listener/bitcoin/bitcoin.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package bitcoin

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-channel-applications/internal/models"
"bsquared.network/b2-message-channel-applications/internal/types"
"bsquared.network/b2-message-channel-applications/internal/utils/aa"
"bsquared.network/b2-message-channel-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/models"
"bsquared.network/b2-message-sharing-applications/internal/types"
"bsquared.network/b2-message-sharing-applications/internal/utils/aa"
"bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bytes"
"context"
"encoding/hex"
Expand Down
12 changes: 6 additions & 6 deletions applications/internal/serves/listener/ethereum/ethereum.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ethereum

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-channel-applications/internal/models"
"bsquared.network/b2-message-channel-applications/internal/utils/ethereum/event"
"bsquared.network/b2-message-channel-applications/internal/utils/ethereum/event/message"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/models"
"bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/event"
"bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/event/message"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"context"
"fmt"
"github.com/ethereum/go-ethereum"
Expand Down
14 changes: 7 additions & 7 deletions applications/internal/serves/proposer/proposer.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package proposer

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-channel-applications/internal/models"
"bsquared.network/b2-message-channel-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-channel-applications/internal/utils/tx"
"bsquared.network/b2-message-channel-applications/internal/vo"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/models"
"bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/utils/tx"
"bsquared.network/b2-message-sharing-applications/internal/vo"
"bufio"
"context"
"crypto/ecdsa"
Expand Down
15 changes: 8 additions & 7 deletions applications/internal/serves/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package validator

import (
"bsquared.network/b2-message-channel-applications/internal/config"
"bsquared.network/b2-message-channel-applications/internal/enums"
"bsquared.network/b2-message-channel-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-channel-applications/internal/utils/log"
"bsquared.network/b2-message-channel-applications/internal/utils/tx"
"bsquared.network/b2-message-channel-applications/internal/vo"
"bsquared.network/b2-message-sharing-applications/internal/config"
"bsquared.network/b2-message-sharing-applications/internal/enums"
"bsquared.network/b2-message-sharing-applications/internal/utils/ethereum/message"
"bsquared.network/b2-message-sharing-applications/internal/utils/log"
"bsquared.network/b2-message-sharing-applications/internal/utils/tx"
"bsquared.network/b2-message-sharing-applications/internal/vo"
"bufio"
"context"
"crypto/ecdsa"
Expand Down Expand Up @@ -196,7 +196,8 @@ func (v *Validator) handleMessage(msg vo.Message) error {
} else {
return errors.New("rpc invalid")
}

fmt.Printf("data :%s\n", msg.Data)
fmt.Printf("validator :%s\n", crypto_.PubkeyToAddress(v.pk.PublicKey))
signature, err := message.SignMessageSend(msg.ChainId, msg.ToMessageContract, msg.FromChainId, common.HexToHash(msg.FromId).Big(), msg.FromSender, msg.ToChainId, msg.ToContractAddress, msg.Data, v.pk)
if err != nil {
v.logger.Errorf("validator sign err: %s", err)
Expand Down
Loading

0 comments on commit 8c865d5

Please sign in to comment.