Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
chore: define properties for automatic wallet link on auth flow (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
einaralex authored Feb 16, 2023
1 parent f303564 commit 3bf845c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,42 @@ yarn add @monerium/sdk
- `watch`: Run Vite in watch mode to detect changes to files during development
- `build`: Run Vite to build a production release distributable

### Getting started
## Getting started

If you are new here, we recommend starting in the [Developer Portal](https://monerium.dev/docs/welcome). There you will more about `client_id`'s and ways of authenticating.

### Import the SDK and initialize a client

```ts
import { MoneriumClient } from '@monerium/sdk'
import { MoneriumClient } from "@monerium/sdk";

const client = new MoneriumClient();
```

/** Authenticate using client credentials */
### Authenticate using client credentials

```ts
await client.auth({
client_id: "your_client_credentials_uuid"
client_secret: "your_client_secret"
})

// User is now authenticated, get authentication data
await client.getAuthContext()
```

/*************************************/
/** Or, authenticate using auth flow */
### Authenticate using auth flow

```ts
// Construct the authFlowUrl for your application and redirect your customer.
let authFlowUrl = client.getAuthFlowURI({
client_id: "your_client_authflow_uuid"
redirect_uri: "http://your-webpage.com/monerium-integration"
// optional automatic wallet selection:
network: "mumbai",
chain: "polygon",
address: "0xValidAddress72413Fa92980B889A1eCE84dD",
signature: "0xValidSignature0df2b6c9e0fc067ab29bdbf322bec30aad7c46dcd97f62498a91ef7795957397e0f49426e000b0f500c347219ddd98dc5080982563055e918031c"

})
// Redirecting to the Monerium onboarding / Authentication flow.
window.location.replace(authFlowUrl)
Expand Down
13 changes: 6 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export class MoneriumClient {
codeVerifier?: string;
bearerProfile?: BearerProfile;

clientId?: string;

constructor(env: "production" | "sandbox" = "sandbox") {
this.#env = MONERIUM_CONFIG.environments[env];
}
Expand All @@ -51,7 +49,7 @@ export class MoneriumClient {
} else {
throw new Error("Authentication method could not be detected.");
}
this.clientId = args.client_id;

this.bearerProfile = (await this.#api(
"post",
`auth/token`,
Expand All @@ -65,27 +63,28 @@ export class MoneriumClient {
/**
* Construct the url to the authorization code flow,
* the code verifier is needed afterwards to obtain an access token and is therefore stored in `this.codeVerifier`
* For automatic wallet link, add the following properties: `address`, `signature`, `chain` & `network`
* @returns string
*/

getAuthFlowURI(args: PKCERequestArgs): string {
this.codeVerifier = wordArray.random(64).toString();
const challenge = encodeBase64Url.stringify(SHA256(this.codeVerifier));

const params: PKCERequest = {
...args,
client_id: this.clientId || args?.client_id,
client_id: args?.client_id,
code_challenge: challenge,
code_challenge_method: "S256",
response_type: "code",
};

return `${this.#env.api}/auth?${new URLSearchParams(params)}`;
}

/**
* @deprecated since v2.0.7, use getAuthFlowURI instead.
* @deprecated since v2.0.7, use {@link getAuthFlowURI} instead.
*/
pkceRequest = this.getAuthFlowURI;
pkceRequest = (args: PKCERequestArgs) => this.getAuthFlowURI(args);

// -- Read Methods

Expand Down
21 changes: 19 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,37 @@ export interface ClientCredentials {

// -- pkceRequest

/**
* @returns A {@link PKCERequest} object with properties omitted that are automatically computed in by the SDK.
*/
export type PKCERequestArgs = Omit<
PKCERequest,
"code_challenge" | "code_challenge_method" | "response_type"
>;

export type PKCERequest = {
/** the authentication flow client id of the application */
client_id: string;
/** the code challenge automatically generated by the SDK */
code_challenge: string;
code_challenge_method: string;
response_type: string;
/** the code challenge method for the authentication flow , handled by the SDK */
code_challenge_method: "S256";
/** the response type of the authentication flow, handled by the SDK */
response_type: "code";
/** the state of the application */
state?: string;
/** the redirect uri of the application */
redirect_uri?: string;
/** the scope of the application */
scope?: string;
/** the address of the wallet to automatically link */
address?: string;
/** the signature of the wallet to automatically link */
signature?: string;
/** the network of the wallet to automatically link */
network?: Network;
/** the chain of the wallet to automatically link */
chain?: Chain;
};

// -- authContext
Expand Down

0 comments on commit 3bf845c

Please sign in to comment.