FISCO-BCOS / go-sdk

golang SDK of FISCO BCOS
Apache License 2.0
73 stars 58 forks source link

交易发起方好像没有变化 #219

Open yekai1003 opened 1 year ago

yekai1003 commented 1 year ago

使用fisco-bcos版本:3.0.2 在go.mod中设置了replace github.com/FISCO-BCOS/go-sdk => ../go-sdk 调用hello合约,合约代码如下:

pragma solidity ^0.8.10;

contract hello{
    string name;
    event onset(string newname);
    constructor() public{
       name = "Hello, World!";
    }

    function get() view public returns(string memory){
        return name;
    }

    function set(string memory n) public{
        emit onset(n);
        name = n;
    }
}

下面是通过go调用hello的代码:

package main

import (
    "context"
    "encoding/hex"
    "log"
    "strings"

    "github.com/FISCO-BCOS/go-sdk/abi/bind"
    "github.com/FISCO-BCOS/go-sdk/client"
    "github.com/ethereum/go-ethereum/common"
)

var bcosClient *client.Client
var chainID string

/*
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"newname","type":"string"}],"name":"onset","type":"event"},{"inputs":[],"name":"get","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"n","type":"string"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}]
abigen-0.8.11 -abi hello.abi -type hello -pkg main -out hello.go
*/

var keystr = `{"address":"6ada94d442e7a07d0a2e117f3d179516d836af44","crypto":{"cipher":"aes-128-ctr","ciphertext":"6757baea66cdf5930bf6b6036856aeba2865fb901d6b9eb0bd80708b6321ddfb","cipherparams":{"iv":"ec187901b3c382349fbe4b107d20cf24"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":4096,"p":6,"r":8,"salt":"d843d31a08f7b85dfdc46ca4283ae5ae6dc4c0a4632ee4d088c6cf6b42d211f9"},"mac":"23510f127694f3da17155a52845f54680b93efa92c1383f65957d10d1de4c78a"},"id":"39b43455-831e-4f92-ad9d-4527f819478d","version":3}`

func init() {

    //key:145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58
    privateKey, _ := hex.DecodeString("145e247e170ba3afd6ae97e88f00dbc976c2345d511b0f6713355d19d8b80b58")
    config := &client.Config{
        IsSMCrypto:  false,
        GroupID:     "group0",
        PrivateKey:  privateKey,
        Host:        "192.168.137.148",
        Port:        20200,
        TLSCaFile:   "sdk/ca.crt",
        TLSKeyFile:  "sdk/sdk.key",
        TLSCertFile: "sdk/sdk.crt"}
    cli, err := client.DialContext(context.Background(), config)
    if err != nil {
        log.Fatal(err)
    }

    bcosClient = cli
    chainID, _ = cli.GetChainID(context.Background())

}

func main() {
    contract_addr := "0xa6cdc4fed96bbf8c9c013cc19200b3c8ba95c93e"

    instance, err := NewHello(common.HexToAddress(contract_addr), bcosClient)
    if err != nil {
        log.Fatal("failed to NewHello ", err)
    }

    keyio := strings.NewReader(keystr)
    auth, err := bind.NewTransactor(keyio, "223")
    if err != nil {
        log.Panic("failed to NewTransactor:", err)
    }
    _, rtx, err := instance.Set(auth, "hello,yekai!")
    log.Printf("%+v\n", rtx)
}

打印的交易回执信息:

2023/06/16 15:12:52 {
        "blockNumber": 25,
        "contractAddress": "",
        "from": "0xfbb18d54e9ee57529cda8c7c52242efe879f064f",
        "gasUsed": "15580",
        "hash": "0x0ab22ddce6a702be0d40b4c002c4dd48705a894d6ce218d99107d06981713a2c",
        "input": "",
        "logEntries": [
                {
                        "blockNumber": "",
                        "address": "a6cdc4fed96bbf8c9c013cc19200b3c8ba95c93e",
                        "data": "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c68656c6c6f2c79656b6169210000000000000000000000000000000000000000",
                        "topics": [
                                "0xafb180742c1292ea5d67c4f6d51283ecb11e49f8389f4539bef82135d689e118"
                        ]
                }
        ],
        "message": "",
        "output": "0x",
        "status": 0,
        "to": "0xa6cdc4fed96bbf8c9c013cc19200b3c8ba95c93e",
        "transactionHash": "0x9fe099a71cc76edda55f44d9d45e68c4ef5059ccd94396df08d740e7adaf6399",
        "txReceiptProof": null,
        "version": 0
}

发现即使设置了set函数的opts,仍然使用client配置的私钥账户,也就是说预期from是0x6ada94d442e7a07d0a2e117f3d179516d836af44,结果每次都是0xfbb18d54e9ee57529cda8c7c52242efe879f064f

bxq2011hust commented 1 year ago

3.0版本逻辑不同,创建client的时候,会把私钥传给c-sdk,目前在client的生命周期内不支持更改私钥