Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients/go: Bump oasis-core to 23.0 #278

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20.x"
go-version: "1.22.x"
# Always run this step so that all linting errors can be seen at once.
if: always()
- name: Lint Go
uses: golangci/golangci-lint-action@v4
with:
version: v1.51.2
version: v1.56.2
working-directory: ./clients/go
# Always run this step so that all linting errors can be seen at once.
if: always()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.20.x"
go-version: "1.22.x"

- name: Test
run: go test -v ./...
Expand Down
9 changes: 9 additions & 0 deletions clients/go/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ linters-settings:
# Enable once suggested fixes are shown: https://github.com/golangci/golangci-lint/issues/2134
#enable:
# - fieldalignment
depguard:
rules:
main:
files:
- $all
allow:
- $gostd
- github.com/oasisprotocol
- github.com/ethereum/go-ethereum

linters:
disable-all: true
Expand Down
89 changes: 61 additions & 28 deletions clients/go/README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,100 @@
# Sapphire ParaTime Compat Lib

[@oasisprotocol/sapphire-paratime] makes it easy to port your dapp to the [Sapphire ParaTime].
You can port over a Go Ethereum application by using a `sapphire.WrappedBackend`
or by packing native Ethereum transactions Sapphire style.
[@oasisprotocol/sapphire-paratime] makes it easy to port your dapp to the
[Sapphire ParaTime]. You can port over a Go Ethereum application by using a
`sapphire.WrappedBackend` or by packing native Ethereum transactions Sapphire
style.

[@oasisprotocol/sapphire-paratime]: https://pkg.go.dev/github.com/oasisprotocol/sapphire-paratime/go/
[sapphire paratime]: https://docs.oasis.io/general/developer-resources/sapphire-paratime/

_If your dapp doesn't port in under 10 minutes, it's a bug!_

If you have more than a little trouble, please file an issue. There should be
_no_ reason _not_ to use the Sapphire ParaTime!
[Sapphire ParaTime]: https://docs.oasis.io/dapp/sapphire

## Building

Sapphire Paratime compatibility library works with `Go` version 1.17 or later and the latest comparable `go-ethereum` version.
Sapphire compatibility library works with Go version 1.22 or later and the
latest compatible `go-ethereum` version.

To build and test locally, import the `Go` package.
To build and test locally:

```shell
go test
```

## Usage

### Import

```go
import (
"context"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/ethclient"

sapphire "github.com/oasisprotocol/sapphire-paratime/clients/go"
)
```

### Go-Ethereum ABI

After [generating](https://geth.ethereum.org/docs/dapp/abigen) the Go bindings
for a particular Solidity contract, you can instantiate an Ethereum client with the
Sapphire Paratime gateway URL and instantiate a `sapphire.WrappedBackend` as a drop in
replacement.
for a particular Solidity contract, you can instantiate an Ethereum client with
the Sapphire gateway URL and instantiate a `sapphire.WrappedBackend` as a drop
in replacement:

```Go
```go
// key := private key
c, _ := ethclient.Dial(sapphire.Networks[SapphireChainID.Uint64()].DefaultGateway)
backend := sapphire.WrapClient(*c, func(digest [32]byte)([]byte, error) {
client, _ := ethclient.Dial(sapphire.Networks[SapphireChainID.Uint64()].DefaultGateway)
backend, _ := sapphire.WrapClient(client, func(digest [32]byte)([]byte, error) {
// Pass in a custom signing function to interact with the signer
return crypto.Sign(digest[:], key)
})
```

Contracts using `go-ethereum`'s `abigen` can be used by passing in `backend` instead of the usual `ethclient`. For example,
Contracts using `go-ethereum`'s `abigen` can now be used by passing in `backend`
instead of the usual `ethclient.Client` instance:

```Go
txOpts := backend.Transactor(senderAddr)
```go
nft, _ := NewNft(addr, backend)
```

Confidential transactions using Go-Ethereum ABI wrapper can be submitted by
passing the Sapphire-specific `bind.TransactOpts` as the first parameter:
matevz marked this conversation as resolved.
Show resolved Hide resolved

```go
txOpts := backend.Transactor(senderAddr)
tx, _ := nft.Transfer(txOpts, tokenId, recipient)
receipt, _ := bind.WaitMined(context.Background(), client, tx)
```

**WARNING:** If you forget to pass `txOpts` as described above, your transaction
**will be sent in plain-text**!

Confidential queries signed with your account's key are also supported on the
Sapphire-wrapped contract above, if you pass the `bind.CallOpts` defining your
`From` address:

```go
balance := nft.BalanceOf(&bind.CallOpts{From: "0xYOUR_ADDRESS"}, common.HexToAddress("0xDce075E1C39b1ae0b75D554558b6451A226ffe00"))
```

### Bring Your Own Signer

You can also package an Ethereum transaction for Sapphire by:
You can also package an existing Ethereum transaction for Sapphire by:

```Go
sapphireTestnetChainId := 0x5aff // Sapphire testnet
```go
sapphireTestnetChainId := 0x5aff // Sapphire Testnet.
packedTx := sapphire.PackTx(tx, sapphire.NewCipher(sapphireTestnetChainId))
signedTx := sign(packedTx) // using your usual signer
signedTx := sign(packedTx) // Using your usual signer.
```

and sending it with a normal, not-wrapped `ethclient`:
and sending it with a normal, not-wrapped `ethclient.Client` instance:

```Go
ethclient.SendTransaction(ctx, signedTx)
```go
_ = c.SendTransaction(ctx, signedTx)
```

## See Also

- [Oasis Testnet Faucet](https://faucet.testnet.oasis.dev/)
- [Oasis Testnet Faucet](https://faucet.testnet.oasis.io/)
- [Creating dapps for Sapphire](https://docs.oasis.io/dapp/sapphire/quickstart)
- [How to Transfer ROSE into an EVM ParaTime](https://docs.oasis.io/general/manage-tokens/how-to-transfer-rose-into-paratime/)
Loading
Loading