Skip to content

Commit

Permalink
add icp example
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrik Amler <hendrik@perun.network>
  • Loading branch information
tinnendo committed Oct 23, 2024
1 parent 91e65f9 commit 48e7e6a
Show file tree
Hide file tree
Showing 17 changed files with 1,074 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- uses: dfinity/setup-dfx@main

- name: Pull ganache-cli image
run: docker pull ${{ env.ganache-image }}

- name: Payment Channel ICP
working-directory: payment-channel-icp
run: |
chmod +x ./startdeploy.sh
chmod +x ./startdfx.sh
chmod +x ./stopdfx.sh
./startdeploy.sh
sleep 30
go run ./
- name: Simple Client
working-directory: simple-client
env:
Expand Down
32 changes: 32 additions & 0 deletions payment-channel-icp/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module perun.network/perun-examples/payment-channel-icp

go 1.22

require (
github.com/aviate-labs/agent-go v0.3.0-alpha.1
perun.network/go-perun v0.10.6
perun.network/perun-icp-backend v0.0.0-20230818121554-97eb2fa7924e
)

require (
github.com/aviate-labs/leb128 v0.3.0 // indirect
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/di-wu/parser v0.3.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/herumi/bls-go-binary v1.35.1 // indirect
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/stretchr/testify v1.8.2 // 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.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
perun.network/perun-demo-tui v0.0.0-20230321094013-3e474bfabc8f // indirect
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 // indirect
)
71 changes: 71 additions & 0 deletions payment-channel-icp/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
github.com/aviate-labs/agent-go v0.3.0-alpha.1 h1:DzJNW5WMpM/eJ6ksMiuZjlfEttXSYIGeiq6IzsWlA50=
github.com/aviate-labs/agent-go v0.3.0-alpha.1/go.mod h1:fy9UekeyeJEq17Cj01Snq23B4HqVyar03wu3uk1eL9U=
github.com/aviate-labs/leb128 v0.3.0 h1:s9htRv3OYk8nuHqJu9PiVFJxv1jIUTIcpEeiURa91uQ=
github.com/aviate-labs/leb128 v0.3.0/go.mod h1:GclhBOjhIKmcDlgHKhj0AEZollzERfZUbcRUKiQVqgY=
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a h1:aQkG/D+l8Y7tr809l8pN+KebH2jzacWReSFQmeEKFgM=
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a/go.mod h1:C/lr3F9TimrVkdZckG5mz+VU0TrmpeyVKUjzv2YyGwA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/di-wu/parser v0.3.0 h1:NMOvy5ifswgt4gsdhySVcKOQtvjC43cHZIfViWctqQY=
github.com/di-wu/parser v0.3.0/go.mod h1:SLp58pW6WamdmznrVRrw2NTyn4wAvT9rrEFynKX7nYo=
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I=
github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/herumi/bls-eth-go-binary v1.36.1 h1:SfLjxbO1fWkKtKS7J3Ezd1/5QXrcaTZgWynxdSe10hQ=
github.com/herumi/bls-eth-go-binary v1.36.1/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/herumi/bls-go-binary v1.28.2 h1:F0AezsC0M1a9aZjk7g0l2hMb1F56Xtpfku97pDndNZE=
github.com/herumi/bls-go-binary v1.28.2/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/herumi/bls-go-binary v1.35.1 h1:bCt7bZADjrpFY2sUw/Nx3FE3KuJBssLE4Y12dp4RSRo=
github.com/herumi/bls-go-binary v1.35.1/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce h1:/pEpMk55wH0X+E5zedGEMOdLuWmV8P4+4W3+LZaM6kg=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
perun.network/go-perun v0.10.6 h1:uj1e33yfCSfE75DK/uwjNp+TwvGG85Qhi6HuYQ9EPrQ=
perun.network/go-perun v0.10.6/go.mod h1:BGBZC3npkX457u87pjDd0NEIXr1a4dsH4H/YpLdGGe8=
perun.network/perun-demo-tui v0.0.0-20230321094013-3e474bfabc8f h1:o38fexcR2G0HH+soMVmpV42oCBidgv9YPgeFwe+7jvI=
perun.network/perun-demo-tui v0.0.0-20230321094013-3e474bfabc8f/go.mod h1:GtxsCSD83BFAME680zlg4ifoi2WVfqj2+rntxbdJCz8=
perun.network/perun-icp-backend v0.0.0-20230818121554-97eb2fa7924e h1:VIltGjFbL/xOrxYKh/E1mbx6+q+zVfx+KSU4vEKJDGk=
perun.network/perun-icp-backend v0.0.0-20230818121554-97eb2fa7924e/go.mod h1:zZr7kz3T24VECiSHqWkejFLaUwPC5D1pUlEHrA6I01k=
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 h1:iA5GzEa/hHfVlQpimEjPV09NATwHXxSjWNB0VVodtew=
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37/go.mod h1:XUBrNtqgEhN3EEOP/5gh7IBd3xVHKidCjXDZfl9+kMU=
87 changes: 87 additions & 0 deletions payment-channel-icp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"fmt"
"log"

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

const (
Host = "http://127.0.0.1"
Port = 4943
perunPrincipal = "be2us-64aaa-aaaaa-qaabq-cai"
ledgerPrincipal = "bkyz2-fmaaa-aaaaa-qaaaq-cai"
userAId = "97520b79b03e38d3f6b38ce5026a813ccc9d1a3e830edb6df5970e6ca6ad84be"
userBId = "40fd2dc85bc7d264b31f1fa24081d7733d303b49b7df84e3d372338f460aa678"
userAPemPath = "./userdata/identities/usera_identity.pem"
userBPemPath = "./userdata/identities/userb_identity.pem"
channelCollateral = 50000
)

func main() {

log.Println("Setting up wallets for Alice and Bob")
perunWalletAlice := wallet.NewWallet()
perunWalletBob := wallet.NewWallet()

log.Println("Create communication channel between Alice and Bob")
bus := wire.NewLocalBus()

log.Println("Setting up Payment Clients")
alice, err := client.SetupPaymentClient("Alice", perunWalletAlice, bus, perunPrincipal, ledgerPrincipal, Host, Port, userAPemPath)
if err != nil {
panic(err)
}

bob, err := client.SetupPaymentClient("Bob", perunWalletBob, bus, perunPrincipal, ledgerPrincipal, Host, Port, userBPemPath)
if err != nil {
panic(err)
}

log.Println("Alice opens Channel with Bob")
alice.OpenChannel(bob.WireAddress(), channelCollateral)
achan := alice.Channel
log.Println("Bob accepts Channel from Alice")
bob.AcceptedChannel()
bchan := bob.Channel

log.Println("Initial Balances in the Channel")
printBalances(achan.GetChannelState().Balances)

// sending payment/s

log.Println("Sending payments...")
achan.SendPayment(1000)

log.Println("Balance after first Payment:")
printBalances(achan.GetChannelState().Balances)

bchan.SendPayment(2000)

log.Println("Balance after second Payment:")
printBalances(achan.GetChannelState().Balances)

log.Println("Settle channel")
achan.Settle()

log.Println("Final Balance after settling:")
printBalances(achan.GetChannelState().Balances)

log.Println("Shutdown Channel")
alice.Shutdown()
bob.Shutdown()
log.Println("Done")
}

func printBalances(balances channel.Balances) {
log.Println("Balances:")
for i, assetBalances := range balances {
fmt.Printf("Asset %d:\n", i+1)
fmt.Printf(" Alice: %s\n", assetBalances[0].String()) // Access balance for Alice
fmt.Printf(" Bob: %s\n", assetBalances[1].String()) // Access balance for Bob
}
}
77 changes: 77 additions & 0 deletions payment-channel-icp/startdeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Get the directory of the current script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Define the function to create the ledger argument
createLedgerArg() {
local ICP_PERUN_MINT_ACC="433bd8e9dd65bdfb34259667578e749136f3e0ea1566e10af1e0dd324cbd9144"
local ICP_PERUN_USERA_ACC="97520b79b03e38d3f6b38ce5026a813ccc9d1a3e830edb6df5970e6ca6ad84be"
local ICP_PERUN_USERB_ACC="40fd2dc85bc7d264b31f1fa24081d7733d303b49b7df84e3d372338f460aa678"

echo "(record {minting_account = \"$ICP_PERUN_MINT_ACC\"; initial_values = vec { record { \"$ICP_PERUN_USERA_ACC\"; record { e8s=10_000_000} }; record { \"$ICP_PERUN_USERB_ACC\"; record { e8s=10_000_000 } }}; send_whitelist = vec {}})"
}

ICP_PERUN_PRINCIPAL="be2us-64aaa-aaaaa-qaabq-cai"
ICP_LEDGER_PRINCIPAL="bkyz2-fmaaa-aaaaa-qaaaq-cai"

# Exporting them so that they can be accessed by other commands or scripts invoked after this script
export ICP_PERUN_PRINCIPAL
export ICP_LEDGER_PRINCIPAL

# Define the function to deploy Perun
deployPerun() {
local execPath=$1

path=$(which dfx)
if [ -z "$path" ]; then
echo "Error: dfx not found in PATH"
return 1
fi

echo "Deploying Perun"
cd $execPath
deployMsg=$($path deploy icp_perun 2>&1)
status=$?

if [ $status -ne 0 ]; then
echo "Error deploying icp_perun:\n$deployMsg\n"
return $status
else
echo "$deployMsg"
return 0
fi
}

# Define the function to deploy the ledger
deployLedger() {
local execPath=$1
local ledgerArg=$2

path=$(which dfx)
if [ -z "$path" ]; then
echo "Error: dfx not found in PATH"
return 1
fi

echo "Deploying the Ledger with the following parameters: $ledgerArg"
cd $execPath
outputLedger=$($path deploy ledger --argument "$ledgerArg" 2>&1)
status=$?

if [ $status -ne 0 ]; then
echo "Error deploying ledger:\n$outputLedger\n"
return $status
else
echo "$outputLedger"
return 0
fi
}

# Call the functions
ledgerArg=$(createLedgerArg)
./startdfx.sh
execPath="$DIR/userdata"

deployLedger $execPath "$ledgerArg"
deployPerun $execPath
27 changes: 27 additions & 0 deletions payment-channel-icp/startdfx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

startDFX() {
path=$(which dfx)
if [ -z "$path" ]; then
echo "Error: dfx not found in PATH"
return 1
fi
execPath="./userdata"
cd $execPath
$path start --clean &
status=$?

# Sleep to allow process to start
sleep 10

if [ $status -ne 0 ]; then
echo "Error starting DFX..."
return $status
else
echo "Starting DFX..."
return $!
fi
}

# Call the function
startDFX
16 changes: 16 additions & 0 deletions payment-channel-icp/stopdfx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

# Send a SIGTERM signal to all processes that have 'dfx' in their command line
pkill -f dfx
dfx_status=$?

# Send a SIGTERM signal to all processes that have 'icx' in their command line
pkill -f icx
icx_status=$?

if [ $dfx_status -ne 0 ] || [ $icx_status -ne 0 ]; then
echo "Error stopping DFX or ICX..."
exit 1
else
echo "DFX and ICX processes stopped."
fi
26 changes: 26 additions & 0 deletions payment-channel-icp/userdata/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"canisters": {
"ledger": {
"wasm": "ledger.wasm",
"candid": "ledger.private.did",
"type": "custom"
},
"icp_perun": {
"candid": "icp_perun.did",
"wasm": "icp_perun-opt.wasm",
"type": "custom"
}
},
"networks": {
"local": {
"bind": "127.0.0.1:4943",
"type": "ephemeral"
}
},
"defaults": {
"replica": {
"subnet_type": "system"
}
},
"version": 1
}
Binary file added payment-channel-icp/userdata/icp_perun-opt.wasm
Binary file not shown.
Loading

0 comments on commit 48e7e6a

Please sign in to comment.