Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $ npm install -g @hyperplay/cli
$ hyperplay COMMAND
running command...
$ hyperplay (--version)
@hyperplay/cli/2.14.6 darwin-arm64 node-v22.11.0
@hyperplay/cli/2.14.9 darwin-arm64 node-v22.11.0
$ hyperplay --help [COMMAND]
USAGE
$ hyperplay COMMAND
Expand Down Expand Up @@ -141,6 +141,7 @@ EXAMPLES
$ hyperplay keygen
```


## `hyperplay publish [ACCOUNT] [PROJECT] [RELEASE]`

Publish a release
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperplay/cli",
"version": "2.14.7",
"version": "2.14.9",
"description": "Hyperplay CLI",
"author": "HyperPlay Labs, Inc.",
"bin": {
Expand Down
51 changes: 38 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,71 @@ export async function logCookiesAndCheckCsrf(
return csrfToken;
}

async function getAuthSession(client: AxiosInstance): Promise<void> {
try {
await client.get("/api/auth/session");
} catch (error) {
console.log(`❌ Session request failed:`, error);
throw error;
}
}

async function getCsrfToken(client: AxiosInstance): Promise<string> {
try {
const csrfResponse = await client.get("/api/auth/csrf");
return csrfResponse.data.csrfToken;
} catch (error) {
console.log(`❌ CSRF request failed:`, error);
throw error;
}
}

async function submitAuthCallback(client: AxiosInstance, formData: string): Promise<void> {
try {
await client.post("/api/auth/callback/ethereum?", formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
});
} catch (error) {
console.log(`❌ Callback request failed:`, error);
throw error;
}
}

export async function login(client: AxiosInstance, cookieJar: CookieJar, signer: ethers.Wallet) {
await client.get("/api/auth/session");
await getAuthSession(client);

const hasCsrfToken = await logCookiesAndCheckCsrf(cookieJar, client.defaults.baseURL as string);
if (!hasCsrfToken) {
throw new Error("CSRF token not found in the cookie jar.");
}

const csrfResponse = await client.get("/api/auth/csrf");
const csrfToken = csrfResponse.data.csrfToken;

CliUx.ux.action.start(`Signing into HyperPlay API with ${signer.address}:`);
const csrfToken = await getCsrfToken(client);
const siweMessage = new SiweMessage({
domain: new URL(client.defaults.baseURL as string).host,
address: signer.address,
statement: "Sign in with Ethereum to HyperPlay",
uri: client.defaults.baseURL as string,
version: "1",
chainId: 137,
chainId: 1,
nonce: csrfToken,
issuedAt: new Date().toISOString(),
});

const message = siweMessage.prepareMessage();
const signature = await signer.signMessage(message);

const formData = qs.stringify({
message: JSON.stringify(siweMessage),
message: message,
redirect: 'false',
signature: signature,
csrfToken: csrfToken,
callbackUrl: "/",
json: 'true',
});

await client.post("/api/auth/callback/ethereum?", formData, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
});
await submitAuthCallback(client, formData);

CliUx.ux.action.stop();
}

Expand Down