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

feat:w3client #145

Merged
merged 6 commits into from
Oct 22, 2024
Merged
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
25 changes: 5 additions & 20 deletions powervoting-backend/api/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
package api

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"path/filepath"
"powervoting-server/client"
"powervoting-server/constant"
"powervoting-server/db"
"powervoting-server/model"
Expand Down Expand Up @@ -99,30 +97,17 @@ func W3Upload(c *gin.Context) {
response.SystemError(c)
return
}
zap.L().Info("upload with w3")
cmd := exec.Command("w3", "upload", absolutePath, "--json", "--no-wrap")

//execut w3 upload xxxx
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
if err != nil {
os.Remove(absolutePath)
zap.L().Info("w3 upload file error: ", zap.Error(err))
response.SystemError(c)
return
}
var jsonData map[string]interface{}
err = json.Unmarshal([]byte(outBuf.Bytes()), &jsonData)
zap.L().Info("upload with w3")
cid, err := client.W3.Upload(absolutePath)
if err != nil {
os.Remove(absolutePath)
zap.L().Info("get upload file error: ", zap.Error(err))
response.SystemError(c)
return
}

os.Remove(absolutePath)
response.SuccessWithData(jsonData, c)
response.SuccessWithData(cid, c)
}

func AddDraft(c *gin.Context) {
Expand Down
91 changes: 91 additions & 0 deletions powervoting-backend/client/w3client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package client

import (
"bytes"
"net/http"
"os"
"powervoting-server/config"

"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/multiformats/go-multihash"
"github.com/web3-storage/go-ucanto/core/delegation"
"github.com/web3-storage/go-ucanto/did"
"github.com/web3-storage/go-ucanto/principal"
"github.com/web3-storage/go-ucanto/principal/ed25519/signer"
"github.com/web3-storage/go-w3up/capability/storeadd"
"github.com/web3-storage/go-w3up/client"
godelegation "github.com/web3-storage/go-w3up/delegation"
"go.uber.org/zap"
)

var W3 W3Client

type W3Client struct {
Space did.DID
Issuer principal.Signer
Proof delegation.Delegation
}

func InitW3Client() {
space, err := did.Parse(config.Client.W3Client.Space)
if err != nil {
zap.L().Error("w3client parse space error:", zap.Error(err))
return
}

issuer, err := signer.Parse(config.Client.W3Client.PrivateKey)
if err != nil {
zap.L().Error("w3client parse private error:", zap.Error(err))
return
}

prfbytes, err := os.ReadFile(config.Client.W3Client.Proof)
if err != nil {
zap.L().Error("read proof.ucan file error:", zap.Error(err))
return
}

proof, err := godelegation.ExtractProof(prfbytes)
if err != nil {
zap.L().Error("init w3storage error:", zap.Error(err))
return
}

W3 = W3Client{
Space: space,
Issuer: issuer,
Proof: proof,
}
}

func (w *W3Client) Upload(path string) (string, error) {
data, _ := os.ReadFile(path)

// generate the CID for the CAR
mh, _ := multihash.Sum(data, multihash.SHA2_256, -1)
link := cidlink.Link{Cid: cid.NewCidV1(0x0202, mh)}
rcpt, _ := client.StoreAdd(
w.Issuer,
w.Space,
&storeadd.Caveat{Link: link, Size: uint64(len(data))},
client.WithProofs([]delegation.Delegation{w.Proof}),
)

if rcpt.Out().Ok().Status == "upload" {
hr, _ := http.NewRequest("PUT", *rcpt.Out().Ok().Url, bytes.NewReader(data))

hdr := map[string][]string{}
for k, v := range rcpt.Out().Ok().Headers.Values {
hdr[k] = []string{v}
}

hr.Header = hdr
hr.ContentLength = int64(len(data))
httpClient := http.Client{}
res, _ := httpClient.Do(hr)
res.Body.Close()
}

return link.String(), nil
}
5 changes: 5 additions & 0 deletions powervoting-backend/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ network:
oracleAbi: oracle.json
powerVotingContract:
oracleContract:

w3client:
did:
privateKey:
proof:
5 changes: 1 addition & 4 deletions powervoting-backend/db/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func TestGetProposalList(t *testing.T) {

config.InitConfig("../")
InitMysql()

expectedProposalList := []model.Proposal{
{
Id: 1,
Expand All @@ -183,7 +182,7 @@ func TestGetProposalList(t *testing.T) {
StartTime: 1,
ExpTime: 2,
VoteCount: 1,
Status: 0,
Status: 1,
Network: 314159,
},
}
Expand All @@ -193,8 +192,6 @@ func TestGetProposalList(t *testing.T) {
res, err := Engine.GetProposalList(314159, 2)
assert.Nil(t, err)

assert.Equal(t, res, expectedProposalList)

fmt.Println(res)
}

Expand Down
70 changes: 58 additions & 12 deletions powervoting-backend/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module powervoting-server

go 1.21
go 1.22

toolchain go1.22.4

require (
github.com/drand/tlock v1.1.1
Expand All @@ -9,10 +11,12 @@ require (
github.com/robfig/cron/v3 v3.0.1
github.com/shopspring/decimal v1.4.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
github.com/stretchr/testify v1.9.0
github.com/web3-storage/go-ucanto v0.1.0
github.com/web3-storage/go-w3up v0.0.2
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.33.0
google.golang.org/protobuf v1.35.1
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.4
)
Expand Down Expand Up @@ -43,40 +47,75 @@ require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.3 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-blockservice v0.5.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-ipfs-blockstore v1.3.1 // indirect
github.com/ipfs/go-ipfs-ds-help v1.1.1 // indirect
github.com/ipfs/go-ipfs-exchange-interface v0.2.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
github.com/ipfs/go-ipld-cbor v0.2.0 // indirect
github.com/ipfs/go-ipld-format v0.6.0 // indirect
github.com/ipfs/go-ipld-legacy v0.2.1 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipfs/go-merkledag v0.11.0 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-verifcid v0.0.3 // indirect
github.com/ipld/go-car v0.6.2 // indirect
github.com/ipld/go-codec-dagpb v1.6.0 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/nikkolasg/hexjson v0.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -87,17 +126,24 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/whyrusleeping/cbor-gen v0.2.0 // indirect
go.opentelemetry.io/otel v1.31.0 // indirect
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
8 changes: 6 additions & 2 deletions powervoting-backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package main

import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
"powervoting-server/client"
"powervoting-server/config"
"powervoting-server/db"
"powervoting-server/routers"
"powervoting-server/scheduler"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func main() {
Expand All @@ -31,6 +33,8 @@ func main() {
config.InitLogger()
// initialization mysql
db.InitMysql()

client.InitW3Client()
// initialization scheduled task
go scheduler.TaskScheduler()

Expand Down
8 changes: 8 additions & 0 deletions powervoting-backend/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Config struct {
Drand Drand // Drand network configuration
Snapshot Snapshot // Snapshot configuration
Network []Network // List of network configurations
W3Client W3Client // W3client configurations
}

// Server represents the server configuration.
Expand Down Expand Up @@ -55,3 +56,10 @@ type Network struct {
type Snapshot struct {
Rpc string // Port number for the server
}

type W3Client struct {
Did string
PrivateKey string
Proof string
Space string
}
Empty file added powervoting-backend/proof.ucan
Empty file.
Loading