Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

- uses: dfinity/setup-dfx@main
with:
dfx-version: 0.25.1
dfx-version: 0.17.0

- name: Pull ganache-cli image
run: docker pull ${{ env.ganache-image }}
Expand Down
26 changes: 26 additions & 0 deletions payment-channel-icp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# payment-channel on internet computer

This demo connects to our [Canister](https://github.com/perun-network/perun-icp-canister) that runs on a by using our [ICP Backend](https://github.com/perun-network/perun-icp-backend) to enables a Perun Payment Channel.

## Example Walkthrough


1. Install [dfx](https://internetcomputer.org/docs/current/references/cli-reference/dfx-parent), the DFINITY command-line execution environment:
```sh
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
```

2. Run the deployment script to start local devnet and deploy Perun Canister.
```sh
cd payment-channel-icp
./startdeploy.sh
```
3. Start the example.
```sh
go run .
```

4. Stop the the local blockchain with.
```sh
./stopdfx.sh
```
5 changes: 3 additions & 2 deletions payment-channel-icp/client/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"perun.network/go-perun/channel"
"perun.network/go-perun/client"
icwallet "perun.network/perun-icp-backend/wallet"
)

// PaymentChannel is a wrapper for a Perun channel for the payment use case.
Expand Down Expand Up @@ -107,8 +108,8 @@ func FormatState(c *PaymentChannel, state *channel.State) string {
balA, _ := bigFloatA.Float64()
balAStr := strconv.FormatFloat(balA, 'f', 4, 64)

fstPartyPaymentAddr := parties[0].String()
sndPartyPaymentAddr := parties[1].String()
fstPartyPaymentAddr := parties[0][icwallet.ICPBackendID].String()
sndPartyPaymentAddr := parties[1][icwallet.ICPBackendID].String()

bigIntB := state.Allocation.Balance(1, c.currency)
bigFloatB := new(big.Float).SetInt(bigIntB)
Expand Down
18 changes: 11 additions & 7 deletions payment-channel-icp/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

pchannel "perun.network/go-perun/channel"
"perun.network/go-perun/client"
"perun.network/go-perun/wallet"
pwallet "perun.network/go-perun/wallet"
"perun.network/go-perun/wire"
"perun.network/perun-icp-backend/channel"
chanconn "perun.network/perun-icp-backend/channel/connector"
Expand All @@ -40,7 +42,7 @@ type PaymentClient struct {
observerMutex sync.Mutex
balanceMutex sync.Mutex
Name string
wAddr wire.Address
wAddr map[pwallet.BackendID]wire.Address
balance *big.Int
}

Expand All @@ -55,17 +57,17 @@ func (c *PaymentClient) startWatching(ch *client.Channel) {
}

// OpenChannel opens a new channel with the specified peer and funding.
func (c *PaymentClient) OpenChannel(peer wire.Address, amount float64) { //*PaymentChannel
func (c *PaymentClient) OpenChannel(peer map[pwallet.BackendID]wire.Address, amount float64) { //*PaymentChannel
// We define the channel participants. The proposer has always index 0. Here
// we use the on-chain addresses as off-chain addresses, but we could also
// use different ones.

participants := []wire.Address{c.WireAddress(), peer}
participants := []map[pwallet.BackendID]wire.Address{c.WireAddress(), peer}

// We create an initial allocation which defines the starting balances.
initBal := big.NewInt(int64(amount))

initAlloc := pchannel.NewAllocation(2, channel.Asset)
initAlloc := pchannel.NewAllocation(2, []pwallet.BackendID{icwallet.ICPBackendID}, channel.Asset)
initAlloc.SetAssetBalances(channel.Asset, []pchannel.Bal{
initBal, // Our initial balance.
initBal, // Peer's initial balance.
Expand All @@ -75,7 +77,9 @@ func (c *PaymentClient) OpenChannel(peer wire.Address, amount float64) { //*Paym
challengeDuration := uint64(10) // On-chain challenge duration in seconds.
proposal, err := client.NewLedgerChannelProposal(
challengeDuration,
c.account.Address(),
map[wallet.BackendID]wallet.Address{
icwallet.ICPBackendID: c.account.Address(),
},
initAlloc,
participants,
)
Expand All @@ -94,7 +98,7 @@ func (c *PaymentClient) OpenChannel(peer wire.Address, amount float64) { //*Paym
c.Channel = newPaymentChannel(ch, c.currency)
}

func (p *PaymentClient) WireAddress() wire.Address {
func (p *PaymentClient) WireAddress() map[pwallet.BackendID]wire.Address {
return p.wAddr
}

Expand All @@ -117,7 +121,7 @@ func (c *PaymentClient) GetChannelBalance() (*big.Int, error) {

chanParams := c.Channel.GetChannelParams()
cid := chanParams.ID()
addr := chanParams.Parts[c.Channel.ch.Idx()]
addr := chanParams.Parts[c.Channel.ch.Idx()][icwallet.ICPBackendID]
addrBytes, err := addr.MarshalBinary()
if err != nil {
panic(err)
Expand Down
6 changes: 5 additions & 1 deletion payment-channel-icp/client/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"perun.network/go-perun/channel"
"perun.network/go-perun/client"
"perun.network/go-perun/wallet"
icwallet "perun.network/perun-icp-backend/wallet"
)

// HandleProposal is the callback for incoming channel proposals.
Expand Down Expand Up @@ -57,7 +59,9 @@ func (c *PaymentClient) HandleProposal(p client.ChannelProposal, r *client.Propo

// Create a channel accept message and send it.
accept := lcp.Accept(
c.account.Address(), // The account we use in the channel.
map[wallet.BackendID]wallet.Address{
icwallet.ICPBackendID: c.account.Address(), // The account we use in the channel.
},
client.WithRandomNonce(), // Our share of the channel nonce.
)
ch, err := r.Accept(context.TODO(), accept)
Expand Down
15 changes: 11 additions & 4 deletions payment-channel-icp/client/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ import (

"github.com/pkg/errors"
"perun.network/go-perun/client"
pwallet "perun.network/go-perun/wallet"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"
"perun.network/go-perun/wire/net/simple"
"perun.network/perun-icp-backend/channel"
chanconn "perun.network/perun-icp-backend/channel/connector"
"perun.network/perun-icp-backend/wallet"
icwallet "perun.network/perun-icp-backend/wallet"
)

func SetupPaymentClient(
name string,
w *wallet.FsWallet, // w is the wallet used to resolve addresses to accounts for channels.
bus *wire.LocalBus,
wireAcc wire.Account,
bus wire.Bus,
perunID string,
ledgerID string,
host string,
Expand All @@ -55,8 +57,13 @@ func SetupPaymentClient(
}

// Setup Perun client.
wireAddr := simple.NewAddress(acc.Address().String())
perunClient, err := client.New(wireAddr, bus, funder, adj, w, watcher)
wallet := map[pwallet.BackendID]pwallet.Wallet{
icwallet.ICPBackendID: w,
}
wireAddr := map[pwallet.BackendID]wire.Address{
icwallet.ICPBackendID: wireAcc.Address(),
}
perunClient, err := client.New(wireAddr, bus, funder, adj, wallet, watcher)
if err != nil {
return nil, errors.WithMessage(err, "creating client")
}
Expand Down
114 changes: 105 additions & 9 deletions payment-channel-icp/go.mod
Original file line number Diff line number Diff line change
@@ -1,33 +1,129 @@
module perun.network/perun-examples/payment-channel-icp

go 1.23
go 1.23.0

toolchain go1.23.4

require (
github.com/aviate-labs/agent-go v0.3.0-alpha.1
github.com/perun-network/perun-libp2p-wire v1.0.1
github.com/pkg/errors v0.9.1
perun.network/go-perun v0.10.6
perun.network/perun-icp-backend v0.1.0
perun.network/go-perun v0.13.1-0.20250528124331-21b590b655d3
perun.network/perun-icp-backend v0.1.1-0.20250818082428-67580b538abb
)

require (
github.com/aviate-labs/leb128 v0.3.0 // indirect
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
github.com/di-wu/parser v0.3.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.3 // indirect
github.com/flynn/noise v1.1.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/herumi/bls-go-binary v1.35.1 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/go-cid v0.5.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/klauspost/compress v1.18.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/koron/go-ssdp v0.0.5 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.2.0 // indirect
github.com/libp2p/go-libp2p v0.41.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-netroute v0.2.2 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v5 v5.0.0 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.63 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // 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-multiaddr v0.15.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.4.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.6.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a // indirect
github.com/onsi/ginkgo/v2 v2.22.2 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pion/datachannel v1.5.10 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/dtls/v3 v3.0.4 // indirect
github.com/pion/ice/v4 v4.0.6 // indirect
github.com/pion/interceptor v0.1.37 // indirect
github.com/pion/logging v0.2.3 // indirect
github.com/pion/mdns/v2 v2.0.7 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.15 // indirect
github.com/pion/rtp v1.8.11 // indirect
github.com/pion/sctp v1.8.36 // indirect
github.com/pion/sdp/v3 v3.0.10 // indirect
github.com/pion/srtp/v3 v3.0.4 // indirect
github.com/pion/stun v0.6.1 // indirect
github.com/pion/stun/v3 v3.0.0 // indirect
github.com/pion/transport/v2 v2.2.10 // indirect
github.com/pion/transport/v3 v3.0.7 // indirect
github.com/pion/turn/v4 v4.0.0 // indirect
github.com/pion/webrtc/v4 v4.0.10 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.21.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.50.0 // indirect
github.com/quic-go/webtransport-go v0.8.1-0.20241018022711-4ac2c9250e66 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
github.com/wlynxg/anet v0.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.26.0 // indirect
go.uber.org/dig v1.18.0 // indirect
go.uber.org/fx v1.23.0 // indirect
go.uber.org/mock v0.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/tools v0.30.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.4.0 // indirect
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 // indirect
)
Loading
Loading