@mysten/enoki 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/cjs/EnokiFlow.js +1 -1
- package/dist/cjs/EnokiFlow.js.map +2 -2
- package/dist/cjs/index.d.ts +3 -1
- package/dist/cjs/index.js +4 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/wallet/initializer.d.ts +11 -0
- package/dist/cjs/wallet/initializer.js +41 -0
- package/dist/cjs/wallet/initializer.js.map +7 -0
- package/dist/cjs/wallet/register.d.ts +7 -0
- package/dist/cjs/wallet/register.js +67 -0
- package/dist/cjs/wallet/register.js.map +7 -0
- package/dist/cjs/wallet/state.d.ts +12 -37
- package/dist/cjs/wallet/state.js +79 -198
- package/dist/cjs/wallet/state.js.map +2 -2
- package/dist/cjs/wallet/types.d.ts +61 -22
- package/dist/cjs/wallet/types.js.map +1 -1
- package/dist/cjs/wallet/utils.d.ts +1 -1
- package/dist/cjs/wallet/utils.js.map +1 -1
- package/dist/cjs/wallet/{index.d.ts → wallet.d.ts} +3 -21
- package/dist/cjs/wallet/wallet.js +424 -0
- package/dist/cjs/wallet/wallet.js.map +7 -0
- package/dist/esm/EnokiFlow.js +1 -1
- package/dist/esm/EnokiFlow.js.map +2 -2
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +3 -1
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/wallet/initializer.d.ts +11 -0
- package/dist/esm/wallet/initializer.js +21 -0
- package/dist/esm/wallet/initializer.js.map +7 -0
- package/dist/esm/wallet/register.d.ts +7 -0
- package/dist/esm/wallet/register.js +47 -0
- package/dist/esm/wallet/register.js.map +7 -0
- package/dist/esm/wallet/state.d.ts +12 -37
- package/dist/esm/wallet/state.js +81 -200
- package/dist/esm/wallet/state.js.map +2 -2
- package/dist/esm/wallet/types.d.ts +61 -22
- package/dist/esm/wallet/utils.d.ts +1 -1
- package/dist/esm/wallet/utils.js.map +1 -1
- package/dist/esm/wallet/{index.d.ts → wallet.d.ts} +3 -21
- package/dist/esm/wallet/wallet.js +402 -0
- package/dist/esm/wallet/wallet.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -5
- package/dist/cjs/wallet/index.js +0 -342
- package/dist/cjs/wallet/index.js.map +0 -7
- package/dist/esm/wallet/index.js +0 -321
- package/dist/esm/wallet/index.js.map +0 -7
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { registerEnokiWallets } from "./register.js";
|
|
2
|
+
function enokiWalletsInitializer(options) {
|
|
3
|
+
return {
|
|
4
|
+
id: "enoki-wallets-initializer",
|
|
5
|
+
async initialize({
|
|
6
|
+
networks,
|
|
7
|
+
getClient
|
|
8
|
+
}) {
|
|
9
|
+
const { unregister } = registerEnokiWallets({
|
|
10
|
+
...options,
|
|
11
|
+
getCurrentNetwork: () => getClient().network,
|
|
12
|
+
clients: networks.map(getClient)
|
|
13
|
+
});
|
|
14
|
+
return { unregister };
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
enokiWalletsInitializer
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=initializer.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/wallet/initializer.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { ClientWithCoreApi, Experimental_SuiClientTypes } from '@mysten/sui/experimental';\nimport { registerEnokiWallets } from './register.js';\nimport type { RegisterEnokiWalletsOptions } from './types.js';\n\nexport function enokiWalletsInitializer(\n\toptions: Omit<RegisterEnokiWalletsOptions, 'clients' | 'getCurrentNetwork'>,\n) {\n\treturn {\n\t\tid: 'enoki-wallets-initializer',\n\t\tasync initialize({\n\t\t\tnetworks,\n\t\t\tgetClient,\n\t\t}: {\n\t\t\tnetworks: readonly Experimental_SuiClientTypes.Network[];\n\t\t\tgetClient: (network?: Experimental_SuiClientTypes.Network) => ClientWithCoreApi;\n\t\t}) {\n\t\t\tconst { unregister } = registerEnokiWallets({\n\t\t\t\t...options,\n\t\t\t\tgetCurrentNetwork: () => getClient().network,\n\t\t\t\tclients: networks.map(getClient),\n\t\t\t});\n\n\t\t\treturn { unregister };\n\t\t},\n\t};\n}\n"],
|
|
5
|
+
"mappings": "AAIA,SAAS,4BAA4B;AAG9B,SAAS,wBACf,SACC;AACD,SAAO;AAAA,IACN,IAAI;AAAA,IACJ,MAAM,WAAW;AAAA,MAChB;AAAA,MACA;AAAA,IACD,GAGG;AACF,YAAM,EAAE,WAAW,IAAI,qBAAqB;AAAA,QAC3C,GAAG;AAAA,QACH,mBAAmB,MAAM,UAAU,EAAE;AAAA,QACrC,SAAS,SAAS,IAAI,SAAS;AAAA,MAChC,CAAC;AAED,aAAO,EAAE,WAAW;AAAA,IACrB;AAAA,EACD;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { EnokiWallet } from './wallet.js';
|
|
2
|
+
import type { RegisterEnokiWalletsOptions } from './types.js';
|
|
3
|
+
import type { AuthProvider } from '../EnokiClient/type.js';
|
|
4
|
+
export declare function registerEnokiWallets({ providers, windowFeatures, ...config }: RegisterEnokiWalletsOptions): {
|
|
5
|
+
wallets: Partial<Record<AuthProvider, EnokiWallet>>;
|
|
6
|
+
unregister: () => void;
|
|
7
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { EnokiWallet } from "./wallet.js";
|
|
2
|
+
import { getWallets } from "@mysten/wallet-standard";
|
|
3
|
+
import { isEnokiNetwork } from "../utils.js";
|
|
4
|
+
import { ENOKI_PROVIDER_WALLETS_INFO } from "./providers.js";
|
|
5
|
+
function registerEnokiWallets({
|
|
6
|
+
providers,
|
|
7
|
+
windowFeatures = defaultWindowFeatures,
|
|
8
|
+
...config
|
|
9
|
+
}) {
|
|
10
|
+
const clients = "clients" in config ? config.clients : [Object.assign(config.client, { network: config.network ?? "mainnet" })];
|
|
11
|
+
const enokiCompatibleClients = clients.filter(({ network }) => isEnokiNetwork(network));
|
|
12
|
+
if (enokiCompatibleClients.length === 0) {
|
|
13
|
+
throw new Error("None of the specified clients are compatible with Enoki.");
|
|
14
|
+
}
|
|
15
|
+
const getCurrentNetwork = "clients" in config ? config.getCurrentNetwork : () => clients[0].network;
|
|
16
|
+
const walletsApi = getWallets();
|
|
17
|
+
const wallets = {};
|
|
18
|
+
for (const { name, icon, provider } of ENOKI_PROVIDER_WALLETS_INFO) {
|
|
19
|
+
const providerOptions = providers[provider];
|
|
20
|
+
if (providerOptions) {
|
|
21
|
+
wallets[provider] = new EnokiWallet({
|
|
22
|
+
...providerOptions,
|
|
23
|
+
name,
|
|
24
|
+
icon,
|
|
25
|
+
provider,
|
|
26
|
+
windowFeatures,
|
|
27
|
+
getCurrentNetwork,
|
|
28
|
+
apiKey: config.apiKey,
|
|
29
|
+
apiUrl: config.apiUrl,
|
|
30
|
+
clients: enokiCompatibleClients
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const unregister = walletsApi.register(...Object.values(wallets));
|
|
35
|
+
return { wallets, unregister };
|
|
36
|
+
}
|
|
37
|
+
function defaultWindowFeatures() {
|
|
38
|
+
const width = 500;
|
|
39
|
+
const height = 800;
|
|
40
|
+
const left = (screen.width - width) / 2;
|
|
41
|
+
const top = (screen.height - height) / 4;
|
|
42
|
+
return `popup=1;toolbar=0;status=0;resizable=1,width=${width},height=${height},top=${top},left=${left}`;
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
registerEnokiWallets
|
|
46
|
+
};
|
|
47
|
+
//# sourceMappingURL=register.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/wallet/register.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { EnokiWallet } from './wallet.js';\nimport type { RegisterEnokiWalletsOptions } from './types.js';\nimport { getWallets } from '@mysten/wallet-standard';\nimport type { ClientWithCoreApi } from '@mysten/sui/experimental';\nimport type { AuthProvider } from '../EnokiClient/type.js';\nimport { isEnokiNetwork } from '../utils.js';\nimport { ENOKI_PROVIDER_WALLETS_INFO } from './providers.js';\n\nexport function registerEnokiWallets({\n\tproviders,\n\twindowFeatures = defaultWindowFeatures,\n\t...config\n}: RegisterEnokiWalletsOptions) {\n\tconst clients: ClientWithCoreApi[] =\n\t\t'clients' in config\n\t\t\t? config.clients\n\t\t\t: [Object.assign(config.client, { network: config.network ?? 'mainnet' })];\n\n\tconst enokiCompatibleClients = clients.filter(({ network }) => isEnokiNetwork(network));\n\tif (enokiCompatibleClients.length === 0) {\n\t\tthrow new Error('None of the specified clients are compatible with Enoki.');\n\t}\n\n\tconst getCurrentNetwork =\n\t\t'clients' in config ? config.getCurrentNetwork : () => clients[0].network;\n\n\tconst walletsApi = getWallets();\n\tconst wallets: Partial<Record<AuthProvider, EnokiWallet>> = {};\n\n\tfor (const { name, icon, provider } of ENOKI_PROVIDER_WALLETS_INFO) {\n\t\tconst providerOptions = providers[provider];\n\t\tif (providerOptions) {\n\t\t\twallets[provider] = new EnokiWallet({\n\t\t\t\t...providerOptions,\n\t\t\t\tname,\n\t\t\t\ticon,\n\t\t\t\tprovider,\n\t\t\t\twindowFeatures,\n\t\t\t\tgetCurrentNetwork,\n\t\t\t\tapiKey: config.apiKey,\n\t\t\t\tapiUrl: config.apiUrl,\n\t\t\t\tclients: enokiCompatibleClients,\n\t\t\t});\n\t\t}\n\t}\n\n\tconst unregister = walletsApi.register(...Object.values(wallets));\n\treturn { wallets, unregister };\n}\n\nfunction defaultWindowFeatures() {\n\tconst width = 500;\n\tconst height = 800;\n\tconst left = (screen.width - width) / 2;\n\tconst top = (screen.height - height) / 4;\n\n\treturn `popup=1;toolbar=0;status=0;resizable=1,width=${width},height=${height},top=${top},left=${left}`;\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,mBAAmB;AAE5B,SAAS,kBAAkB;AAG3B,SAAS,sBAAsB;AAC/B,SAAS,mCAAmC;AAErC,SAAS,qBAAqB;AAAA,EACpC;AAAA,EACA,iBAAiB;AAAA,EACjB,GAAG;AACJ,GAAgC;AAC/B,QAAM,UACL,aAAa,SACV,OAAO,UACP,CAAC,OAAO,OAAO,OAAO,QAAQ,EAAE,SAAS,OAAO,WAAW,UAAU,CAAC,CAAC;AAE3E,QAAM,yBAAyB,QAAQ,OAAO,CAAC,EAAE,QAAQ,MAAM,eAAe,OAAO,CAAC;AACtF,MAAI,uBAAuB,WAAW,GAAG;AACxC,UAAM,IAAI,MAAM,0DAA0D;AAAA,EAC3E;AAEA,QAAM,oBACL,aAAa,SAAS,OAAO,oBAAoB,MAAM,QAAQ,CAAC,EAAE;AAEnE,QAAM,aAAa,WAAW;AAC9B,QAAM,UAAsD,CAAC;AAE7D,aAAW,EAAE,MAAM,MAAM,SAAS,KAAK,6BAA6B;AACnE,UAAM,kBAAkB,UAAU,QAAQ;AAC1C,QAAI,iBAAiB;AACpB,cAAQ,QAAQ,IAAI,IAAI,YAAY;AAAA,QACnC,GAAG;AAAA,QACH;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,QAAQ,OAAO;AAAA,QACf,QAAQ,OAAO;AAAA,QACf,SAAS;AAAA,MACV,CAAC;AAAA,IACF;AAAA,EACD;AAEA,QAAM,aAAa,WAAW,SAAS,GAAG,OAAO,OAAO,OAAO,CAAC;AAChE,SAAO,EAAE,SAAS,WAAW;AAC9B;AAEA,SAAS,wBAAwB;AAChC,QAAM,QAAQ;AACd,QAAM,SAAS;AACf,QAAM,QAAQ,OAAO,QAAQ,SAAS;AACtC,QAAM,OAAO,OAAO,SAAS,UAAU;AAEvC,SAAO,gDAAgD,KAAK,WAAW,MAAM,QAAQ,GAAG,SAAS,IAAI;AACtG;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,43 +1,18 @@
|
|
|
1
|
-
import type { ZkLoginSignatureInputs } from '@mysten/sui/zklogin';
|
|
2
1
|
import type { WritableAtom } from 'nanostores';
|
|
3
2
|
import type { EnokiClientConfig } from '../EnokiClient/index.js';
|
|
4
|
-
import type {
|
|
5
|
-
import {
|
|
6
|
-
type
|
|
7
|
-
|
|
3
|
+
import type { ClientWithCoreApi, Experimental_SuiClientTypes } from '@mysten/sui/experimental';
|
|
4
|
+
import type { EnokiSessionContext, ZkLoginSession, ZkLoginState } from './types.js';
|
|
5
|
+
export type EnokiWalletStateConfig = EnokiClientConfig & {
|
|
6
|
+
clients: ClientWithCoreApi[];
|
|
7
|
+
clientId: string;
|
|
8
8
|
};
|
|
9
|
-
|
|
10
|
-
provider?: AuthProvider;
|
|
11
|
-
address?: string;
|
|
12
|
-
salt?: string;
|
|
13
|
-
publicKey?: string;
|
|
14
|
-
}
|
|
15
|
-
interface ZkLoginSession {
|
|
16
|
-
maxEpoch: number;
|
|
17
|
-
randomness: string;
|
|
18
|
-
expiresAt: number;
|
|
19
|
-
jwt?: string;
|
|
20
|
-
proof?: ZkLoginSignatureInputs;
|
|
21
|
-
}
|
|
22
|
-
export declare class INTERNAL_ONLY_EnokiFlow {
|
|
9
|
+
export declare class EnokiWalletState {
|
|
23
10
|
#private;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
$zkLoginState: WritableAtom<ZkLoginState>;
|
|
29
|
-
constructor(config: EnokiFlowConfig);
|
|
30
|
-
get network(): EnokiNetwork;
|
|
31
|
-
createAuthorizationURL(input: {
|
|
32
|
-
provider: AuthProvider;
|
|
33
|
-
clientId: string;
|
|
34
|
-
redirectUrl: string;
|
|
35
|
-
extraParams?: Record<string, string>;
|
|
36
|
-
}): Promise<string>;
|
|
37
|
-
handleAuthCallback(hash?: string): Promise<string | null>;
|
|
38
|
-
getSession(): Promise<ZkLoginSession | null>;
|
|
11
|
+
constructor(config: EnokiWalletStateConfig);
|
|
12
|
+
get zkLoginState(): WritableAtom<ZkLoginState>;
|
|
13
|
+
get sessionContextByNetwork(): Map<Experimental_SuiClientTypes.Network, EnokiSessionContext>;
|
|
14
|
+
getSessionContext(network: Experimental_SuiClientTypes.Network): EnokiSessionContext;
|
|
39
15
|
logout(): Promise<void>;
|
|
40
|
-
|
|
41
|
-
|
|
16
|
+
setSession(state: EnokiSessionContext, newValue: ZkLoginSession | null): Promise<void>;
|
|
17
|
+
getSession({ $zkLoginSession, storageKey }: EnokiSessionContext): Promise<ZkLoginSession | null>;
|
|
42
18
|
}
|
|
43
|
-
export {};
|
package/dist/esm/wallet/state.js
CHANGED
|
@@ -6,137 +6,80 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
|
|
|
6
6
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
7
|
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
8
|
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
-
var
|
|
10
|
-
import {
|
|
11
|
-
import { decodeJwt } from "@mysten/sui/zklogin";
|
|
12
|
-
import { clear, createStore, get, set } from "idb-keyval";
|
|
9
|
+
var _encryption, _encryptionKey, _sessionStore, _stateStorageKey, _sessionContextByNetwork, _zkLoginState, _EnokiWalletState_instances, createZkLoginState_fn;
|
|
10
|
+
import { clear, createStore } from "idb-keyval";
|
|
13
11
|
import { atom, onMount, onSet } from "nanostores";
|
|
14
12
|
import { createDefaultEncryption } from "../encryption.js";
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
import { createSessionStorage } from "../stores.js";
|
|
18
|
-
const createStorageKeys = (apiKey, network) => ({
|
|
19
|
-
STATE: `@enoki/flow/state/${apiKey}/${network}`,
|
|
20
|
-
SESSION: `@enoki/flow/session/${apiKey}/${network}`
|
|
21
|
-
});
|
|
22
|
-
class INTERNAL_ONLY_EnokiFlow {
|
|
13
|
+
import { createLocalStorage, createSessionStorage } from "../stores.js";
|
|
14
|
+
class EnokiWalletState {
|
|
23
15
|
constructor(config) {
|
|
24
|
-
__privateAdd(this,
|
|
25
|
-
__privateAdd(this, _storageKeys);
|
|
26
|
-
__privateAdd(this, _enokiClient);
|
|
27
|
-
__privateAdd(this, _network);
|
|
16
|
+
__privateAdd(this, _EnokiWalletState_instances);
|
|
28
17
|
__privateAdd(this, _encryption);
|
|
29
18
|
__privateAdd(this, _encryptionKey);
|
|
30
|
-
__privateAdd(this,
|
|
31
|
-
__privateAdd(this,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
apiUrl: config.apiUrl
|
|
35
|
-
}));
|
|
36
|
-
__privateSet(this, _network, config.network);
|
|
19
|
+
__privateAdd(this, _sessionStore);
|
|
20
|
+
__privateAdd(this, _stateStorageKey);
|
|
21
|
+
__privateAdd(this, _sessionContextByNetwork);
|
|
22
|
+
__privateAdd(this, _zkLoginState);
|
|
37
23
|
__privateSet(this, _encryptionKey, config.apiKey);
|
|
38
24
|
__privateSet(this, _encryption, createDefaultEncryption());
|
|
39
|
-
__privateSet(this,
|
|
40
|
-
__privateSet(this,
|
|
41
|
-
__privateSet(this,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
});
|
|
25
|
+
__privateSet(this, _sessionStore, createSessionStorage());
|
|
26
|
+
__privateSet(this, _stateStorageKey, `@enoki/flow/state/${config.apiKey}/${config.clientId}`);
|
|
27
|
+
__privateSet(this, _zkLoginState, __privateMethod(this, _EnokiWalletState_instances, createZkLoginState_fn).call(this));
|
|
28
|
+
__privateSet(this, _sessionContextByNetwork, config.clients.reduce((accumulator, client) => {
|
|
29
|
+
const network = client.network;
|
|
30
|
+
const storageKey = `@enoki/flow/session/${config.apiKey}/${network}/${config.clientId}`;
|
|
31
|
+
const idbStore = createStore(`${config.apiKey}_${network}_${config.clientId}`, "enoki");
|
|
32
|
+
const sessionContext = {
|
|
33
|
+
$zkLoginSession: atom({ initialized: false, value: null }),
|
|
34
|
+
client,
|
|
35
|
+
storageKey,
|
|
36
|
+
idbStore
|
|
37
|
+
};
|
|
38
|
+
onMount(sessionContext.$zkLoginSession, () => {
|
|
39
|
+
this.getSession(sessionContext);
|
|
40
|
+
});
|
|
41
|
+
return accumulator.set(network, sessionContext);
|
|
42
|
+
}, /* @__PURE__ */ new Map()));
|
|
58
43
|
}
|
|
59
|
-
get
|
|
60
|
-
return __privateGet(this,
|
|
44
|
+
get zkLoginState() {
|
|
45
|
+
return __privateGet(this, _zkLoginState);
|
|
61
46
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
...input.extraParams,
|
|
70
|
-
nonce,
|
|
71
|
-
client_id: input.clientId,
|
|
72
|
-
redirect_uri: input.redirectUrl,
|
|
73
|
-
response_type: "id_token",
|
|
74
|
-
// TODO: Eventually fetch the scopes for this client ID from the Enoki service:
|
|
75
|
-
scope: [
|
|
76
|
-
"openid",
|
|
77
|
-
// Merge the requested scopes in with the required openid scopes:
|
|
78
|
-
...input.extraParams?.scope ? input.extraParams.scope.split(" ") : []
|
|
79
|
-
].filter(Boolean).join(" ")
|
|
80
|
-
});
|
|
81
|
-
let oauthUrl;
|
|
82
|
-
switch (input.provider) {
|
|
83
|
-
case "google": {
|
|
84
|
-
oauthUrl = `https://accounts.google.com/o/oauth2/v2/auth?${params}`;
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
case "facebook": {
|
|
88
|
-
oauthUrl = `https://www.facebook.com/v17.0/dialog/oauth?${params}`;
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
case "twitch": {
|
|
92
|
-
params.set("force_verify", "true");
|
|
93
|
-
oauthUrl = `https://id.twitch.tv/oauth2/authorize?${params}`;
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
default:
|
|
97
|
-
throw new Error(`Invalid provider: ${input.provider}`);
|
|
47
|
+
get sessionContextByNetwork() {
|
|
48
|
+
return __privateGet(this, _sessionContextByNetwork);
|
|
49
|
+
}
|
|
50
|
+
getSessionContext(network) {
|
|
51
|
+
const context = __privateGet(this, _sessionContextByNetwork).get(network);
|
|
52
|
+
if (!context) {
|
|
53
|
+
throw new Error(`The network ${network} isn't supported.`);
|
|
98
54
|
}
|
|
99
|
-
|
|
100
|
-
await set("ephemeralKeyPair", ephemeralKeyPair.export(), __privateGet(this, _idbStore));
|
|
101
|
-
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
102
|
-
expiresAt: estimatedExpiration,
|
|
103
|
-
maxEpoch,
|
|
104
|
-
randomness
|
|
105
|
-
});
|
|
106
|
-
return oauthUrl;
|
|
55
|
+
return context;
|
|
107
56
|
}
|
|
108
|
-
async
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
);
|
|
57
|
+
async logout() {
|
|
58
|
+
__privateGet(this, _zkLoginState).set({});
|
|
59
|
+
__privateGet(this, _sessionStore).delete(__privateGet(this, _stateStorageKey));
|
|
60
|
+
for (const state of __privateGet(this, _sessionContextByNetwork).values()) {
|
|
61
|
+
await clear(state.idbStore);
|
|
62
|
+
await this.setSession(state, null);
|
|
115
63
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
64
|
+
}
|
|
65
|
+
async setSession(state, newValue) {
|
|
66
|
+
if (newValue) {
|
|
67
|
+
const storedValue = await __privateGet(this, _encryption).encrypt(
|
|
68
|
+
__privateGet(this, _encryptionKey),
|
|
69
|
+
JSON.stringify(newValue)
|
|
70
|
+
);
|
|
71
|
+
__privateGet(this, _sessionStore).set(state.storageKey, storedValue);
|
|
72
|
+
} else {
|
|
73
|
+
__privateGet(this, _sessionStore).delete(state.storageKey);
|
|
119
74
|
}
|
|
120
|
-
|
|
121
|
-
const { address, salt, publicKey } = await __privateGet(this, _enokiClient).getZkLogin({ jwt });
|
|
122
|
-
this.$zkLoginState.set({
|
|
123
|
-
...this.$zkLoginState.get(),
|
|
124
|
-
salt,
|
|
125
|
-
address,
|
|
126
|
-
publicKey
|
|
127
|
-
});
|
|
128
|
-
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
129
|
-
...zkp,
|
|
130
|
-
jwt
|
|
131
|
-
});
|
|
132
|
-
return params.get("state");
|
|
75
|
+
state.$zkLoginSession.set({ initialized: true, value: newValue });
|
|
133
76
|
}
|
|
134
|
-
async getSession() {
|
|
135
|
-
if (
|
|
136
|
-
return
|
|
77
|
+
async getSession({ $zkLoginSession, storageKey }) {
|
|
78
|
+
if ($zkLoginSession.get().initialized) {
|
|
79
|
+
return $zkLoginSession.get().value;
|
|
137
80
|
}
|
|
138
81
|
try {
|
|
139
|
-
const storedValue = __privateGet(this,
|
|
82
|
+
const storedValue = __privateGet(this, _sessionStore).get(storageKey);
|
|
140
83
|
if (!storedValue) return null;
|
|
141
84
|
const state = JSON.parse(
|
|
142
85
|
await __privateGet(this, _encryption).decrypt(__privateGet(this, _encryptionKey), storedValue)
|
|
@@ -144,100 +87,38 @@ class INTERNAL_ONLY_EnokiFlow {
|
|
|
144
87
|
if (state?.expiresAt && Date.now() > state.expiresAt) {
|
|
145
88
|
await this.logout();
|
|
146
89
|
} else {
|
|
147
|
-
|
|
90
|
+
$zkLoginSession.set({ initialized: true, value: state });
|
|
148
91
|
}
|
|
149
92
|
} catch {
|
|
150
|
-
|
|
93
|
+
$zkLoginSession.set({ initialized: true, value: null });
|
|
151
94
|
}
|
|
152
|
-
return
|
|
153
|
-
}
|
|
154
|
-
async logout() {
|
|
155
|
-
this.$zkLoginState.set({});
|
|
156
|
-
__privateGet(this, _store).delete(__privateGet(this, _storageKeys).STATE);
|
|
157
|
-
await clear(__privateGet(this, _idbStore));
|
|
158
|
-
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, null);
|
|
159
|
-
}
|
|
160
|
-
// TODO: Should this return the proof if it already exists?
|
|
161
|
-
async getProof() {
|
|
162
|
-
const zkp = await this.getSession();
|
|
163
|
-
const { salt } = this.$zkLoginState.get();
|
|
164
|
-
if (zkp?.proof) {
|
|
165
|
-
if (zkp.expiresAt && Date.now() > zkp.expiresAt) {
|
|
166
|
-
throw new Error("Stored proof is expired.");
|
|
167
|
-
}
|
|
168
|
-
return zkp.proof;
|
|
169
|
-
}
|
|
170
|
-
if (!salt || !zkp || !zkp.jwt) {
|
|
171
|
-
throw new Error("Missing required parameters for proof generation");
|
|
172
|
-
}
|
|
173
|
-
const storedNativeSigner = await get(
|
|
174
|
-
"ephemeralKeyPair",
|
|
175
|
-
__privateGet(this, _idbStore)
|
|
176
|
-
);
|
|
177
|
-
if (!storedNativeSigner) {
|
|
178
|
-
throw new Error("Native signer not found in store.");
|
|
179
|
-
}
|
|
180
|
-
const ephemeralKeyPair = WebCryptoSigner.import(storedNativeSigner);
|
|
181
|
-
const proof = await __privateGet(this, _enokiClient).createZkLoginZkp({
|
|
182
|
-
network: __privateGet(this, _network),
|
|
183
|
-
jwt: zkp.jwt,
|
|
184
|
-
maxEpoch: zkp.maxEpoch,
|
|
185
|
-
randomness: zkp.randomness,
|
|
186
|
-
ephemeralPublicKey: ephemeralKeyPair.getPublicKey()
|
|
187
|
-
});
|
|
188
|
-
await __privateMethod(this, _INTERNAL_ONLY_EnokiFlow_instances, setSession_fn).call(this, {
|
|
189
|
-
...zkp,
|
|
190
|
-
proof
|
|
191
|
-
});
|
|
192
|
-
return proof;
|
|
193
|
-
}
|
|
194
|
-
async getKeypair() {
|
|
195
|
-
await this.getProof();
|
|
196
|
-
const zkp = await this.getSession();
|
|
197
|
-
const { address } = this.$zkLoginState.get();
|
|
198
|
-
if (!address || !zkp || !zkp.proof) {
|
|
199
|
-
throw new Error("Missing required data for keypair generation.");
|
|
200
|
-
}
|
|
201
|
-
if (Date.now() > zkp.expiresAt) {
|
|
202
|
-
throw new Error("Stored proof is expired.");
|
|
203
|
-
}
|
|
204
|
-
const storedNativeSigner = await get(
|
|
205
|
-
"ephemeralKeyPair",
|
|
206
|
-
__privateGet(this, _idbStore)
|
|
207
|
-
);
|
|
208
|
-
if (!storedNativeSigner) {
|
|
209
|
-
throw new Error("Native signer not found in store.");
|
|
210
|
-
}
|
|
211
|
-
const ephemeralKeypair = WebCryptoSigner.import(storedNativeSigner);
|
|
212
|
-
return new EnokiKeypair({
|
|
213
|
-
address,
|
|
214
|
-
ephemeralKeypair,
|
|
215
|
-
maxEpoch: zkp.maxEpoch,
|
|
216
|
-
proof: zkp.proof
|
|
217
|
-
});
|
|
95
|
+
return $zkLoginSession.get().value;
|
|
218
96
|
}
|
|
219
97
|
}
|
|
220
|
-
_storageKeys = new WeakMap();
|
|
221
|
-
_enokiClient = new WeakMap();
|
|
222
|
-
_network = new WeakMap();
|
|
223
98
|
_encryption = new WeakMap();
|
|
224
99
|
_encryptionKey = new WeakMap();
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
100
|
+
_sessionStore = new WeakMap();
|
|
101
|
+
_stateStorageKey = new WeakMap();
|
|
102
|
+
_sessionContextByNetwork = new WeakMap();
|
|
103
|
+
_zkLoginState = new WeakMap();
|
|
104
|
+
_EnokiWalletState_instances = new WeakSet();
|
|
105
|
+
createZkLoginState_fn = function() {
|
|
106
|
+
const storage = createLocalStorage();
|
|
107
|
+
let storedState = null;
|
|
108
|
+
try {
|
|
109
|
+
const rawStoredValue = storage.get(__privateGet(this, _stateStorageKey));
|
|
110
|
+
if (rawStoredValue) {
|
|
111
|
+
storedState = JSON.parse(rawStoredValue);
|
|
112
|
+
}
|
|
113
|
+
} catch {
|
|
237
114
|
}
|
|
238
|
-
|
|
115
|
+
const $zkLoginState = atom(storedState || {});
|
|
116
|
+
onSet($zkLoginState, ({ newValue }) => {
|
|
117
|
+
storage.set(__privateGet(this, _stateStorageKey), JSON.stringify(newValue));
|
|
118
|
+
});
|
|
119
|
+
return $zkLoginState;
|
|
239
120
|
};
|
|
240
121
|
export {
|
|
241
|
-
|
|
122
|
+
EnokiWalletState
|
|
242
123
|
};
|
|
243
124
|
//# sourceMappingURL=state.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/wallet/state.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport
|
|
5
|
-
"mappings": ";;;;;;;;AAAA;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { clear, createStore } from 'idb-keyval';\nimport type { WritableAtom } from 'nanostores';\nimport { atom, onMount, onSet } from 'nanostores';\n\nimport type { Encryption } from '../encryption.js';\nimport { createDefaultEncryption } from '../encryption.js';\nimport type { EnokiClientConfig } from '../EnokiClient/index.js';\nimport type { ClientWithCoreApi, Experimental_SuiClientTypes } from '@mysten/sui/experimental';\nimport type { SyncStore } from '../stores.js';\nimport { createLocalStorage, createSessionStorage } from '../stores.js';\n\nimport type { EnokiSessionContext, ZkLoginSession, ZkLoginState } from './types.js';\n\nexport type EnokiWalletStateConfig = EnokiClientConfig & {\n\tclients: ClientWithCoreApi[];\n\tclientId: string;\n};\n\nexport class EnokiWalletState {\n\t#encryption: Encryption;\n\t#encryptionKey: string;\n\t#sessionStore: SyncStore;\n\t#stateStorageKey: string;\n\t#sessionContextByNetwork: Map<Experimental_SuiClientTypes.Network, EnokiSessionContext>;\n\t#zkLoginState: WritableAtom<ZkLoginState>;\n\n\tconstructor(config: EnokiWalletStateConfig) {\n\t\tthis.#encryptionKey = config.apiKey;\n\t\tthis.#encryption = createDefaultEncryption();\n\t\tthis.#sessionStore = createSessionStorage();\n\t\tthis.#stateStorageKey = `@enoki/flow/state/${config.apiKey}/${config.clientId}`;\n\t\tthis.#zkLoginState = this.#createZkLoginState();\n\n\t\tthis.#sessionContextByNetwork = config.clients.reduce((accumulator, client) => {\n\t\t\tconst network = client.network;\n\t\t\tconst storageKey = `@enoki/flow/session/${config.apiKey}/${network}/${config.clientId}`;\n\t\t\tconst idbStore = createStore(`${config.apiKey}_${network}_${config.clientId}`, 'enoki');\n\n\t\t\tconst sessionContext: EnokiSessionContext = {\n\t\t\t\t$zkLoginSession: atom({ initialized: false, value: null }),\n\t\t\t\tclient,\n\t\t\t\tstorageKey,\n\t\t\t\tidbStore,\n\t\t\t};\n\n\t\t\tonMount(sessionContext.$zkLoginSession, () => {\n\t\t\t\tthis.getSession(sessionContext);\n\t\t\t});\n\n\t\t\treturn accumulator.set(network, sessionContext);\n\t\t}, new Map());\n\t}\n\n\tget zkLoginState() {\n\t\treturn this.#zkLoginState;\n\t}\n\n\tget sessionContextByNetwork() {\n\t\treturn this.#sessionContextByNetwork;\n\t}\n\n\tgetSessionContext(network: Experimental_SuiClientTypes.Network) {\n\t\tconst context = this.#sessionContextByNetwork.get(network);\n\t\tif (!context) {\n\t\t\tthrow new Error(`The network ${network} isn't supported.`);\n\t\t}\n\n\t\treturn context;\n\t}\n\n\tasync logout() {\n\t\tthis.#zkLoginState.set({});\n\t\tthis.#sessionStore.delete(this.#stateStorageKey);\n\n\t\tfor (const state of this.#sessionContextByNetwork.values()) {\n\t\t\tawait clear(state.idbStore);\n\t\t\tawait this.setSession(state, null);\n\t\t}\n\t}\n\n\tasync setSession(state: EnokiSessionContext, newValue: ZkLoginSession | null) {\n\t\tif (newValue) {\n\t\t\tconst storedValue = await this.#encryption.encrypt(\n\t\t\t\tthis.#encryptionKey,\n\t\t\t\tJSON.stringify(newValue),\n\t\t\t);\n\n\t\t\tthis.#sessionStore.set(state.storageKey, storedValue);\n\t\t} else {\n\t\t\tthis.#sessionStore.delete(state.storageKey);\n\t\t}\n\n\t\tstate.$zkLoginSession.set({ initialized: true, value: newValue });\n\t}\n\n\tasync getSession({ $zkLoginSession, storageKey }: EnokiSessionContext) {\n\t\tif ($zkLoginSession.get().initialized) {\n\t\t\treturn $zkLoginSession.get().value;\n\t\t}\n\n\t\ttry {\n\t\t\tconst storedValue = this.#sessionStore.get(storageKey);\n\t\t\tif (!storedValue) return null;\n\n\t\t\tconst state: ZkLoginSession = JSON.parse(\n\t\t\t\tawait this.#encryption.decrypt(this.#encryptionKey, storedValue),\n\t\t\t);\n\n\t\t\tif (state?.expiresAt && Date.now() > state.expiresAt) {\n\t\t\t\tawait this.logout();\n\t\t\t} else {\n\t\t\t\t$zkLoginSession.set({ initialized: true, value: state });\n\t\t\t}\n\t\t} catch {\n\t\t\t$zkLoginSession.set({ initialized: true, value: null });\n\t\t}\n\n\t\treturn $zkLoginSession.get().value;\n\t}\n\n\t#createZkLoginState() {\n\t\tconst storage = createLocalStorage();\n\t\tlet storedState: ZkLoginState | null = null;\n\n\t\ttry {\n\t\t\tconst rawStoredValue = storage.get(this.#stateStorageKey);\n\t\t\tif (rawStoredValue) {\n\t\t\t\tstoredState = JSON.parse(rawStoredValue);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\n\t\tconst $zkLoginState = atom<ZkLoginState>(storedState || {});\n\n\t\tonSet($zkLoginState, ({ newValue }) => {\n\t\t\tstorage.set(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