Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zhiyilian patch 1_ok #105

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 2024-shenzhen-FinTechathon/zhiyilian/Contracts/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
这是项目中使用的合约文件
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"fmt"
"log"
"math/big"

"github.com/FISCO-BCOS/go-sdk/client"
"github.com/FISCO-BCOS/go-sdk/conf"
"github.com/FISCO-BCOS/go-sdk/auth"
"github.com/ethereum/go-ethereum/common"
)

func main() {
// 解析配置文件
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
log.Fatal(err)
}
config := &configs[0]
contractAddress := common.HexToAddress("0x8eb4ade74b32802a2a23aD9e3fC887258c0DC53C")


// 假设买方账户创建交易
// 初始化买方client
buyerpath := ".ci/0xc89979c91f21f1b01df59440e954533d6f7978aa.pem"
buyerKey, _, err := conf.LoadECPrivateKeyFromPEM(buyerpath)
if err != nil {
log.Fatal(err)
}
config.PrivateKey = buyerKey
buyerClient, err := client.Dial(config)
if err != nil {
log.Fatal(err)
}
buyerinstance, err := auth.NewModelAuthorization(contractAddress, buyerClient)
if err != nil {
log.Fatal(err)
}
buyerSession := &auth.ModelAuthorizationSession{
Contract: buyerinstance,
CallOpts: *buyerClient.GetCallOpts(),
TransactOpts: *buyerClient.GetTransactOpts(),
}

// 买方查询模型
// 调用另一个合约,略

// 买方创建授权请求
// 以下为测试数据
modelId := "testmodel1"
seller := common.HexToAddress("0x83309d045a19c44dc3722d15a6abd472f95866ac") // 替换为卖家地址, 在查询模型时可以获取
buyer := common.HexToAddress("0xc89979c91f21f1b01df59440e954533d6f7978aa") // 替换为买家地址, 即当前用户
tx, receipt, err := buyerSession.RequestAuthorization(modelId, seller, buyer)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())
fmt.Printf("AuthRequest Created by buyer %s\n", buyerSession.CallOpts.From.Hex())

// 初始化卖方client
sellerpath := ".ci/0x83309d045a19c44dc3722d15a6abd472f95866ac.pem"
sellerKey, _, err := conf.LoadECPrivateKeyFromPEM(sellerpath)
if err != nil {
log.Fatal(err)
}
config.PrivateKey = sellerKey
sellerClient, err := client.Dial(config)
if err != nil {
log.Fatal(err)
}
sellerinstance, err := auth.NewModelAuthorization(contractAddress, sellerClient)
if err != nil {
log.Fatal(err)
}
sellerSession := &auth.ModelAuthorizationSession{
Contract: sellerinstance,
CallOpts: *sellerClient.GetCallOpts(),
TransactOpts: *sellerClient.GetTransactOpts(),
}

// 卖方查询交易
result, err := sellerSession.GetAuthorizationsByModel(modelId)
if err != nil {
log.Fatal(err)
}
sellers := result.Sellers
buyers := result.Buyers
timestamps := result.Timestamps
auths := result.Auths
usages := result.Usages
fmt.Printf("【Auths for model ID - query by seller】: %s\n", modelId)
for i := 0; i < len(sellers); i++ {
fmt.Printf("AuthRequest %d:\n", i+1)
fmt.Printf("Seller: %s\n", sellers[i].Hex())
fmt.Printf("Buyer: %s\n", buyers[i].Hex())
fmt.Printf("Timestamp: %s\n", timestamps[i].String())
fmt.Printf("Auths: %v\n", auths[i])
fmt.Printf("Usages: %v\n", usages[i])
fmt.Printf("---------------------------------------\n")
}


// 卖方投票
tx, receipt, err = sellerSession.VoteAuthorization(modelId, big.NewInt(0), true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())
fmt.Printf("Voted with seller %s\n", sellerSession.CallOpts.From.Hex())

// 查询交易
result, err = buyerSession.GetAuthorizationsByModel(modelId)
if err != nil {
log.Fatal(err)
}
sellers = result.Sellers
buyers = result.Buyers
timestamps = result.Timestamps
auths = result.Auths
usages = result.Usages
fmt.Printf("【Auths for model ID - query by seller】: %s\n", modelId)
for i := 0; i < len(sellers); i++ {
fmt.Printf("AuthRequest %d:\n", i+1)
fmt.Printf("Seller: %s\n", sellers[i].Hex())
fmt.Printf("Buyer: %s\n", buyers[i].Hex())
fmt.Printf("Timestamp: %s\n", timestamps[i].String())
fmt.Printf("Auths: %v\n", auths[i])
fmt.Printf("Usages: %v\n", usages[i])
fmt.Printf("---------------------------------------\n")
}

// 买方投票
tx, receipt, err = buyerSession.VoteAuthorization(modelId, big.NewInt(0), true)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())
fmt.Printf("Voted with buyer %s\n", sellerSession.CallOpts.From.Hex())

// 查询交易
result, err = buyerSession.GetAuthorizationsByModel(modelId)
if err != nil {
log.Fatal(err)
}
sellers = result.Sellers
buyers = result.Buyers
timestamps = result.Timestamps
auths = result.Auths
usages = result.Usages
fmt.Printf("【Auths for model ID - query by seller】: %s\n", modelId)
for i := 0; i < len(sellers); i++ {
fmt.Printf("AuthRequest %d:\n", i+1)
fmt.Printf("Seller: %s\n", sellers[i].Hex())
fmt.Printf("Buyer: %s\n", buyers[i].Hex())
fmt.Printf("Timestamp: %s\n", timestamps[i].String())
fmt.Printf("Auths: %v\n", auths[i])
fmt.Printf("Usages: %v\n", usages[i])
fmt.Printf("---------------------------------------\n")
}

certainAuth, err := buyerSession.GetAuthorization(modelId, big.NewInt(0))
fmt.Printf("Final test:\n")
fmt.Printf("Seller: %s\n", certainAuth.Seller.Hex())
fmt.Printf("Buyer: %s\n", certainAuth.Buyer.Hex())
fmt.Printf("Timestamp: %s\n", certainAuth.Timestamp.String())
fmt.Printf("Auths: %v\n", certainAuth.IsAuthorized)
fmt.Printf("Usages: %v\n", certainAuth.IsUsed)

}
113 changes: 113 additions & 0 deletions 2024-shenzhen-FinTechathon/zhiyilian/Contracts/auth_use/Auth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.4.25;

contract ModelAuthorization {
// 授权/使用存证结构
struct AuthorizationRecord {
string modelId; // 模型ID
address seller; // 模型授权者
address buyer; // 模型使用者
uint256 timestamp; // 授权/使用时间
bool isAuthorized; // 是否已授权
bool isUsed; // 是否已使用
}

// 存储所有授权/使用存证
mapping(string => AuthorizationRecord[]) private authorizations;

// 事件
event AuthorizationRequested(string modelId, address seller, address buyer, uint256 timestamp);
event AuthorizationVoted(string modelId, uint256 authorizationIndex, address voter, bool isAuthorization);
event AuthorizationCompleted(string modelId, uint256 authorizationIndex);

// 发起模型授权请求
function requestAuthorization(string _modelId, address _seller, address _buyer) public {
require(_seller != address(0), "Seller address is invalid.");
require(_buyer != address(0), "Buyer address is invalid.");

// 创建授权/使用存证
authorizations[_modelId].length++;
AuthorizationRecord storage record = authorizations[_modelId][authorizations[_modelId].length - 1];
record.modelId = _modelId;
record.seller = _seller;
record.buyer = _buyer;
record.timestamp = block.timestamp;
record.isAuthorized = false;
record.isUsed = false;

emit AuthorizationRequested(_modelId, _seller, _buyer, block.timestamp);
}

// 投票授权/使用
function voteAuthorization(string _modelId, uint256 _authorizationIndex, bool _isAuthorization) public {
require(_authorizationIndex < authorizations[_modelId].length, "Authorization does not exist.");
AuthorizationRecord storage record = authorizations[_modelId][_authorizationIndex];
require(!record.isAuthorized || !record.isUsed, "Authorization is already completed.");
require(msg.sender == record.seller || msg.sender == record.buyer, "Only seller or buyer can vote.");

// 卖方授权
if (msg.sender == record.seller && !record.isAuthorized) {
record.isAuthorized = _isAuthorization;
emit AuthorizationVoted(_modelId, _authorizationIndex, msg.sender, _isAuthorization);
}

// 买方表示已使用
if (msg.sender == record.buyer && record.isAuthorized && !record.isUsed) {
record.isUsed = _isAuthorization;
emit AuthorizationVoted(_modelId, _authorizationIndex, msg.sender, _isAuthorization);
}

// 检查是否双方都已完成
if (record.isAuthorized && record.isUsed) {
emit AuthorizationCompleted(_modelId, _authorizationIndex);
}
}

// 获取授权/使用存证详情
function getAuthorization(string _modelId, uint256 _authorizationIndex) public view returns (
string modelId,
address seller,
address buyer,
uint256 timestamp,
bool isAuthorized,
bool isUsed
) {
require(_authorizationIndex < authorizations[_modelId].length, "Authorization does not exist.");

AuthorizationRecord storage record = authorizations[_modelId][_authorizationIndex];

return (
record.modelId,
record.seller,
record.buyer,
record.timestamp,
record.isAuthorized,
record.isUsed
);
}

// 获取模型的所有授权/使用记录
function getAuthorizationsByModel(string _modelId) public view returns (
address[] sellers,
address[] buyers,
uint256[] timestamps,
bool[] auths,
bool[] usages
) {
uint256 count = authorizations[_modelId].length;
sellers = new address[](count);
buyers = new address[](count);
timestamps = new uint256[](count);
auths = new bool[](count);
usages = new bool[](count);

for (uint256 i = 0; i < count; i++) {
AuthorizationRecord storage record = authorizations[_modelId][i];
sellers[i] = record.seller;
buyers[i] = record.buyer;
timestamps[i] = record.timestamp;
auths[i] = record.isAuthorized;
usages[i] = record.isUsed;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"log"
"fmt"

"github.com/FISCO-BCOS/go-sdk/client"
"github.com/FISCO-BCOS/go-sdk/conf"
"github.com/FISCO-BCOS/go-sdk/model"
"github.com/ethereum/go-ethereum/common"
)

func main() {
// 解析配置文件
configs, err := conf.ParseConfigFile("config.toml")
if err != nil {
log.Fatal(err)
}
config := &configs[0]
client, err := client.Dial(config)
if err != nil {
log.Fatal(err)
}

// 获取合约实例
contractAddress := common.HexToAddress("0xC27e774a6D5a0C48E2f611CABd0DA4D812aA62f7")
instance, err := model.NewModelRegistry(contractAddress, client)
if err != nil {
log.Fatal(err)
}

modelRegistrySession := &model.ModelRegistrySession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}

// 将三个账户设置为评估机构
evaluators := []common.Address{
common.HexToAddress("0xdcc91e225ccf2bffec2d82f9828b53f426556ef2"),
common.HexToAddress("0x1fe1129f1cef820beb94d40101360df0da6a3879"),
common.HexToAddress("0xaf816e9bc6f805e10405c0eecc260179d631d3c7"),
common.HexToAddress("0xbec5fea6a971423e3e27cda3417307b765bf7aab"),
}

for _, evaluator := range evaluators {
tx, receipt, err := modelRegistrySession.AddEvaluator(evaluator)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())
}
}
Loading