diff --git a/.gitignore b/.gitignore index cbf4fd1..5ff5ff3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .cartesi -examples/go.mod -examples/go.sum -examples/example.go +example-apps/go.mod +example-apps/go.sum +example-apps/app.go +example-apps/app diff --git a/README.md b/README.md index a93377e..74c8b7c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Cartesi Rollups GO high level framework ``` -This works for Cartesi Rollups Node version 1.5.x (cartesi cli version 0.16.x) +This works for Cartesi Rollups Node version 2.0.x (cartesi cli version ?.?.x) ``` -Create cartesi rolllups DApp with codes like: +Create cartesi rolllups applications with simple lines of code like: ```go package main @@ -31,20 +31,33 @@ func main() { } ``` -Check the [examples](examples) for more use cases. +Check the [examples](example-apps/apps-src) for more use cases. -You will need [cartesi cli](https://github.com/cartesi/cli) to create and run the example, and [curl](https://curl.se/) to interact with the dapp. +You will need [cartesi cli](https://github.com/cartesi/cli) to create and run the example, and [curl](https://curl.se/) to interact with the app. To run an example ```shell -cd examples -rm -rf example.go -ln -sr example1_rollups_helpers.go example.go +cd example-apps +rm -rf app.go +ln -sr ln -sr apps-src/example11_wallet.go app.go +``` + +The you can build and run with + +```shell cartesi build cartesi run ``` +Optionally, you can compile directly in the host and run with [nonodo](https://github.com/Calindra/nonodo/) and the [cartesi machine](https://github.com/cartesi/machine-emulator) (you'll also need `riscv64-linux-gnu-gcc`). First start nonodo: + + +```shell +GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 CC=riscv64-linux-gnu-gcc go build -o app app.go +nonodo -- cartesi-machine --env=ROLLUP_HTTP_SERVER_URL=http://10.0.2.2:5004 --network --flash-drive=label:root,filename:.cartesi/image.ext2 --volume=.:/mnt --workdir=/mnt -- ./app +``` + You can send inputs with (account and private key of anvil test accounts) ```shell diff --git a/examples/Dockerfile b/example-apps/Dockerfile similarity index 87% rename from examples/Dockerfile rename to example-apps/Dockerfile index 6505d8d..a18e416 100644 --- a/examples/Dockerfile +++ b/example-apps/Dockerfile @@ -23,11 +23,11 @@ ENV CGO_ENABLED=1 ENV CC=riscv64-linux-gnu-gcc ENV PATH=/usr/local/go/bin:${PATH} -COPY example.go . +COPY app.go . -RUN go mod init dapp && \ +RUN go mod init app && \ go mod tidy && \ - go build -o dapp example.go + go build -o app app.go # runtime stage: produces final image that will be executed FROM --platform=linux/riscv64 riscv64/ubuntu:22.04 @@ -47,15 +47,15 @@ apt-get update apt-get install -y --no-install-recommends \ busybox-static=1:1.30.1-7ubuntu3 rm -rf /var/lib/apt/lists/* /var/log/* /var/cache/* -useradd --create-home --user-group dapp +useradd --create-home --user-group app EOF ENV PATH="/opt/cartesi/bin:${PATH}" -WORKDIR /opt/cartesi/dapp -COPY --from=build-stage /opt/build/dapp . +WORKDIR /opt/cartesi/app +COPY app . ENV ROLLUP_HTTP_SERVER_URL="http://127.0.0.1:5004" ENTRYPOINT ["rollup-init"] -CMD ["/opt/cartesi/dapp/dapp"] +CMD ["/opt/cartesi/app/app"] diff --git a/examples/example10_json_and_uri_handler.go b/example-apps/apps-src/example10_json_and_uri_handler.go similarity index 100% rename from examples/example10_json_and_uri_handler.go rename to example-apps/apps-src/example10_json_and_uri_handler.go diff --git a/examples/example11_wallet.go b/example-apps/apps-src/example11_wallet.go similarity index 89% rename from examples/example11_wallet.go rename to example-apps/apps-src/example11_wallet.go index f512902..6055880 100644 --- a/examples/example11_wallet.go +++ b/example-apps/apps-src/example11_wallet.go @@ -1,15 +1,15 @@ package main import ( - "fmt" - "log" - "os" - "math/big" - "encoding/json" - - "github.com/prototyp3-dev/go-rollups/rollups" - "github.com/prototyp3-dev/go-rollups/handler/abi" - "github.com/prototyp3-dev/go-rollups/wallet" + "encoding/json" + "fmt" + "log" + "math/big" + "os" + + "github.com/prototyp3-dev/go-rollups/handler/abi" + "github.com/prototyp3-dev/go-rollups/rollups" + "github.com/prototyp3-dev/go-rollups/wallet" ) var infolog = log.New(os.Stderr, "[ info ] ", log.Lshortfile) @@ -145,7 +145,7 @@ func main() { myApp.dappWallet = wallet.NewWalletApp(); myApp.dappWallet.SetAbiHandler(appHandler) - // setups the dapp relay and fixed portal deposit routes + // setups fixed portal deposit routes // overrides any fixed address handler // and extra routes to control assets myApp.dappWallet.SetupRoutes([]wallet.WalletRoute{ @@ -154,9 +154,9 @@ func main() { wallet.TransferEtherAdvanceRoute, wallet.BalanceInspectRoute,wallet.BalanceUriInspectRoute}) - appHandler.HandleAdvanceRoute(abihandler.NewHeaderCodec("dapp","fee",[]string{}), myApp.PayFee) - appHandler.HandleFixedAddressAdvance(abihandler.Address2Hex(developerAddress),abihandler.NewHeaderCodec("dapp","changeFee",[]string{"uint256 fee"}), myApp.ChangeFee) - appHandler.HandleInspectRoute(abihandler.NewHeaderCodec("dapp","fee",[]string{"address address"}), myApp.GetFee) + appHandler.HandleAdvanceRoute(abihandler.NewHeaderCodec("dapp.fee",[]string{}), myApp.PayFee) + appHandler.HandleFixedAddressAdvance(abihandler.Address2Hex(developerAddress),abihandler.NewHeaderCodec("dapp.changeFee",[]string{"uint256 fee"}), myApp.ChangeFee) + appHandler.HandleInspectRoute(abihandler.NewHeaderCodec("dapp.fee",[]string{"address address"}), myApp.GetFee) myApp.dappWallet.UriHandler().HandleInspectRoute("/fee/:address", myApp.GetFeeUri) appHandler.HandleDefault(myApp.HandleWrongWay) diff --git a/examples/example1_rollups_helpers.go b/example-apps/apps-src/example1_rollups_helpers.go similarity index 94% rename from examples/example1_rollups_helpers.go rename to example-apps/apps-src/example1_rollups_helpers.go index 74a8b28..604304e 100644 --- a/examples/example1_rollups_helpers.go +++ b/example-apps/apps-src/example1_rollups_helpers.go @@ -2,7 +2,7 @@ package main import ( "encoding/json" - "io/ioutil" + "io" "strconv" "fmt" "log" @@ -64,6 +64,10 @@ func Handler(response *rollups.FinishResponse) error { } func main() { + if rollups.GetRollupServer() == "" { + rollups.SetRollupServer(os.Getenv("ROLLUP_HTTP_SERVER_URL")) + } + finish := rollups.Finish{Status: "accept"} for true { @@ -78,7 +82,7 @@ func main() { infolog.Println("No pending rollup request, trying again") } else { - resBody, err := ioutil.ReadAll(res.Body) + resBody, err := io.ReadAll(res.Body) if err != nil { errlog.Panicln("Error: could not read response body: ", err) } diff --git a/examples/example2_handler.go b/example-apps/apps-src/example2_handler.go similarity index 100% rename from examples/example2_handler.go rename to example-apps/apps-src/example2_handler.go diff --git a/examples/example3_handler_advance_and_inspect.go b/example-apps/apps-src/example3_handler_advance_and_inspect.go similarity index 98% rename from examples/example3_handler_advance_and_inspect.go rename to example-apps/apps-src/example3_handler_advance_and_inspect.go index 44068cf..bacf766 100644 --- a/examples/example3_handler_advance_and_inspect.go +++ b/example-apps/apps-src/example3_handler_advance_and_inspect.go @@ -27,7 +27,7 @@ func HandleAdvance(metadata *rollups.Metadata, payloadHex string) error { return fmt.Errorf("HandleAdvance: error making http request: %s", err) } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return fmt.Errorf("HandleAdvance: could not read response body: %s", err) } diff --git a/examples/example4_handler_class.go b/example-apps/apps-src/example4_handler_class.go similarity index 100% rename from examples/example4_handler_class.go rename to example-apps/apps-src/example4_handler_class.go diff --git a/examples/example5_handler_default_and_fixed_address.go b/example-apps/apps-src/example5_handler_default_and_fixed_address.go similarity index 69% rename from examples/example5_handler_default_and_fixed_address.go rename to example-apps/apps-src/example5_handler_default_and_fixed_address.go index e027fd2..cd42d54 100644 --- a/examples/example5_handler_default_and_fixed_address.go +++ b/example-apps/apps-src/example5_handler_default_and_fixed_address.go @@ -11,7 +11,7 @@ import ( var infolog = log.New(os.Stderr, "[ info ] ", log.Lshortfile) -var relayMessage string +var operatorMessage string func GenericHandler(payloadHex string) error { payload, err := rollups.Hex2Str(payloadHex) @@ -20,7 +20,7 @@ func GenericHandler(payloadHex string) error { } infolog.Println("Generic request payload:", payload) - report := rollups.Report{Payload: rollups.Str2Hex("Generic " + payload + relayMessage)} + report := rollups.Report{Payload: rollups.Str2Hex("Generic " + payload + operatorMessage)} _, err = rollups.SendReport(&report) if err != nil { return fmt.Errorf("GenericHandler: error making http request: %s", err) @@ -41,11 +41,15 @@ func HandleFixed(metadata *rollups.Metadata, payloadHex string) error { } -func HandleRelay(metadata *rollups.Metadata, payloadHex string) error { - infolog.Println("Hey, I know this address, sender is",metadata.MsgSender,"and the my address is", payloadHex) - relayMessage = fmt.Sprint(" and the dapp address is ",payloadHex) - report := rollups.Report{Payload: rollups.Str2Hex("Set address relay")} - _, err := rollups.SendReport(&report) +func HandleOperator(metadata *rollups.Metadata, payloadHex string) error { + infolog.Println("Hey, I know this address, sender is",metadata.MsgSender,"and the message is", payloadHex) + payloadStr, err := rollups.Hex2Str(payloadHex) + if err != nil { + return fmt.Errorf("HandleOperator: hex error decoding payload:", err) + } + operatorMessage = fmt.Sprint(" and the message is ",payloadStr) + report := rollups.Report{Payload: rollups.Str2Hex("Set operator message")} + _, err = rollups.SendReport(&report) if err != nil { return fmt.Errorf("HandleFixed: error making http request: %s", err) } @@ -57,9 +61,11 @@ func HandleRelay(metadata *rollups.Metadata, payloadHex string) error { func main() { handler.InitializeRollupsAddresses("localhost") + operator := "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + handler.HandleDefault(GenericHandler) handler.HandleRollupsFixedAddresses(HandleFixed) - handler.HandleFixedAddress(handler.RollupsAddresses.DappAddressRelay, HandleRelay) + handler.HandleFixedAddress(operator, HandleOperator) err := handler.RunDebug() if err != nil { diff --git a/examples/example6_handler_assets.go b/example-apps/apps-src/example6_handler_assets.go similarity index 52% rename from examples/example6_handler_assets.go rename to example-apps/apps-src/example6_handler_assets.go index 8dfe46a..a623017 100644 --- a/examples/example6_handler_assets.go +++ b/example-apps/apps-src/example6_handler_assets.go @@ -13,8 +13,6 @@ import ( var infolog = log.New(os.Stderr, "[ info ] ", log.Lshortfile) -var dappAddress string - func HandleEther(metadata *rollups.Metadata, payloadHex string) error { deposit, err := rollups.DecodeEtherDeposit(payloadHex) if err != nil { @@ -23,17 +21,13 @@ func HandleEther(metadata *rollups.Metadata, payloadHex string) error { infolog.Println("Received",new(big.Float).Quo(new(big.Float).SetInt(deposit.Amount),big.NewFloat(1e18)),"native token deposit from",deposit.Depositor,"data:",string(deposit.Data)) - if dappAddress != "" { - voucher := rollups.EtherWithdralVoucher(dappAddress, deposit.Depositor, deposit.Amount) - infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) - res, err := rollups.SendVoucher(&voucher) - if err != nil { - return fmt.Errorf("HandleEther: error making http request: %s", err) - } - infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) - } else { - infolog.Println("Can't generate voucher as there is no dapp address configured") + voucher := rollups.EtherWithdralVoucher(deposit.Depositor, deposit.Amount) + infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) + res, err := rollups.SendVoucher(&voucher) + if err != nil { + return fmt.Errorf("HandleEther: error making http request: %s", err) } + infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) return nil } @@ -41,8 +35,8 @@ func HandleErc20(metadata *rollups.Metadata, payloadHex string) error { deposit, err := rollups.DecodeErc20Deposit(payloadHex) if err != nil { return fmt.Errorf("HandleErc20: error decoding deposit: %s", err) - } - infolog.Println("Received",new(big.Float).Quo(new(big.Float).SetInt(deposit.Amount),big.NewFloat(1e18)),"tokens",deposit.TokenAddress,"Erc20 deposit from",deposit.Depositor,"data:",string(deposit.Data)) + } + infolog.Println("Received",deposit.Amount,"tokens",deposit.TokenAddress,"Erc20 deposit from",deposit.Depositor,"data:",string(deposit.Data)) voucher := rollups.Erc20TransferVoucher(deposit.Depositor, deposit.TokenAddress, deposit.Amount) infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) @@ -62,17 +56,13 @@ func HandleErc721(metadata *rollups.Metadata, payloadHex string) error { infolog.Println("Received id",deposit.TokenId,deposit.TokenAddress,"Erc721 deposit from",deposit.Depositor,"data:",string(deposit.Data)) - if dappAddress != "" { - voucher := rollups.Erc721SafeTransferVoucher(dappAddress, deposit.Depositor, deposit.TokenAddress, deposit.TokenId) - infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) - res, err := rollups.SendVoucher(&voucher) - if err != nil { - return fmt.Errorf("HandleErc721: error making http request: %s", err) - } - infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) - } else { - infolog.Println("Can't generate voucher as there is no dapp address configured") + voucher := rollups.Erc721SafeTransferVoucher(metadata.AppContract, deposit.Depositor, deposit.TokenAddress, deposit.TokenId) + infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) + res, err := rollups.SendVoucher(&voucher) + if err != nil { + return fmt.Errorf("HandleErc721: error making http request: %s", err) } + infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) return nil } @@ -84,17 +74,13 @@ func HandleErc1155Single(metadata *rollups.Metadata, payloadHex string) error { infolog.Println("Received ",deposit.Amount,"tokens of id",deposit.TokenId,deposit.TokenAddress,"Erc1155 Single deposit from",deposit.Depositor,"base layer data:",string(deposit.BaseLayerData),"and exec layer data:",string(deposit.ExecLayerData)) - if dappAddress != "" { - voucher := rollups.Erc1155SafeTransferFromVoucher(dappAddress, deposit.Depositor, deposit.TokenAddress, deposit.TokenId, deposit.Amount, make([]byte,0)) - infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) - res, err := rollups.SendVoucher(&voucher) - if err != nil { - return fmt.Errorf("HandleErc721: error making http request: %s", err) - } - infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) - } else { - infolog.Println("Can't generate voucher as there is no dapp address configured") + voucher := rollups.Erc1155SafeTransferFromVoucher(metadata.AppContract, deposit.Depositor, deposit.TokenAddress, deposit.TokenId, deposit.Amount, make([]byte,0)) + infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) + res, err := rollups.SendVoucher(&voucher) + if err != nil { + return fmt.Errorf("HandleErc721: error making http request: %s", err) } + infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) return nil } @@ -106,36 +92,19 @@ func HandleErc1155Batch(metadata *rollups.Metadata, payloadHex string) error { infolog.Println("Received ",deposit.Amounts,"amounts of ids",deposit.TokenIds,deposit.TokenAddress,"Erc1155 Batch deposit from",deposit.Depositor,"base layer data:",string(deposit.BaseLayerData),"and exec layer data:",string(deposit.ExecLayerData)) - if dappAddress != "" { - voucher := rollups.Erc1155SafeBatchTransferFromVoucher(dappAddress, deposit.Depositor, deposit.TokenAddress, deposit.TokenIds, deposit.Amounts, make([]byte,0)) - infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) - res, err := rollups.SendVoucher(&voucher) - if err != nil { - return fmt.Errorf("HandleErc721: error making http request: %s", err) - } - infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) - } else { - infolog.Println("Can't generate voucher as there is no dapp address configured") - } - return nil -} - -func HandleRelay(metadata *rollups.Metadata, payloadHex string) error { - infolog.Println("Dapp Relay, sender is",metadata.MsgSender,"and the my address is", payloadHex) - dappAddress = payloadHex - report := rollups.Report{Payload: rollups.Str2Hex("Set address relay")} - _, err := rollups.SendReport(&report) + voucher := rollups.Erc1155SafeBatchTransferFromVoucher(metadata.AppContract, deposit.Depositor, deposit.TokenAddress, deposit.TokenIds, deposit.Amounts, make([]byte,0)) + infolog.Println("Sending voucher destination",voucher.Destination,"payload",voucher.Payload) + res, err := rollups.SendVoucher(&voucher) if err != nil { - return fmt.Errorf("HandleFixed: error making http request: %s", err) + return fmt.Errorf("HandleErc721: error making http request: %s", err) } - + infolog.Println("Received voucher status", strconv.Itoa(res.StatusCode)) return nil } func main() { handler.InitializeRollupsAddresses("localhost") - - handler.HandleFixedAddress(handler.RollupsAddresses.DappAddressRelay, HandleRelay) + handler.HandleFixedAddress(handler.RollupsAddresses.EtherPortalAddress, HandleEther) handler.HandleFixedAddress(handler.RollupsAddresses.Erc20PortalAddress, HandleErc20) handler.HandleFixedAddress(handler.RollupsAddresses.Erc721PortalAddress, HandleErc721) diff --git a/examples/example7_json_handler.go b/example-apps/apps-src/example7_json_handler.go similarity index 100% rename from examples/example7_json_handler.go rename to example-apps/apps-src/example7_json_handler.go diff --git a/examples/example8_abi_handler.go b/example-apps/apps-src/example8_abi_handler.go similarity index 94% rename from examples/example8_abi_handler.go rename to example-apps/apps-src/example8_abi_handler.go index dad55a6..2f4e50d 100644 --- a/examples/example8_abi_handler.go +++ b/example-apps/apps-src/example8_abi_handler.go @@ -75,7 +75,7 @@ func HandleSet(metadata *rollups.Metadata, payloadMap map[string]interface{}) er return fmt.Errorf("HandleSet: error making http request: %s", err) } - noticePayload,err := noticeCodec.Encode([]interface{}{metadata.Timestamp,key,value}) + noticePayload,err := noticeCodec.Encode([]interface{}{metadata.BlockTimestamp,key,value}) if err != nil { return fmt.Errorf("HandleSet: encoding notice: %s", err) } @@ -96,10 +96,10 @@ func main() { noticeCodec = abihandler.NewCodec([]string{"uint","string","string"}) - setCodec := abihandler.NewHeaderCodec("dapp","set",[]string{"string key","string value"}) + setCodec := abihandler.NewHeaderCodec("dapp.set",[]string{"string key","string value"}) handler.HandleAdvanceRoute(setCodec, HandleSet) - getCodec := abihandler.NewHeaderCodec("dapp","get",[]string{"string"}) + getCodec := abihandler.NewHeaderCodec("dapp.get",[]string{"string"}) handler.HandleAdvanceRoute(getCodec, HandleAdvanceGet) handler.HandleInspectRoute(getCodec, HandleGet) diff --git a/examples/example9_uri_handler.go b/example-apps/apps-src/example9_uri_handler.go similarity index 100% rename from examples/example9_uri_handler.go rename to example-apps/apps-src/example9_uri_handler.go diff --git a/examples/go.mod b/examples/go.mod deleted file mode 100644 index c9e3594..0000000 --- a/examples/go.mod +++ /dev/null @@ -1,18 +0,0 @@ -module dapp - -go 1.20 - -require github.com/prototyp3-dev/go-rollups v0.0.0 - -require ( - github.com/google/gofuzz v1.2.0 // indirect - github.com/lynoferraz/abigo v0.0.2 // indirect - github.com/mitchellh/mapstructure v1.1.2 // indirect - github.com/umbracle/ethgo v0.1.3 // indirect - github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722 // indirect - github.com/valyala/fastjson v1.4.1 // indirect - golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect - golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect -) - -replace github.com/prototyp3-dev/go-rollups => ../ diff --git a/examples/go.sum b/examples/go.sum deleted file mode 100644 index 751559e..0000000 --- a/examples/go.sum +++ /dev/null @@ -1,40 +0,0 @@ -github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= -github.com/Microsoft/go-winio v0.4.13 h1:Hmi80lzZuI/CaYmlJp/b+FjZdRZhKu9c2mDVqKlLWVs= -github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= -github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= -github.com/containerd/continuity v0.0.0-20191214063359-1097c8bae83b h1:pik3LX++5O3UiNWv45wfP/WT81l7ukBJzd3uUiifbSU= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= -github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= -github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= -github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= -github.com/lynoferraz/abigo v0.0.2 h1:cq1PvHKgskDWRTFBP1tEGvlWsh5cP+RhyeSwC942IhE= -github.com/lynoferraz/abigo v0.0.2/go.mod h1:D+4dY+ZHBtUHySuDmosGW8GUyXTEDlYKGtw4Ornaou0= -github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ= -github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI= -github.com/opencontainers/runc v0.1.1 h1:GlxAyO6x8rfZYN9Tt0Kti5a/cP41iuiO2yYT0IJGY8Y= -github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA= -github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/umbracle/ethgo v0.1.3 h1:s8D7Rmphnt71zuqrgsGTMS5gTNbueGO1zKLh7qsFzTM= -github.com/umbracle/ethgo v0.1.3/go.mod h1:g9zclCLixH8liBI27Py82klDkW7Oo33AxUOr+M9lzrU= -github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722 h1:10Nbw6cACsnQm7r34zlpJky+IzxVLRk6MKTS2d3Vp0E= -github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722/go.mod h1:c8J0h9aULj2i3umrfyestM6jCq0LK0U6ly6bWy96nd4= -github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE= -github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/go.mod b/go.mod index b534a29..e5804fc 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/prototyp3-dev/go-rollups -go 1.20 +go 1.23 require ( github.com/lynoferraz/abigo v0.0.2 diff --git a/go.sum b/go.sum index 751559e..f7c1cb8 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,43 @@ github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= +github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Microsoft/go-winio v0.4.13 h1:Hmi80lzZuI/CaYmlJp/b+FjZdRZhKu9c2mDVqKlLWVs= +github.com/Microsoft/go-winio v0.4.13/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= +github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/containerd/continuity v0.0.0-20191214063359-1097c8bae83b h1:pik3LX++5O3UiNWv45wfP/WT81l7ukBJzd3uUiifbSU= +github.com/containerd/continuity v0.0.0-20191214063359-1097c8bae83b/go.mod h1:Dq467ZllaHgAtVp4p1xUQWBrFXR9s/wyoTpG8zOJGkY= 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/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/lynoferraz/abigo v0.0.2 h1:cq1PvHKgskDWRTFBP1tEGvlWsh5cP+RhyeSwC942IhE= github.com/lynoferraz/abigo v0.0.2/go.mod h1:D+4dY+ZHBtUHySuDmosGW8GUyXTEDlYKGtw4Ornaou0= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/opencontainers/go-digest v1.0.0-rc1 h1:WzifXhOVOEOuFYOJAW6aQqW0TooG2iki3E3Ii+WN7gQ= +github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/image-spec v1.0.1 h1:JMemWkRwHx4Zj+fVxWoMCFm/8sYGGrUVojFA6h/TRcI= +github.com/opencontainers/image-spec v1.0.1/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= github.com/opencontainers/runc v0.1.1 h1:GlxAyO6x8rfZYN9Tt0Kti5a/cP41iuiO2yYT0IJGY8Y= +github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59PVA73FjuZG0U= github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA= +github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.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/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/umbracle/ethgo v0.1.3 h1:s8D7Rmphnt71zuqrgsGTMS5gTNbueGO1zKLh7qsFzTM= github.com/umbracle/ethgo v0.1.3/go.mod h1:g9zclCLixH8liBI27Py82klDkW7Oo33AxUOr+M9lzrU= github.com/umbracle/fastrlp v0.0.0-20220527094140-59d5dd30e722 h1:10Nbw6cACsnQm7r34zlpJky+IzxVLRk6MKTS2d3Vp0E= @@ -32,9 +49,11 @@ golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHR golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 h1:fHDIZ2oxGnUZRN6WgWFCbYBjH9uqVPRCUVUDhs0wnbA= +golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/handler/abi/abi.go b/handler/abi/abi.go index 2d62521..fa440e9 100644 --- a/handler/abi/abi.go +++ b/handler/abi/abi.go @@ -1,9 +1,11 @@ package abihandler import ( - "strings" - hdl "github.com/prototyp3-dev/go-rollups/handler" - "github.com/prototyp3-dev/go-rollups/rollups" + "math/big" + "strings" + + hdl "github.com/prototyp3-dev/go-rollups/handler" + "github.com/prototyp3-dev/go-rollups/rollups" ) type AdvanceMapHandlerFunc func(*rollups.Metadata,map[string]interface{}) error @@ -47,8 +49,8 @@ func (h *AbiHandler) HandleAdvanceRoute(routeCodec *Codec, fnHandle AdvanceMapHa if fnHandle == nil { panic("abi handler: nil handler") } - if len(routeCodec.Header) > 0 && len(routeCodec.Header) != 66 { - panic("abi handler: codec header format") + if len(routeCodec.Header) > 0 && len(routeCodec.Header) != CodecHeaderLength { + panic("abi handler: codec header format" ) } if len(routeCodec.Fields) != 0 && len(routeCodec.PackedFields) != 0 { panic("abi handler: ambiguous codec fields") @@ -75,7 +77,7 @@ func (h *AbiHandler) HandleFixedAddressAdvance(address string, routeCodec *Codec if fnHandle == nil { panic("abi handler: nil handler") } - if len(routeCodec.Header) > 0 && len(routeCodec.Header) != 66 { + if len(routeCodec.Header) > 0 && len(routeCodec.Header) != CodecHeaderLength { panic("abi handler: codec header format") } if len(routeCodec.Fields) != 0 && len(routeCodec.PackedFields) != 0 { @@ -116,7 +118,7 @@ func (h *AbiHandler) HandleInspectRoute(routeCodec *Codec, fnHandle InspectMapHa if fnHandle == nil { panic("abi handler: nil handler") } - if len(routeCodec.Header) > 0 && len(routeCodec.Header) != 66 { + if len(routeCodec.Header) > 0 && len(routeCodec.Header) != CodecHeaderLength { panic("abi handler: codec header format") } if len(routeCodec.Fields) != 0 && len(routeCodec.PackedFields) != 0 { @@ -150,8 +152,8 @@ func (h *AbiHandler) abiAdvanceHandler(metadata *rollups.Metadata, payloadHex st if h.Handler.LogLevel >= hdl.Trace {hdl.TraceLogger.Println("Received ABI no header Advance Request:",result) } return h.RouteAdvanceHandlers[""].Handler.Handle(metadata,result),true } - if len(payloadHex) >= 66 { - header := payloadHex[:66] + if len(payloadHex) >= CodecHeaderLength { + header := payloadHex[:CodecHeaderLength] if h.RouteAdvanceHandlers[header] != nil { codec := h.AdvanceCodecs[header] result,err := codec.Decode(payloadHex) @@ -175,8 +177,8 @@ func (h *AbiHandler) abiInspectHandler(payloadHex string) (error,bool) { if h.Handler.LogLevel >= hdl.Trace {hdl.TraceLogger.Println("Received ABI no header Inspect Request:",result) } return h.RouteInspectHandlers[""].Handler.Handle(result),true } - if len(payloadHex) >= 66 { - header := payloadHex[:66] + if len(payloadHex) >= CodecHeaderLength { + header := payloadHex[:CodecHeaderLength] if h.RouteInspectHandlers[header] != nil { codec := h.InspectCodecs[header] result,err := codec.Decode(payloadHex) @@ -202,8 +204,8 @@ func (h *AbiHandler) abiFixedAdvanceHandler(address string) (func(*rollups.Metad if h.Handler.LogLevel >= hdl.Trace {hdl.TraceLogger.Println("Received ABI no header Fixed Advance Request:",result) } return h.FixedAddressAdvanceHandlers[address][""].Handler.Handle(metadata,result),true } - if len(payloadHex) >= 66 { - header := payloadHex[:66] + if len(payloadHex) >= CodecHeaderLength { + header := payloadHex[:CodecHeaderLength] if h.FixedAddressAdvanceHandlers[address][header] != nil { codec := h.FixedAdvanceCodecs[address][header] result,err := codec.Decode(payloadHex) @@ -226,7 +228,7 @@ func (h *AbiHandler) HandleAdvance(fnHandle hdl.AdvanceHandlerFunc) {h.Handler.H func (h *AbiHandler) HandleRollupsFixedAddresses(fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleRollupsFixedAddresses(fnHandle)} func (h *AbiHandler) HandleFixedAddress(address string, fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleFixedAddress(address,fnHandle)} func (h *AbiHandler) SendNotice(payloadHex string) (uint64,error) {return h.Handler.SendNotice(payloadHex)} -func (h *AbiHandler) SendVoucher(destination string, payloadHex string) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex)} +func (h *AbiHandler) SendVoucher(destination string, payloadHex string, value *big.Int) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex,value)} func (h *AbiHandler) SendReport(payloadHex string) error {return h.Handler.SendReport(payloadHex)} func (h *AbiHandler) SendException(payloadHex string) error {return h.Handler.SendException(payloadHex)} func (h *AbiHandler) Run() error {return h.Handler.Run()} diff --git a/handler/abi/codec.go b/handler/abi/codec.go index e5e662d..ab8294f 100644 --- a/handler/abi/codec.go +++ b/handler/abi/codec.go @@ -12,6 +12,8 @@ import ( type Address = ethgo.Address //[20]byte +var CodecHeaderLength = 10 + func Address2Hex(bin Address) string { return rollups.Bin2Hex(bin[:]) } @@ -34,7 +36,6 @@ func Bin2Address(addrBytes []byte) (Address,error) { } type Codec struct { - Framework string Method string Header string PackedFields []string @@ -44,9 +45,8 @@ type Codec struct { func (c Codec) String() string { atts := make([]string,0) - if c.Header != "" && c.Framework != "" &&c.Method != "" { + if c.Header != "" && c.Method != "" { atts = append(atts, fmt.Sprintf("Header(%s)",c.Header)) - atts = append(atts, fmt.Sprintf("Framework(%s)",c.Framework)) atts = append(atts, fmt.Sprintf("Method(%s)",c.Method)) } if len(c.Fields) > 0 { @@ -63,12 +63,11 @@ func NewPackedCodec(fields []string) *Codec { return &Codec{PackedFields: fields, typ: typ} } -func NewHeaderPackedCodec(framework string, method string, fields []string) *Codec { +func NewHeaderPackedCodec(method string, fields []string) *Codec { codec := NewPackedCodec(fields) cleanFields := CleanFields(codec.typ) - header := CodecHeader(framework, method, cleanFields) + header := CodecHeader(method, cleanFields) codec.Header = header - codec.Framework = framework codec.Method = method return codec } @@ -78,12 +77,11 @@ func NewCodec(fields []string) *Codec { return &Codec{Fields: fields, typ: typ} } -func NewHeaderCodec(framework string, method string, fields []string) *Codec { +func NewHeaderCodec(method string, fields []string) *Codec { codec := NewCodec(fields) cleanFields := CleanFields(codec.typ) - header := CodecHeader(framework, method, cleanFields) + header := CodecHeader(method, cleanFields) codec.Header = header - codec.Framework = framework codec.Method = method return codec } @@ -100,27 +98,15 @@ func CleanFields(typ *abi.Type) []string { return cleanFields } -func CodecHeader(framework string, method string, fields []string) string { - frameworkeccak := ethgo.Keccak256([]byte(framework)) - methodkeccak := ethgo.Keccak256([]byte(method)) - fieldskeccak := ethgo.Keccak256([]byte("("+strings.Join(fields, ",")+")")) - headerAllbytes := append(frameworkeccak, methodkeccak...) - headerAllbytes = append(headerAllbytes, fieldskeccak...) - return rollups.Bin2Hex(ethgo.Keccak256(headerAllbytes)) +func CodecHeader(method string, fields []string) string { + headerKeccak := ethgo.Keccak256([]byte(method+"("+strings.Join(fields, ",")+")")) + return rollups.Bin2Hex(headerKeccak[:4]) } func NewVoucherCodec(method string, fields []string) *Codec { - codec := NewCodec(fields) - header := VoucherHeader(method, codec.Fields) - codec.Header = header - codec.Method = method - return codec + return NewHeaderCodec(method, fields) } -func VoucherHeader(method string, fields []string) string { - headerKeccak := ethgo.Keccak256([]byte(method+"("+strings.Join(fields, ",")+")")) - return rollups.Bin2Hex(headerKeccak[:4]) -} func (c *Codec) Decode(payloadHex string) (map[string]interface{},error) { var result map[string]interface{} payloadBytes, err := rollups.Hex2Bin(payloadHex) @@ -128,10 +114,10 @@ func (c *Codec) Decode(payloadHex string) (map[string]interface{},error) { return result,fmt.Errorf("Decode: %s", err) } if len(c.Header) > 0 { - if payloadHex[:66] != c.Header { + if payloadHex[:CodecHeaderLength] != c.Header { return result,fmt.Errorf("Decode: Header does not match") } - payloadBytes = payloadBytes[32:] + payloadBytes = payloadBytes[4:] } var fields []string diff --git a/handler/handler.go b/handler/handler.go index 7b1393f..bbf4cd2 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -10,6 +10,7 @@ import ( "os" "strconv" "strings" + "math/big" "github.com/prototyp3-dev/go-rollups/rollups" ) @@ -26,12 +27,11 @@ const ( ) type NetworkAddresses struct { - DappAddressRelay string `json:"DAPP_RELAY_ADDRESS"` - EtherPortalAddress string `json:"ETHER_PORTAL_ADDRESS"` - Erc20PortalAddress string `json:"ERC20_PORTAL_ADDRESS"` - Erc721PortalAddress string `json:"ERC721_PORTAL_ADDRESS"` - Erc1155SinglePortalAddress string `json:"ERC1155_SINGLE_PORTAL_ADDRESS"` - Erc1155BatchPortalAddress string `json:"ERC1155_BATCH_PORTAL_ADDRESS"` + EtherPortalAddress string `json:"EtherPortal"` + Erc20PortalAddress string `json:"ERC20Portal"` + Erc721PortalAddress string `json:"ERC721Portal"` + Erc1155SinglePortalAddress string `json:"ERC1155SinglePortal"` + Erc1155BatchPortalAddress string `json:"ERC1155BatchPortal"` } type AdvanceHandlerFunc func(*rollups.Metadata,string) error @@ -221,8 +221,8 @@ func (h *Handler) SendNotice(payloadHex string) (uint64,error) { return indexRes.Index,nil } -func (h *Handler) SendVoucher(destination string, payloadHex string) (uint64,error) { - voucher := &rollups.Voucher{Destination: destination, Payload: payloadHex} +func (h *Handler) SendVoucher(destination string, payloadHex string, value *big.Int) (uint64,error) { + voucher := &rollups.Voucher{Destination: destination, Payload: payloadHex, Value: value} if h.LogLevel >= Trace {TraceLogger.Println("Sending voucher status",voucher)} res, err := rollups.SendVoucher(voucher) if err != nil { @@ -456,21 +456,12 @@ func InitializeRollupsAddresses(currentNetwork string) error { if KnownRollupsAddresses != nil { return nil } - var result map[string]interface{} - json.Unmarshal([]byte(networks), &result) - if result[currentNetwork] == nil { - panic("InitializeRollupsAddresses: Unknown network") - } - - jsonNetwork, _ := json.Marshal(result[currentNetwork]) - - err := json.Unmarshal(jsonNetwork, &RollupsAddresses) + err := json.Unmarshal([]byte(rollupsAddresses), &RollupsAddresses) if err != nil { panic(fmt.Sprint("InitializeRollupsAddresses: error unmarshaling network: ", err)) } KnownRollupsAddresses = make(map[string]bool) - KnownRollupsAddresses[strings.ToLower(RollupsAddresses.DappAddressRelay)] = true KnownRollupsAddresses[strings.ToLower(RollupsAddresses.EtherPortalAddress)] = true KnownRollupsAddresses[strings.ToLower(RollupsAddresses.Erc20PortalAddress)] = true KnownRollupsAddresses[strings.ToLower(RollupsAddresses.Erc721PortalAddress)] = true diff --git a/handler/json/json.go b/handler/json/json.go index 61141a2..5addba8 100644 --- a/handler/json/json.go +++ b/handler/json/json.go @@ -2,6 +2,7 @@ package jsonhandler import ( "encoding/json" + "math/big" hdl "github.com/prototyp3-dev/go-rollups/handler" "github.com/prototyp3-dev/go-rollups/rollups" @@ -126,7 +127,7 @@ func (h *JsonHandler) HandleAdvance(fnHandle hdl.AdvanceHandlerFunc) {h.Handler. func (h *JsonHandler) HandleRollupsFixedAddresses(fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleRollupsFixedAddresses(fnHandle)} func (h *JsonHandler) HandleFixedAddress(address string, fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleFixedAddress(address,fnHandle)} func (h *JsonHandler) SendNotice(payloadHex string) (uint64,error) {return h.Handler.SendNotice(payloadHex)} -func (h *JsonHandler) SendVoucher(destination string, payloadHex string) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex)} +func (h *JsonHandler) SendVoucher(destination string, payloadHex string, value *big.Int) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex,value)} func (h *JsonHandler) SendReport(payloadHex string) error {return h.Handler.SendReport(payloadHex)} func (h *JsonHandler) SendException(payloadHex string) error {return h.Handler.SendException(payloadHex)} func (h *JsonHandler) Run() error {return h.Handler.Run()} diff --git a/handler/networks.go b/handler/networks.go deleted file mode 100644 index 9007a2e..0000000 --- a/handler/networks.go +++ /dev/null @@ -1,63 +0,0 @@ -package handler - - -var networks = ` -{ - "localhost":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "sepolia":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "arbitrum-goerli":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "optimism-goerli":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "arbitrum":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "optimism":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - }, - "mainnet":{ - "DAPP_RELAY_ADDRESS":"0xF5DE34d6BbC0446E2a45719E718efEbaaE179daE", - "ETHER_PORTAL_ADDRESS":"0xFfdbe43d4c855BF7e0f105c400A50857f53AB044", - "ERC20_PORTAL_ADDRESS":"0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB", - "ERC721_PORTAL_ADDRESS":"0x237F8DD094C0e47f4236f12b4Fa01d6Dae89fb87", - "ERC1155_SINGLE_PORTAL_ADDRESS":"0x7CFB0193Ca87eB6e48056885E026552c3A941FC4", - "ERC1155_BATCH_PORTAL_ADDRESS":"0xedB53860A6B52bbb7561Ad596416ee9965B055Aa" - } -} -` \ No newline at end of file diff --git a/handler/rollups_addresses.go b/handler/rollups_addresses.go new file mode 100644 index 0000000..b593869 --- /dev/null +++ b/handler/rollups_addresses.go @@ -0,0 +1,18 @@ +package handler + + +var rollupsAddresses = ` +{ + "ApplicationFactory": "0xA1DA32BF664109D62208a1cb0d69aACc6a484873", + "AuthorityFactory": "0xbDC5D42771A4Ae55eC7670AAdD2458D1d9C7C8A8", + "ERC1155BatchPortal": "0x4a218D331C0933d7E3EB496ac901669f28D94981", + "ERC1155SinglePortal": "0x2f0D587DD6EcF67d25C558f2e9c3839c579e5e38", + "ERC20Portal": "0xB0e28881FF7ee9CD5B1229d570540d74bce23D39", + "ERC721Portal": "0x874b3245ead7474Cb9f3b83cD1446dC522f6bd36", + "EtherPortal": "0xfa2292f6D85ea4e629B156A4f99219e30D12EE17", + "InputBox": "0x593E5BCf894D6829Dd26D0810DA7F064406aebB6", + "QuorumFactory": "0x68C3d53a095f66A215a8bEe096Cd3Ba4fFB7bAb3", + "SafeERC20Transfer": "0x817b126F242B5F184Fa685b4f2F91DC99D8115F9", + "SelfHostedApplicationFactory": "0x0678FAA399F0193Fb9212BE41590316D275b1392" +} +` \ No newline at end of file diff --git a/handler/uri/uri.go b/handler/uri/uri.go index 7017d8c..dd95b44 100644 --- a/handler/uri/uri.go +++ b/handler/uri/uri.go @@ -2,6 +2,7 @@ package urihandler import ( "regexp" + "math/big" hdl "github.com/prototyp3-dev/go-rollups/handler" "github.com/prototyp3-dev/go-rollups/rollups" ) @@ -160,7 +161,7 @@ func (h *UriHandler) HandleAdvance(fnHandle hdl.AdvanceHandlerFunc) {h.Handler.H func (h *UriHandler) HandleRollupsFixedAddresses(fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleRollupsFixedAddresses(fnHandle)} func (h *UriHandler) HandleFixedAddress(address string, fnHandle hdl.AdvanceHandlerFunc) {h.Handler.HandleFixedAddress(address,fnHandle)} func (h *UriHandler) SendNotice(payloadHex string) (uint64,error) {return h.Handler.SendNotice(payloadHex)} -func (h *UriHandler) SendVoucher(destination string, payloadHex string) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex)} +func (h *UriHandler) SendVoucher(destination string, payloadHex string, value *big.Int) (uint64,error) {return h.Handler.SendVoucher(destination,payloadHex,value)} func (h *UriHandler) SendReport(payloadHex string) error {return h.Handler.SendReport(payloadHex)} func (h *UriHandler) SendException(payloadHex string) error {return h.Handler.SendException(payloadHex)} func (h *UriHandler) Run() error {return h.Handler.Run()} diff --git a/rollups/asset.go b/rollups/asset.go index a6cd8da..50558b0 100644 --- a/rollups/asset.go +++ b/rollups/asset.go @@ -62,9 +62,9 @@ func DecodeErc20Deposit(payloadHex string) (Erc20Deposit,error) { } amount := new(big.Int) - amount.SetBytes(bin[41:73]) + amount.SetBytes(bin[40:72]) - return Erc20Deposit{Depositor:Bin2Hex(bin[21:41]), TokenAddress:Bin2Hex(bin[1:21]), Amount:amount, Data:bin[73:]},nil + return Erc20Deposit{Depositor:Bin2Hex(bin[20:40]), TokenAddress:Bin2Hex(bin[:20]), Amount:amount, Data:bin[72:]},nil } func DecodeErc721Deposit(payloadHex string) (Erc721Deposit,error) { @@ -140,9 +140,8 @@ func DecodeErc1155BatchDeposit(payloadHex string) (Erc1155BatchDeposit,error) { return Erc1155BatchDeposit{Depositor:depositor, TokenAddress:token, TokenIds:idList, Amounts:amountList, BaseLayerData:blData, ExecLayerData:elData}, nil } -func EtherWithdralVoucher(Sender string, Receiver string, Amount *big.Int) Voucher { - payload := "0x522f6815" + hex.EncodeToString(append(Address2Bin(Receiver),PadBytes(Amount.Bytes(),32)...)) - return Voucher{Destination: Sender, Payload: payload} +func EtherWithdralVoucher(Receiver string, Amount *big.Int) Voucher { + return Voucher{Destination: Receiver, Payload: "0x", Value: Amount} } func Erc20TransferVoucher(Receiver string, TokenAddress string, Amount *big.Int) Voucher { diff --git a/rollups/message.go b/rollups/message.go index 1e4ffc9..dd8ed96 100644 --- a/rollups/message.go +++ b/rollups/message.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "net/http" + "math/big" ) type FinishResponse struct { @@ -21,11 +22,13 @@ type AdvanceResponse struct { } type Metadata struct { - MsgSender string `json:"msg_sender"` - EpochIndex uint64 `json:"epoch_index"` - InputIndex uint64 `json:"input_index"` - BlockNumber uint64 `json:"block_number"` - Timestamp uint64 `json:"timestamp"` + ChainId uint64 `json:"chain_id"` + AppContract string `json:"app_contract"` + MsgSender string `json:"msg_sender"` + InputIndex uint64 `json:"input_index"` + BlockNumber uint64 `json:"block_number"` + BlockTimestamp uint64 `json:"block_timestamp"` + PrevRandao string `json:"prev_randao"` } type Finish struct { @@ -43,6 +46,14 @@ type Notice struct { type Voucher struct { Destination string `json:"destination"` Payload string `json:"payload"` + Value *big.Int `json:"value"` +} +func (v Voucher) MarshalJSON() ([]byte, error) { + return json.Marshal(struct{ + Destination string `json:"destination"` + Payload string `json:"payload"` + Value string `json:"value"` + }{v.Destination,v.Payload,Bin2Hex(PadBytes(v.Value.Bytes(),32))}) } type Exception struct { @@ -101,6 +112,10 @@ func SendNotice(notice *Notice) (*http.Response, error) { } func SendVoucher(voucher *Voucher) (*http.Response, error) { + if voucher.Value == nil { + voucher.Value = new(big.Int) + } + body, err := json.Marshal(voucher) if err != nil { return &http.Response{}, err diff --git a/wallet/wallet.go b/wallet/wallet.go index 2b4ce3d..8d6a619 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -160,8 +160,7 @@ func (w *Wallet) WithdrawErc1155(tokenAddress abihandler.Address, tokenId *big.I type WalletRoute uint8 const ( - DappRelayAdvanceRoute WalletRoute = iota - EtherCodecAdvanceRoutes + EtherCodecAdvanceRoutes WalletRoute = iota Erc20CodecAdvanceRoutes Erc721CodecAdvanceRoutes Erc1155CodecAdvanceRoutes @@ -206,7 +205,6 @@ type WalletApp struct { handler *hdl.Handler abiHandler *abihandler.AbiHandler uriHandler *urihandler.UriHandler - DappAddress abihandler.Address Wallets map[abihandler.Address]*Wallet } @@ -225,7 +223,6 @@ var erc1155NoticeCodec *abihandler.Codec var erc1155BatchValueCodec *abihandler.Codec -var etherVoucherCodec *abihandler.Codec var erc20VoucherCodec *abihandler.Codec var erc721VoucherCodec *abihandler.Codec var erc1155SingleVoucherCodec *abihandler.Codec @@ -245,7 +242,6 @@ func NewWalletApp() *WalletApp { erc1155BatchValueCodec = abihandler.NewCodec([]string{"uint256[]","uint256[]","bytes","bytes"}) // tokenIdList, amountList, base data, eec data - etherVoucherCodec = abihandler.NewVoucherCodec("withdrawEther",[]string{"address","uint256"}) // receiver, balance erc20VoucherCodec = abihandler.NewVoucherCodec("transfer",[]string{"address","uint256"}) // receiver, amount erc721VoucherCodec = abihandler.NewVoucherCodec("safeTransferFrom",[]string{"address","address","uint256"}) // sender, receiver, tokenId erc1155SingleVoucherCodec = abihandler.NewVoucherCodec("safeTransferFrom",[]string{"address","address","uint256","uint256","bytes"}) // sender, receiver, tokenId, amount, data @@ -306,110 +302,73 @@ func (w *WalletApp) UriHandler() *urihandler.UriHandler { func (w *WalletApp) SetupRoutes(routes []WalletRoute) { - var forceRelayRoute bool - var relayRouteAdded bool - for _, route := range routes { switch route { - case DappRelayAdvanceRoute: - relayRouteAdded = true - w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.DappAddressRelay, abihandler.NewPackedCodec([]string{"address"}), w.HandleRelay) case EtherCodecAdvanceRoutes: - forceRelayRoute = true w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.EtherPortalAddress, abihandler.NewPackedCodec([]string{"address","uint256","bytes"}), w.EtherPortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","EtherWithdraw",[]string{"uint256","bytes"}), w.EtherWithdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","EtherTransfer",[]string{"address","uint256","bytes"}), w.TransferEtherCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.EtherWithdraw",[]string{"uint256","bytes"}), w.EtherWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.EtherTransfer",[]string{"address","uint256","bytes"}), w.TransferEtherCodec) case DepositEtherAdvanceRoute: w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.EtherPortalAddress, abihandler.NewPackedCodec([]string{"address","uint256","bytes"}), w.EtherPortalDeposit) case Erc20CodecAdvanceRoutes: w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc20PortalAddress, abihandler.NewPackedCodec([]string{"bool","address","address","uint256","bytes"}), w.Erc20PortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc20Withdraw",[]string{"address","uint256","bytes"}), w.Erc20Withdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc20Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc20Codec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc20Withdraw",[]string{"address","uint256","bytes"}), w.Erc20Withdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc20Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc20Codec) case DepositErc20AdvanceRoute: - w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc20PortalAddress, abihandler.NewPackedCodec([]string{"bool","address","address","uint256","bytes"}), w.Erc20PortalDeposit) + w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc20PortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","bytes"}), w.Erc20PortalDeposit) case Erc721CodecAdvanceRoutes: - forceRelayRoute = true w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc721PortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","bytes"}), w.Erc721PortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc721Withdraw",[]string{"address","uint256","bytes"}), w.Erc721Withdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc721Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc721Codec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc721Withdraw",[]string{"address","uint256","bytes"}), w.Erc721Withdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc721Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc721Codec) case DepositErc721AdvanceRoute: w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc721PortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","bytes"}), w.Erc721PortalDeposit) case Erc1155CodecAdvanceRoutes: - forceRelayRoute = true w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155SinglePortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","uint256","bytes"}), w.Erc1155SinglePortalDeposit) w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155BatchPortalAddress, abihandler.NewPackedCodec([]string{"address","address","bytes"}), w.Erc1155BatchPortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) case Erc1155SingleCodecAdvanceRoutes: - forceRelayRoute = true w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155SinglePortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","uint256","bytes"}), w.Erc1155SinglePortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) case DepositErc1155SingleAdvanceRoute: w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155SinglePortalAddress, abihandler.NewPackedCodec([]string{"address","address","uint256","uint256","bytes"}), w.Erc1155SinglePortalDeposit) case Erc1155BatchCodecAdvanceRoutes: - forceRelayRoute = true w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155BatchPortalAddress, abihandler.NewPackedCodec([]string{"address","address","bytes"}), w.Erc1155BatchPortalDeposit) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) case DepositErc1155AdvanceRoute,DepositErc1155BatchAdvanceRoute: w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.Erc1155BatchPortalAddress, abihandler.NewPackedCodec([]string{"address","address","bytes"}), w.Erc1155BatchPortalDeposit) case WithdrawEtherAdvanceRoute,WithdrawEtherCodecAdvanceRoute: - forceRelayRoute = true - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","EtherWithdraw",[]string{"uint256","bytes"}), w.EtherWithdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.EtherWithdraw",[]string{"uint256","bytes"}), w.EtherWithdraw) case WithdrawErc20AdvanceRoute,WithdrawErc20CodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc20Withdraw",[]string{"address","uint256","bytes"}), w.Erc20Withdraw) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc20Withdraw",[]string{"address","uint256","bytes"}), w.Erc20Withdraw) case WithdrawErc721AdvanceRoute,WithdrawErc721CodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc721Withdraw",[]string{"address","uint256","bytes"}), w.Erc721Withdraw) - forceRelayRoute = true + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc721Withdraw",[]string{"address","uint256","bytes"}), w.Erc721Withdraw) case WithdrawErc1155SingleAdvanceRoute,WithdrawErc1155SingleCodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) - forceRelayRoute = true + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleWithdraw",[]string{"address","uint256","uint256","bytes"}), w.Erc1155SingleWithdraw) case WithdrawErc1155AdvanceRoute,WithdrawErc1155BatchAdvanceRoute,WithdrawErc1155BatchCodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) - forceRelayRoute = true + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchWithdraw",[]string{"address","uint256[]","uint256[]","bytes"}), w.Erc1155BatchWithdraw) case TransferEtherAdvanceRoute,TransferEtherCodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","EtherTransfer",[]string{"address","uint256","bytes"}), w.TransferEtherCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.EtherTransfer",[]string{"address","uint256","bytes"}), w.TransferEtherCodec) case TransferErc20AdvanceRoute,TransferErc20CodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc20Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc20Codec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc20Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc20Codec) case TransferErc721AdvanceRoute,TransferErc721CodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc721Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc721Codec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc721Transfer",[]string{"address","address","uint256","bytes"}), w.TransferErc721Codec) case TransferErc1155SingleAdvanceRoute,TransferErc1155SingleCodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155SingleTransfer",[]string{"address","address","uint256","uint256","bytes"}), w.TransferErc1155SingleCodec) case TransferErc1155AdvanceRoute,TransferErc1155BatchAdvanceRoute,TransferErc1155BatchCodecAdvanceRoute: - w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet","Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) + w.AbiHandler().HandleAdvanceRoute(abihandler.NewHeaderCodec("wallet.Erc1155BatchTransfer",[]string{"address","address","uint256[]","uint256[]","bytes"}), w.TransferErc1155BatchCodec) case BalanceInspectRoute,BalanceCodecInspectRoute: - w.AbiHandler().HandleInspectRoute(abihandler.NewHeaderCodec("wallet","Balance",[]string{"address address"}), w.BalanceAbi) + w.AbiHandler().HandleInspectRoute(abihandler.NewHeaderCodec("wallet.Balance",[]string{"address address"}), w.BalanceAbi) case BalanceUriInspectRoute: w.UriHandler().HandleInspectRoute("/balance/:address", w.BalanceUri) default: panic("Unrecognized route") } } - if forceRelayRoute && !relayRouteAdded { - w.AbiHandler().HandleFixedAddressAdvance(hdl.RollupsAddresses.DappAddressRelay, abihandler.NewPackedCodec([]string{"address"}), w.HandleRelay) - } -} - -// -// Relay -// - -func (w *WalletApp) HandleRelay(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { - addr, ok1 := payloadMap["0"].(abihandler.Address) - - if !ok1 { - message := "HandleRelay: parameters error" - return fmt.Errorf(message) - } - - w.DappAddress = addr - - if w.handler.LogLevel >= hdl.Debug {DebugLogger.Println("Dapp Relay, dapp address is", addr)} - - return nil } // @@ -452,13 +411,12 @@ func (w *WalletApp) EtherPortalDeposit(metadata *rollups.Metadata, payloadMap ma func (w *WalletApp) Erc20PortalDeposit(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { - // ret, ok1 := payloadMap["0"].(bool) - tokenAddress, ok2 := payloadMap["1"].(abihandler.Address) - depositor, ok3 := payloadMap["2"].(abihandler.Address) - amount, ok4 := payloadMap["3"].(*big.Int) - // dataBytes, ok5 := payloadMap["4"].([]byte) + tokenAddress, ok1 := payloadMap["0"].(abihandler.Address) + depositor, ok2 := payloadMap["1"].(abihandler.Address) + amount, ok3 := payloadMap["2"].(*big.Int) + // dataBytes, ok4 := payloadMap["3"].([]byte) - if !ok2 || !ok3 || !ok4 { + if !ok1 || !ok2 || !ok3 { message := "Erc20PortalDeposit: parameters error" return fmt.Errorf(message) } @@ -615,10 +573,6 @@ func (w *WalletApp) Erc1155BatchPortalDeposit(metadata *rollups.Metadata, payloa // func (w *WalletApp) EtherWithdraw(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { - if w.DappAddress == (abihandler.Address{}) { - return fmt.Errorf("EtherWithdraw: Can not generate voucher as there is no dapp address configured") - } - if w.handler.LogLevel >= hdl.Debug {DebugLogger.Println("EtherWithdraw: payload:",payloadMap)} amount, ok1 := payloadMap["0"].(*big.Int) dataBytes, ok2 := payloadMap["1"].([]byte) @@ -642,13 +596,9 @@ func (w *WalletApp) EtherWithdraw(metadata *rollups.Metadata, payloadMap map[str } // Voucher - voucherPayload,err := etherVoucherCodec.Encode([]interface{}{addr,amount}) - if err != nil { - return fmt.Errorf("EtherWithdraw: encoding voucher: %s", err) - } - _, err = w.handler.SendVoucher(w.DappAddress.String(),voucherPayload) + _, err = w.handler.SendVoucher(addr.String(),"0x",amount) if err != nil { - return fmt.Errorf("EtherPortalDeposit: error making http request: %s", err) + return fmt.Errorf("EtherWithdraw: error making http request: %s", err) } // Notice @@ -695,7 +645,7 @@ func (w *WalletApp) Erc20Withdraw(metadata *rollups.Metadata, payloadMap map[str if err != nil { return fmt.Errorf("Erc20Withdraw: encoding voucher: %s", err) } - _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload) + _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload,nil) if err != nil { return fmt.Errorf("Erc20Withdraw: error making http request: %s", err) } @@ -718,10 +668,6 @@ func (w *WalletApp) Erc20Withdraw(metadata *rollups.Metadata, payloadMap map[str func (w *WalletApp) Erc721Withdraw(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { if w.handler.LogLevel >= hdl.Debug {DebugLogger.Println("Erc721Withdraw: payload:",payloadMap)} - if w.DappAddress == (abihandler.Address{}) { - return fmt.Errorf("Erc721Withdraw: Can not generate voucher as there is no dapp address configured") - } - tokenAddress, ok1 := payloadMap["0"].(abihandler.Address) tokenId, ok2 := payloadMap["1"].(*big.Int) dataBytes, ok3 := payloadMap["2"].([]byte) @@ -745,11 +691,11 @@ func (w *WalletApp) Erc721Withdraw(metadata *rollups.Metadata, payloadMap map[st } // Voucher - voucherPayload,err := erc721VoucherCodec.Encode([]interface{}{w.DappAddress,addr,tokenId}) + voucherPayload,err := erc721VoucherCodec.Encode([]interface{}{metadata.AppContract,addr,tokenId}) if err != nil { return fmt.Errorf("Erc721Withdraw: encoding voucher: %s", err) } - _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload) + _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload,nil) if err != nil { return fmt.Errorf("Erc721Withdraw: error making http request: %s", err) } @@ -772,10 +718,6 @@ func (w *WalletApp) Erc721Withdraw(metadata *rollups.Metadata, payloadMap map[st func (w *WalletApp) Erc1155SingleWithdraw(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { if w.handler.LogLevel >= hdl.Debug {DebugLogger.Println("Erc1155SingleWithdraw: payload:",payloadMap)} - if w.DappAddress == (abihandler.Address{}) { - return fmt.Errorf("Erc1155SingleWithdraw: Can not generate voucher as there is no dapp address configured") - } - tokenAddress, ok1 := payloadMap["0"].(abihandler.Address) tokenId, ok2 := payloadMap["1"].(*big.Int) amount, ok3 := payloadMap["2"].(*big.Int) @@ -800,11 +742,11 @@ func (w *WalletApp) Erc1155SingleWithdraw(metadata *rollups.Metadata, payloadMap } // Voucher - voucherPayload,err := erc1155SingleVoucherCodec.Encode([]interface{}{w.DappAddress,addr,tokenId,amount,[]byte{}}) + voucherPayload,err := erc1155SingleVoucherCodec.Encode([]interface{}{metadata.AppContract,addr,tokenId,amount,[]byte{}}) if err != nil { return fmt.Errorf("Erc1155SingleWithdraw: encoding voucher: %s", err) } - _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload) + _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload,nil) if err != nil { return fmt.Errorf("Erc1155SingleWithdraw: error making http request: %s", err) } @@ -828,10 +770,6 @@ func (w *WalletApp) Erc1155SingleWithdraw(metadata *rollups.Metadata, payloadMap func (w *WalletApp) Erc1155BatchWithdraw(metadata *rollups.Metadata, payloadMap map[string]interface{}) error { if w.handler.LogLevel >= hdl.Debug {DebugLogger.Println("Erc1155SingleWithdraw: payload:",payloadMap)} - if w.DappAddress == (abihandler.Address{}) { - return fmt.Errorf("Erc1155BatchWithdraw: Can not generate voucher as there is no dapp address configured") - } - tokenAddress, ok1 := payloadMap["0"].(abihandler.Address) tokenIds, ok2 := payloadMap["1"].([]*big.Int) amounts, ok3 := payloadMap["2"].([]*big.Int) @@ -843,7 +781,7 @@ func (w *WalletApp) Erc1155BatchWithdraw(metadata *rollups.Metadata, payloadMap } if len(tokenIds) != len(amounts) { - message := "Erc1155BatchPortalDeposit: parameters error" + message := "Erc1155BatchWithdraw: parameters error" return fmt.Errorf(message) } @@ -861,18 +799,18 @@ func (w *WalletApp) Erc1155BatchWithdraw(metadata *rollups.Metadata, payloadMap for i := 0 ; i < numTokens ; i++ { err = wallet.WithdrawErc1155(tokenAddress, tokenIds[i], amounts[i]) if err != nil { - return fmt.Errorf("Erc1155BatchPortalDeposit: error adding id: %s", err) + return fmt.Errorf("Erc1155BatchWithdraw: error adding id: %s", err) } negIds = append(negIds,new(big.Int).Neg(tokenIds[i])) negAmounts = append(negAmounts,new(big.Int).Neg(amounts[i])) } // Voucher - voucherPayload,err := erc1155BatchVoucherCodec.Encode([]interface{}{w.DappAddress,addr,tokenIds,amounts,[]byte{}}) + voucherPayload,err := erc1155BatchVoucherCodec.Encode([]interface{}{metadata.AppContract,addr,tokenIds,amounts,[]byte{}}) if err != nil { return fmt.Errorf("Erc1155BatchWithdraw: encoding voucher: %s", err) } - _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload) + _, err = w.handler.SendVoucher(tokenAddress.String(),voucherPayload,nil) if err != nil { return fmt.Errorf("Erc1155BatchWithdraw: error making http request: %s", err) } @@ -1136,7 +1074,7 @@ func (w *WalletApp) TransferErc1155Batch(tokenAddress abihandler.Address, sender for i := 0 ; i < numTokens ; i++ { err := walletReceiver.DepositErc1155(tokenAddress, tokenIds[i], amounts[i]) if err != nil { - return fmt.Errorf("Erc1155BatchPortalDeposit: error adding id: %s", err) + return fmt.Errorf("TransferErc1155: error adding id: %s", err) } }