-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare for multiple subscriptions and add test
- Loading branch information
Showing
9 changed files
with
762 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.