Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
Jordan-Mysten authored Nov 17, 2023
1 parent 9d9c90f commit 9ac7e2f
Show file tree
Hide file tree
Showing 24 changed files with 887 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-pillows-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@mysten/sui.js': patch
'@mysten/zklogin': patch
---

Add additional type exports to zklogin package
5 changes: 5 additions & 0 deletions .changeset/two-hounds-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/enoki': patch
---

Introduce Enoki SDK
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module.exports = {
},
overrides: [
{
files: ['sdk/typescript/src/**/*', 'sdk/dapp-kit/**/*', 'sdk/zklogin/**/*'],
files: ['sdk/typescript/src/**/*', 'sdk/dapp-kit/**/*', 'sdk/zklogin/**/*', 'sdk/enoki/**/*'],
rules: {
'require-extensions/require-extensions': 'error',
'require-extensions/require-index': 'error',
Expand Down
56 changes: 56 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ packages:
- '!sdk/typescript/utils'
- '!sdk/typescript/bcs'
- '!sdk/typescript/zklogin'
- '!sdk/enoki/react'
2 changes: 1 addition & 1 deletion sdk/bcs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"sideEffects": false,
"files": [
"dist",
"src",
Expand Down
2 changes: 2 additions & 0 deletions sdk/enoki/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHANGELOG.md
dist/
1 change: 1 addition & 0 deletions sdk/enoki/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# `@mysten/enoki`
74 changes: 74 additions & 0 deletions sdk/enoki/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@mysten/enoki",
"version": "0.0.1",
"description": "TODO: Description",
"license": "Apache-2.0",
"author": "Mysten Labs <[email protected]>",
"type": "commonjs",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/cjs/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./react": {
"import": "./dist/esm/react.js",
"require": "./dist/cjs/react.js"
}
},
"files": [
"CHANGELOG.md",
"dist",
"react",
"src"
],
"scripts": {
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
"build": "build-package",
"prepublishOnly": "pnpm build",
"prettier:check": "prettier -c --ignore-unknown .",
"prettier:fix": "prettier -w --ignore-unknown .",
"eslint:check": "eslint --max-warnings=0 .",
"eslint:fix": "pnpm run eslint:check --fix",
"lint": "pnpm run eslint:check && pnpm run prettier:check",
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mystenlabs/sui.git"
},
"bugs": {
"url": "https://github.com/mystenlabs/sui/issues"
},
"homepage": "https://github.com/mystenlabs/sui#readme",
"devDependencies": {
"@mysten/build-scripts": "workspace:*",
"@types/node": "^20.4.2",
"@types/react": "^18.2.15",
"react": "^18.2.0",
"tsx": "^3.12.7",
"typescript": "^5.1.6"
},
"dependencies": {
"@metamask/browser-passworder": "^4.1.0",
"@mysten/sui.js": "workspace:*",
"@mysten/zklogin": "workspace:*",
"@nanostores/react": "^0.7.1",
"jose": "^4.14.4",
"nanostores": "^0.9.3"
},
"peerDependencies": {
"@types/react": ">=17.0.0",
"react": ">=17.0.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
},
"@types/react": {
"optional": true
}
}
}
5 changes: 5 additions & 0 deletions sdk/enoki/react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"private": true,
"import": "../dist/esm/react.js",
"main": "../dist/cjs/react.js"
}
97 changes: 97 additions & 0 deletions sdk/enoki/src/EnokiClient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type {
CreateZkLoginNonceApiInput,
CreateZkLoginNonceApiResponse,
CreateZkLoginZkpApiInput,
CreateZkLoginZkpApiResponse,
GetAppApiInput,
GetAppApiResponse,
GetZkLoginApiInput,
GetZkLoginApiResponse,
} from './type.js';

const DEFAULT_API_URL = 'https://api.enoki.mystenlabs.com';
const ZKLOGIN_HEADER = 'zklogin-jwt';

export interface EnokiClientConfig {
/** The API key for the Enoki app, available in the Enoki Portal. */
apiKey: string;

/** The API URL for Enoki. In most cases, this should not be set. */
apiUrl?: string;
}

/**
* A low-level client for interacting with the Enoki API.
*/
export class EnokiClient {
#version: string;
#apiUrl: string;
#apiKey: string;

constructor(config: EnokiClientConfig) {
this.#version = 'v1';
this.#apiUrl = config.apiUrl ?? DEFAULT_API_URL;
this.#apiKey = config.apiKey;
}

getApp(_input?: GetAppApiInput) {
return this.#fetch<GetAppApiResponse>('app', {
method: 'GET',
});
}

getZkLogin(input: GetZkLoginApiInput) {
return this.#fetch<GetZkLoginApiResponse>('zklogin', {
method: 'GET',
headers: {
[ZKLOGIN_HEADER]: input.jwt,
},
});
}

createZkLoginNonce(input: CreateZkLoginNonceApiInput) {
return this.#fetch<CreateZkLoginNonceApiResponse>('zklogin/nonce', {
method: 'POST',
body: JSON.stringify({
ephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),
}),
});
}

createZkLoginZkp(input: CreateZkLoginZkpApiInput) {
return this.#fetch<CreateZkLoginZkpApiResponse>('zklogin/kzp', {
method: 'POST',
headers: {
[ZKLOGIN_HEADER]: input.jwt,
},
body: JSON.stringify({
ephemeralPublicKey: input.ephemeralPublicKey.toSuiPublicKey(),
maxEpoch: input.maxEpoch,
randomness: input.randomness,
}),
});
}

async #fetch<T = unknown>(path: string, init: RequestInit): Promise<T> {
const res = await fetch(`${this.#apiUrl}/${this.#version}/${path}`, {
...init,
headers: {
...init.headers,
Authorization: `Bearer ${this.#apiKey}`,
'Content-Type': 'application/json',
'Request-Id': crypto.randomUUID(),
},
});

if (!res.ok) {
throw new Error('Failed to fetch');
}

const { data } = await res.json();

return data as T;
}
}
42 changes: 42 additions & 0 deletions sdk/enoki/src/EnokiClient/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import type { PublicKey } from '@mysten/sui.js/cryptography';
import type { ZkLoginSignatureInputs } from '@mysten/sui.js/zklogin';

import type { AuthProvider } from '../EnokiFlow.js';

export interface GetAppApiInput {}
export interface GetAppApiResponse {
authenticationProviders: {
providerType: AuthProvider;
clientId: string;
}[];
}

export interface GetZkLoginApiInput {
jwt: string;
}
export interface GetZkLoginApiResponse {
address: string;
salt: string;
}

export interface CreateZkLoginNonceApiInput {
ephemeralPublicKey: PublicKey;
}
export interface CreateZkLoginNonceApiResponse {
nonce: string;
randomness: string;
epoch: number;
maxEpoch: number;
estimatedExpiration: number;
}

export interface CreateZkLoginZkpApiInput {
jwt: string;
ephemeralPublicKey: PublicKey;
randomness: string;
maxEpoch: number;
}
export interface CreateZkLoginZkpApiResponse extends ZkLoginSignatureInputs {}
Loading

0 comments on commit 9ac7e2f

Please sign in to comment.