Skip to content

Commit

Permalink
working hardcoded connect code
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 14, 2024
1 parent df785a6 commit 6c351a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
10 changes: 7 additions & 3 deletions app/services/engine/v1/handlers/gamegrp/gamegrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type handlers struct {

// connect is used to return a game token for API usage.
func (h *handlers) connect(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
address, err := validateSignature(r, h.connectTimeout)
address, err := validateSignature(ctx, h.log, r, h.connectTimeout)
if err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
}
Expand Down Expand Up @@ -473,7 +473,7 @@ func (h *handlers) updateOut(ctx context.Context, w http.ResponseWriter, r *http
return h.state(ctx, w, r)
}

func validateSignature(r *http.Request, timeout time.Duration) (string, error) {
func validateSignature(ctx context.Context, log *logger.Logger, r *http.Request, timeout time.Duration) (string, error) {
var dt struct {
Address string `json:"address"`
DateTime string `json:"dateTime"` // YYYYMMDDHHMMSS
Expand All @@ -489,8 +489,10 @@ func validateSignature(r *http.Request, timeout time.Duration) (string, error) {
return "", fmt.Errorf("parse time: %w", err)
}

log.Info(ctx, "validate signature", "datetime", dt.DateTime, "address", dt.Address, "signature", dt.Signature, "parsedDT", t.Format("20060102150405"))

if d := time.Since(t); d > timeout {
return "", fmt.Errorf("data is too old, %v", d.Seconds())
return "", fmt.Errorf("data is too old, %v utc now, %v seconds passed, %v seconds timeout", time.Now().UTC().Format("20060102150405"), d.Seconds(), timeout.Seconds())
}

data := struct {
Expand All @@ -506,6 +508,8 @@ func validateSignature(r *http.Request, timeout time.Duration) (string, error) {
return "", fmt.Errorf("unable to extract address: %w", err)
}

log.Info(ctx, "validate signature", "calc address", address, "recv address", dt.Address)

if !strings.EqualFold(strings.ToLower(address), strings.ToLower(data.Address)) {
return "", fmt.Errorf("invalid address match, got[%s] exp[%s]", address, data.Address)
}
Expand Down
77 changes: 73 additions & 4 deletions app/services/ui/assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,56 @@
<head>
<title>Testing Metamask Wallet</title>
<script src="https://c0f4f41c-2f55-4863-921b-sdk-docs.github.io/cdn/metamask-sdk.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
function currentDateTime() {
const dt = new Date();

const year = dt.getUTCFullYear();
const month = String(dt.getUTCMonth() + 1).padStart(2, '0'); // Month (0-indexed)
const day = String(dt.getUTCDate()).padStart(2, '0');
const hours = String(dt.getUTCHours()).padStart(2, '0');
const minutes = String(dt.getUTCMinutes()).padStart(2, '0');
const seconds = String(dt.getUTCSeconds()).padStart(2, '0');

return `${year}${month}${day}${hours}${minutes}${seconds}`;
}
</script>
<script>
// Setting some ajax specific global settings.
$.ajaxSetup({
contentType: "application/json; charset=utf-8",
});

// handleAjaxError is a helper function for handling the response from any
// ajax request that is made.
function handleAjaxError(jqXHR, exception) {
var msg = '';

switch (jqXHR.status) {
case 0:
msg = 'Not connected, verify network.';
case 404:
msg = 'Requested page not found. [404]';
case 500:
msg = 'Internal Server Error [500].';
default:
switch (exception) {
case "parsererror":
msg = 'Requested JSON parse failed.';
case "timeout":
msg = 'Time out error.';
case "abort":
msg = 'Ajax request aborted.';
default:
const o = JSON.parse(jqXHR.responseText);
msg = o.error;
}
}

console.log(msg);
}
</script>
<script>
const sdk = new MetaMaskSDK.MetaMaskSDK({
dappMetadata: {
Expand Down Expand Up @@ -73,25 +123,44 @@
.then((res) => console.log('accounts', res))
.catch((e) => console.log('accounts ERR', e));
}


var signed;
var dateTime;

function personalSign() {
dateTime = currentDateTime();
ethereum
.request({
method: 'personal_sign',
params: [
hexer("hello world"),
hexer(`{"address":"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd","dateTime":"${dateTime}"}`),
"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd"
],
})
.then((res) => console.log('personal sign', res))
.then((res) => { signed=res; console.log('personal sign', res) })
.catch((e) => console.log('personal sign ERR', e));
}
}

function connectGE() {
$.ajax({
type: "post",
url: "http://0.0.0.0:3000/v1/game/connect",
data: `{"address":"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd","dateTime":"${dateTime}","sig":"${signed}"}`,
success: function (response) {
console.log(response);
},
error: function (jqXHR, exception) {
console.log(exception);
},
});
}
</script>
</head>
<body>
<h1>Testing Metamask Wallet 4</h1>
<button onclick="connect()">Connect</button>
<button onclick="accounts()">Accounts</button>
<button onclick="personalSign()">Sign Data</button>
<button onclick="connectGE()">Conn GE</button>
</body>
</html>

0 comments on commit 6c351a1

Please sign in to comment.