Skip to content

Commit

Permalink
use typescript, add signed header request using postmessage (#11)
Browse files Browse the repository at this point in the history
* use typescript, add signed header request using postmessage

* add example usage

* add example usage

* add session id to request

* update sign interface

* rename to login, add message

* fix api

* add example react app

* add docs

* add installation instruction

* remove session id

* add more options, add signify-extension-client message

* move signify-extension-client message to constructor

* fix readme
  • Loading branch information
lenkan authored Jun 25, 2024
1 parent 697eb6a commit 1ca8aab
Show file tree
Hide file tree
Showing 20 changed files with 6,079 additions and 266 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"printWidth": 120
}
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# polaris-web
# polaris-web


## Getting started

See [web-react](./examples/web-react/src/App.tsx) for an example react web app that uses this library to communicate with a signify browser extension.

To test the example,

- `npm install`
- `npm -w web-react start`


To view generated documentation,

- `npm install`
- `npm run docs:serve`

## Usage

Install the package. At this point in time, the easiest way is to install straight from github.

```
npm install --save github:weboftrust/polaris-web#main
```

Change repository owner and branch as needed.
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-check
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";
import prettierConfig from "eslint-config-prettier";

export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended, prettierConfig);
10 changes: 10 additions & 0 deletions examples/web-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Polaris Web Example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions examples/web-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"private": true,
"type": "module",
"scripts": {
"start": "vite",
"dev": "vite"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react": "^4.3.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"vite": "^5.3.1",
"signify-polaris-web": "file://../../"
}
}
109 changes: 109 additions & 0 deletions examples/web-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { AuthorizeResult, createClient } from "signify-polaris-web";
import { FormEvent, useEffect, useState } from "react";

const client = createClient();

export function App() {
const [extensionId, setExtensionId] = useState<string | false | null>(null);
const [error, setError] = useState<string | null>(null);
const [authorizeResult, setAuthorizeResult] = useState<AuthorizeResult | null>(null);
const [headers, setHeaders] = useState<Record<string, string> | null>(null);
const [pending, setPending] = useState(false);
const [url, setUrl] = useState(window.location.href);
const [method, setMethod] = useState("GET");

useEffect(() => {
client.isExtensionInstalled().then((result) => setExtensionId(result));
}, []);

async function handleAuthorize(ev: FormEvent) {
ev.preventDefault();
setError(null);
setAuthorizeResult(null);
setPending(true);

try {
const result = await client.authorize({ message: `Message ${Date.now()}` });
setAuthorizeResult(result);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}

async function handleSignHeaders(ev: FormEvent) {
ev.preventDefault();
setError(null);
setPending(true);
setHeaders(null);

try {
const result = await client.signRequest({ url, method });
setHeaders(result.headers);
} catch (error) {
setError(error.message ?? "Something went wrong");
} finally {
setPending(false);
}
}

return (
<div style={{ maxWidth: 800, margin: "auto" }}>
<section>
<h1>Request login</h1>
<form id="login-form" onSubmit={handleAuthorize}>
<button type="submit" disabled={pending}>
Request Credential
</button>
</form>
</section>
<section>
<h1>Request signed headers</h1>
<form id="headers-form" onSubmit={handleSignHeaders}>
<div>
<label htmlFor="url">URL</label>
<input id="url" value={url} onChange={(ev) => setUrl(ev.currentTarget.value)} />
</div>
<div>
<label htmlFor="method">Method</label>
<input id="method" value={method} onChange={(ev) => setMethod(ev.currentTarget.value)} />
</div>
<div>
<button type="submit" disabled={!authorizeResult}>
Request Signed Headers
</button>
</div>
</form>
</section>
<section>
<h1>Current state</h1>
<pre style={{ maxHeight: 400, overflowY: "scroll" }}>
<code>
{JSON.stringify(
{
isInstalled: extensionId,
headers,
error,
},
null,
2,
)}
</code>
</pre>
<h1>Current credential</h1>
<pre style={{ maxHeight: 400, overflowY: "scroll" }}>
<code>
{JSON.stringify(
{
credential: authorizeResult?.credential,
},
null,
2,
)}
</code>
</pre>
</section>
</div>
);
}
8 changes: 8 additions & 0 deletions examples/web-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createRoot } from "react-dom/client";
import { App } from "./App.tsx";

const root = document.getElementById("root");

if (root) {
createRoot(root).render(<App />);
}
10 changes: 10 additions & 0 deletions examples/web-react/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"allowImportingTsExtensions": true,
"noEmit": true,
"paths": {
"signify-polaris-web": ["../../"]
}
}
}
6 changes: 6 additions & 0 deletions examples/web-react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
plugins: [react()],
});
Loading

0 comments on commit 1ca8aab

Please sign in to comment.