Skip to content

Commit

Permalink
prepare for multiple subscriptions and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Oct 25, 2023
1 parent 4162ba4 commit 3104000
Show file tree
Hide file tree
Showing 5 changed files with 644 additions and 147 deletions.
31 changes: 31 additions & 0 deletions integration/obscurogateway/events_contract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
// Specify the Solidity version
pragma solidity ^0.8.0;

contract SimpleMessageContract {

// State variable to store the message
string public message;
string public message2;

// Event declaration
event MessageUpdatedWithAddress(string newMessage, address indexed sender);
event Message2Updated(string newMessage);

// Constructor to initialize the message
constructor() {
message = "foo";
message2 = "foo";
}

// Function to set a new message
function setMessage(string memory newMessage) public {
message = newMessage;
emit MessageUpdatedWithAddress(newMessage, msg.sender); // Emit the event (only sender can see it)
}

function setMessage2(string memory newMessage) public {
message2 = newMessage;
emit Message2Updated(newMessage); // Emit the event (everyone can see it)
}
}
121 changes: 121 additions & 0 deletions integration/obscurogateway/gateway_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package faucet

import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/obscuronet/go-obscuro/go/wallet"
"github.com/valyala/fasthttp"
)

type GatewayUser struct {
UserID string
Wallets []wallet.Wallet
HTTPClient *ethclient.Client
WSClient *ethclient.Client
ServerAddressHTTP string
ServerAddressWS string
}

func NewUser(wallets []wallet.Wallet, serverAddressHTTP string, serverAddressWS string) (*GatewayUser, error) {
// automatically join OG
userID, err := joinObscuroGateway(serverAddressHTTP)
if err != nil {
return nil, err
}

// create clients
httpClient, err := ethclient.Dial(serverAddressHTTP + "/v1/" + "?u=" + userID)
if err != nil {
return nil, err
}
wsClient, err := ethclient.Dial(serverAddressWS + "/v1/" + "?u=" + userID)
if err != nil {
return nil, err
}

return &GatewayUser{
UserID: userID,
Wallets: wallets,
HTTPClient: httpClient,
WSClient: wsClient,
ServerAddressHTTP: serverAddressHTTP,
ServerAddressWS: serverAddressWS,
}, nil
}

func (u GatewayUser) RegisterAccounts() error {
for _, w := range u.Wallets {
response, err := registerAccount(u.ServerAddressHTTP, u.UserID, w.PrivateKey(), w.Address().Hex())
if err != nil {
return err
}
fmt.Printf("Successfully registered address %s for user: %s with response: %s \n", w.Address().Hex(), u.UserID, response)
}

return nil
}

func (u GatewayUser) PrintUserAccountsBalances() error {
for _, w := range u.Wallets {
balance, err := u.HTTPClient.BalanceAt(context.Background(), w.Address(), nil)
if err != nil {
return err
}
fmt.Println("Balance for account ", w.Address().Hex(), " - ", balance.String())
}
return nil
}

func registerAccount(url string, userID string, pk *ecdsa.PrivateKey, hexAddress string) ([]byte, error) {
payload := prepareRegisterPayload(userID, pk, hexAddress)

req, err := http.NewRequestWithContext(
context.Background(),
http.MethodPost,
url+"/v1/authenticate/?u="+userID,
strings.NewReader(payload),
)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")

client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return nil, err
}

defer response.Body.Close()
return io.ReadAll(response.Body)
}

func prepareRegisterPayload(userID string, pk *ecdsa.PrivateKey, hexAddress string) string {
message := fmt.Sprintf("Register %s for %s", userID, strings.ToLower(hexAddress))
prefixedMessage := fmt.Sprintf("\u0019Ethereum Signed Message:\n%d%s", len(message), message)
messageHash := crypto.Keccak256([]byte(prefixedMessage))
sig, err := crypto.Sign(messageHash, pk)
if err != nil {
fmt.Printf("Failed to sign message: %v\n", err)
}
sig[64] += 27
signature := "0x" + hex.EncodeToString(sig)
payload := fmt.Sprintf("{\"signature\": \"%s\", \"message\": \"%s\"}", signature, message)
return payload
}

func joinObscuroGateway(url string) (string, error) {
statusCode, userID, err := fasthttp.Get(nil, fmt.Sprintf("%s/v1/join/", url))
if err != nil || statusCode != 200 {
return "", fmt.Errorf(fmt.Sprintf("Failed to get userID. Status code: %d, err: %s", statusCode, err))
}
return string(userID), nil
}
Loading

0 comments on commit 3104000

Please sign in to comment.