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: add pactus bridge #16

Merged
merged 4 commits into from
Apr 4, 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
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.5

require (
github.com/ethereum/go-ethereum v1.13.14
github.com/pactus-project/pactus v1.0.2
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.32.0
Expand All @@ -19,28 +20,33 @@ require (
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/glebarez/sqlite v1.11.0
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kilic/bls12-381 v0.1.0 // indirect
github.com/matoous/go-nanoid v1.5.0
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
Expand All @@ -49,6 +55,7 @@ require (
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.15.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/gorm v1.25.7
Expand Down
102 changes: 78 additions & 24 deletions go.sum

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions sides/pactus/bridge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pactus

import (
"fmt"

"github.com/PACZone/wrapto/types/bypass"
"github.com/PACZone/wrapto/types/message"
)

type Bridge struct {
wallet *Wallet
bypassName bypass.Name
bypass chan message.Message
}

func NewBridge(w *Wallet, b chan message.Message, bn bypass.Name) Bridge {
return Bridge{
wallet: w,
bypass: b,
bypassName: bn,
}
}

func (b Bridge) Start() {
for msg := range b.bypass {
err := b.ProcessMsg(msg)
if err != nil {
// TODO: Log
continue
}
}
}

func (b Bridge) ProcessMsg(msg message.Message) error {
err := msg.BasicCheck(b.bypassName)
if err != nil {
return err
}

payload := msg.Payload

amt := int64(payload.Amount())
memo := fmt.Sprintf("bridge from %s to %s by Wraptor.app", msg.From, msg.To)

_, err = b.wallet.TransferTransaction(payload.Receiver, memo, amt) // TODO: update order
if err != nil {
return err
}

return nil
}
16 changes: 16 additions & 0 deletions sides/pactus/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package pactus

import "fmt"

type InvalidMemeError struct{}

func (e InvalidMemeError) Error() string {
return "invalid memo"
}

type WalletNotExistError struct {
path string
}

func (e WalletNotExistError) Error() string {
return fmt.Sprintf("wallet not exist at: %s", e.path)
}

type SaveWalletError struct{}

func (e SaveWalletError) Error() string {
return "failed to save error"
}
4 changes: 2 additions & 2 deletions sides/pactus/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (l *Listener) ProcessBlocks() error {
return err // TODO: handle errors from client
}

validTxs := l.FilterValidTxs(blk.Txs)
validTxs := filterValidTxs(blk.Txs)

for _, tx := range validTxs {
dest, err := parseMemo(tx.Memo)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (l *Listener) isEligibleBlock(h uint32) (bool, error) {
return h < lst, nil
}

func (l *Listener) FilterValidTxs(txs []*pactus.TransactionInfo) []*pactus.TransactionInfo {
func filterValidTxs(txs []*pactus.TransactionInfo) []*pactus.TransactionInfo {
validTxs := make([]*pactus.TransactionInfo, 0)

for _, tx := range txs {
Expand Down
6 changes: 5 additions & 1 deletion sides/pactus/pactus.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@
type Side struct {
client *Client
listener *Listener
bridge Bridge
highway chan message.Message

ctx context.Context
}

func NewSide(ctx context.Context, highway chan message.Message, startBlock uint32) (*Side, error) {
func NewSide(ctx context.Context, highway chan message.Message, startBlock uint32, w *Wallet, b chan message.Message) (*Side, error) {

Check failure on line 19 in sides/pactus/pactus.go

View workflow job for this annotation

GitHub Actions / lint

line is 134 characters (lll)
client, err := NewClient(ctx, "") // TODO:read rpc url from config
if err != nil {
return nil, err
}

listener := NewListener(ctx, client, bypass.PACTUS, highway, startBlock)

bridge := NewBridge(w, b, bypass.PACTUS)

return &Side{
client: client,
listener: listener,
highway: highway,
bridge: bridge,

ctx: ctx,
}, nil
Expand Down
90 changes: 90 additions & 0 deletions sides/pactus/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pactus

import (
"os"

"github.com/pactus-project/pactus/types/tx/payload"
pWallet "github.com/pactus-project/pactus/wallet"
)

type Wallet struct {
address string
password string
wallet pWallet.Wallet
}

func Open(path, addr, rpcURL, pass string) (*Wallet, error) {
if !doesWalletExist(path) {
return nil, WalletNotExistError{
path: path,
}
}

wt, err := pWallet.Open(path, true)
if err != nil {
return nil, err
}

err = wt.Connect(rpcURL)
if err != nil {
return nil, err
}

return &Wallet{
wallet: *wt,
address: addr,
password: pass,
}, nil
}

func (w *Wallet) TransferTransaction(toAddress, memo string, amount int64) (string, error) {
fee, err := w.wallet.CalculateFee(amount, payload.TypeTransfer)
if err != nil {
return "", err
}

opts := []pWallet.TxOption{
pWallet.OptionFee(fee),
pWallet.OptionMemo(memo),
}

tx, err := w.wallet.MakeTransferTx(w.address, toAddress, amount, opts...)
if err != nil {
return "", err
}

// sign transaction
err = w.wallet.SignTransaction(w.password, tx)
if err != nil {
return "", err
}

// broadcast transaction
res, err := w.wallet.BroadcastTransaction(tx)
if err != nil {
return "", err
}

err = w.wallet.Save()
if err != nil {
return "", SaveWalletError{}
}

return res, nil
}

func (w *Wallet) Address() string {
return w.address
}

func (w *Wallet) Balance() int64 {
balance, _ := w.wallet.Balance(w.address)

return balance
}

func doesWalletExist(fileName string) bool {
_, err := os.Stat(fileName)

return !os.IsNotExist(err)
}
11 changes: 11 additions & 0 deletions types/message/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package message

import "fmt"

type BasicCheckError struct {
Reason string
}

func (e BasicCheckError) Error() string {
return fmt.Sprintf("invalid message: %s", e.Reason)
}
12 changes: 12 additions & 0 deletions types/message/message.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package message

import (
"fmt"

"github.com/PACZone/wrapto/types/bypass"
"github.com/PACZone/wrapto/types/order"
)
Expand All @@ -18,3 +20,13 @@ func NewMessage(to, from bypass.Name, payload *order.Order) Message {
Payload: payload,
}
}

func (m Message) BasicCheck(to bypass.Name) error {
if m.To != to {
return BasicCheckError{
Reason: fmt.Sprintf("invalid to value: %s", to),
}
}

return nil
}
Loading