Skip to content

Commit

Permalink
Merge branch 'main' into ZigaMr/sapphirepy/signed-queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigaMr authored Oct 10, 2024
2 parents 530961d + f708f39 commit c6707d1
Show file tree
Hide file tree
Showing 55 changed files with 16,839 additions and 16,111 deletions.
27 changes: 23 additions & 4 deletions .github/workflows/ci-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,38 @@ jobs:
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm --filter @oasisprotocol/sapphire-paratime install
run: pnpm install

- name: Build docs
- name: Build client/js docs
run: |
mkdir ../../docs-api
pnpm build # Required for integrations/
pnpm typedoc
mv docs/api ../../docs-api/sapphire-paratime
- name: Build integrations docs
working-directory: ./integrations
run: |
for i in */ ; do
pushd $i
pnpm build # Required for typedoc
if [ "$i" == "hardhat/" ]; then
# XXX: ExampleModule.ts is not compilable but is there for linting and example.
pnpm typedoc --tsconfig tsconfig.build.json
else
pnpm typedoc
fi
mv docs/api ../../docs-api/sapphire-$i
popd
done
- name: Deploy to api-reference branch
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: clients/js/docs/api
publish_dir: docs-api
publish_branch: api-reference
destination_dir: js/sapphire-paratime
destination_dir: js
commit_message: Deploy js API reference ${{ github.event.head_commit.message }}
user_name: github-actions[bot]
user_email: 41898282+github-actions[bot]@users.noreply.github.com
Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/ci-test-go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: ci-test

on:
push:
branches:
- main
- stable/*
- rc/*
pull_request:
branches:
- main
- stable/*
- rc/*

jobs:
test-client-go:
name: test-client-go
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./clients/go
services:
sapphire-localnet-ci:
image: ghcr.io/oasisprotocol/sapphire-localnet:latest
ports:
- 8545:8545
- 8546:8546
env:
OASIS_DEPOSIT_BINARY: /oasis-deposit -test-mnemonic -n 2
options: >-
--rm
--health-cmd="test -f /CONTAINER_READY"
--health-start-period=90s
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.22.x"

- name: Test
run: go test -v ./...
1 change: 1 addition & 0 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ jobs:
- name: Run tests
run: |
pytest /sapphirepy/tests/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ visible on the docs website:
3. Merge changes into Oasis docs repository `main` branch. CI will deploy the
docs to the website automatically.

Note: If you want to introduce a new markdown file, don't forget to add
it to the [Oasis documentation's sidebar](https://github.com/oasisprotocol/docs/blob/main/sidebarDapp.js).
If you remove any chapters, don't forget to define sensible [redirects](https://github.com/oasisprotocol/docs/blob/main/redirects.js).
Note: If you want to introduce a new markdown file, you will need to add it to
the [Oasis documentation's sidebar](https://github.com/oasisprotocol/docs/blob/main/sidebarDapp.ts).
If you remove any chapters, don't forget to define sensible [redirects](https://github.com/oasisprotocol/docs/blob/main/redirects.ts).
For more info on how to write the Oasis documentation, manage images and
diagrams, reference cross-repo markdown files and similar consult the
[official README](https://github.com/oasisprotocol/docs/blob/main/README.md).
Expand Down
41 changes: 6 additions & 35 deletions clients/go/cipher.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package sapphire

import (
"bytes"
"context"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/oasisprotocol/curve25519-voi/primitives/x25519"
"github.com/oasisprotocol/deoxysii"
Expand Down Expand Up @@ -262,44 +260,17 @@ func (c X25519DeoxysIICipher) DecryptEncoded(response []byte) ([]byte, error) {
}

// GetRuntimePublicKey fetches the runtime calldata public key from the default Sapphire gateway.
func GetRuntimePublicKey(chainID uint64) (*x25519.PublicKey, uint64, error) {
network, exists := Networks[chainID]
if !exists {
return nil, 0, fmt.Errorf("could not fetch public key for network with chain id %d", chainID)
}
request := Request{
Version: "2.0",
Method: "oasis_callDataPublicKey",
ID: 1,
}
rawReq, _ := json.Marshal(request)

req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, network.DefaultGateway, bytes.NewBuffer(rawReq))
if err != nil {
return nil, 0, fmt.Errorf("failed to create request for runtime calldata public key: %w", err)
}
req.Header.Set("Content-Type", "application/json")

client := http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, 0, fmt.Errorf("failed to request runtime calldata public key: %w", err)
}

decoder := json.NewDecoder(res.Body)
rpcRes := new(Response)
if err := decoder.Decode(&rpcRes); err != nil {
return nil, 0, fmt.Errorf("unexpected response to request for runtime calldata public key: %w", err)
}
res.Body.Close()

func GetRuntimePublicKey(c *ethclient.Client) (*x25519.PublicKey, uint64, error) {
var pubKey CallDataPublicKey
if err := json.Unmarshal(rpcRes.Result, &pubKey); err != nil {

if err := c.Client().Call(&pubKey, "oasis_callDataPublicKey"); err != nil {
return nil, 0, fmt.Errorf("invalid response when fetching runtime calldata public key: %w", err)
}

if len(pubKey.PublicKey) != x25519.PublicKeySize {
return nil, 0, fmt.Errorf("invalid public key length")
}

return (*x25519.PublicKey)(pubKey.PublicKey), pubKey.Epoch, nil
}

Expand Down
6 changes: 3 additions & 3 deletions clients/go/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ type WrappedBackend struct {
//
// If you use cipher over a longer period of time, you should create a new
// cipher instance every epoch to refresh the ParaTime's ephemeral key!
func NewCipher(chainID uint64) (Cipher, error) {
runtimePublicKey, epoch, err := GetRuntimePublicKey(chainID)
func NewCipher(c *ethclient.Client) (Cipher, error) {
runtimePublicKey, epoch, err := GetRuntimePublicKey(c)
if err != nil {
return nil, fmt.Errorf("failed to fetch runtime callata public key: %w", err)
}
Expand All @@ -151,7 +151,7 @@ func WrapClient(c *ethclient.Client, sign SignerFn) (*WrappedBackend, error) {
if err != nil {
return nil, fmt.Errorf("failed to fetch chain ID: %w", err)
}
cipher, err := NewCipher(chainID.Uint64())
cipher, err := NewCipher(c)
if err != nil {
return nil, err
}
Expand Down
62 changes: 62 additions & 0 deletions clients/go/compat_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package sapphire

import (
"context"
"encoding/base64"
"encoding/json"
"log"
"math/big"
"testing"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/evm"

"github.com/ethereum/go-ethereum/ethclient"
)

func TestPackSignedCall(t *testing.T) {
Expand Down Expand Up @@ -89,3 +95,59 @@ func TestPackSignedCall(t *testing.T) {
t.Fatalf("err innerdata leash mismatch: expected %s got %s", leashOrig, leashDecoded)
}
}

func TestDial(t *testing.T) {
key, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
if err != nil {
log.Fatal(err)
}
addr1 := crypto.PubkeyToAddress(key.PublicKey)

key2, err := crypto.GenerateKey()
if err != nil {
log.Fatal(err)
}
addr2 := crypto.PubkeyToAddress(key2.PublicKey)

SapphireChainID := uint64(0x5afd)
client, _ := ethclient.Dial(Networks[SapphireChainID].DefaultGateway)
backend, err := WrapClient(client, func(digest [32]byte) ([]byte, error) {
// Pass in a custom signing function to interact with the signer
return crypto.Sign(digest[:], key)
})
if err != nil {
t.Fatalf("failed to wrap client %v", err)
}

nonce, err := client.PendingNonceAt(context.Background(), addr1)
if err != nil {
t.Fatalf("failed to get pending nonce %v", err)
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
t.Fatalf("failed to get gas price %v", err)
}

txOpts := backend.Transactor(addr1)

tx := types.NewTransaction(nonce, addr2, big.NewInt(1), uint64(100000), gasPrice, nil)
signedTx, err := txOpts.Signer(addr1, tx)
if err != nil {
t.Fatalf("failed to sign transaction %v", err)
}

err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
t.Fatalf("failed to send transaction %v", err)
}

receipt, err := bind.WaitMined(context.Background(), client, signedTx)
if err != nil {
t.Fatalf("transaction failed! %v", err)
}

if receipt.Status != uint64(1) {
t.Fatalf("transaction failed! (status=%v)", receipt.Status)
}
}
8 changes: 8 additions & 0 deletions clients/js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is inspired by [Keep a Changelog].

[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/

## 2.0.1 (2024-09)

https://github.com/oasisprotocol/sapphire-paratime/milestone/2?closed=1

### Fixed

- Use `core.CallDataPublicKey` subcall, instead of `oasis_callDataPublicKey` RPC call

## 2.0.0-next.1 (2024-08)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion clients/js/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NPM ?= pnpm

all: build test lint

build test lint::
build test lint format::
$(NPM) $@

clean:
Expand Down
4 changes: 2 additions & 2 deletions clients/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "module",
"name": "@oasisprotocol/sapphire-paratime",
"license": "Apache-2.0",
"version": "2.0.0-next.3",
"version": "2.0.1",
"description": "The Sapphire ParaTime Web3 integration library.",
"homepage": "https://github.com/oasisprotocol/sapphire-paratime/tree/main/clients/js",
"repository": {
Expand Down Expand Up @@ -60,7 +60,7 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"tweetnacl": "^1.0.3",
"typedoc": "^0.25.4",
"typedoc": "^0.25.13",
"typescript": "^4.8.3"
}
}
Loading

0 comments on commit c6707d1

Please sign in to comment.