{"id":40967440,"url":"https://github.com/mover-code/golang-web3","last_synced_at":"2026-01-22T06:25:48.485Z","repository":{"id":57650725,"uuid":"448167647","full_name":"mover-code/golang-web3","owner":"mover-code","description":"use go for blockChain","archived":false,"fork":false,"pushed_at":"2023-05-29T09:21:46.000Z","size":217,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-06-20T05:29:11.645Z","etag":null,"topics":["blockchain","contract","go","golang","web3"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mover-code.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-15T03:05:44.000Z","updated_at":"2024-05-27T10:37:22.000Z","dependencies_parsed_at":"2024-06-20T04:44:39.404Z","dependency_job_id":"06862d2e-4b93-4bf1-8ab6-8d877aac372e","html_url":"https://github.com/mover-code/golang-web3","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/mover-code/golang-web3","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mover-code%2Fgolang-web3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mover-code%2Fgolang-web3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mover-code%2Fgolang-web3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mover-code%2Fgolang-web3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mover-code","download_url":"https://codeload.github.com/mover-code/golang-web3/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mover-code%2Fgolang-web3/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28656816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T01:17:37.254Z","status":"online","status_checked_at":"2026-01-22T02:00:07.137Z","response_time":144,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","contract","go","golang","web3"],"created_at":"2026-01-22T06:25:47.553Z","updated_at":"2026-01-22T06:25:48.475Z","avatar_url":"https://github.com/mover-code.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# golang-web3\n\n## install\n\n```shell\ngo get -u github.com/mover-code/golang-web3.git@latest\n```\n\n\u003e go get -u github.com/btcsuite/btcd@v0.22.0-beta 遇到ambiguous import\n\n## desc\n\n使用go操作区块链账户，发起交易，查询账户资产、合约交互、部署，事件监听等功能\n\n## use\n\n- 创建一个web3实例\n\n```go\n// It creates a new client for the given url.\n//\n// Args:\n//   url (string): The address of the RPC service.\nfunc NewCli(url string) *jsonrpc.Client {\n    cli, err := jsonrpc.NewClient(url)\n    if err != nil {\n        panic(fmt.Sprintf(\"error:%s\", url))\n    }\n    return cli\n}\n```\n\n- 数据签名示例\n\n```go\n    privateKey, _ := crypto.HexToECDSA(key)  // 账户私钥\n    u256, _ := abi.NewType(\"uint256\")\n    account, _ := abi.NewType(\"address\")\n    type Ord struct {\n        Id      *big.Int\n        Account web3.Address\n        Amount  *big.Int\n        Fee     *big.Int\n        Solt    *big.Int\n        End     *big.Int\n        Type    *big.Int\n        State   *big.Int\n    }\n\n     argumentInfo := []*abi.TupleElem{\n        \u0026abi.TupleElem{\n            Name: \"id\",\n            Elem: u256,\n        },\n        \u0026abi.TupleElem{\n            Name: \"account\",\n            Elem: account,\n        },\n        \u0026abi.TupleElem{\n            Name: \"amount\",\n            Elem: u256,\n        },\n        \u0026abi.TupleElem{\n            Name: \"solt\",\n            Elem: u256,\n        },\n        \u0026abi.TupleElem{\n            Name: \"end\",\n            Elem: u256,\n        },\n        \u0026abi.TupleElem{\n            Name: \"type\",\n            Elem: u256,\n        },\n    }\n\n    param := abi.NewTupleType(argumentInfo)\n    \n    // 数据hash\n    hash, _ := param.Encode(\u0026Ord{ \n        Id:      id,\n        Account: addr,\n        Amount:  withDrawAmount,\n        Fee:     big.NewInt(0),\n        Solt:    solt,\n        End:     endTime,\n        Type:    wType,\n        State:   big.NewInt(0),\n    })\n\n    hashs := crypto.Keccak256Hash(signString(\"\"), crypto.Keccak256(hash)) // 按照合约中的方式组装数据\n    signature, _ := crypto.Sign(hashs.Bytes(), privateKey) // 使用私钥签名获取签名后的数据\n    r,s,v := SignRSV(signature) // 签名后的rsv数据\n```\n\n- 验证签名信息示例\n\n```go\nfunc VerifySig(msg, from, sigHex string) bool {\n\n    sig := hexutil.MustDecode(sigHex)\n    if sig[64] != 27 \u0026\u0026 sig[64] != 28 {\n        return false\n    }\n    sig[64] -= 27\n\n    addr, _ := wallet.Ecrecover(web3.SignHash([]byte(msg)), sig)\n    return addr == web3.HexToAddress(from)\n}\n```\n\n- 代币转账示例 主币\n\n```go\n\nimport(\n    web3 \"github.com/mover-code/golang-web3\"\n    \"github.com/mover-code/golang-web3/jsonrpc\"\n    \"github.com/mover-code/golang-web3/wallet\"\n\n)\n     // rpc地址\n    cli, err := jsonrpc.NewClient(url)\n    if err != nil {\n        panic(fmt.Sprintf(\"error:%s\", url))\n    }\n\n    block, _ := cli.Eth().BlockNumber() // 获取当前区块\n    balance, _ := cli.Eth().GetBalance(web3.HexToAddress(addr), web3.BlockNumber(block)) // 查询账户余额\n\n    nonce, err := cli.Eth().GetNonce(sender, web3.BlockNumber(block)) // 发起交易账户的序号\n    gas, _ := cli.Eth().GasPrice() // 当前gas\n    gasLimit, err := cli.Eth().EstimateGas(\u0026web3.CallMsg{\n        From:     sender,    // 发起账户\n        To:       \u0026receiver, // 接收账户\n        GasPrice: gas,\n        Value:    big.NewInt(amount), // 转账金额\n    })\n    // 组装交易信息\n    t := \u0026web3.Transaction{\n        From:        sender,\n        To:          \u0026receiver,\n        Value:       big.NewInt(100000000000000000),\n        Nonce:       nonce,\n        BlockNumber: uint64(block),\n        GasPrice:    gas,\n        Gas:         gasLimit,\n    }\n\n    chainId, _ := cli.Eth().ChainID()\n    signer := wallet.NewEIP155Signer(chainId.Uint64()) \n    byteKey, _ := hex.DecodeString(private_hash) // 发起交易账户 私钥\n    key, _ := wallet.NewWalletFromPrivKey(byteKey)\n    data, err := signer.SignTx(t, key) // 签名数据\n    cli.Eth().SenSignTransaction(data) // 发起交易\n\n```\n\n- [关于事件监听](./event/event_test.go)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmover-code%2Fgolang-web3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmover-code%2Fgolang-web3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmover-code%2Fgolang-web3/lists"}