Skip to content

Commit

Permalink
working again with small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ardan-bkennedy committed Mar 15, 2024
1 parent 40973ea commit ad18754
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 15 deletions.
25 changes: 20 additions & 5 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(ctx, h.log, r, h.connectTimeout)
address, err := validateSignature(ctx, h.log, r, h.connectTimeout, h.bank.Client().ChainID())
if err != nil {
return v1.NewTrustedError(err, http.StatusBadRequest)
}
Expand Down Expand Up @@ -156,12 +156,20 @@ func (h *handlers) events(ctx context.Context, w http.ResponseWriter, r *http.Re

// configuration returns the basic configuration the front end needs to use.
func (h *handlers) configuration(ctx context.Context, w http.ResponseWriter, r *http.Request) error {

// TODO: This is a Hack right now since this namespace doesn't exist
// for the client.
network := h.bank.Client().Network()
if strings.Contains(network, "geth-service.liars-system.svc.cluster.local") {
network = "http://localhost:8545"
}

info := struct {
Network string `json:"network"`
ChainID int `json:"chainId"`
ContractID common.Address `json:"contractId"`
}{
Network: h.bank.Client().Network(),
Network: network,
ChainID: h.bank.Client().ChainID(),
ContractID: h.bank.ContractID(),
}
Expand Down Expand Up @@ -473,9 +481,10 @@ func (h *handlers) updateOut(ctx context.Context, w http.ResponseWriter, r *http
return h.state(ctx, w, r)
}

func validateSignature(ctx context.Context, log *logger.Logger, r *http.Request, timeout time.Duration) (string, error) {
func validateSignature(ctx context.Context, log *logger.Logger, r *http.Request, timeout time.Duration, chainID int) (string, error) {
var dt struct {
Address string `json:"address"`
ChainID int `json:"chainId"`
DateTime string `json:"dateTime"` // YYYYMMDDHHMMSS
Signature string `json:"sig"`
}
Expand All @@ -489,17 +498,23 @@ func validateSignature(ctx context.Context, log *logger.Logger, r *http.Request,
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"))
log.Info(ctx, "validate signature", "datetime", "curtime", time.Now().UTC().Format("20060102150405"), dt.DateTime, "address", dt.Address, "signature", dt.Signature)

if d := time.Since(t); d > timeout {
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())
return "", fmt.Errorf("data is too old, %v seconds passed > %v seconds timeout", d.Seconds(), timeout.Seconds())
}

if dt.ChainID != chainID {
return "", fmt.Errorf("invalid chain id, got %d, exp %d", dt.ChainID, chainID)
}

data := struct {
Address string `json:"address"`
ChainID int `json:"chainId"`
DateTime string `json:"dateTime"`
}{
Address: dt.Address,
ChainID: dt.ChainID,
DateTime: dt.DateTime,
}

Expand Down
70 changes: 60 additions & 10 deletions app/services/ui/assets/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,38 @@
}
</script>
<script>
var chainId;

function config() {
$.ajax({
type: "get",
url: "http://0.0.0.0:3000/v1/game/config",
success: function (res) {
console.log(res);
chainId = res.chainId;
$("#config").text(JSON.stringify(res));
},
error: function (jqXHR, exception) {
console.log(exception);
$("#config").text(exception);
},
});
}

function connect() {
ethereum
.request({
method: 'eth_requestAccounts',
params: [],
})
.then((res) => console.log('request accounts', res))
.catch((e) => console.log('request accounts ERR', e));
.then((res) => {
console.log('request accounts', res);
$("#connect").text(JSON.stringify(res));
})
.catch((e) => {
console.log('request accounts ERR', e)
$("#connect").text(e);
});
}

function accounts() {
Expand All @@ -120,47 +144,73 @@
method: 'eth_accounts',
params: [],
})
.then((res) => console.log('accounts', res))
.catch((e) => console.log('accounts ERR', e));
.then((res) => {
console.log('accounts', res);
$("#sign").text(JSON.stringify(res));
})
.catch((e) => {
console.log('accounts ERR', e)
$("#sign").text(e);
});
}

var signed;
var dateTime;

function personalSign() {
dateTime = currentDateTime();
const data = `{"address":"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd","chainId":${chainId},"dateTime":"${dateTime}"}`;

ethereum
.request({
method: 'personal_sign',
params: [
hexer(`{"address":"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd","dateTime":"${dateTime}"}`),
hexer(data),
"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd"
],
})
.then((res) => { signed=res; console.log('personal sign', res) })
.catch((e) => console.log('personal sign ERR', e));
.then((res) => {
signed=res;
console.log('personal sign', res);
$("#psign").text(JSON.stringify(res) + " " + data);
})
.catch((e) => {
console.log('personal sign ERR', e)
$("#psign").text(e + " " + data);
});
}

function connectGE() {
const data = `{"address":"0x6327a38415c53ffb36c11db55ea74cc9cb4976fd","chainId":${chainId},"dateTime":"${dateTime}","sig":"${signed}"}`;

$.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);
data: data,
success: function (res) {
console.log(res);
$("#cge").text(JSON.stringify(res));
},
error: function (jqXHR, exception) {
console.log(exception);
$("#cge").text(exception + " " + data);
},
});
}
</script>
</head>
<body>
<h1>Testing Metamask Wallet 4</h1>
<button onclick="config()">Config</button>
<button onclick="connect()">Connect</button>
<button onclick="accounts()">Accounts</button>
<button onclick="personalSign()">Sign Data</button>
<button onclick="connectGE()">Conn GE</button>
<h2></h2>
<div id="config"></div>
<div id="connect"></div>
<div id="sign"></div>
<div id="psign"></div>
<div id="cge"></div>
</body>
</html>

0 comments on commit ad18754

Please sign in to comment.